From 938beb35e99c58a3fd1a6a7ba7bffca107b8d2c4 Mon Sep 17 00:00:00 2001 From: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2026 23:31:01 +0000 Subject: [PATCH] build(rust): stage pinned Lance source artifact --- Cargo.lock | 21 - Cargo.toml | 17 +- lance-artifact/Cargo.toml | 266 + .../LANCEDB_PATCH.md | 9 +- lance-artifact/LICENSE | 255 + lance-artifact/README.md | 247 + lance-artifact/protos/AGENTS.md | 19 + lance-artifact/protos/CLAUDE.md | 1 + lance-artifact/protos/ann.proto | 72 + lance-artifact/protos/encodings_v2_0.proto | 347 + lance-artifact/protos/encodings_v2_1.proto | 635 + lance-artifact/protos/file.proto | 207 + lance-artifact/protos/file2.proto | 210 + lance-artifact/protos/filtered_read.proto | 99 + lance-artifact/protos/index.proto | 251 + lance-artifact/protos/index_old.proto | 104 + lance-artifact/protos/license_header.txt | 2 + lance-artifact/protos/rowids.proto | 113 + lance-artifact/protos/table.proto | 805 + lance-artifact/protos/table_identifier.proto | 19 + lance-artifact/protos/transaction.proto | 377 + lance-artifact/rust/.gitignore | 1 + lance-artifact/rust/AGENTS.md | 90 + lance-artifact/rust/CLAUDE.md | 1 + lance-artifact/rust/CONTRIBUTING.md | 41 + lance-artifact/rust/README.md | 2 + lance-artifact/rust/arrow-scalar/Cargo.toml | 31 + lance-artifact/rust/arrow-scalar/README.md | 57 + .../rust/arrow-scalar/src/convert.rs | 105 + lance-artifact/rust/arrow-scalar/src/lib.rs | 621 + lance-artifact/rust/arrow-scalar/src/serde.rs | 558 + lance-artifact/rust/arrow-stats/Cargo.toml | 25 + lance-artifact/rust/arrow-stats/README.md | 62 + .../arrow-stats/proptest-regressions/lib.txt | 8 + lance-artifact/rust/arrow-stats/src/lib.rs | 1294 ++ lance-artifact/rust/arrow-stats/src/nan.rs | 32 + .../rust/compression/bitpacking/Cargo.toml | 24 + .../src/bitpacker_internal/bitpacker4x.rs | 820 + .../src/bitpacker_internal/bitpacker8x.rs | 872 + .../src/bitpacker_internal/macros.rs | 669 + .../bitpacking/src/bitpacker_internal/mod.rs | 147 + .../rust/compression/bitpacking/src/lib.rs | 2278 +++ .../rust/compression/fsst/Cargo.toml | 27 + .../compression/fsst/examples/benchmark.rs | 157 + .../rust/compression/fsst/src/fsst.rs | 1744 ++ .../rust/compression/fsst/src/lib.rs | 4 + lance-artifact/rust/examples/Cargo.toml | 54 + .../rust/examples/src/full_text_search.rs | 129 + lance-artifact/rust/examples/src/hnsw.rs | 151 + lance-artifact/rust/examples/src/ivf_hnsw.rs | 155 + .../rust/examples/src/llm_dataset_creation.rs | 256 + .../rust/examples/src/write_read_ds.rs | 84 + lance-artifact/rust/img.png | Bin 0 -> 312479 bytes lance-artifact/rust/lance-arrow/Cargo.toml | 36 + lance-artifact/rust/lance-arrow/README.md | 8 + .../rust/lance-arrow/src/bfloat16.rs | 404 + .../rust/lance-arrow/src/deepcopy.rs | 238 + lance-artifact/rust/lance-arrow/src/floats.rs | 291 + lance-artifact/rust/lance-arrow/src/ipc.rs | 620 + lance-artifact/rust/lance-arrow/src/json.rs | 1445 ++ lance-artifact/rust/lance-arrow/src/lib.rs | 2764 +++ lance-artifact/rust/lance-arrow/src/list.rs | 176 + lance-artifact/rust/lance-arrow/src/memory.rs | 91 + lance-artifact/rust/lance-arrow/src/scalar.rs | 282 + lance-artifact/rust/lance-arrow/src/schema.rs | 207 + lance-artifact/rust/lance-arrow/src/stream.rs | 504 + lance-artifact/rust/lance-arrow/src/struct.rs | 191 + lance-artifact/rust/lance-core/Cargo.toml | 72 + .../rust/lance-core/benches/cache_keys.rs | 365 + .../io/writer/statistics.txt | 12 + .../proptest-regressions/utils/mask.txt | 9 + .../rust/lance-core/src/cache/backend.rs | 137 + .../rust/lance-core/src/cache/backend_uri.rs | 265 + .../rust/lance-core/src/cache/codec.rs | 553 + .../rust/lance-core/src/cache/entry_io.rs | 202 + .../rust/lance-core/src/cache/key.rs | 476 + .../rust/lance-core/src/cache/mod.rs | 1435 ++ .../rust/lance-core/src/cache/moka.rs | 349 + .../rust/lance-core/src/cache/quick.rs | 298 + .../rust/lance-core/src/cache/registry.rs | 364 + .../rust/lance-core/src/container.rs | 4 + .../rust/lance-core/src/container/list.rs | 306 + .../rust/lance-core/src/datatypes.rs | 687 + .../rust/lance-core/src/datatypes/field.rs | 2044 ++ .../rust/lance-core/src/datatypes/schema.rs | 3163 ++++ .../rust/lance-core/src/deepsize.rs | 467 + lance-artifact/rust/lance-core/src/error.rs | 1506 ++ .../rust/lance-core/src/levenshtein.rs | 133 + lance-artifact/rust/lance-core/src/lib.rs | 68 + lance-artifact/rust/lance-core/src/traits.rs | 27 + lance-artifact/rust/lance-core/src/utils.rs | 22 + .../rust/lance-core/src/utils/address.rs | 116 + .../rust/lance-core/src/utils/aimd.rs | 623 + .../rust/lance-core/src/utils/assume.rs | 41 + .../rust/lance-core/src/utils/backoff.rs | 291 + .../rust/lance-core/src/utils/bit.rs | 153 + .../rust/lance-core/src/utils/blob.rs | 46 + .../rust/lance-core/src/utils/bloomfilter.rs | 10 + .../src/utils/bloomfilter/as_bytes.rs | 164 + .../lance-core/src/utils/bloomfilter/sbbf.rs | 620 + .../rust/lance-core/src/utils/cpu.rs | 357 + .../rust/lance-core/src/utils/deletion.rs | 522 + .../rust/lance-core/src/utils/futures.rs | 491 + .../rust/lance-core/src/utils/hash.rs | 50 + .../rust/lance-core/src/utils/io_stats.rs | 30 + .../rust/lance-core/src/utils/parse.rs | 23 + .../rust/lance-core/src/utils/path.rs | 18 + .../lance-core/src/utils/row_addr_remap.rs | 413 + .../rust/lance-core/src/utils/tempfile.rs | 343 + .../rust/lance-core/src/utils/testing.rs | 300 + .../rust/lance-core/src/utils/tokio.rs | 232 + .../rust/lance-core/src/utils/tracing.rs | 88 + .../lance-core/tests/cache_key_allocations.rs | 166 + .../rust/lance-datafusion/Cargo.toml | 52 + lance-artifact/rust/lance-datafusion/build.rs | 27 + lance-artifact/rust/lance-datafusion/protos | 1 + .../rust/lance-datafusion/src/aggregate.rs | 41 + .../rust/lance-datafusion/src/chunker.rs | 401 + .../rust/lance-datafusion/src/dataframe.rs | 307 + .../rust/lance-datafusion/src/datagen.rs | 62 + .../rust/lance-datafusion/src/exec.rs | 1346 ++ .../rust/lance-datafusion/src/expr.rs | 886 + .../rust/lance-datafusion/src/lib.rs | 29 + .../rust/lance-datafusion/src/logical_expr.rs | 536 + .../rust/lance-datafusion/src/planner.rs | 2096 +++ .../rust/lance-datafusion/src/projection.rs | 733 + .../rust/lance-datafusion/src/spill.rs | 897 + .../rust/lance-datafusion/src/sql.rs | 177 + .../rust/lance-datafusion/src/substrait.rs | 1156 ++ .../rust/lance-datafusion/src/udf.rs | 210 + .../rust/lance-datafusion/src/udf/json.rs | 1392 ++ .../rust/lance-datafusion/src/utils.rs | 257 + .../src/utils/background_iterator.rs | 120 + lance-artifact/rust/lance-datagen/Cargo.toml | 37 + .../rust/lance-datagen/benches/array_gen.rs | 152 + .../rust/lance-datagen/src/generator.rs | 3682 ++++ lance-artifact/rust/lance-datagen/src/lib.rs | 6 + lance-artifact/rust/lance-derive/Cargo.toml | 22 + lance-artifact/rust/lance-derive/src/lib.rs | 119 + lance-artifact/rust/lance-encoding/.gitignore | 2 + lance-artifact/rust/lance-encoding/Cargo.toml | 86 + lance-artifact/rust/lance-encoding/README.md | 6 + .../rust/lance-encoding/benches/buffer.rs | 78 + .../rust/lance-encoding/benches/common/mod.rs | 278 + .../rust/lance-encoding/benches/decoder.rs | 738 + .../rust/lance-encoding/benches/encoder.rs | 178 + lance-artifact/rust/lance-encoding/build.rs | 23 + lance-artifact/rust/lance-encoding/protos | 1 + .../rust/lance-encoding/src/array_encoding.rs | 13 + .../src/array_encoding/logical.rs | 8 + .../src/array_encoding/logical/binary.rs | 186 + .../src/array_encoding/logical/blob.rs | 532 + .../src/array_encoding/logical/list.rs | 1283 ++ .../src/array_encoding/logical/primitive.rs | 533 + .../src/array_encoding/logical/struct.rs | 617 + .../src/array_encoding/physical.rs | 329 + .../src/array_encoding/physical/basic.rs | 236 + .../src/array_encoding/physical/binary.rs | 571 + .../src/array_encoding/physical/bitmap.rs | 223 + .../src/array_encoding/physical/bitpack.rs | 880 + .../src/array_encoding/physical/block.rs | 75 + .../src/array_encoding/physical/dictionary.rs | 588 + .../physical/fixed_size_binary.rs | 306 + .../physical/fixed_size_list.rs | 264 + .../src/array_encoding/physical/fsst.rs | 198 + .../array_encoding/physical/packed_struct.rs | 337 + .../src/array_encoding/physical/value.rs | 243 + .../src/array_encoding/strategy.rs | 653 + .../rust/lance-encoding/src/buffer.rs | 503 + .../rust/lance-encoding/src/compression.rs | 2703 +++ .../lance-encoding/src/compression_config.rs | 299 + .../rust/lance-encoding/src/constants.rs | 65 + .../rust/lance-encoding/src/data.rs | 2704 +++ .../rust/lance-encoding/src/decoder.rs | 3491 ++++ .../rust/lance-encoding/src/encoder.rs | 524 + .../lance-encoding/src/encoder/structural.rs | 285 + .../rust/lance-encoding/src/encodings.rs | 8 + .../src/encodings/fuzz_tests.rs | 545 + .../lance-encoding/src/encodings/logical.rs | 9 + .../src/encodings/logical/blob.rs | 904 + .../src/encodings/logical/fixed_size_list.rs | 766 + .../src/encodings/logical/list.rs | 1444 ++ .../src/encodings/logical/map.rs | 798 + .../src/encodings/logical/primitive.rs | 10307 ++++++++++ .../src/encodings/logical/primitive/blob.rs | 532 + .../logical/primitive/chunk_index.rs | 536 + .../encodings/logical/primitive/constant.rs | 502 + .../src/encodings/logical/primitive/dict.rs | 573 + .../encodings/logical/primitive/fullzip.rs | 54 + .../src/encodings/logical/primitive/layout.rs | 72 + .../encodings/logical/primitive/miniblock.rs | 201 + .../src/encodings/logical/primitive/sparse.rs | 5614 ++++++ .../logical/primitive/sparse/writer.rs | 2408 +++ .../src/encodings/logical/struct.rs | 951 + .../lance-encoding/src/encodings/physical.rs | 14 + .../src/encodings/physical/binary.rs | 1325 ++ .../src/encodings/physical/bitpacking.rs | 859 + .../src/encodings/physical/block.rs | 842 + .../encodings/physical/byte_stream_split.rs | 452 + .../src/encodings/physical/constant.rs | 57 + .../src/encodings/physical/fsst.rs | 410 + .../src/encodings/physical/general.rs | 683 + .../src/encodings/physical/packed.rs | 1214 ++ .../src/encodings/physical/rle.rs | 3250 ++++ .../src/encodings/physical/value.rs | 1286 ++ .../rust/lance-encoding/src/format.rs | 773 + lance-artifact/rust/lance-encoding/src/lib.rs | 134 + .../rust/lance-encoding/src/repdef.rs | 3853 ++++ .../rust/lance-encoding/src/statistics.rs | 1269 ++ .../rust/lance-encoding/src/testing.rs | 1534 ++ .../rust/lance-encoding/src/utils.rs | 7 + .../lance-encoding/src/utils/accumulation.rs | 105 + .../rust/lance-encoding/src/utils/bytepack.rs | 263 + lance-artifact/rust/lance-file/Cargo.toml | 71 + lance-artifact/rust/lance-file/README.md | 6 + .../rust/lance-file/benches/reader.rs | 477 + .../rust/lance-file/benches/schema.rs | 73 + lance-artifact/rust/lance-file/build.rs | 29 + lance-artifact/rust/lance-file/protos | 1 + .../lance-file/src/compatibility_tests.rs | 524 + .../rust/lance-file/src/datatypes.rs | 565 + lance-artifact/rust/lance-file/src/format.rs | 33 + lance-artifact/rust/lance-file/src/io.rs | 148 + lance-artifact/rust/lance-file/src/lib.rs | 59 + lance-artifact/rust/lance-file/src/reader.rs | 4606 +++++ .../rust/lance-file/src/reader/structural.rs | 445 + lance-artifact/rust/lance-file/src/testing.rs | 123 + lance-artifact/rust/lance-file/src/version.rs | 422 + .../rust/lance-file/src/versions/mod.rs | 396 + .../lance-file/src/versions/v1/encoding.rs | 192 + .../src/versions/v1/encoding/binary.rs | 710 + .../src/versions/v1/encoding/dictionary.rs | 253 + .../src/versions/v1/encoding/plain.rs | 852 + .../src/versions/v1/format/metadata.rs | 255 + .../lance-file/src/versions/v1/format/mod.rs | 4 + .../rust/lance-file/src/versions/v1/mod.rs | 116 + .../lance-file/src/versions/v1/page_table.rs | 287 + .../rust/lance-file/src/versions/v1/reader.rs | 1562 ++ .../lance-file/src/versions/v1/writer/mod.rs | 1280 ++ .../src/versions/v1/writer/statistics.rs | 2247 +++ .../rust/lance-file/src/versions/v2_0/mod.rs | 164 + .../lance-file/src/versions/v2_0/reader.rs | 415 + .../lance-file/src/versions/v2_0/writer.rs | 957 + .../src/versions/v2_1/compression.rs | 130 + .../rust/lance-file/src/versions/v2_1/mod.rs | 180 + .../lance-file/src/versions/v2_1/reader.rs | 304 + .../lance-file/src/versions/v2_1/writer.rs | 223 + .../src/versions/v2_2/compression.rs | 130 + .../rust/lance-file/src/versions/v2_2/mod.rs | 177 + .../lance-file/src/versions/v2_2/reader.rs | 308 + .../lance-file/src/versions/v2_2/writer.rs | 223 + .../src/versions/v2_3/compression.rs | 130 + .../rust/lance-file/src/versions/v2_3/mod.rs | 200 + .../lance-file/src/versions/v2_3/reader.rs | 341 + .../lance-file/src/versions/v2_3/writer.rs | 223 + lance-artifact/rust/lance-file/src/writer.rs | 279 + .../rust/lance-file/src/writer/structural.rs | 837 + .../rust/lance-file/src/writer_tests.rs | 1382 ++ .../test_data/exact_versions/README.md | 41 + .../test_data/exact_versions/datagen.py | 129 + .../test_data/exact_versions/datagen.rs | 228 + .../test_data/exact_versions/v1.lance | Bin 0 -> 387133 bytes .../test_data/exact_versions/v2_0.lance | 10040 ++++++++++ .../test_data/exact_versions/v2_0_mini.lance | Bin 0 -> 22961 bytes .../exact_versions/v2_0_self_described.lance | Bin 0 -> 23077 bytes .../test_data/exact_versions/v2_1.lance | 639 + .../test_data/exact_versions/v2_2.lance | 633 + lance-artifact/rust/lance-geo/Cargo.toml | 28 + lance-artifact/rust/lance-geo/src/bbox.rs | 368 + lance-artifact/rust/lance-geo/src/lib.rs | 14 + lance-artifact/rust/lance-geo/src/udf.rs | 20 + .../rust/lance-index-core/Cargo.toml | 33 + .../rust/lance-index-core/README.md | 6 + .../rust/lance-index-core/src/lib.rs | 313 + .../rust/lance-index-core/src/metrics.rs | 255 + .../rust/lance-index-core/src/scalar.rs | 585 + lance-artifact/rust/lance-index/Cargo.toml | 173 + lance-artifact/rust/lance-index/README.md | 8 + .../lance-index/benches/4bitpq_dist_table.rs | 140 + .../rust/lance-index/benches/bitmap.rs | 474 + .../rust/lance-index/benches/btree.rs | 714 + .../rust/lance-index/benches/common.rs | 155 + .../lance-index/benches/compute_partition.rs | 64 + .../lance-index/benches/find_partitions.rs | 69 + .../rust/lance-index/benches/geo.rs | 180 + .../rust/lance-index/benches/hnsw.rs | 373 + .../rust/lance-index/benches/inverted.rs | 260 + .../rust/lance-index/benches/kmeans.rs | 105 + .../rust/lance-index/benches/ngram.rs | 134 + .../rust/lance-index/benches/pq_assignment.rs | 46 + .../rust/lance-index/benches/pq_dist_table.rs | 138 + .../lance-index/benches/residual_transform.rs | 62 + lance-artifact/rust/lance-index/benches/rq.rs | 649 + lance-artifact/rust/lance-index/benches/sq.rs | 114 + .../rust/lance-index/benches/zonemap.rs | 128 + lance-artifact/rust/lance-index/build.rs | 43 + .../rust/lance-index/examples/acorn_bench.rs | 259 + .../lance-index/examples/acorn_bench_sift.rs | 329 + lance-artifact/rust/lance-index/protos | 1 + .../rust/lance-index/protos-cache/cache.proto | 201 + .../rust/lance-index/src/frag_reuse.rs | 106 + lance-artifact/rust/lance-index/src/lib.rs | 197 + .../rust/lance-index/src/mem_wal.rs | 80 + .../rust/lance-index/src/metrics.rs | 4 + .../rust/lance-index/src/optimize.rs | 116 + .../rust/lance-index/src/prefilter.rs | 73 + .../rust/lance-index/src/progress.rs | 54 + .../rust/lance-index/src/registry.rs | 176 + lance-artifact/rust/lance-index/src/scalar.rs | 751 + .../rust/lance-index/src/scalar/bitmap.rs | 2901 +++ .../lance-index/src/scalar/bloomfilter.rs | 2990 +++ .../rust/lance-index/src/scalar/btree.rs | 6505 +++++++ .../rust/lance-index/src/scalar/btree/flat.rs | 601 + .../rust/lance-index/src/scalar/expression.rs | 5621 ++++++ .../rust/lance-index/src/scalar/fmindex.rs | 3358 ++++ .../rust/lance-index/src/scalar/inverted.rs | 392 + .../src/scalar/inverted/builder.rs | 4882 +++++ .../src/scalar/inverted/cache_codec.rs | 1648 ++ .../src/scalar/inverted/compound.rs | 2652 +++ .../src/scalar/inverted/documents.rs | 2254 +++ .../src/scalar/inverted/encoding.rs | 1473 ++ .../lance-index/src/scalar/inverted/impact.rs | 1055 ++ .../lance-index/src/scalar/inverted/index.rs | 14530 ++++++++++++++ .../lance-index/src/scalar/inverted/iter.rs | 217 + .../lance-index/src/scalar/inverted/json.rs | 170 + .../lance-index/src/scalar/inverted/parser.rs | 347 + .../lance-index/src/scalar/inverted/query.rs | 1121 ++ .../lance-index/src/scalar/inverted/scorer.rs | 234 + .../src/scalar/inverted/tokenizer.rs | 1766 ++ .../inverted/tokenizer/document_tokenizer.rs | 393 + .../src/scalar/inverted/tokenizer/jieba.rs | 99 + .../src/scalar/inverted/tokenizer/lindera.rs | 61 + .../lance-index/src/scalar/inverted/wand.rs | 6907 +++++++ .../rust/lance-index/src/scalar/json.rs | 1377 ++ .../rust/lance-index/src/scalar/label_list.rs | 977 + .../lance-index/src/scalar/lance_format.rs | 2020 ++ .../rust/lance-index/src/scalar/ngram.rs | 2652 +++ .../src/scalar/ngram/ngram_regex.rs | 673 + .../rust/lance-index/src/scalar/registry.rs | 367 + .../rust/lance-index/src/scalar/rtree.rs | 1637 ++ .../rust/lance-index/src/scalar/rtree/sort.rs | 13 + .../src/scalar/rtree/sort/hilbert_sort.rs | 326 + .../rust/lance-index/src/scalar/seed.rs | 59 + .../rust/lance-index/src/scalar/zoned.rs | 863 + .../rust/lance-index/src/scalar/zonemap.rs | 4615 +++++ lance-artifact/rust/lance-index/src/traits.rs | 164 + lance-artifact/rust/lance-index/src/vector.rs | 437 + .../rust/lance-index/src/vector/bq.rs | 276 + .../rust/lance-index/src/vector/bq/builder.rs | 1100 ++ .../src/vector/bq/dist_table_quant.rs | 935 + .../rust/lance-index/src/vector/bq/ex_dot.rs | 1078 ++ .../rust/lance-index/src/vector/bq/prune.rs | 527 + .../lance-index/src/vector/bq/rotation.rs | 234 + .../rust/lance-index/src/vector/bq/storage.rs | 4527 +++++ .../lance-index/src/vector/bq/transform.rs | 689 + .../src/vector/distributed/index_merger.rs | 3023 +++ .../lance-index/src/vector/distributed/mod.rs | 7 + .../rust/lance-index/src/vector/flat.rs | 138 + .../rust/lance-index/src/vector/flat/index.rs | 662 + .../lance-index/src/vector/flat/storage.rs | 586 + .../lance-index/src/vector/flat/transform.rs | 47 + .../rust/lance-index/src/vector/graph.rs | 921 + .../lance-index/src/vector/graph/builder.rs | 63 + .../rust/lance-index/src/vector/graph/io.rs | 2 + .../rust/lance-index/src/vector/hnsw.rs | 147 + .../lance-index/src/vector/hnsw/builder.rs | 2677 +++ .../rust/lance-index/src/vector/hnsw/index.rs | 337 + .../lance-index/src/vector/hnsw/online.rs | 797 + .../rust/lance-index/src/vector/ivf.rs | 362 + .../lance-index/src/vector/ivf/builder.rs | 182 + .../lance-index/src/vector/ivf/shuffler.rs | 1279 ++ .../lance-index/src/vector/ivf/storage.rs | 352 + .../lance-index/src/vector/ivf/transform.rs | 178 + .../rust/lance-index/src/vector/kmeans.rs | 1812 ++ .../rust/lance-index/src/vector/pq.rs | 732 + .../rust/lance-index/src/vector/pq/builder.rs | 196 + .../lance-index/src/vector/pq/distance.rs | 380 + .../rust/lance-index/src/vector/pq/storage.rs | 1343 ++ .../lance-index/src/vector/pq/transform.rs | 113 + .../rust/lance-index/src/vector/pq/utils.rs | 96 + .../rust/lance-index/src/vector/quantizer.rs | 420 + .../rust/lance-index/src/vector/residual.rs | 192 + .../rust/lance-index/src/vector/shared/mod.rs | 11 + .../src/vector/shared/partition_merger.rs | 151 + .../rust/lance-index/src/vector/sq.rs | 362 + .../rust/lance-index/src/vector/sq/builder.rs | 36 + .../rust/lance-index/src/vector/sq/storage.rs | 860 + .../lance-index/src/vector/sq/transform.rs | 80 + .../rust/lance-index/src/vector/storage.rs | 788 + .../rust/lance-index/src/vector/transform.rs | 387 + .../rust/lance-index/src/vector/utils.rs | 412 + .../rust/lance-index/src/vector/v3.rs | 5 + .../lance-index/src/vector/v3/shuffler.rs | 1068 ++ .../lance-index/src/vector/v3/subindex.rs | 152 + lance-artifact/rust/lance-io/Cargo.toml | 81 + .../rust}/lance-io/README.md | 0 .../rust/lance-io/benches/scheduler.rs | 308 + .../rust}/lance-io/src/ffi.rs | 2 - .../rust}/lance-io/src/lib.rs | 2 - .../rust}/lance-io/src/local.rs | 2 - .../rust}/lance-io/src/object_reader.rs | 2 - .../rust}/lance-io/src/object_store.rs | 0 .../src/object_store/dynamic_credentials.rs | 2 - .../src/object_store/dynamic_opendal.rs | 0 .../lance-io/src/object_store/list_retry.rs | 2 - .../lance-io/src/object_store/metrics.rs | 2 - .../lance-io/src/object_store/providers.rs | 2 - .../src/object_store/providers/aws.rs | 19 +- .../src/object_store/providers/azure.rs | 2 - .../src/object_store/providers/gcp.rs | 2 - .../src/object_store/providers/goosefs.rs | 2 - .../src/object_store/providers/huggingface.rs | 2 - .../src/object_store/providers/local.rs | 2 - .../src/object_store/providers/memory.rs | 2 - .../src/object_store/providers/oss.rs | 2 - .../object_store/providers/shared_memory.rs | 2 - .../src/object_store/providers/tencent.rs | 2 - .../src/object_store/providers/tos.rs | 2 - .../src/object_store/storage_options.rs | 2 - .../lance-io/src/object_store/test_utils.rs | 2 - .../lance-io/src/object_store/throttle.rs | 2 - .../lance-io/src/object_store/tracing.rs | 2 - .../rust}/lance-io/src/object_writer.rs | 2 - .../rust}/lance-io/src/scheduler.rs | 2 - .../rust}/lance-io/src/scheduler/lite.rs | 2 - .../rust}/lance-io/src/spill.rs | 2 - .../rust}/lance-io/src/stream.rs | 2 - .../rust}/lance-io/src/testing.rs | 2 - .../rust}/lance-io/src/traits.rs | 2 - .../rust}/lance-io/src/uring.rs | 2 - .../lance-io/src/uring/current_thread.rs | 2 - .../src/uring/current_thread_future.rs | 2 - .../rust}/lance-io/src/uring/future.rs | 2 - .../rust}/lance-io/src/uring/reader.rs | 2 - .../rust}/lance-io/src/uring/requests.rs | 2 - .../rust}/lance-io/src/uring/tests.rs | 2 - .../rust}/lance-io/src/uring/thread.rs | 2 - .../rust}/lance-io/src/utils.rs | 2 - .../lance-io/src/utils/tracking_store.rs | 2 - .../rust/lance-io/tests/gcs_integration.rs | 99 + .../lance-io/tests/goosefs_integration.rs | 485 + .../rust/lance-io/tests/tos_integration.rs | 81 + lance-artifact/rust/lance-linalg/Cargo.toml | 64 + lance-artifact/rust/lance-linalg/README.md | 6 + .../rust/lance-linalg/benches/argmin.rs | 65 + .../rust/lance-linalg/benches/cosine.rs | 133 + .../rust/lance-linalg/benches/dist_table.rs | 66 + .../rust/lance-linalg/benches/dot.rs | 185 + .../rust/lance-linalg/benches/l2.rs | 189 + .../rust/lance-linalg/benches/norm_l2.rs | 129 + lance-artifact/rust/lance-linalg/build.rs | 198 + .../proptest-regressions/distance/cosine.txt | 7 + .../rust/lance-linalg/src/clustering.rs | 41 + .../rust/lance-linalg/src/distance.rs | 344 + .../rust/lance-linalg/src/distance/cosine.rs | 1753 ++ .../lance-linalg/src/distance/cosine_u8.rs | 373 + .../rust/lance-linalg/src/distance/dot.rs | 1125 ++ .../rust/lance-linalg/src/distance/dot_u8.rs | 259 + .../rust/lance-linalg/src/distance/hamming.rs | 1854 ++ .../rust/lance-linalg/src/distance/l2.rs | 1414 ++ .../rust/lance-linalg/src/distance/l2_u8.rs | 276 + .../rust/lance-linalg/src/distance/norm_l2.rs | 612 + .../rust/lance-linalg/src/kernels.rs | 610 + lance-artifact/rust/lance-linalg/src/lib.rs | 22 + lance-artifact/rust/lance-linalg/src/simd.rs | 105 + .../rust/lance-linalg/src/simd/bf16.c | 74 + .../rust/lance-linalg/src/simd/dist_table.c | 60 + .../rust/lance-linalg/src/simd/dist_table.rs | 786 + .../rust/lance-linalg/src/simd/f16.c | 86 + .../rust/lance-linalg/src/simd/f32.rs | 1145 ++ .../rust/lance-linalg/src/simd/f64.rs | 860 + .../rust/lance-linalg/src/simd/i32.rs | 321 + .../rust/lance-linalg/src/simd/u8.rs | 442 + .../rust/lance-linalg/src/simd/x86.rs | 89 + .../rust/lance-linalg/src/test_utils.rs | 81 + .../lance-namespace-datafusion/Cargo.toml | 28 + .../rust/lance-namespace-datafusion/README.md | 46 + .../lance-namespace-datafusion/src/catalog.rs | 136 + .../lance-namespace-datafusion/src/error.rs | 10 + .../lance-namespace-datafusion/src/lib.rs | 13 + .../src/namespace_level.rs | 127 + .../lance-namespace-datafusion/src/schema.rs | 80 + .../src/session_builder.rs | 199 + .../lance-namespace-datafusion/tests/sql.rs | 382 + .../rust/lance-namespace-impls/BENCHMARK.md | 73 + .../rust/lance-namespace-impls/Cargo.toml | 115 + .../rust/lance-namespace-impls/README.md | 81 + .../benches/manifest_commit_sweep.sh | 146 + .../examples/manifest_bench.rs | 714 + .../rust/lance-namespace-impls/src/connect.rs | 295 + .../rust/lance-namespace-impls/src/context.rs | 161 + .../lance-namespace-impls/src/credentials.rs | 861 + .../src/credentials/aws.rs | 1276 ++ .../src/credentials/azure.rs | 1338 ++ .../src/credentials/cache.rs | 454 + .../src/credentials/gcp.rs | 1401 ++ .../rust/lance-namespace-impls/src/dir.rs | 15708 ++++++++++++++++ .../lance-namespace-impls/src/dir/manifest.rs | 6119 ++++++ .../src/dir/manifest_feature_flags.rs | 194 + .../rust/lance-namespace-impls/src/lib.rs | 118 + .../rust/lance-namespace-impls/src/rest.rs | 2217 +++ .../lance-namespace-impls/src/rest_adapter.rs | 3928 ++++ .../rust/lance-namespace/Cargo.toml | 23 + lance-artifact/rust/lance-namespace/README.md | 44 + .../rust/lance-namespace/src/error.rs | 464 + .../rust/lance-namespace/src/lib.rs | 40 + .../rust/lance-namespace/src/namespace.rs | 558 + .../rust/lance-namespace/src/schema.rs | 829 + lance-artifact/rust/lance-select/Cargo.toml | 39 + .../lance-select/benches/index_expr_result.rs | 148 + .../lance-select/benches/row_addr_mask.rs | 266 + lance-artifact/rust/lance-select/src/lib.rs | 27 + lance-artifact/rust/lance-select/src/mask.rs | 2547 +++ .../rust/lance-select/src/mask/nullable.rs | 820 + .../rust/lance-select/src/result.rs | 536 + lance-artifact/rust/lance-table/Cargo.toml | 78 + lance-artifact/rust/lance-table/README.md | 6 + .../lance-table/benches/manifest_intern.rs | 263 + .../rust/lance-table/benches/row_id_index.rs | 323 + lance-artifact/rust/lance-table/build.rs | 29 + lance-artifact/rust/lance-table/protos | 1 + .../rust/lance-table/src/feature_flags.rs | 308 + lance-artifact/rust/lance-table/src/format.rs | 71 + .../rust/lance-table/src/format/fragment.rs | 1011 + .../rust/lance-table/src/format/index.rs | 377 + .../rust/lance-table/src/format/manifest.rs | 1633 ++ .../rust/lance-table/src/format/overlay.rs | 443 + .../lance-table/src/format/transaction.rs | 42 + lance-artifact/rust/lance-table/src/io.rs | 6 + .../rust/lance-table/src/io/commit.rs | 2274 +++ .../lance-table/src/io/commit/dynamodb.rs | 495 + .../src/io/commit/external_manifest.rs | 919 + .../rust/lance-table/src/io/deletion.rs | 370 + .../rust/lance-table/src/io/manifest.rs | 339 + lance-artifact/rust/lance-table/src/lib.rs | 9 + lance-artifact/rust/lance-table/src/rowids.rs | 1398 ++ .../rust/lance-table/src/rowids/bitmap.rs | 314 + .../lance-table/src/rowids/encoded_array.rs | 400 + .../rust/lance-table/src/rowids/index.rs | 926 + .../rust/lance-table/src/rowids/segment.rs | 1141 ++ .../rust/lance-table/src/rowids/serde.rs | 641 + .../rust/lance-table/src/rowids/version.rs | 713 + .../rust/lance-table/src/system_index.rs | 15 + .../src/system_index/frag_reuse.rs | 480 + .../lance-table/src/system_index/mem_wal.rs | 434 + lance-artifact/rust/lance-table/src/utils.rs | 49 + .../rust/lance-table/src/utils/stream.rs | 945 + .../rust/lance-test-macros/Cargo.toml | 22 + .../rust/lance-test-macros/README.md | 3 + .../rust/lance-test-macros/src/lib.rs | 145 + lance-artifact/rust/lance-testing/Cargo.toml | 25 + lance-artifact/rust/lance-testing/README.md | 3 + .../rust/lance-testing/src/datagen.rs | 289 + lance-artifact/rust/lance-testing/src/lib.rs | 7 + .../rust/lance-testing/src/pprof.rs | 57 + .../rust/lance-testing/src/progress.rs | 49 + .../rust/lance-tokenizer/Cargo.toml | 49 + lance-artifact/rust/lance-tokenizer/README.md | 3 + .../rust/lance-tokenizer/src/alphanum_only.rs | 60 + .../rust/lance-tokenizer/src/analyzer.rs | 92 + .../src/ascii_folding_filter.rs | 160 + .../lance-tokenizer/src/code_tokenizer.rs | 191 + .../rust/lance-tokenizer/src/icu.rs | 204 + .../rust/lance-tokenizer/src/jieba.rs | 70 + .../rust/lance-tokenizer/src/lib.rs | 43 + .../rust/lance-tokenizer/src/lindera.rs | 97 + .../rust/lance-tokenizer/src/lower_caser.rs | 112 + .../lance-tokenizer/src/ngram_tokenizer.rs | 221 + .../rust/lance-tokenizer/src/raw_tokenizer.rs | 51 + .../rust/lance-tokenizer/src/remove_long.rs | 76 + .../lance-tokenizer/src/simple_tokenizer.rs | 68 + .../rust/lance-tokenizer/src/stemmer.rs | 157 + .../lance-tokenizer/src/stop_word_filter.rs | 185 + .../src/stop_word_filter/stopwords.rs | 1900 ++ .../rust/lance-tokenizer/src/tokenizer_api.rs | 146 + .../src/whitespace_tokenizer.rs | 68 + .../src/word_delimiter_filter.rs | 297 + lance-artifact/rust/lance-tools/Cargo.toml | 25 + lance-artifact/rust/lance-tools/README.md | 3 + lance-artifact/rust/lance-tools/src/cli.rs | 52 + lance-artifact/rust/lance-tools/src/lib.rs | 6 + lance-artifact/rust/lance-tools/src/main.rs | 82 + lance-artifact/rust/lance-tools/src/meta.rs | 58 + lance-artifact/rust/lance-tools/src/util.rs | 60 + lance-artifact/rust/lance/Cargo.toml | 327 + lance-artifact/rust/lance/README.md | 88 + .../rust/lance/benches/concurrent_append.rs | 454 + .../rust/lance/benches/count_pushdown.rs | 128 + .../lance/benches/distributed_vector_build.rs | 452 + .../rust/lance/benches/fts_search.rs | 460 + lance-artifact/rust/lance/benches/hamming.rs | 228 + lance-artifact/rust/lance/benches/ivf_pq.rs | 143 + .../rust/lance/benches/manifest_commit.rs | 371 + .../benches/mem_wal/fts/LuceneFtsBench.java | Bin 0 -> 13363 bytes .../mem_wal/fts/mem_wal_fineweb_fts.rs | 983 + .../benches/mem_wal/fts/mem_wal_fts_bench.rs | 837 + .../mem_wal/fts/mem_wal_fts_read_bench.rs | 885 + .../benches/mem_wal/fts/run_fineweb_fts.sh | 119 + .../benches/mem_wal/fts/run_fts_compare.sh | 166 + .../benches/mem_wal/fts/run_fts_read_sweep.sh | 172 + .../mem_wal/kv/mem_wal_kv_point_lookup.rs | 1991 ++ .../benches/mem_wal/kv/run_kv_compare.sh | 134 + .../mem_wal_point_lookup_bench.rs | 536 + .../benches/mem_wal/read/memtable_read.rs | 1062 ++ .../mem_wal/vector/hnsw/disk_ann_compare.py | 372 + .../mem_wal/vector/hnsw/mem_wal_hnsw_bench.rs | 364 + .../vector/hnsw/mem_wal_hnswlib_bench.cpp | 403 + .../vector/hnsw/mem_wal_recall_hnsw.rs | 823 + .../vector/hnsw/run_mem_wal_hnsw_compare.sh | 59 + .../mem_wal/vector/hnsw/run_parity_suite.sh | 99 + .../mem_wal/vector/mem_wal_index_micro.rs | 468 + .../mem_wal/vector/mem_wal_vector_bench.rs | 940 + .../benches/mem_wal/write/mem_wal_replay.rs | 353 + .../mem_wal_shard_writer_backpressure.rs | 1054 ++ .../benches/mem_wal/write/mem_wal_write.rs | 704 + .../write/run_shard_writer_backpressure.sh | 120 + .../rust/lance/benches/merge_insert.rs | 325 + .../rust/lance/benches/random_access.rs | 167 + .../rust/lance/benches/regex_ngram.rs | 134 + .../benches/s3_file_reader_diagnostics.rs | 2357 +++ .../rust/lance/benches/scalar_index.rs | 235 + lance-artifact/rust/lance/benches/scan.rs | 134 + .../lance/benches/streaming_ivf_training.rs | 114 + lance-artifact/rust/lance/benches/take.rs | 471 + .../rust/lance/benches/take_blob.rs | 318 + .../rust/lance/benches/vector_index.rs | 213 + .../rust/lance/benches/vector_throughput.rs | 360 + lance-artifact/rust/lance/build.rs | 24 + lance-artifact/rust/lance/protos | 1 + lance-artifact/rust/lance/src/arrow.rs | 16 + lance-artifact/rust/lance/src/arrow/json.rs | 535 + .../rust/lance/src/bin/fm_contains_bench.rs | 610 + .../rust/lance/src/bin/fm_index_tool.rs | 161 + lance-artifact/rust/lance/src/bin/lq.rs | 181 + lance-artifact/rust/lance/src/blob.rs | 1497 ++ lance-artifact/rust/lance/src/datafusion.rs | 9 + .../rust/lance/src/datafusion/dataframe.rs | 312 + .../rust/lance/src/datafusion/logical_plan.rs | 222 + lance-artifact/rust/lance/src/dataset.rs | 4031 ++++ lance-artifact/rust/lance/src/dataset/blob.rs | 7950 ++++++++ .../rust/lance/src/dataset/branch_location.rs | 377 + .../rust/lance/src/dataset/builder.rs | 901 + .../rust/lance/src/dataset/cleanup.rs | 4388 +++++ .../rust/lance/src/dataset/delta.rs | 1604 ++ .../rust/lance/src/dataset/files.rs | 1187 ++ .../rust/lance/src/dataset/files/arrow.rs | 128 + .../lance/src/dataset/files/file_types.rs | 33 + .../rust/lance/src/dataset/fragment.rs | 6212 ++++++ .../lance/src/dataset/fragment/session.rs | 326 + .../rust/lance/src/dataset/fragment/write.rs | 766 + .../rust/lance/src/dataset/hash_joiner.rs | 442 + .../rust/lance/src/dataset/index.rs | 368 + .../lance/src/dataset/index/frag_reuse.rs | 599 + .../rust/lance/src/dataset/mem_wal.rs | 108 + .../rust/lance/src/dataset/mem_wal/api.rs | 875 + .../lance/src/dataset/mem_wal/hnsw/graph.rs | 1211 ++ .../lance/src/dataset/mem_wal/hnsw/mod.rs | 22 + .../lance/src/dataset/mem_wal/hnsw/storage.rs | 590 + .../rust/lance/src/dataset/mem_wal/index.rs | 2189 +++ .../dataset/mem_wal/index/arena_skiplist.rs | 607 + .../lance/src/dataset/mem_wal/index/btree.rs | 1354 ++ .../lance/src/dataset/mem_wal/index/fts.rs | 6267 ++++++ .../lance/src/dataset/mem_wal/index/hnsw.rs | 779 + .../lance/src/dataset/mem_wal/index/pk_key.rs | 204 + .../lance/src/dataset/mem_wal/manifest.rs | 837 + .../lance/src/dataset/mem_wal/memtable.rs | 1099 ++ .../dataset/mem_wal/memtable/batch_store.rs | 1398 ++ .../src/dataset/mem_wal/memtable/flush.rs | 2241 +++ .../src/dataset/mem_wal/memtable/scanner.rs | 40 + .../mem_wal/memtable/scanner/builder.rs | 2578 +++ .../dataset/mem_wal/memtable/scanner/exec.rs | 76 + .../scanner/exec/brute_force_vector.rs | 884 + .../mem_wal/memtable/scanner/exec/btree.rs | 695 + .../memtable/scanner/exec/dedup_scan.rs | 435 + .../mem_wal/memtable/scanner/exec/fts.rs | 882 + .../mem_wal/memtable/scanner/exec/scan.rs | 536 + .../mem_wal/memtable/scanner/exec/vector.rs | 383 + .../rust/lance/src/dataset/mem_wal/scanner.rs | 82 + .../src/dataset/mem_wal/scanner/block_list.rs | 830 + .../src/dataset/mem_wal/scanner/builder.rs | 2178 +++ .../src/dataset/mem_wal/scanner/collector.rs | 510 + .../dataset/mem_wal/scanner/data_source.rs | 291 + .../lance/src/dataset/mem_wal/scanner/exec.rs | 27 + .../mem_wal/scanner/exec/bloom_guard.rs | 374 + .../mem_wal/scanner/exec/coalesce_first.rs | 421 + .../mem_wal/scanner/exec/generation_tag.rs | 282 + .../src/dataset/mem_wal/scanner/exec/pk.rs | 135 + .../mem_wal/scanner/exec/pk_block_filter.rs | 374 + .../src/dataset/mem_wal/scanner/fts_search.rs | 2804 +++ .../src/dataset/mem_wal/scanner/planner.rs | 2287 +++ .../dataset/mem_wal/scanner/point_lookup.rs | 2296 +++ .../src/dataset/mem_wal/scanner/projection.rs | 323 + .../dataset/mem_wal/scanner/sstable_cache.rs | 415 + .../dataset/mem_wal/scanner/vector_search.rs | 3367 ++++ .../lance/src/dataset/mem_wal/sharding.rs | 542 + .../lance/src/dataset/mem_wal/test_util.rs | 244 + .../rust/lance/src/dataset/mem_wal/util.rs | 427 + .../rust/lance/src/dataset/mem_wal/wal.rs | 2667 +++ .../rust/lance/src/dataset/mem_wal/write.rs | 8827 +++++++++ .../rust/lance/src/dataset/metadata.rs | 880 + .../rust/lance/src/dataset/optimize.rs | 9009 +++++++++ .../lance/src/dataset/optimize/binary_copy.rs | 550 + .../lance/src/dataset/optimize/remapping.rs | 597 + .../src/dataset/optimize/tests/binary_copy.rs | 837 + .../rust/lance/src/dataset/overlay.rs | 1433 ++ .../rust/lance/src/dataset/progress.rs | 93 + lance-artifact/rust/lance/src/dataset/refs.rs | 1500 ++ .../rust/lance/src/dataset/rowids.rs | 969 + .../rust/lance/src/dataset/scanner.rs | 15128 +++++++++++++++ .../lance/src/dataset/schema_evolution.rs | 3965 ++++ .../src/dataset/schema_evolution/optimize.rs | 153 + lance-artifact/rust/lance/src/dataset/sql.rs | 452 + .../rust/lance/src/dataset/statistics.rs | 264 + lance-artifact/rust/lance/src/dataset/take.rs | 1476 ++ .../src/dataset/tests/dataset_aggregate.rs | 1643 ++ .../lance/src/dataset/tests/dataset_common.rs | 118 + .../tests/dataset_concurrency_store.rs | 534 + .../lance/src/dataset/tests/dataset_geo.rs | 232 + .../lance/src/dataset/tests/dataset_index.rs | 4825 +++++ .../lance/src/dataset/tests/dataset_io.rs | 2620 +++ .../src/dataset/tests/dataset_merge_update.rs | 4375 +++++ .../src/dataset/tests/dataset_migrations.rs | 513 + .../tests/dataset_overlay_index_masking.rs | 2170 +++ .../src/dataset/tests/dataset_scanner.rs | 1047 + .../dataset/tests/dataset_schema_evolution.rs | 639 + .../src/dataset/tests/dataset_transactions.rs | 890 + .../src/dataset/tests/dataset_versioning.rs | 1192 ++ .../tests/fragment_validate_tombstones.rs | 60 + .../rust/lance/src/dataset/tests/mod.rs | 19 + .../rust/lance/src/dataset/transaction.rs | 6986 +++++++ lance-artifact/rust/lance/src/dataset/udtf.rs | 346 + .../rust/lance/src/dataset/updater.rs | 516 + .../rust/lance/src/dataset/utils.rs | 310 + .../rust/lance/src/dataset/versions/mod.rs | 248 + .../rust/lance/src/dataset/write.rs | 4621 +++++ .../rust/lance/src/dataset/write/commit.rs | 1198 ++ .../rust/lance/src/dataset/write/delete.rs | 1072 ++ .../rust/lance/src/dataset/write/insert.rs | 792 + .../lance/src/dataset/write/merge_insert.rs | 12683 +++++++++++++ .../write/merge_insert/assign_action.rs | 165 + .../src/dataset/write/merge_insert/exec.rs | 108 + .../dataset/write/merge_insert/exec/delete.rs | 368 + .../dataset/write/merge_insert/exec/write.rs | 1312 ++ .../write/merge_insert/inserted_rows.rs | 713 + .../write/merge_insert/logical_plan.rs | 278 + .../rust/lance/src/dataset/write/retry.rs | 107 + .../rust/lance/src/dataset/write/update.rs | 1948 ++ lance-artifact/rust/lance/src/index.rs | 9267 +++++++++ lance-artifact/rust/lance/src/index/api.rs | 338 + lance-artifact/rust/lance/src/index/append.rs | 4139 ++++ lance-artifact/rust/lance/src/index/create.rs | 3704 ++++ .../rust/lance/src/index/frag_reuse.rs | 178 + .../rust/lance/src/index/mem_wal.rs | 567 + .../rust/lance/src/index/prefilter.rs | 671 + lance-artifact/rust/lance/src/index/scalar.rs | 2506 +++ .../rust/lance/src/index/scalar/bitmap.rs | 77 + .../lance/src/index/scalar/bloomfilter.rs | 99 + .../rust/lance/src/index/scalar/btree.rs | 166 + .../rust/lance/src/index/scalar/fmindex.rs | 118 + .../rust/lance/src/index/scalar/inverted.rs | 1069 ++ .../rust/lance/src/index/scalar/label_list.rs | 149 + .../rust/lance/src/index/scalar/ngram.rs | 170 + .../rust/lance/src/index/scalar/rtree.rs | 93 + .../rust/lance/src/index/scalar/zonemap.rs | 87 + .../rust/lance/src/index/scalar_logical.rs | 1899 ++ lance-artifact/rust/lance/src/index/vector.rs | 3892 ++++ .../rust/lance/src/index/vector/builder.rs | 2477 +++ .../rust/lance/src/index/vector/details.rs | 1546 ++ .../lance/src/index/vector/fixture_test.rs | 290 + .../rust/lance/src/index/vector/hamming.rs | 1304 ++ .../rust/lance/src/index/vector/ivf.rs | 6945 +++++++ .../lance/src/index/vector/ivf/builder.rs | 305 + .../rust/lance/src/index/vector/ivf/io.rs | 1014 + .../src/index/vector/ivf/partition_serde.rs | 1194 ++ .../rust/lance/src/index/vector/ivf/v2.rs | 7448 ++++++++ .../rust/lance/src/index/vector/pq.rs | 1007 + .../rust/lance/src/index/vector/utils.rs | 1539 ++ lance-artifact/rust/lance/src/io.rs | 17 + lance-artifact/rust/lance/src/io/commit.rs | 2961 +++ .../lance/src/io/commit/conflict_resolver.rs | 4820 +++++ .../rust/lance/src/io/commit/dynamodb.rs | 387 + .../lance/src/io/commit/external_manifest.rs | 1023 + .../lance/src/io/commit/namespace_manifest.rs | 298 + .../rust/lance/src/io/commit/s3_test.rs | 354 + lance-artifact/rust/lance/src/io/deletion.rs | 43 + lance-artifact/rust/lance/src/io/exec.rs | 57 + .../rust/lance/src/io/exec/ann_proto.rs | 745 + .../rust/lance/src/io/exec/count_from_mask.rs | 707 + .../rust/lance/src/io/exec/count_pushdown.rs | 832 + .../rust/lance/src/io/exec/filter.rs | 109 + .../rust/lance/src/io/exec/filtered_read.rs | 5842 ++++++ .../lance/src/io/exec/filtered_read_proto.rs | 830 + lance-artifact/rust/lance/src/io/exec/fts.rs | 4615 +++++ lance-artifact/rust/lance/src/io/exec/knn.rs | 3969 ++++ .../rust/lance/src/io/exec/optimizer.rs | 413 + .../rust/lance/src/io/exec/projection.rs | 381 + .../rust/lance/src/io/exec/pushdown_scan.rs | 1547 ++ .../rust/lance/src/io/exec/rowids.rs | 757 + .../rust/lance/src/io/exec/scalar_index.rs | 1315 ++ lance-artifact/rust/lance/src/io/exec/scan.rs | 889 + .../lance/src/io/exec/table_identifier.rs | 186 + lance-artifact/rust/lance/src/io/exec/take.rs | 1263 ++ .../rust/lance/src/io/exec/testing.rs | 81 + .../rust/lance/src/io/exec/utils.rs | 750 + .../rust/lance/src/io/object_store.rs | 5 + lance-artifact/rust/lance/src/lib.rs | 123 + lance-artifact/rust/lance/src/metrics.md | 40 + lance-artifact/rust/lance/src/metrics.rs | 10 + lance-artifact/rust/lance/src/session.rs | 473 + .../rust/lance/src/session/caches.rs | 294 + .../rust/lance/src/session/index_caches.rs | 211 + .../rust/lance/src/session/index_extension.rs | 421 + lance-artifact/rust/lance/src/table.rs | 4 + lance-artifact/rust/lance/src/utils.rs | 9 + lance-artifact/rust/lance/src/utils/future.rs | 103 + .../rust/lance/src/utils/temporal.rs | 35 + lance-artifact/rust/lance/src/utils/test.rs | 937 + .../lance/src/utils/test/failing_store.rs | 108 + .../lance/src/utils/test/serializing_cache.rs | 245 + .../lance/src/utils/test/throttle_store.rs | 22 + lance-artifact/rust/lance/tests/README.md | 19 + .../rust/lance/tests/count_pushdown/mod.rs | 183 + .../rust/lance/tests/integration_tests.rs | 11 + .../rust/lance/tests/mem_wal/mod.rs | 131 + .../rust/lance/tests/query/inverted.rs | 1589 ++ lance-artifact/rust/lance/tests/query/mod.rs | 279 + .../rust/lance/tests/query/primitives.rs | 603 + .../rust/lance/tests/query/vectors.rs | 64 + .../rust/lance/tests/resource_test/mod.rs | 5 + .../rust/lance/tests/resource_test/utils.rs | 192 + .../rust/lance/tests/resource_test/vector.rs | 81 + .../rust/lance/tests/resource_test/write.rs | 45 + .../rust/lance/tests/resource_tests.rs | 7 + .../rust/lance/tests/scalar_index_spill.rs | 69 + lance-artifact/rust/lance/tests/utils/mod.rs | 334 + lance-artifact/rust/license_header.txt | 2 + vendor/lance-io/Cargo.toml | 74 - 837 files changed, 646376 insertions(+), 182 deletions(-) create mode 100644 lance-artifact/Cargo.toml rename {vendor/lance-io => lance-artifact}/LANCEDB_PATCH.md (53%) create mode 100644 lance-artifact/LICENSE create mode 100644 lance-artifact/README.md create mode 100644 lance-artifact/protos/AGENTS.md create mode 120000 lance-artifact/protos/CLAUDE.md create mode 100644 lance-artifact/protos/ann.proto create mode 100644 lance-artifact/protos/encodings_v2_0.proto create mode 100644 lance-artifact/protos/encodings_v2_1.proto create mode 100644 lance-artifact/protos/file.proto create mode 100644 lance-artifact/protos/file2.proto create mode 100644 lance-artifact/protos/filtered_read.proto create mode 100644 lance-artifact/protos/index.proto create mode 100644 lance-artifact/protos/index_old.proto create mode 100644 lance-artifact/protos/license_header.txt create mode 100644 lance-artifact/protos/rowids.proto create mode 100644 lance-artifact/protos/table.proto create mode 100644 lance-artifact/protos/table_identifier.proto create mode 100644 lance-artifact/protos/transaction.proto create mode 100644 lance-artifact/rust/.gitignore create mode 100644 lance-artifact/rust/AGENTS.md create mode 120000 lance-artifact/rust/CLAUDE.md create mode 100644 lance-artifact/rust/CONTRIBUTING.md create mode 100644 lance-artifact/rust/README.md create mode 100644 lance-artifact/rust/arrow-scalar/Cargo.toml create mode 100644 lance-artifact/rust/arrow-scalar/README.md create mode 100644 lance-artifact/rust/arrow-scalar/src/convert.rs create mode 100644 lance-artifact/rust/arrow-scalar/src/lib.rs create mode 100644 lance-artifact/rust/arrow-scalar/src/serde.rs create mode 100644 lance-artifact/rust/arrow-stats/Cargo.toml create mode 100644 lance-artifact/rust/arrow-stats/README.md create mode 100644 lance-artifact/rust/arrow-stats/proptest-regressions/lib.txt create mode 100644 lance-artifact/rust/arrow-stats/src/lib.rs create mode 100644 lance-artifact/rust/arrow-stats/src/nan.rs create mode 100644 lance-artifact/rust/compression/bitpacking/Cargo.toml create mode 100644 lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker4x.rs create mode 100644 lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker8x.rs create mode 100644 lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/macros.rs create mode 100644 lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/mod.rs create mode 100644 lance-artifact/rust/compression/bitpacking/src/lib.rs create mode 100644 lance-artifact/rust/compression/fsst/Cargo.toml create mode 100644 lance-artifact/rust/compression/fsst/examples/benchmark.rs create mode 100644 lance-artifact/rust/compression/fsst/src/fsst.rs create mode 100644 lance-artifact/rust/compression/fsst/src/lib.rs create mode 100644 lance-artifact/rust/examples/Cargo.toml create mode 100644 lance-artifact/rust/examples/src/full_text_search.rs create mode 100644 lance-artifact/rust/examples/src/hnsw.rs create mode 100644 lance-artifact/rust/examples/src/ivf_hnsw.rs create mode 100644 lance-artifact/rust/examples/src/llm_dataset_creation.rs create mode 100644 lance-artifact/rust/examples/src/write_read_ds.rs create mode 100644 lance-artifact/rust/img.png create mode 100644 lance-artifact/rust/lance-arrow/Cargo.toml create mode 100644 lance-artifact/rust/lance-arrow/README.md create mode 100644 lance-artifact/rust/lance-arrow/src/bfloat16.rs create mode 100644 lance-artifact/rust/lance-arrow/src/deepcopy.rs create mode 100644 lance-artifact/rust/lance-arrow/src/floats.rs create mode 100644 lance-artifact/rust/lance-arrow/src/ipc.rs create mode 100644 lance-artifact/rust/lance-arrow/src/json.rs create mode 100644 lance-artifact/rust/lance-arrow/src/lib.rs create mode 100644 lance-artifact/rust/lance-arrow/src/list.rs create mode 100644 lance-artifact/rust/lance-arrow/src/memory.rs create mode 100644 lance-artifact/rust/lance-arrow/src/scalar.rs create mode 100644 lance-artifact/rust/lance-arrow/src/schema.rs create mode 100644 lance-artifact/rust/lance-arrow/src/stream.rs create mode 100644 lance-artifact/rust/lance-arrow/src/struct.rs create mode 100644 lance-artifact/rust/lance-core/Cargo.toml create mode 100644 lance-artifact/rust/lance-core/benches/cache_keys.rs create mode 100644 lance-artifact/rust/lance-core/proptest-regressions/io/writer/statistics.txt create mode 100644 lance-artifact/rust/lance-core/proptest-regressions/utils/mask.txt create mode 100644 lance-artifact/rust/lance-core/src/cache/backend.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/backend_uri.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/codec.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/entry_io.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/key.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/mod.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/moka.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/quick.rs create mode 100644 lance-artifact/rust/lance-core/src/cache/registry.rs create mode 100644 lance-artifact/rust/lance-core/src/container.rs create mode 100644 lance-artifact/rust/lance-core/src/container/list.rs create mode 100644 lance-artifact/rust/lance-core/src/datatypes.rs create mode 100644 lance-artifact/rust/lance-core/src/datatypes/field.rs create mode 100644 lance-artifact/rust/lance-core/src/datatypes/schema.rs create mode 100644 lance-artifact/rust/lance-core/src/deepsize.rs create mode 100644 lance-artifact/rust/lance-core/src/error.rs create mode 100644 lance-artifact/rust/lance-core/src/levenshtein.rs create mode 100644 lance-artifact/rust/lance-core/src/lib.rs create mode 100644 lance-artifact/rust/lance-core/src/traits.rs create mode 100644 lance-artifact/rust/lance-core/src/utils.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/address.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/aimd.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/assume.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/backoff.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/bit.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/blob.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/bloomfilter.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/bloomfilter/as_bytes.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/bloomfilter/sbbf.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/cpu.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/deletion.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/futures.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/hash.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/io_stats.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/parse.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/path.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/row_addr_remap.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/tempfile.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/testing.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/tokio.rs create mode 100644 lance-artifact/rust/lance-core/src/utils/tracing.rs create mode 100644 lance-artifact/rust/lance-core/tests/cache_key_allocations.rs create mode 100644 lance-artifact/rust/lance-datafusion/Cargo.toml create mode 100644 lance-artifact/rust/lance-datafusion/build.rs create mode 120000 lance-artifact/rust/lance-datafusion/protos create mode 100644 lance-artifact/rust/lance-datafusion/src/aggregate.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/chunker.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/dataframe.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/datagen.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/exec.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/expr.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/lib.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/logical_expr.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/planner.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/projection.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/spill.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/sql.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/substrait.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/udf.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/udf/json.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/utils.rs create mode 100644 lance-artifact/rust/lance-datafusion/src/utils/background_iterator.rs create mode 100644 lance-artifact/rust/lance-datagen/Cargo.toml create mode 100644 lance-artifact/rust/lance-datagen/benches/array_gen.rs create mode 100644 lance-artifact/rust/lance-datagen/src/generator.rs create mode 100644 lance-artifact/rust/lance-datagen/src/lib.rs create mode 100644 lance-artifact/rust/lance-derive/Cargo.toml create mode 100644 lance-artifact/rust/lance-derive/src/lib.rs create mode 100644 lance-artifact/rust/lance-encoding/.gitignore create mode 100644 lance-artifact/rust/lance-encoding/Cargo.toml create mode 100644 lance-artifact/rust/lance-encoding/README.md create mode 100644 lance-artifact/rust/lance-encoding/benches/buffer.rs create mode 100644 lance-artifact/rust/lance-encoding/benches/common/mod.rs create mode 100644 lance-artifact/rust/lance-encoding/benches/decoder.rs create mode 100644 lance-artifact/rust/lance-encoding/benches/encoder.rs create mode 100644 lance-artifact/rust/lance-encoding/build.rs create mode 120000 lance-artifact/rust/lance-encoding/protos create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical/binary.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical/blob.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical/list.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical/primitive.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/logical/struct.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/basic.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/binary.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitmap.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitpack.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/block.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/dictionary.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_binary.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_list.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/fsst.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/packed_struct.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/physical/value.rs create mode 100644 lance-artifact/rust/lance-encoding/src/array_encoding/strategy.rs create mode 100644 lance-artifact/rust/lance-encoding/src/buffer.rs create mode 100644 lance-artifact/rust/lance-encoding/src/compression.rs create mode 100644 lance-artifact/rust/lance-encoding/src/compression_config.rs create mode 100644 lance-artifact/rust/lance-encoding/src/constants.rs create mode 100644 lance-artifact/rust/lance-encoding/src/data.rs create mode 100644 lance-artifact/rust/lance-encoding/src/decoder.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encoder.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encoder/structural.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/fuzz_tests.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/blob.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/fixed_size_list.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/list.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/map.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/blob.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/chunk_index.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/constant.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/dict.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/fullzip.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/layout.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/miniblock.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse/writer.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/logical/struct.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/binary.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/bitpacking.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/block.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/byte_stream_split.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/constant.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/fsst.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/general.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/packed.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/rle.rs create mode 100644 lance-artifact/rust/lance-encoding/src/encodings/physical/value.rs create mode 100644 lance-artifact/rust/lance-encoding/src/format.rs create mode 100644 lance-artifact/rust/lance-encoding/src/lib.rs create mode 100644 lance-artifact/rust/lance-encoding/src/repdef.rs create mode 100644 lance-artifact/rust/lance-encoding/src/statistics.rs create mode 100644 lance-artifact/rust/lance-encoding/src/testing.rs create mode 100644 lance-artifact/rust/lance-encoding/src/utils.rs create mode 100644 lance-artifact/rust/lance-encoding/src/utils/accumulation.rs create mode 100644 lance-artifact/rust/lance-encoding/src/utils/bytepack.rs create mode 100644 lance-artifact/rust/lance-file/Cargo.toml create mode 100644 lance-artifact/rust/lance-file/README.md create mode 100644 lance-artifact/rust/lance-file/benches/reader.rs create mode 100644 lance-artifact/rust/lance-file/benches/schema.rs create mode 100644 lance-artifact/rust/lance-file/build.rs create mode 120000 lance-artifact/rust/lance-file/protos create mode 100644 lance-artifact/rust/lance-file/src/compatibility_tests.rs create mode 100644 lance-artifact/rust/lance-file/src/datatypes.rs create mode 100644 lance-artifact/rust/lance-file/src/format.rs create mode 100644 lance-artifact/rust/lance-file/src/io.rs create mode 100644 lance-artifact/rust/lance-file/src/lib.rs create mode 100644 lance-artifact/rust/lance-file/src/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/reader/structural.rs create mode 100644 lance-artifact/rust/lance-file/src/testing.rs create mode 100644 lance-artifact/rust/lance-file/src/version.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/encoding.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/encoding/binary.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/encoding/dictionary.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/encoding/plain.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/format/metadata.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/format/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/page_table.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/writer/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v1/writer/statistics.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_0/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_0/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_0/writer.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_1/compression.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_1/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_1/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_1/writer.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_2/compression.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_2/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_2/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_2/writer.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_3/compression.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_3/mod.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_3/reader.rs create mode 100644 lance-artifact/rust/lance-file/src/versions/v2_3/writer.rs create mode 100644 lance-artifact/rust/lance-file/src/writer.rs create mode 100644 lance-artifact/rust/lance-file/src/writer/structural.rs create mode 100644 lance-artifact/rust/lance-file/src/writer_tests.rs create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/README.md create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/datagen.py create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/datagen.rs create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v1.lance create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v2_0.lance create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v2_0_mini.lance create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v2_0_self_described.lance create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v2_1.lance create mode 100644 lance-artifact/rust/lance-file/test_data/exact_versions/v2_2.lance create mode 100644 lance-artifact/rust/lance-geo/Cargo.toml create mode 100644 lance-artifact/rust/lance-geo/src/bbox.rs create mode 100644 lance-artifact/rust/lance-geo/src/lib.rs create mode 100644 lance-artifact/rust/lance-geo/src/udf.rs create mode 100644 lance-artifact/rust/lance-index-core/Cargo.toml create mode 100644 lance-artifact/rust/lance-index-core/README.md create mode 100644 lance-artifact/rust/lance-index-core/src/lib.rs create mode 100644 lance-artifact/rust/lance-index-core/src/metrics.rs create mode 100644 lance-artifact/rust/lance-index-core/src/scalar.rs create mode 100644 lance-artifact/rust/lance-index/Cargo.toml create mode 100644 lance-artifact/rust/lance-index/README.md create mode 100644 lance-artifact/rust/lance-index/benches/4bitpq_dist_table.rs create mode 100644 lance-artifact/rust/lance-index/benches/bitmap.rs create mode 100644 lance-artifact/rust/lance-index/benches/btree.rs create mode 100644 lance-artifact/rust/lance-index/benches/common.rs create mode 100644 lance-artifact/rust/lance-index/benches/compute_partition.rs create mode 100644 lance-artifact/rust/lance-index/benches/find_partitions.rs create mode 100644 lance-artifact/rust/lance-index/benches/geo.rs create mode 100644 lance-artifact/rust/lance-index/benches/hnsw.rs create mode 100644 lance-artifact/rust/lance-index/benches/inverted.rs create mode 100644 lance-artifact/rust/lance-index/benches/kmeans.rs create mode 100644 lance-artifact/rust/lance-index/benches/ngram.rs create mode 100644 lance-artifact/rust/lance-index/benches/pq_assignment.rs create mode 100644 lance-artifact/rust/lance-index/benches/pq_dist_table.rs create mode 100644 lance-artifact/rust/lance-index/benches/residual_transform.rs create mode 100644 lance-artifact/rust/lance-index/benches/rq.rs create mode 100644 lance-artifact/rust/lance-index/benches/sq.rs create mode 100644 lance-artifact/rust/lance-index/benches/zonemap.rs create mode 100644 lance-artifact/rust/lance-index/build.rs create mode 100644 lance-artifact/rust/lance-index/examples/acorn_bench.rs create mode 100644 lance-artifact/rust/lance-index/examples/acorn_bench_sift.rs create mode 120000 lance-artifact/rust/lance-index/protos create mode 100644 lance-artifact/rust/lance-index/protos-cache/cache.proto create mode 100644 lance-artifact/rust/lance-index/src/frag_reuse.rs create mode 100644 lance-artifact/rust/lance-index/src/lib.rs create mode 100644 lance-artifact/rust/lance-index/src/mem_wal.rs create mode 100644 lance-artifact/rust/lance-index/src/metrics.rs create mode 100644 lance-artifact/rust/lance-index/src/optimize.rs create mode 100644 lance-artifact/rust/lance-index/src/prefilter.rs create mode 100644 lance-artifact/rust/lance-index/src/progress.rs create mode 100644 lance-artifact/rust/lance-index/src/registry.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/bitmap.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/bloomfilter.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/btree.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/btree/flat.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/expression.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/fmindex.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/cache_codec.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/compound.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/documents.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/encoding.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/impact.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/index.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/iter.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/json.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/parser.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/query.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/scorer.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/tokenizer.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/tokenizer/document_tokenizer.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/tokenizer/jieba.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/tokenizer/lindera.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/inverted/wand.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/json.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/label_list.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/lance_format.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/ngram.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/ngram/ngram_regex.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/registry.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/rtree.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/rtree/sort.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/rtree/sort/hilbert_sort.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/seed.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/zoned.rs create mode 100644 lance-artifact/rust/lance-index/src/scalar/zonemap.rs create mode 100644 lance-artifact/rust/lance-index/src/traits.rs create mode 100644 lance-artifact/rust/lance-index/src/vector.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/dist_table_quant.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/ex_dot.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/prune.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/rotation.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/bq/transform.rs create mode 100755 lance-artifact/rust/lance-index/src/vector/distributed/index_merger.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/distributed/mod.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/flat.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/flat/index.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/flat/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/flat/transform.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/graph.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/graph/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/graph/io.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/hnsw.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/hnsw/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/hnsw/index.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/hnsw/online.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/ivf.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/ivf/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/ivf/shuffler.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/ivf/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/ivf/transform.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/kmeans.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq/distance.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq/transform.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/pq/utils.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/quantizer.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/residual.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/shared/mod.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/shared/partition_merger.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/sq.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/sq/builder.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/sq/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/sq/transform.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/storage.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/transform.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/utils.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/v3.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/v3/shuffler.rs create mode 100644 lance-artifact/rust/lance-index/src/vector/v3/subindex.rs create mode 100644 lance-artifact/rust/lance-io/Cargo.toml rename {vendor => lance-artifact/rust}/lance-io/README.md (100%) create mode 100644 lance-artifact/rust/lance-io/benches/scheduler.rs rename {vendor => lance-artifact/rust}/lance-io/src/ffi.rs (94%) rename {vendor => lance-artifact/rust}/lance-io/src/lib.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/local.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_reader.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store.rs (100%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/dynamic_credentials.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/dynamic_opendal.rs (100%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/list_retry.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/metrics.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/aws.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/azure.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/gcp.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/goosefs.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/huggingface.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/local.rs (97%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/memory.rs (96%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/oss.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/shared_memory.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/tencent.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/providers/tos.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/storage_options.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/test_utils.rs (86%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/throttle.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_store/tracing.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/object_writer.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/scheduler.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/scheduler/lite.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/spill.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/stream.rs (95%) rename {vendor => lance-artifact/rust}/lance-io/src/testing.rs (95%) rename {vendor => lance-artifact/rust}/lance-io/src/traits.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/uring.rs (97%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/current_thread.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/current_thread_future.rs (97%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/future.rs (94%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/reader.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/requests.rs (94%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/tests.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/uring/thread.rs (99%) rename {vendor => lance-artifact/rust}/lance-io/src/utils.rs (98%) rename {vendor => lance-artifact/rust}/lance-io/src/utils/tracking_store.rs (99%) create mode 100644 lance-artifact/rust/lance-io/tests/gcs_integration.rs create mode 100644 lance-artifact/rust/lance-io/tests/goosefs_integration.rs create mode 100644 lance-artifact/rust/lance-io/tests/tos_integration.rs create mode 100644 lance-artifact/rust/lance-linalg/Cargo.toml create mode 100644 lance-artifact/rust/lance-linalg/README.md create mode 100644 lance-artifact/rust/lance-linalg/benches/argmin.rs create mode 100644 lance-artifact/rust/lance-linalg/benches/cosine.rs create mode 100644 lance-artifact/rust/lance-linalg/benches/dist_table.rs create mode 100644 lance-artifact/rust/lance-linalg/benches/dot.rs create mode 100644 lance-artifact/rust/lance-linalg/benches/l2.rs create mode 100644 lance-artifact/rust/lance-linalg/benches/norm_l2.rs create mode 100644 lance-artifact/rust/lance-linalg/build.rs create mode 100644 lance-artifact/rust/lance-linalg/proptest-regressions/distance/cosine.txt create mode 100644 lance-artifact/rust/lance-linalg/src/clustering.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/cosine.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/cosine_u8.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/dot.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/dot_u8.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/hamming.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/l2.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/l2_u8.rs create mode 100644 lance-artifact/rust/lance-linalg/src/distance/norm_l2.rs create mode 100644 lance-artifact/rust/lance-linalg/src/kernels.rs create mode 100644 lance-artifact/rust/lance-linalg/src/lib.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/bf16.c create mode 100644 lance-artifact/rust/lance-linalg/src/simd/dist_table.c create mode 100644 lance-artifact/rust/lance-linalg/src/simd/dist_table.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/f16.c create mode 100644 lance-artifact/rust/lance-linalg/src/simd/f32.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/f64.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/i32.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/u8.rs create mode 100644 lance-artifact/rust/lance-linalg/src/simd/x86.rs create mode 100644 lance-artifact/rust/lance-linalg/src/test_utils.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/Cargo.toml create mode 100755 lance-artifact/rust/lance-namespace-datafusion/README.md create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/catalog.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/error.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/lib.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/namespace_level.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/schema.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/src/session_builder.rs create mode 100755 lance-artifact/rust/lance-namespace-datafusion/tests/sql.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/BENCHMARK.md create mode 100644 lance-artifact/rust/lance-namespace-impls/Cargo.toml create mode 100644 lance-artifact/rust/lance-namespace-impls/README.md create mode 100755 lance-artifact/rust/lance-namespace-impls/benches/manifest_commit_sweep.sh create mode 100644 lance-artifact/rust/lance-namespace-impls/examples/manifest_bench.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/connect.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/context.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/credentials.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/credentials/aws.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/credentials/azure.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/credentials/cache.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/credentials/gcp.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/dir.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/dir/manifest.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/dir/manifest_feature_flags.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/lib.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/rest.rs create mode 100644 lance-artifact/rust/lance-namespace-impls/src/rest_adapter.rs create mode 100644 lance-artifact/rust/lance-namespace/Cargo.toml create mode 100644 lance-artifact/rust/lance-namespace/README.md create mode 100644 lance-artifact/rust/lance-namespace/src/error.rs create mode 100644 lance-artifact/rust/lance-namespace/src/lib.rs create mode 100644 lance-artifact/rust/lance-namespace/src/namespace.rs create mode 100644 lance-artifact/rust/lance-namespace/src/schema.rs create mode 100644 lance-artifact/rust/lance-select/Cargo.toml create mode 100644 lance-artifact/rust/lance-select/benches/index_expr_result.rs create mode 100644 lance-artifact/rust/lance-select/benches/row_addr_mask.rs create mode 100644 lance-artifact/rust/lance-select/src/lib.rs create mode 100644 lance-artifact/rust/lance-select/src/mask.rs create mode 100644 lance-artifact/rust/lance-select/src/mask/nullable.rs create mode 100644 lance-artifact/rust/lance-select/src/result.rs create mode 100644 lance-artifact/rust/lance-table/Cargo.toml create mode 100644 lance-artifact/rust/lance-table/README.md create mode 100644 lance-artifact/rust/lance-table/benches/manifest_intern.rs create mode 100644 lance-artifact/rust/lance-table/benches/row_id_index.rs create mode 100644 lance-artifact/rust/lance-table/build.rs create mode 120000 lance-artifact/rust/lance-table/protos create mode 100644 lance-artifact/rust/lance-table/src/feature_flags.rs create mode 100644 lance-artifact/rust/lance-table/src/format.rs create mode 100644 lance-artifact/rust/lance-table/src/format/fragment.rs create mode 100644 lance-artifact/rust/lance-table/src/format/index.rs create mode 100644 lance-artifact/rust/lance-table/src/format/manifest.rs create mode 100644 lance-artifact/rust/lance-table/src/format/overlay.rs create mode 100755 lance-artifact/rust/lance-table/src/format/transaction.rs create mode 100644 lance-artifact/rust/lance-table/src/io.rs create mode 100644 lance-artifact/rust/lance-table/src/io/commit.rs create mode 100644 lance-artifact/rust/lance-table/src/io/commit/dynamodb.rs create mode 100644 lance-artifact/rust/lance-table/src/io/commit/external_manifest.rs create mode 100644 lance-artifact/rust/lance-table/src/io/deletion.rs create mode 100644 lance-artifact/rust/lance-table/src/io/manifest.rs create mode 100644 lance-artifact/rust/lance-table/src/lib.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/bitmap.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/encoded_array.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/index.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/segment.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/serde.rs create mode 100644 lance-artifact/rust/lance-table/src/rowids/version.rs create mode 100644 lance-artifact/rust/lance-table/src/system_index.rs create mode 100644 lance-artifact/rust/lance-table/src/system_index/frag_reuse.rs create mode 100644 lance-artifact/rust/lance-table/src/system_index/mem_wal.rs create mode 100644 lance-artifact/rust/lance-table/src/utils.rs create mode 100644 lance-artifact/rust/lance-table/src/utils/stream.rs create mode 100644 lance-artifact/rust/lance-test-macros/Cargo.toml create mode 100644 lance-artifact/rust/lance-test-macros/README.md create mode 100644 lance-artifact/rust/lance-test-macros/src/lib.rs create mode 100644 lance-artifact/rust/lance-testing/Cargo.toml create mode 100644 lance-artifact/rust/lance-testing/README.md create mode 100644 lance-artifact/rust/lance-testing/src/datagen.rs create mode 100644 lance-artifact/rust/lance-testing/src/lib.rs create mode 100644 lance-artifact/rust/lance-testing/src/pprof.rs create mode 100644 lance-artifact/rust/lance-testing/src/progress.rs create mode 100644 lance-artifact/rust/lance-tokenizer/Cargo.toml create mode 100644 lance-artifact/rust/lance-tokenizer/README.md create mode 100644 lance-artifact/rust/lance-tokenizer/src/alphanum_only.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/analyzer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/ascii_folding_filter.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/code_tokenizer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/icu.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/jieba.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/lib.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/lindera.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/lower_caser.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/ngram_tokenizer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/raw_tokenizer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/remove_long.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/simple_tokenizer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/stemmer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/stop_word_filter.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/stop_word_filter/stopwords.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/tokenizer_api.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/whitespace_tokenizer.rs create mode 100644 lance-artifact/rust/lance-tokenizer/src/word_delimiter_filter.rs create mode 100644 lance-artifact/rust/lance-tools/Cargo.toml create mode 100644 lance-artifact/rust/lance-tools/README.md create mode 100644 lance-artifact/rust/lance-tools/src/cli.rs create mode 100644 lance-artifact/rust/lance-tools/src/lib.rs create mode 100644 lance-artifact/rust/lance-tools/src/main.rs create mode 100644 lance-artifact/rust/lance-tools/src/meta.rs create mode 100644 lance-artifact/rust/lance-tools/src/util.rs create mode 100644 lance-artifact/rust/lance/Cargo.toml create mode 100644 lance-artifact/rust/lance/README.md create mode 100644 lance-artifact/rust/lance/benches/concurrent_append.rs create mode 100644 lance-artifact/rust/lance/benches/count_pushdown.rs create mode 100644 lance-artifact/rust/lance/benches/distributed_vector_build.rs create mode 100644 lance-artifact/rust/lance/benches/fts_search.rs create mode 100644 lance-artifact/rust/lance/benches/hamming.rs create mode 100644 lance-artifact/rust/lance/benches/ivf_pq.rs create mode 100644 lance-artifact/rust/lance/benches/manifest_commit.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/fts/LuceneFtsBench.java create mode 100644 lance-artifact/rust/lance/benches/mem_wal/fts/mem_wal_fineweb_fts.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/fts/mem_wal_fts_bench.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/fts/mem_wal_fts_read_bench.rs create mode 100755 lance-artifact/rust/lance/benches/mem_wal/fts/run_fineweb_fts.sh create mode 100755 lance-artifact/rust/lance/benches/mem_wal/fts/run_fts_compare.sh create mode 100755 lance-artifact/rust/lance/benches/mem_wal/fts/run_fts_read_sweep.sh create mode 100644 lance-artifact/rust/lance/benches/mem_wal/kv/mem_wal_kv_point_lookup.rs create mode 100755 lance-artifact/rust/lance/benches/mem_wal/kv/run_kv_compare.sh create mode 100644 lance-artifact/rust/lance/benches/mem_wal/point_lookup/mem_wal_point_lookup_bench.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/read/memtable_read.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/disk_ann_compare.py create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/mem_wal_hnsw_bench.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/mem_wal_hnswlib_bench.cpp create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/mem_wal_recall_hnsw.rs create mode 100755 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/run_mem_wal_hnsw_compare.sh create mode 100755 lance-artifact/rust/lance/benches/mem_wal/vector/hnsw/run_parity_suite.sh create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/mem_wal_index_micro.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/vector/mem_wal_vector_bench.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/write/mem_wal_replay.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/write/mem_wal_shard_writer_backpressure.rs create mode 100644 lance-artifact/rust/lance/benches/mem_wal/write/mem_wal_write.rs create mode 100755 lance-artifact/rust/lance/benches/mem_wal/write/run_shard_writer_backpressure.sh create mode 100644 lance-artifact/rust/lance/benches/merge_insert.rs create mode 100644 lance-artifact/rust/lance/benches/random_access.rs create mode 100644 lance-artifact/rust/lance/benches/regex_ngram.rs create mode 100644 lance-artifact/rust/lance/benches/s3_file_reader_diagnostics.rs create mode 100644 lance-artifact/rust/lance/benches/scalar_index.rs create mode 100644 lance-artifact/rust/lance/benches/scan.rs create mode 100644 lance-artifact/rust/lance/benches/streaming_ivf_training.rs create mode 100644 lance-artifact/rust/lance/benches/take.rs create mode 100644 lance-artifact/rust/lance/benches/take_blob.rs create mode 100644 lance-artifact/rust/lance/benches/vector_index.rs create mode 100644 lance-artifact/rust/lance/benches/vector_throughput.rs create mode 100644 lance-artifact/rust/lance/build.rs create mode 120000 lance-artifact/rust/lance/protos create mode 100644 lance-artifact/rust/lance/src/arrow.rs create mode 100644 lance-artifact/rust/lance/src/arrow/json.rs create mode 100644 lance-artifact/rust/lance/src/bin/fm_contains_bench.rs create mode 100644 lance-artifact/rust/lance/src/bin/fm_index_tool.rs create mode 100644 lance-artifact/rust/lance/src/bin/lq.rs create mode 100644 lance-artifact/rust/lance/src/blob.rs create mode 100644 lance-artifact/rust/lance/src/datafusion.rs create mode 100644 lance-artifact/rust/lance/src/datafusion/dataframe.rs create mode 100644 lance-artifact/rust/lance/src/datafusion/logical_plan.rs create mode 100644 lance-artifact/rust/lance/src/dataset.rs create mode 100644 lance-artifact/rust/lance/src/dataset/blob.rs create mode 100644 lance-artifact/rust/lance/src/dataset/branch_location.rs create mode 100644 lance-artifact/rust/lance/src/dataset/builder.rs create mode 100644 lance-artifact/rust/lance/src/dataset/cleanup.rs create mode 100644 lance-artifact/rust/lance/src/dataset/delta.rs create mode 100644 lance-artifact/rust/lance/src/dataset/files.rs create mode 100644 lance-artifact/rust/lance/src/dataset/files/arrow.rs create mode 100644 lance-artifact/rust/lance/src/dataset/files/file_types.rs create mode 100644 lance-artifact/rust/lance/src/dataset/fragment.rs create mode 100644 lance-artifact/rust/lance/src/dataset/fragment/session.rs create mode 100644 lance-artifact/rust/lance/src/dataset/fragment/write.rs create mode 100644 lance-artifact/rust/lance/src/dataset/hash_joiner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/index.rs create mode 100644 lance-artifact/rust/lance/src/dataset/index/frag_reuse.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/api.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/hnsw/graph.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/hnsw/mod.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/hnsw/storage.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index/arena_skiplist.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index/btree.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index/fts.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index/hnsw.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/index/pk_key.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/manifest.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/batch_store.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/flush.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/builder.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/brute_force_vector.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/btree.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/dedup_scan.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/fts.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/scan.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/memtable/scanner/exec/vector.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/block_list.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/builder.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/collector.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/data_source.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec/bloom_guard.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec/coalesce_first.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec/generation_tag.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec/pk.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/exec/pk_block_filter.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/fts_search.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/planner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/point_lookup.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/projection.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/sstable_cache.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/scanner/vector_search.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/sharding.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/test_util.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/util.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/wal.rs create mode 100644 lance-artifact/rust/lance/src/dataset/mem_wal/write.rs create mode 100644 lance-artifact/rust/lance/src/dataset/metadata.rs create mode 100644 lance-artifact/rust/lance/src/dataset/optimize.rs create mode 100644 lance-artifact/rust/lance/src/dataset/optimize/binary_copy.rs create mode 100644 lance-artifact/rust/lance/src/dataset/optimize/remapping.rs create mode 100644 lance-artifact/rust/lance/src/dataset/optimize/tests/binary_copy.rs create mode 100644 lance-artifact/rust/lance/src/dataset/overlay.rs create mode 100644 lance-artifact/rust/lance/src/dataset/progress.rs create mode 100644 lance-artifact/rust/lance/src/dataset/refs.rs create mode 100644 lance-artifact/rust/lance/src/dataset/rowids.rs create mode 100644 lance-artifact/rust/lance/src/dataset/scanner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/schema_evolution.rs create mode 100644 lance-artifact/rust/lance/src/dataset/schema_evolution/optimize.rs create mode 100644 lance-artifact/rust/lance/src/dataset/sql.rs create mode 100644 lance-artifact/rust/lance/src/dataset/statistics.rs create mode 100644 lance-artifact/rust/lance/src/dataset/take.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_aggregate.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_common.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_concurrency_store.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_geo.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_index.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_io.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_merge_update.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_migrations.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_overlay_index_masking.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_scanner.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_schema_evolution.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_transactions.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/dataset_versioning.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/fragment_validate_tombstones.rs create mode 100644 lance-artifact/rust/lance/src/dataset/tests/mod.rs create mode 100644 lance-artifact/rust/lance/src/dataset/transaction.rs create mode 100644 lance-artifact/rust/lance/src/dataset/udtf.rs create mode 100644 lance-artifact/rust/lance/src/dataset/updater.rs create mode 100644 lance-artifact/rust/lance/src/dataset/utils.rs create mode 100644 lance-artifact/rust/lance/src/dataset/versions/mod.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/commit.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/delete.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/insert.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/assign_action.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/exec.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/exec/delete.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/exec/write.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/inserted_rows.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/merge_insert/logical_plan.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/retry.rs create mode 100644 lance-artifact/rust/lance/src/dataset/write/update.rs create mode 100644 lance-artifact/rust/lance/src/index.rs create mode 100644 lance-artifact/rust/lance/src/index/api.rs create mode 100644 lance-artifact/rust/lance/src/index/append.rs create mode 100644 lance-artifact/rust/lance/src/index/create.rs create mode 100644 lance-artifact/rust/lance/src/index/frag_reuse.rs create mode 100644 lance-artifact/rust/lance/src/index/mem_wal.rs create mode 100644 lance-artifact/rust/lance/src/index/prefilter.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/bitmap.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/bloomfilter.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/btree.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/fmindex.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/inverted.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/label_list.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/ngram.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/rtree.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar/zonemap.rs create mode 100644 lance-artifact/rust/lance/src/index/scalar_logical.rs create mode 100644 lance-artifact/rust/lance/src/index/vector.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/builder.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/details.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/fixture_test.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/hamming.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/ivf.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/ivf/builder.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/ivf/io.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/ivf/partition_serde.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/ivf/v2.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/pq.rs create mode 100644 lance-artifact/rust/lance/src/index/vector/utils.rs create mode 100644 lance-artifact/rust/lance/src/io.rs create mode 100644 lance-artifact/rust/lance/src/io/commit.rs create mode 100644 lance-artifact/rust/lance/src/io/commit/conflict_resolver.rs create mode 100644 lance-artifact/rust/lance/src/io/commit/dynamodb.rs create mode 100644 lance-artifact/rust/lance/src/io/commit/external_manifest.rs create mode 100644 lance-artifact/rust/lance/src/io/commit/namespace_manifest.rs create mode 100644 lance-artifact/rust/lance/src/io/commit/s3_test.rs create mode 100644 lance-artifact/rust/lance/src/io/deletion.rs create mode 100644 lance-artifact/rust/lance/src/io/exec.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/ann_proto.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/count_from_mask.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/count_pushdown.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/filter.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/filtered_read.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/filtered_read_proto.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/fts.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/knn.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/optimizer.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/projection.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/pushdown_scan.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/rowids.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/scalar_index.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/scan.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/table_identifier.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/take.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/testing.rs create mode 100644 lance-artifact/rust/lance/src/io/exec/utils.rs create mode 100644 lance-artifact/rust/lance/src/io/object_store.rs create mode 100644 lance-artifact/rust/lance/src/lib.rs create mode 100644 lance-artifact/rust/lance/src/metrics.md create mode 100644 lance-artifact/rust/lance/src/metrics.rs create mode 100644 lance-artifact/rust/lance/src/session.rs create mode 100644 lance-artifact/rust/lance/src/session/caches.rs create mode 100644 lance-artifact/rust/lance/src/session/index_caches.rs create mode 100644 lance-artifact/rust/lance/src/session/index_extension.rs create mode 100644 lance-artifact/rust/lance/src/table.rs create mode 100644 lance-artifact/rust/lance/src/utils.rs create mode 100644 lance-artifact/rust/lance/src/utils/future.rs create mode 100644 lance-artifact/rust/lance/src/utils/temporal.rs create mode 100644 lance-artifact/rust/lance/src/utils/test.rs create mode 100644 lance-artifact/rust/lance/src/utils/test/failing_store.rs create mode 100644 lance-artifact/rust/lance/src/utils/test/serializing_cache.rs create mode 100644 lance-artifact/rust/lance/src/utils/test/throttle_store.rs create mode 100644 lance-artifact/rust/lance/tests/README.md create mode 100644 lance-artifact/rust/lance/tests/count_pushdown/mod.rs create mode 100644 lance-artifact/rust/lance/tests/integration_tests.rs create mode 100644 lance-artifact/rust/lance/tests/mem_wal/mod.rs create mode 100644 lance-artifact/rust/lance/tests/query/inverted.rs create mode 100644 lance-artifact/rust/lance/tests/query/mod.rs create mode 100644 lance-artifact/rust/lance/tests/query/primitives.rs create mode 100644 lance-artifact/rust/lance/tests/query/vectors.rs create mode 100644 lance-artifact/rust/lance/tests/resource_test/mod.rs create mode 100644 lance-artifact/rust/lance/tests/resource_test/utils.rs create mode 100644 lance-artifact/rust/lance/tests/resource_test/vector.rs create mode 100644 lance-artifact/rust/lance/tests/resource_test/write.rs create mode 100644 lance-artifact/rust/lance/tests/resource_tests.rs create mode 100644 lance-artifact/rust/lance/tests/scalar_index_spill.rs create mode 100644 lance-artifact/rust/lance/tests/utils/mod.rs create mode 100644 lance-artifact/rust/license_header.txt delete mode 100644 vendor/lance-io/Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 3b1e74919..262df6b6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3456,7 +3456,6 @@ checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" [[package]] name = "fsst" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "rand 0.9.5", @@ -4816,7 +4815,6 @@ checksum = "e037a2e1d8d5fdbd49b16a4ea09d5d6401c1f29eca5ff29d03d3824dba16256a" [[package]] name = "lance" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arc-swap", "arrow", @@ -4891,7 +4889,6 @@ dependencies = [ [[package]] name = "lance-arrow" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-buffer", @@ -4913,7 +4910,6 @@ dependencies = [ [[package]] name = "lance-arrow-scalar" version = "58.0.0" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-buffer", @@ -4927,7 +4923,6 @@ dependencies = [ [[package]] name = "lance-arrow-stats" version = "58.0.0" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-schema", @@ -4937,7 +4932,6 @@ dependencies = [ [[package]] name = "lance-bitpacking" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrayref", "crunchy", @@ -4948,7 +4942,6 @@ dependencies = [ [[package]] name = "lance-core" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-buffer", @@ -4989,7 +4982,6 @@ dependencies = [ [[package]] name = "lance-datafusion" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow", "arrow-array", @@ -5020,7 +5012,6 @@ dependencies = [ [[package]] name = "lance-datagen" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow", "arrow-array", @@ -5038,7 +5029,6 @@ dependencies = [ [[package]] name = "lance-derive" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "proc-macro2", "quote", @@ -5048,7 +5038,6 @@ dependencies = [ [[package]] name = "lance-encoding" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-arith", "arrow-array", @@ -5083,7 +5072,6 @@ dependencies = [ [[package]] name = "lance-file" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-arith", "arrow-array", @@ -5115,7 +5103,6 @@ dependencies = [ [[package]] name = "lance-index" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arc-swap", "arrow", @@ -5183,7 +5170,6 @@ dependencies = [ [[package]] name = "lance-index-core" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-schema", @@ -5242,7 +5228,6 @@ dependencies = [ [[package]] name = "lance-linalg" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-buffer", @@ -5259,7 +5244,6 @@ dependencies = [ [[package]] name = "lance-namespace" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow", "async-trait", @@ -5272,7 +5256,6 @@ dependencies = [ [[package]] name = "lance-namespace-impls" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow", "arrow-ipc", @@ -5327,7 +5310,6 @@ dependencies = [ [[package]] name = "lance-select" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-buffer", @@ -5343,7 +5325,6 @@ dependencies = [ [[package]] name = "lance-table" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow", "arrow-array", @@ -5383,7 +5364,6 @@ dependencies = [ [[package]] name = "lance-testing" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "arrow-array", "arrow-schema", @@ -5397,7 +5377,6 @@ dependencies = [ [[package]] name = "lance-tokenizer" version = "11.0.0-beta.2" -source = "git+https://github.com/lance-format/lance.git?tag=v11.0.0-beta.2#35da5d920159b49d1b53032652f7615ab699c160" dependencies = [ "frostem", "icu_segmenter", diff --git a/Cargo.toml b/Cargo.toml index 3ca51a022..fa4946ba3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = ["rust/lancedb", "nodejs", "python"] -exclude = ["vendor/lance-io"] +exclude = ["lance-artifact"] resolver = "2" [workspace.package] @@ -69,7 +69,20 @@ semver = "1.0.25" chrono = "0.4" [patch."https://github.com/lance-format/lance.git"] -lance-io = { path = "vendor/lance-io" } +lance = { path = "lance-artifact/rust/lance" } +lance-arrow = { path = "lance-artifact/rust/lance-arrow" } +lance-core = { path = "lance-artifact/rust/lance-core" } +lance-datafusion = { path = "lance-artifact/rust/lance-datafusion" } +lance-datagen = { path = "lance-artifact/rust/lance-datagen" } +lance-encoding = { path = "lance-artifact/rust/lance-encoding" } +lance-file = { path = "lance-artifact/rust/lance-file" } +lance-index = { path = "lance-artifact/rust/lance-index" } +lance-io = { path = "lance-artifact/rust/lance-io" } +lance-linalg = { path = "lance-artifact/rust/lance-linalg" } +lance-namespace = { path = "lance-artifact/rust/lance-namespace" } +lance-namespace-impls = { path = "lance-artifact/rust/lance-namespace-impls" } +lance-table = { path = "lance-artifact/rust/lance-table" } +lance-testing = { path = "lance-artifact/rust/lance-testing" } [profile.ci] debug = "line-tables-only" diff --git a/lance-artifact/Cargo.toml b/lance-artifact/Cargo.toml new file mode 100644 index 000000000..83abe056d --- /dev/null +++ b/lance-artifact/Cargo.toml @@ -0,0 +1,266 @@ +[workspace] +members = [ + "rust/examples", + "rust/lance", + "rust/lance-arrow", + "rust/lance-core", + "rust/lance-datagen", + "rust/lance-encoding", + "rust/lance-file", + "rust/lance-geo", + "rust/lance-index", + "rust/lance-index-core", + "rust/lance-io", + "rust/lance-linalg", + "rust/lance-namespace", + "rust/lance-namespace-impls", + "rust/lance-namespace-datafusion", + "rust/lance-select", + "rust/lance-tokenizer", + "rust/lance-table", + "rust/lance-derive", + "rust/lance-test-macros", + "rust/lance-testing", + "rust/lance-tools", + "rust/compression/fsst", + "rust/compression/bitpacking", + "rust/arrow-scalar", + "rust/arrow-stats", +] +exclude = ["python", "java/lance-jni"] +# Python package needs to be built by maturin. +resolver = "3" + + +[workspace.package] +version = "11.0.0-beta.2" +edition = "2024" +authors = ["Lance Devs "] +license = "Apache-2.0" +repository = "https://github.com/lance-format/lance" +readme = "README.md" +description = "A columnar data format that is 100x faster than Parquet for random access." +keywords = [ + "data-format", + "data-science", + "machine-learning", + "apache-arrow", + "data-analytics", +] +categories = [ + "database-implementations", + "data-structures", + "development-tools", + "science", +] +rust-version = "1.91.0" + +[workspace.dependencies] +arc-swap = "1.7" +libc = "0.2.176" +lance = { version = "=11.0.0-beta.2", path = "./rust/lance", default-features = false } +lance-arrow = { version = "=11.0.0-beta.2", path = "./rust/lance-arrow" } +lance-core = { version = "=11.0.0-beta.2", path = "./rust/lance-core" } +lance-datafusion = { version = "=11.0.0-beta.2", path = "./rust/lance-datafusion" } +lance-datagen = { version = "=11.0.0-beta.2", path = "./rust/lance-datagen" } +lance-derive = { version = "=11.0.0-beta.2", path = "./rust/lance-derive" } +lance-encoding = { version = "=11.0.0-beta.2", path = "./rust/lance-encoding" } +lance-file = { version = "=11.0.0-beta.2", path = "./rust/lance-file" } +lance-geo = { version = "=11.0.0-beta.2", path = "./rust/lance-geo" } +lance-index = { version = "=11.0.0-beta.2", path = "./rust/lance-index" } +lance-index-core = { version = "=11.0.0-beta.2", path = "./rust/lance-index-core" } +lance-io = { version = "=11.0.0-beta.2", path = "./rust/lance-io", default-features = false } +lance-linalg = { version = "=11.0.0-beta.2", path = "./rust/lance-linalg" } +lance-namespace = { version = "=11.0.0-beta.2", path = "./rust/lance-namespace" } +lance-namespace-impls = { version = "=11.0.0-beta.2", path = "./rust/lance-namespace-impls" } +lance-namespace-datafusion = { version = "=7.0.0-beta.9", path = "./rust/lance-namespace-datafusion" } +lance-namespace-reqwest-client = "0.8.6" +lance-select = { version = "=11.0.0-beta.2", path = "./rust/lance-select" } +lance-tokenizer = { version = "=11.0.0-beta.2", path = "./rust/lance-tokenizer" } +lance-table = { version = "=11.0.0-beta.2", path = "./rust/lance-table" } +lance-test-macros = { version = "=11.0.0-beta.2", path = "./rust/lance-test-macros" } +lance-testing = { version = "=11.0.0-beta.2", path = "./rust/lance-testing" } +approx = "0.5.1" +# Note that this one does not include pyarrow +arrow = { version = "58.0.0", optional = false, features = ["prettyprint"] } +lance-arrow-scalar = { version = "=58.0.0", path = "./rust/arrow-scalar" } +lance-arrow-stats = { version = "=58.0.0", path = "./rust/arrow-stats" } +arrow-arith = "58.0.0" +arrow-array = "58.0.0" +arrow-buffer = "58.0.0" +arrow-cast = "58.0.0" +arrow-data = "58.0.0" +arrow-ipc = { version = "58.0.0", features = ["zstd"] } +arrow-ord = "58.0.0" +arrow-row = "58.0.0" +arrow-schema = "58.0.0" +arrow-select = "58.0.0" +async-recursion = "1.0" +async-trait = "0.1" +axum = "0.7" +aws-config = "1.2.0" +aws-credential-types = "1.2.0" +aws-sdk-dynamodb = { version = "1.38.0", default-features = false } +aws-sdk-s3 = { version = "1.38.0", default-features = false } +half = { "version" = "2.1", default-features = false, features = [ + "num-traits", + "std", + "bytemuck", +] } +lance-bitpacking = { version = "=11.0.0-beta.2", path = "./rust/compression/bitpacking" } +bitpacking = "0.9" +bitvec = "1" +blake3 = "1.8.5" +bytemuck = { version = "1", default-features = false, features = [ + "extern_crate_alloc", +] } +bytes = "1.11.1" +byteorder = "1.5" +clap = { version = "4", features = ["derive"] } +chrono = { version = "0.4.41", default-features = false, features = [ + "std", + "now", + "serde", +] } +criterion = { version = "0.8.2", features = [ + "async", + "async_tokio", + "html_reports", +] } +crossbeam-queue = "0.3" +crossbeam-skiplist = "0.1" +datafusion = { version = "54.0.0", default-features = false, features = [ + "crypto_expressions", + "datetime_expressions", + "encoding_expressions", + "nested_expressions", + "regex_expressions", + "sql", + "string_expressions", + "unicode_expressions", +] } +datafusion-common = "54.0.0" +datafusion-functions = { version = "54.0.0", default-features = false, features = ["regex_expressions"] } +datafusion-sql = "54.0.0" +datafusion-expr = "54.0.0" +datafusion-ffi = "54.0.0" +datafusion-physical-expr = "54.0.0" +datafusion-physical-plan = "54.0.0" +datafusion-substrait = { version = "54.0.0", default-features = false } +dirs = "6.0.0" +either = "1.0" +fst = { version = "0.4.7", features = ["levenshtein"] } +fsst = { version = "=11.0.0-beta.2", path = "./rust/compression/fsst" } +futures = "0.3" +geoarrow-array = "0.8" +geoarrow-schema = "0.8" +geodatafusion = "0.5.0" +geo-traits = "0.3.0" +geo-types = "0.7.16" +http = "1.1.0" +humantime = "2.2.0" +hyperloglogplus = { version = "0.4.1", features = ["const-loop"] } +icu_segmenter = { version = "2.2", default-features = false, features = ["compiled_data"] } +io-uring = "0.7" +itertools = "0.14" +jieba-rs = { version = "0.10.0", default-features = false } +jsonb = { version = "0.5.3", default-features = false, features = ["databend"] } +libm = "0.2.15" +log = "0.4" +metrics = { version = "0.24" } +metrics-util = { version = "0.19" } +mockall = { version = "0.14.0" } +mock_instant = { version = "0.6.0" } +moka = { version = "0.12", features = ["future", "sync"] } +ndarray = { version = "0.16.1", features = ["matrixmultiply-threading"] } +num-traits = "0.2" +object_store = { version = "0.13.2" } +opendal = { version = "0.58.1" } +object_store_opendal = { version = "0.58" } +pin-project = "1.0" +path_abs = "0.5" +pprof = { version = "0.15.0", features = ["flamegraph"] } +proptest = "1.3.1" +prost = "0.14.1" +prost-build = "0.14.1" +prost-types = "0.14.1" +rand = { version = "0.9.1", features = ["small_rng"] } +rand_distr = { version = "0.5.1" } +rand_xoshiro = "0.7.0" +rangemap = { version = "1.0" } +rayon = "1.10" +regex-syntax = "0.8.10" +roaring = "0.11.4" +rstest = "0.26.1" +serde = { version = "^1" } +serde_json = { version = "1" } +semver = "1.0" +serial_test = "3" +snafu = "0.9" +lindera = { version = "3.0.7" } +tempfile = "3" +test-log = { version = "0.2.15" } +tokio = { version = "1.23", features = [ + "rt-multi-thread", + "macros", + "fs", + "sync", +] } +tokio-stream = "0.1.14" +tokio-util = { version = "0.7.16" } +tower = "0.5" +tower-http = "0.5" +tracing = "0.1" +tracing-mock = { version = "=0.1.0-beta.3" } +twox-hash = "2.0" +url = "2.5.7" +uuid = { version = "1.2", features = ["v4", "serde"] } +wiremock = "0.6" +pretty_assertions = "1.4.0" + +[profile.bench] +opt-level = 3 +debug = true +strip = false + +[profile.ci] +debug = "line-tables-only" +inherits = "dev" +incremental = false + +# This rule applies to every package except workspace members (dependencies +# such as `arrow` and `tokio`). It disables debug info and related features on +# dependencies so their binaries stay smaller, improving cache reuse. +[profile.ci.package."*"] +debug = false +debug-assertions = false +strip = "debuginfo" +incremental = false + +[workspace.lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] } +unsafe_op_in_unsafe_fn = "allow" + +[workspace.lints.clippy] +all = { level = "deny", priority = -1 } +style = { level = "deny", priority = -1 } +cargo = { level = "deny", priority = -1 } +fallible_impl_from = "deny" +manual_let_else = "deny" +redundant_pub_crate = "deny" +string_add_assign = "deny" +string_add = "deny" +string_lit_as_bytes = "deny" +use_self = "deny" +dbg_macro = "deny" +trait_duplication_in_bounds = "deny" +redundant_clone = "deny" +# We should always use log instead of println +print_stdout = "deny" +print_stderr = "deny" +# not too much we can do to avoid multiple crate versions +multiple-crate-versions = "allow" +# We use Vec> in a lot of places and it is very common to use a single range in the vec. +single_range_in_vec_init = "allow" +large_futures = "deny" +disallowed_macros = "deny" diff --git a/vendor/lance-io/LANCEDB_PATCH.md b/lance-artifact/LANCEDB_PATCH.md similarity index 53% rename from vendor/lance-io/LANCEDB_PATCH.md rename to lance-artifact/LANCEDB_PATCH.md index 87aaafa3e..7d8c4a6ca 100644 --- a/vendor/lance-io/LANCEDB_PATCH.md +++ b/lance-artifact/LANCEDB_PATCH.md @@ -1,11 +1,8 @@ # LanceDB patch provenance -This directory vendors `lance-io` 11.0.0-beta.2 from Lance commit -`35da5d920159b49d1b53032652f7615ab699c160`. - -`Cargo.toml` uses the equivalent standalone dependency metadata from the published crate. Upstream -benchmark and integration-test targets are omitted because this copy is compiled only as a patched -dependency; the library sources are otherwise retained. +This artifact vendors the Lance 11.0.0-beta.2 Rust workspace from Lance commit +`35da5d920159b49d1b53032652f7615ab699c160`. The complete workspace keeps all mutually coupled +Lance crates on one Cargo source identity when `lancedb` consumes the pinned artifact commit. The local patch makes AWS credential-family merging atomic before backend selection and teaches the built-in OpenDAL S3 path to refresh credential-only storage options. Keeping the change inside diff --git a/lance-artifact/LICENSE b/lance-artifact/LICENSE new file mode 100644 index 000000000..cd4c38213 --- /dev/null +++ b/lance-artifact/LICENSE @@ -0,0 +1,255 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +-------------------------------------------------------------------------------- + +This project includes code from Ritchie Vink's Polars project, which is licensed +under the MIT license: + + Copyright (c) 2020 Ritchie Vink + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +https://github.com/pola-rs/polars/blob/main/LICENSE + +-------------------------------------------------------------------------------- + +This project includes code adapted from the quickwit-oss/bitpacking crate, which +is licensed under the MIT license: + + Copyright (c) 2016 Paul Masurel + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +https://github.com/quickwit-oss/bitpacking/blob/main/LICENSE diff --git a/lance-artifact/README.md b/lance-artifact/README.md new file mode 100644 index 000000000..08716c84e --- /dev/null +++ b/lance-artifact/README.md @@ -0,0 +1,247 @@ +
+

+ +Lance Logo + +**The Open Lakehouse Format for Multimodal AI**
+**High-performance vector search, full-text search, random access, and feature engineering capabilities for the lakehouse.**
+**Compatible with Pandas, DuckDB, Polars, PyArrow, Ray, Spark, and more integrations on the way.** + +Documentation • +Community • +Discord • +Mailing List + +[CI]: https://github.com/lance-format/lance/actions/workflows/rust.yml +[CI Badge]: https://github.com/lance-format/lance/actions/workflows/rust.yml/badge.svg +[Docs]: https://lance.org +[Docs Badge]: https://img.shields.io/badge/docs-passing-brightgreen +[crates.io]: https://crates.io/crates/lance +[crates.io badge]: https://img.shields.io/crates/v/lance.svg +[Python versions]: https://pypi.org/project/pylance/ +[Python versions badge]: https://img.shields.io/pypi/pyversions/pylance + +[![CI Badge]][CI] +[![Docs Badge]][Docs] +[![crates.io badge]][crates.io] +[![Python versions badge]][Python versions] + +

+
+ +
+ +Lance is an open lakehouse format for multimodal AI. It contains a file format, table format, and catalog spec that allows you to build a complete lakehouse on top of object storage to power your AI workflows. Lance is perfect for: + +1. Building search engines and feature stores with hybrid search capabilities. +2. Large-scale ML training requiring high performance IO and random access. +3. Storing, querying, and managing multimodal data including images, videos, audio, text, and embeddings. + +The key features of Lance include: + +* **Expressive hybrid search:** Combine vector similarity search, full-text search (BM25), and SQL analytics on the same dataset with accelerated secondary indices. + +* **Lightning-fast random access:** 100x faster than Parquet or Iceberg for random access without sacrificing scan performance. + +* **Native multimodal data support:** Store images, videos, audio, text, and embeddings in a single unified format with efficient blob encoding and lazy loading. + +* **Data evolution:** Efficiently add columns with backfilled values without full table rewrites, perfect for ML feature engineering. + +* **Zero-copy versioning:** Automatic versioning with ACID transactions, time travel, tags, and branches—no extra infrastructure needed. + +* **Rich ecosystem integrations:** Apache Arrow, Pandas, Polars, DuckDB, Apache Spark, Ray, Trino, Apache Flink, and open catalogs (Apache Polaris, Unity Catalog, Apache Gravitino). + +For more details, see the full [Lance format specification](https://lance.org/format). + +> [!TIP] +> Lance is in active development and we welcome contributions. Please see our [contributing guide](https://lance.org/community/contributing/) for more information. + +## File format stability + +Lance releases frequently because the SDKs, integrations, and performance work are moving quickly. This does not mean the Lance file format changes incompatibly in every release. The Lance file format is identified by the `data_storage_version` stored in each dataset, and stable storage versions are a long-term compatibility contract. + +* Once a dataset is written with a stable `data_storage_version`, future Lance releases will continue to support reading that storage version. +* SDK and API compatibility is separate from file format compatibility. SDK/API changes follow semantic versioning and are documented in the [migration guide](https://lance.org/guide/migration/). +* Older Lance releases may not understand file format versions introduced later. If you run mixed Lance versions, pin `data_storage_version` for deterministic writes. +* The `next` file format alias is unstable and should only be used for experimentation, never for production data. + +For production, write data with a stable `data_storage_version`. See the [format versioning guide](https://lance.org/format/file/versioning/) for the current compatibility matrix. + +## Quick Start + +**Installation** + +```shell +pip install pylance +``` + +To install a preview release: + +```shell +pip install --pre --extra-index-url https://pypi.fury.io/lance-format pylance +``` + +> [!TIP] +> Preview releases are released more often than full releases and contain the +> latest features and bug fixes. They receive the same level of testing as full releases. +> We guarantee they will remain published and available for download for at +> least 6 months. When you want to pin to a specific version, prefer a stable release. + +**Converting to Lance** + +```python +import lance + +import pandas as pd +import pyarrow as pa +import pyarrow.dataset + +df = pd.DataFrame({"a": [5], "b": [10]}) +uri = "/tmp/test.parquet" +tbl = pa.Table.from_pandas(df) +pa.dataset.write_dataset(tbl, uri, format='parquet') + +parquet = pa.dataset.dataset(uri, format='parquet') +lance.write_dataset(parquet, "/tmp/test.lance") +``` + +**Reading Lance data** +```python +dataset = lance.dataset("/tmp/test.lance") +assert isinstance(dataset, pa.dataset.Dataset) +``` + +**Pandas** +```python +df = dataset.to_table().to_pandas() +df +``` + +**DuckDB** +```python +import duckdb + +# If this segfaults, make sure you have duckdb v0.7+ installed +duckdb.query("SELECT * FROM dataset LIMIT 10").to_df() +``` + +**Vector search** + +Download the sift1m subset + +```shell +wget ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz +tar -xzf sift.tar.gz +``` + +Convert it to Lance + +```python +import lance +from lance.vector import vec_to_table +import numpy as np +import struct + +nvecs = 1000000 +ndims = 128 +with open("sift/sift_base.fvecs", mode="rb") as fobj: + buf = fobj.read() + data = np.array(struct.unpack("<128000000f", buf[4 : 4 + 4 * nvecs * ndims])).reshape((nvecs, ndims)) + dd = dict(zip(range(nvecs), data)) + +table = vec_to_table(dd) +uri = "vec_data.lance" +sift1m = lance.write_dataset(table, uri, max_rows_per_group=8192, max_rows_per_file=1024*1024) +``` + +Build the index + +```python +sift1m.create_index("vector", + index_type="IVF_PQ", + num_partitions=256, # IVF + num_sub_vectors=16) # PQ +``` + +Search the dataset + +```python +# Get top 10 similar vectors +import duckdb + +dataset = lance.dataset(uri) + +# Sample 100 query vectors. If this segfaults, make sure you have duckdb v0.7+ installed +sample = duckdb.query("SELECT vector FROM dataset USING SAMPLE 100").to_df() +query_vectors = np.array([np.array(x) for x in sample.vector]) + +# Get nearest neighbors for all of them +rs = [dataset.to_table(nearest={"column": "vector", "k": 10, "q": q}) + for q in query_vectors] +``` + +## Directory structure + +| Directory | Description | +|--------------------|--------------------------| +| [rust](./rust) | Core Rust implementation | +| [python](./python) | Python bindings (PyO3) | +| [java](./java) | Java bindings (JNI) | +| [docs](./docs) | Documentation source | + +## Benchmarks + +### Vector search + +We used the SIFT dataset to benchmark our results with 1M vectors of 128D + +1. For 100 randomly sampled query vectors, we get <1ms average response time (on a 2023 m2 MacBook Air) + +![avg_latency.png](docs/src/images/avg_latency.png) + +2. ANNs are always a trade-off between recall and performance + +![avg_latency.png](docs/src/images/recall_vs_latency.png) + +### Vs. parquet + +We create a Lance dataset using the Oxford Pet dataset to do some preliminary performance testing of Lance as compared to Parquet and raw image/XMLs. For analytics queries, Lance is 50-100x better than reading the raw metadata. For batched random access, Lance is 100x better than both parquet and raw files. + +![](docs/src/images/lance_perf.png) + +## Why Lance for AI/ML workflows? + +The machine learning development cycle involves multiple stages: + +```mermaid +graph LR + A[Collection] --> B[Exploration]; + B --> C[Analytics]; + C --> D[Feature Engineer]; + D --> E[Training]; + E --> F[Evaluation]; + F --> C; + E --> G[Deployment]; + G --> H[Monitoring]; + H --> A; +``` + +Traditional lakehouse formats were designed for SQL analytics and struggle with AI/ML workloads that require: +- **Vector search** for similarity and semantic retrieval +- **Fast random access** for sampling and interactive exploration +- **Multimodal data** storage (images, videos, audio alongside embeddings) +- **Data evolution** for feature engineering without full table rewrites +- **Hybrid search** combining vectors, full-text, and SQL predicates + +While existing formats (Parquet, Iceberg, Delta Lake) excel at SQL analytics, they require additional specialized systems for AI capabilities. Lance brings these AI-first features directly into the lakehouse format. + +A comparison of different formats across ML development stages: + +| | Lance | Parquet & ORC | JSON & XML | TFRecord | Database | Warehouse | +|---------------------|-------|---------------|------------|----------|----------|-----------| +| Analytics | Fast | Fast | Slow | Slow | Decent | Fast | +| Feature Engineering | Fast | Fast | Decent | Slow | Decent | Good | +| Training | Fast | Decent | Slow | Fast | N/A | N/A | +| Exploration | Fast | Slow | Fast | Slow | Fast | Decent | +| Infra Support | Rich | Rich | Decent | Limited | Rich | Rich | + diff --git a/lance-artifact/protos/AGENTS.md b/lance-artifact/protos/AGENTS.md new file mode 100644 index 000000000..2dba0e23d --- /dev/null +++ b/lance-artifact/protos/AGENTS.md @@ -0,0 +1,19 @@ +# Protobuf Guidelines + +Also see [root AGENTS.md](../AGENTS.md) for cross-language standards. + +## Compatibility + +- Protobuf schemas that are part of a stable file format or any other stable persisted contract must remain backwards compatible. Never reuse or change their existing field numbers. +- Protobuf schemas used exclusively by an unstable file format follow the root file-format stability contract: do not preserve compatibility with prior unstable revisions. Before making a breaking protobuf change, verify that the schema is not shared with a stable format or another persisted contract. + +## Schema Design + +- Use `optional` when you need to distinguish "not set" from "zero value" — `optional` enables presence tracking (`has_*` methods) and maps to `Option` in Rust. Bare proto3 fields have no presence semantics: they always hold a value (defaulting to zero), so you cannot tell if the sender explicitly set them. +- Use structured message types (e.g., `BasePath`) instead of plain scalars, and scope fields to operation-specific messages (e.g., `InsertTransaction`) rather than generic top-level ones. +- Don't duplicate data across messages — store each fact once and derive relationships. Prefer parallel sequences over maps when keys already exist in another field. + +## Documentation + +- Document the semantic meaning of both present and absent states for `optional` fields — explain when each case applies. +- Use precise domain terminology in field descriptions — avoid ambiguous abbreviations or terms that collide with domain concepts. diff --git a/lance-artifact/protos/CLAUDE.md b/lance-artifact/protos/CLAUDE.md new file mode 120000 index 000000000..47dc3e3d8 --- /dev/null +++ b/lance-artifact/protos/CLAUDE.md @@ -0,0 +1 @@ +AGENTS.md \ No newline at end of file diff --git a/lance-artifact/protos/ann.proto b/lance-artifact/protos/ann.proto new file mode 100644 index 000000000..f5de5e25e --- /dev/null +++ b/lance-artifact/protos/ann.proto @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.pb; + +import "table_identifier.proto"; +import "table.proto"; +import "index.proto"; + +// Query-time approximation mode for vector search. +// +// This currently only affects RQ-quantized vector indexes, such as IVF_RQ. +// Other index types ignore this setting. +enum VectorApproxMode { + // Use all RQ bits for query-time scoring with u8-quantized lookup tables. + Normal = 0; + // Use only one RQ bit for query-time scoring, even for multi-bit indexes. + Fast = 1; + // Use all RQ bits for query-time scoring with u16-quantized lookup tables + // to reduce estimator quantization error. + Accurate = 2; +} + +// Serialized vector query parameters. +message VectorQueryProto { + // Query vector as Arrow IPC bytes (supports Float16, Float32, Float64, UInt8, etc.) + bytes query_vector_arrow_ipc = 1; + string column = 2; + uint32 k = 3; + optional float lower_bound = 4; + optional float upper_bound = 5; + optional uint32 minimum_nprobes = 6; + optional uint32 maximum_nprobes = 7; + optional uint32 ef = 8; + optional uint32 refine_factor = 9; + // Distance metric type. Absent means None (use the index's default metric). + optional lance.index.pb.VectorMetricType metric_type = 10; + bool use_index = 11; + optional float dist_q_c = 12; + optional int32 query_parallelism = 13; + // Query-time approximation mode. Currently only affects RQ-quantized vector + // indexes, such as IVF_RQ. Other index types ignore this setting. + VectorApproxMode approx_mode = 14; +} + +// Serializable form of ANNIvfSubIndexExec — the IVF sub-index search node. +// +// The prefilter child ExecutionPlan is serialized by DataFusion's codec +// automatically via children() / with_new_children(). The prefilter_type +// field tells the decoder which PreFilterSource variant to use when +// reconstructing from the deserialized child inputs. +message ANNIvfSubIndexExecProto { + enum PreFilterType { + NONE = 0; + FILTERED_ROW_IDS = 1; + SCALAR_INDEX_QUERY = 2; + } + + VectorQueryProto query = 1; + lance.datafusion.TableIdentifier table = 2; + repeated lance.table.IndexMetadata indices = 3; + PreFilterType prefilter_type = 4; +} + +// Serializable form of ANNIvfPartitionExec — the IVF centroid routing node. +message ANNIvfPartitionExecProto { + VectorQueryProto query = 1; + lance.datafusion.TableIdentifier table = 2; + repeated string index_uuids = 3; +} diff --git a/lance-artifact/protos/encodings_v2_0.proto b/lance-artifact/protos/encodings_v2_0.proto new file mode 100644 index 000000000..acfc15087 --- /dev/null +++ b/lance-artifact/protos/encodings_v2_0.proto @@ -0,0 +1,347 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.encodings; + +import "google/protobuf/empty.proto"; + +// This file contains a specification for encodings that can be used +// to store and load Arrow data into a Lance file for the 2.0 format. It +// has been superseded by encodings21.proto which is used for the 2.1 format. +// +// # Types +// +// This file assumes the user wants to load data into Arrow arrays and +// explains how to map Arrow arrays into Lance files. Encodings are divided +// into "array encoding" (which maps to an Arrow array and may contain multiple +// buffers) and "buffer encoding" (which encodes a single buffer of data). +// +// # Encoding Tree +// +// Most encodings are layered on top of each other. These form a tree of +// encodings with a single root node. To encode an array you will typically +// start with the root node and then take the output from that root encoding +// and feed it into child encodings. The decoding process works in reverse. +// +// # Multi-column Encodings +// +// Some Arrow arrays will map to more than one column of Lance data. For +// example, struct arrays and list arrays. This file only contains encodings +// for a single column. However, it does describe how multi-column arrays can +// be encoded. + +// A pointer to a buffer in a Lance file +// +// A writer can place a buffer in three different locations. The buffer +// can go in the data page, in the column metadata, or in the file metadata. +// The writer is free to choose whatever is most appropriate (for example, a dictionary +// that is shared across all pages in a column will probably go in the column +// metadata). This specification does not dictate where the buffer should go. +message Buffer { + // The index of the buffer in the collection of buffers + uint32 buffer_index = 1; + // The collection holding the buffer + enum BufferType { + // The buffer is stored in the data page itself + page = 0; + // The buffer is stored in the column metadata + column = 1; + // The buffer is stored in the file metadata + file = 2; + }; + BufferType buffer_type = 2; +} + +// An encoding that adds nullability to another array encoding +// +// This can wrap any array encoding and add nullability information +message Nullable { + message NoNull { + ArrayEncoding values = 1; + } + message AllNull {} + message SomeNull { + ArrayEncoding validity = 1; + ArrayEncoding values = 2; + } + oneof nullability { + // The array has no nulls and there is a single buffer needed + NoNull no_nulls = 1; + // The array may have nulls and we need two buffers + SomeNull some_nulls = 2; + // All values are null (no buffers needed) + AllNull all_nulls = 3; + } +} + +// An array encoding for variable-length list fields +message List { + // An array containing the offsets into an items array. + // + // This array will have num_rows items and will never + // have nulls. + // + // If the list at index i is not null then offsets[i] will + // contain `base + len(list)` where `base` is defined as: + // i == 0: 0 + // i > 0: (offsets[i-1] % null_offset_adjustment) + // + // To help understand we can consider the following example list: + // [ [A, B], null, [], [C, D, E] ] + // + // The offsets will be [2, ?, 2, 5] + // + // If the incoming list at index i IS null then offsets[i] will + // contain `base + len(list) + null_offset_adjustment` where `base` + // is defined the same as above. + // + // To complete the above example let's assume that `null_offset_adjustment` + // is 7. Then the offsets will be [2, 9, 2, 5] + // + // If there are no nulls then the offsets we write here are exactly the + // same as the offsets in an Arrow list array (except we omit the leading + // 0 which is redundant) + // + // The reason we do this is so that reading a single list at index i only + // requires us to load the indices at i and i-1. + // + // If the offset at index i is greater than `null_offset_adjustment`` + // then the list at index i is null. + // + // Otherwise the length of the list is `offsets[i] - base` where + // base is defined the same as above. + // + // Let's consider our example offsets: [2, 9, 2, 5] + // + // We can take any range of lists and determine how many list items are + // referenced by the sublist. + // + // 0..3: [_, 5] -> items 0..5 (base = 0* and end is 5) + // 0..2: [_, 2] -> items 0..2 (base = 0* and end is 2) + // 0..1: [_, 9] -> items 0..2 (base = 0* and end is 9 % 7) + // 1..3: [2, 5] -> items 2..5 (base = 2 and end is 5) + // 1..2: [2, 2] -> items 2..2 (base = 2 and end is 2) + // 2..3: [9, 5] -> items 2..5 (base = 9 % 7 and end is 5) + // + // * When the start of our range is the 0th item the base is always 0 and we only + // need to load a single index from disk to determine the range. + // + // The data type of the offsets array is flexible and does not need + // to match the data type of the destination array. Please note that the offsets + // array is very likely to be efficiently encoded by bit packing deltas. + ArrayEncoding offsets = 1; + // If a list is null then we add this value to the offset + // + // This value must be greater than the length of the items so that + // (offset + null_offset_adjustment) is never used by a non-null list. + // + // Note that this value cannot be equal to the length of the items + // because then a page with a single list would store [ X ] and we + // couldn't know if that is a null list or a list with X items. + // + // Therefore, the best choice for this value is 1 + # of items. + // Choosing this will maximize the bit packing that we can apply to the offsets. + uint64 null_offset_adjustment = 2; + // How many items are referenced by these offsets. This is needed in + // order to determine which items pages map to this offsets page. + uint64 num_items = 3; +} + +// An array encoding for fixed-size list fields +message FixedSizeList { + /// The number of items in each list + uint32 dimension = 1; + /// True if the list is nullable + bool has_validity = 3; + /// The items in the list + ArrayEncoding items = 2; +} + +message Compression { + string scheme = 1; + optional int32 level = 2; +} + +// Fixed width items placed contiguously in a buffer +message Flat { + // the number of bits per value, must be greater than 0, does + // not need to be a multiple of 8 + uint64 bits_per_value = 1; + // the buffer of values + Buffer buffer = 2; + // The Compression message can specify the compression scheme (e.g. zstd) and any + // other information that is needed for decompression. + // + // If this array is compressed then the bits_per_value refers to the uncompressed + // data. + Compression compression = 3; +} + +// Compression algorithm where all values have a constant value +message Constant { + // The value (TODO: define encoding for literals?) + bytes value = 1; +} + +// Items are bitpacked in a buffer +message Bitpacked { + // the number of bits used for a value in the buffer + uint64 compressed_bits_per_value = 1; + + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 2; + + // The items in the list + Buffer buffer = 3; + + // Whether or not a sign bit is included in the bitpacked value + bool signed = 4; +} + +// Items are bitpacked in a buffer +message BitpackedForNonNeg { + // the number of bits used for a value in the buffer + uint64 compressed_bits_per_value = 1; + + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 2; + + // The items in the list + Buffer buffer = 3; +} + +// Opaque bitpacking variant where the bits per value are stored inline in the chunks themselves +message InlineBitpacking { + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 2; +} + +// Transparent bitpacking variant where the number of bits per value is fixed through the whole buffer +message OutOfLineBitpacking { + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 2; + // The number of compressed bits per value, fixed across the entire buffer + uint64 compressed_bits_per_value = 3; +} + +// An array encoding for shredded structs that will never be null +// +// There is no actual data in this column. +// +// TODO: Struct validity bitmaps will be placed here. +message SimpleStruct {} + +// An array encoding for binary fields +message Binary { + ArrayEncoding indices = 1; + ArrayEncoding bytes = 2; + uint64 null_adjustment = 3; +} + +message Variable { + uint32 bits_per_offset = 1; +} + +message Fsst { + ArrayEncoding binary = 1; + bytes symbol_table = 2; +} + +// An array encoding for dictionary-encoded fields +message Dictionary { + ArrayEncoding indices = 1; + ArrayEncoding items = 2; + uint32 num_dictionary_items = 3; +} + +message PackedStruct { + repeated ArrayEncoding inner = 1; + Buffer buffer = 2; +} + +message PackedStructFixedWidthMiniBlock { + ArrayEncoding Flat = 1; + repeated uint32 bits_per_values = 2; +} + +message FixedSizeBinary { + ArrayEncoding bytes = 1; + uint32 byte_width = 2; +} + +message Block { + string scheme = 1; +} + +// Run-Length Encoding for miniblock format +message Rle { + // Number of bits per value (8, 16, 32, 64, or 128) + uint64 bits_per_value = 1; +} + +// Byte Stream Split encoding for floating point values +message ByteStreamSplit { + // Number of bits per value (32 for float, 64 for double) + uint64 bits_per_value = 1; +} + +// General miniblock encoding - wraps another miniblock encoding with compression +message GeneralMiniBlock { + // The inner miniblock encoding (e.g., Rle, Bitpacked, etc.) + ArrayEncoding inner = 1; + // The compression scheme to apply to the miniblock buffers + Compression compression = 2; +} + +// Encodings that decode into an Arrow array +message ArrayEncoding { + oneof array_encoding { + Flat flat = 1; + Nullable nullable = 2; + FixedSizeList fixed_size_list = 3; + List list = 4; + SimpleStruct struct = 5; + Binary binary = 6; + Dictionary dictionary = 7; + Fsst fsst = 8; + PackedStruct packed_struct = 9; + Bitpacked bitpacked = 10; + FixedSizeBinary fixed_size_binary = 11; + BitpackedForNonNeg bitpacked_for_non_neg = 12; + Constant constant = 13; + InlineBitpacking inline_bitpacking = 14; + OutOfLineBitpacking out_of_line_bitpacking = 15; + Variable variable = 16; + PackedStructFixedWidthMiniBlock packed_struct_fixed_width_mini_block = 17; + Block block = 18; + Rle rle = 19; + GeneralMiniBlock general_mini_block = 20; + ByteStreamSplit byte_stream_split = 21; + } +} + +// Wraps a column with a zone map index that can be used +// to apply pushdown filters +message ZoneIndex { + uint32 rows_per_zone = 1; + Buffer zone_map_buffer = 2; + ColumnEncoding inner = 3; +} + +// Marks a column as blob data. It will contain a packed struct +// with fields position and size (u64) +message Blob { + ColumnEncoding inner = 1; +} + +// Encodings that describe a column of values +message ColumnEncoding { + oneof column_encoding { + // No special encoding, just column values + google.protobuf.Empty values = 1; + ZoneIndex zone_index = 2; + Blob blob = 3; + } +} diff --git a/lance-artifact/protos/encodings_v2_1.proto b/lance-artifact/protos/encodings_v2_1.proto new file mode 100644 index 000000000..514273320 --- /dev/null +++ b/lance-artifact/protos/encodings_v2_1.proto @@ -0,0 +1,635 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.encodings21; + +// This file contains a specification for encodings that can be used +// to store and load Arrow data into a Lance file for the 2.1 format. +// +// # Types +// +// This file assumes the user wants to load data into Arrow arrays and +// explains how to map Arrow arrays into Lance files. Encodings are divided +// into "structural encodings" (which are used to encode the structure of the +// data such as any list or struct layers) and "compressive encodings" (which +// are used to compress the actual data values). +// +// # Standardized Interpretation of Counting Terms +// +// When working with 2.1 encodings we have a number of different "counting terms" and it can be +// difficult to understand what we mean when we are talking about a "number of values". Here is +// a standard interpretation of these terms: +// +// To understand these definitions consider a data type FIXED_SIZE_LIST>. +// +// A "value" is an abstract term when we aren't being specific. +// +// - num_rows: This is the highest level counting term. A single row includes everything in the +// fixed size list. This is what the user asks for when they asks for a range of rows. +// - num_elements: The number of elements is the number of rows multiplied by the dimension of any +// fixed size list wrappers. This is what you get when you flatten the FSL layer and +// is the starting point for structural encoding. Note that an element can be a list +// value or a single primitive value. +// - num_items: The number of items is the number of values in the repetition and definition vectors +// after everything has been flattened. +// - num_visible_items: The number of visible items is the number of items after invisible items +// have been removed. Invisible items are rep/def levels that don't correspond to an +// actual value. + + +// # Structural Encodings +// +// The following message are used to describe the structural encoding of the +// data. In this document, we refer to these structural encodings as layouts. + +// Repetition and definition levels are described in more detail elsewhere. As we peel through +// the structure of an array we will encounter layers of struct and list. Each of these layers +// potentially adds a new level to the repetition and definition levels. This message describes +// the meaning of each layer. +enum RepDefLayer { + // Should never be used, included for debugging purporses and general protobuf best practice + REPDEF_UNSPECIFIED = 0; + // All values are valid (can be primitive or struct) + REPDEF_ALL_VALID_ITEM = 1; + // All list values are valid + REPDEF_ALL_VALID_LIST = 2; + // There are one or more null items (can be primitive or struct) + REPDEF_NULLABLE_ITEM = 3; + // A list layer with null lists but no empty lists + REPDEF_NULLABLE_LIST = 4; + // A list layer with empty lists but no null lists + REPDEF_EMPTYABLE_LIST = 5; + // A list layer with both empty lists and null lists + REPDEF_NULL_AND_EMPTY_LIST = 6; +} + +// A layout used for pages where the data is small +// +// In this case we can fit many values into a single disk sector and transposing buffers is +// expensive. As a result, we do not transpose the buffers but compress the data into small +// chunks (called mini blocks) which are roughly the size of a disk sector. +// +// The end result is a small amount of read amplification (since we must read an entire page +// at a time) but we have more flexibility in compression and do less work per value when +// compressing and decompressing in bulk. +message MiniBlockLayout { + // Description of the compression of repetition levels (e.g. how many bits per rep) + // + // Optional, if there is no repetition then this field is not present + CompressiveEncoding rep_compression = 1; + // Description of the compression of definition levels (e.g. how many bits per def) + // + // Optional, if there is no definition then this field is not present + CompressiveEncoding def_compression = 2; + // Description of the compression of values + CompressiveEncoding value_compression = 3; + // Description of the compression of the dictionary data + // + // Optional, if there is no dictionary then this field is not present + CompressiveEncoding dictionary = 4; + // Number of items in the dictionary + uint64 num_dictionary_items = 5; + // The meaning of each repdef layer, used to interpret repdef buffers correctly + repeated RepDefLayer layers = 6; + // The number of buffers in each mini-block, this is determined by the compression and does + // NOT include the repetition or definition buffers (the presence of these buffers can be determined + // by looking at the rep_compression and def_compression fields) + uint64 num_buffers = 7; + // The depth of the repetition index. + // + // If there is repetition then the depth must be at least 1. If there are many layers + // of repetition then deeper repetition indices will support deeper nested random access. For + // example, given 5 layers of repetition then the repetition index depth must be at least + // 3 to support access like `rows[50][17][3]`. + // + // We require `repetition_index_depth + 1` u64 values per mini-block to store the repetition + // index if the `repetition_index_depth` is greater than 0. The +1 is because we need to store + // the number of "leftover items" at the end of the chunk. Otherwise, we wouldn't have any way + // to know if the final item in a chunk is valid or not. + uint32 repetition_index_depth = 8; + // The page already records how many rows are in the page. For mini-block we also need to know how + // many "items" are in the page. A row and an item are the same thing unless the page has lists. + uint64 num_items = 9; + + // Since Lance 2.2, miniblocks have larger chunk sizes (>= 64KB) + bool has_large_chunk = 10; +} + +// A layout used for pages where the data is large +// +// In this case the cost of transposing the data is relatively small (compared to the cost of writing the data) +// and so we just zip the buffers together +message FullZipLayout { + // The number of bits of repetition info (0 if there is no repetition) + uint32 bits_rep = 1; + // The number of bits of definition info (0 if there is no definition) + uint32 bits_def = 2; + // The number of bits of value info + // + // Note: we use bits here (and not bytes) for consistency with other encodings. However, in practice, + // there is never a reason to use a bits per value that is not a multiple of 8. The complexity is not + // worth the small savings in space since this encoding is typically used with large values already. + oneof details { + // If this is a fixed width block then we need to have a fixed number of bits per value + uint32 bits_per_value = 3; + // If this is a variable width block then we need to have a fixed number of bits per offset + uint32 bits_per_offset = 4; + } + // The number of items in the page + uint32 num_items = 5; + // The number of visible items in the page + uint32 num_visible_items = 6; + // Description of the compression of values + CompressiveEncoding value_compression = 7; + // The meaning of each repdef layer, used to interpret repdef buffers correctly + repeated RepDefLayer layers = 8; +} + +// A layout used for sparse flat or nested pages where Arrow structure is represented directly +// in layer-local slot domains instead of as dense repetition / definition events. +// +// Structural layers are ordered from outer-most to inner-most. Values remain mini-block +// compressed and are split into independently readable chunks. +message SparseLayout { + // Description of the compression of values. + CompressiveEncoding value_compression = 1; + // Number of value buffers in each mini-block chunk. This does not include structural buffers. + uint64 num_buffers = 2; + // Number of entries in the equivalent dense repetition / definition stream. This equals + // num_visible_items plus one structural placeholder for every list slot without children. + // Null leaf slots count as visible items because they still occupy positions in Arrow's + // leaf value buffer. For example, a nullable primitive with 100 slots, 30 of them null, + // has num_items = num_visible_items = 100. + uint64 num_items = 3; + // Number of leaf value slots encoded in the value chunks, including null leaf slots. + uint64 num_visible_items = 4; + // If true, chunk-local value buffer sizes use u32. Otherwise they use u16. + bool has_large_chunk = 5; + // Structural layers ordered from outer-most to inner-most. This may be empty for a flat, + // non-nullable leaf page whose scheduling domain equals num_visible_items. + repeated SparseStructuralLayer structural_layers = 6; +} + +// A domain is a layer-local integer coordinate space [0, num_slots). A slot is one +// element in that space. The outer-most domain is the page's top-level rows; each +// layer's child domain is the next layer's parent domain, and the terminal child +// domain contains num_visible_items leaf value slots. +message SparseStructuralLayer { + // Exactly one layer kind is required. + oneof layer { + SparseValidityLayer validity = 1; + SparseListLayer list = 2; + SparseFixedSizeListLayer fixed_size_list = 3; + } +} + +message SparseValidityLayer { + // Number of nullable item or struct slots in this layer's parent and child domain. + uint64 num_slots = 1; + // Validity for the slots in this layer. + SparseValiditySet validity = 2; +} + +message SparseListLayer { + // Number of list, large-list, or map slots in this layer's parent domain. + uint64 num_slots = 1; + // Number of slots in this layer's child domain. + uint64 num_child_slots = 2; + // Non-empty parent slots. Valid parent slots absent from this set are empty lists. + SparsePositionSet non_empty_positions = 3; + // Positive child counts corresponding one-for-one with non_empty_positions. + SparseCountSet counts = 4; + // Validity for the parent slots in this layer. + SparseValiditySet validity = 5; +} + +message SparseFixedSizeListLayer { + // Number of fixed-size-list slots in this layer's parent domain. + uint64 num_slots = 1; + // Number of children per parent slot. The child domain has num_slots * dimension slots. + uint64 dimension = 2; + // Validity for the parent slots in this layer. + SparseValiditySet validity = 3; +} + +message SparseValiditySet { + enum Meaning { + SPARSE_VALIDITY_UNSPECIFIED = 0; + // Stored positions are null; all other positions are valid. + SPARSE_VALIDITY_NULL_POSITIONS = 1; + // Stored positions are valid; all other positions are null. + SPARSE_VALIDITY_VALID_POSITIONS = 2; + } + + Meaning meaning = 1; + SparsePositionSet positions = 2; +} + +message SparsePositionEmpty {} + +message SparsePositionAll {} + +message SparsePositionRange { + uint64 start = 1; + uint64 length = 2; +} + +message SparsePositionSet { + oneof positions { + // Delta-compressed u64 positions. Cardinality is num_positions. + CompressiveEncoding explicit = 1; + // One contiguous, non-empty range. + SparsePositionRange range = 2; + // Every position in the domain. + SparsePositionAll all = 3; + // No positions in the domain. + SparsePositionEmpty empty = 4; + } + // Semantic cardinality of this set. + uint64 num_positions = 5; +} + +message SparseCountEmpty {} + +message SparseCountConstant { + // Child count shared by every non-empty list slot. + uint64 value = 1; +} + +message SparseCountSet { + oneof counts { + // Compressed u64 child counts. Cardinality comes from the containing position set. + CompressiveEncoding explicit = 1; + // One positive child count shared by every non-empty list slot. + SparseCountConstant constant = 2; + // No counts; valid only when there are no non-empty list slots. + SparseCountEmpty empty = 3; + } +} + +// A layout used for pages where all (visible) values are the same scalar value. +// +// This generalizes the prior AllNullLayout semantics for file_version >= 2.2. +// +// There may be buffers of repetition and definition information if required in order +// to interpret what kind of nulls are present / which items are visible. +message ConstantLayout { + // The meaning of each repdef layer, used to interpret repdef buffers correctly + repeated RepDefLayer layers = 5; + + // Inline fixed-width scalar value bytes. + // + // This MUST only be used for types where a single non-null element is represented by a single + // fixed-width Arrow value buffer (i.e. no offsets buffer, no child data). + // + // Constraints: + // - MUST be absent for an all-null page + // - MUST be <= 32 bytes if present + optional bytes inline_value = 6; + + // Optional compression algorithm used for the repetition buffer. + // If absent, repetition levels are stored as raw u16 values. + CompressiveEncoding rep_compression = 7; + // Optional compression algorithm used for the definition buffer. + // If absent, definition levels are stored as raw u16 values. + CompressiveEncoding def_compression = 8; + // Number of values in repetition buffer after decompression. + uint64 num_rep_values = 9; + // Number of values in definition buffer after decompression. + uint64 num_def_values = 10; +} + +// A layout where large binary data is encoded externally and only +// the descriptions (position + size) are placed in the page +// +// Repdef information is stored in the descriptions. A description with a size of +// 0 and a position of 0 is an empty value. A description with a size of 0 and a +// non-zero position is a null value and the position is the repdef value. +message BlobLayout { + // The inner layout used to store the descriptions + PageLayout inner_layout = 1; + // The meaning of each repdef layer, used to interpret repdef buffers correctly + // + // The inner layout's repdef layers will always be 1 all valid item layer + repeated RepDefLayer layers = 2; +} + +// Describes the structural encoding of a page +message PageLayout { + oneof layout { + // A layout used for pages where the data is small + MiniBlockLayout mini_block_layout = 1; + // A layout used for pages where all (visible) values are the same scalar value or null. + ConstantLayout constant_layout = 2; + // A layout used for pages where the data is large + FullZipLayout full_zip_layout = 3; + // A layout where large binary data is encoded externally + // and only the descriptions are put in the page + BlobLayout blob_layout = 4; + // A sparse structural layout. This variant requires file version 2.3 or later. + SparseLayout sparse_layout = 5; + } +} + +// # Compressive Encodings +// +// These encodings describe how an array is compressed. An encoding may split an +// array into multiple buffers. The buffers can then be compressed further (and split +// into yet more buffers). The entire process forms a tree of encodings with the root +// of the tree being the initial array and the leaves being the final compressed buffers. +// +// # Data blocks and buffers +// +// Data blocks are a simplified version of arrays and represent a collection of buffers grouped +// with some kind of interpretation. Data blocks are the input and output of compressive encodings. +// There are different kinds of data blocks: +// - Fixed width data blocks (e.g. u8, u16, ...) +// - Variable width data blocks (e.g. strings, binary) +// - Struct data blocks (note: this is for packed structs, normal structs are encoded in the structural encoding) +// +// In addition, leaf encodings may output "buffers". These are fully compressed buffers of data that +// are stored in the page and no longer compressed. + +enum CompressionScheme { + COMPRESSION_ALGORITHM_UNSPECIFIED = 0; + COMPRESSION_ALGORITHM_LZ4 = 1; + COMPRESSION_ALGORITHM_ZSTD = 2; +} + +// Compression applied to a single buffer of data +// +// A buffer is the leaf of the compression tree. Unlike data blocks, which can +// be further compressed with a variety of techniques, a buffer cannot be understood +// in any particular way. +// +// A general compression scheme may be applied to a buffer. This is something like +// zstd, lz4, etc. The entire buffer is compressed as a single unit. If this happens +// then any parent encoding becomes opaque, even if it would normally be transparent. +// +// This is a leaf, no further compression is applied to the data. +message BufferCompression { + // A general compression scheme to apply to the buffer + CompressionScheme scheme = 1; + // The compression level + // + // Optional, if not present a scheme-specific default value will be used. + // + // Interpretation of this value depends on the compression scheme. Generally, larger + // values indicate more compression at the expense of more CPU time. + optional int32 level = 2; +} + +// Fixed width items placed contiguously in a single buffer +// +// This is a leaf encoding, there is no compression applied to the data. +// +// This is a transparent encoding by definition. +// +// The input is a fixed-width data block. +// The output is a single buffer. +message Flat { + // the number of bits per value, must be greater than 0, does + // not need to be a multiple of 8 + uint64 bits_per_value = 1; + // The compression applied to the data + optional BufferCompression data = 2; +} + +// Variable width items have the values stored in one buffer and the +// offsets are output as a data block that may be further compressed. +// +// This is a partial leaf encoding. Values are not compressed but +// the offsets may be further compressed. +// +// This is a transparent encoding by definition. +// +// The input is a variable-width data block. +// The output is a single fixed-width data block (the offsets) and +// a single buffer (the values) +message Variable { + // Describes how the offsets data block is compressed + CompressiveEncoding offsets = 1; + // The compression applied to the values + optional BufferCompression values = 2; +} + +// Compression algorithm where all values have a constant value (encoded in the description) +// +// This is a leaf encoding, there is no compression applied to the data. +// +// The input can be any kind of data block. +// There is no output. +message Constant { + // The value (TODO: define encoding for literals?) + optional bytes value = 1; +} + +// A compression scheme in which a single fixed-width block is "packed" into +// a smaller fixed-width block values where each value has fewer bits. +// +// This is typically done by throwing away the most significant bits of each value when +// those bits are all the same. +// +// In this scheme the number of bits per value is fixed across the entire buffer and stored +// in this message. +// +// This is a transparent encoding. +// +// The input is a fixed-width data block. +// The output is a single fixed-width data block. +message OutOfLineBitpacking { + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 1; + // The compression used to store the bitpacked values data block + CompressiveEncoding values = 3; +} + +// Bitpacking variant where the bits per value are stored inline in the chunks themselves +// +// This variation of bitpacking allows for the number of bits per value to change throughout the +// buffer, which makes the compression more robust to outliers. +// +// This is an opaque encoding. +// +// The input is a fixed-width data block. +// The output is a single buffer. +message InlineBitpacking { + // the number of bits of the uncompressed value. e.g. for a u32, this will be 32 + uint64 uncompressed_bits_per_value = 1; + // The compression applied to the values + optional BufferCompression values = 2; +} + +// A compression scheme for variable-width data +// +// A small dictionary (referred to as a "symbol table") is used to compress the values. +// In this scheme there is a single symbol table for the entire page and it is stored in the +// encoding description itself. +// +// This is a transparent encoding. +// +// The input is a variable-width data block. +// The output is a single variable-width data block. +message Fsst { + // The FSST symbol table + bytes symbol_table = 1; + // The compression used to store the compressed values data block + CompressiveEncoding values = 2; +} + +// A compression scheme where common values are stored in a dictionary and the values are +// encoded as indices into the dictionary. +// +// This is an opaque encoding unless the dictionary is considered metadata. +// +// The input is a any kind of data block. +// There are two outputs: +// - A data block of the same kind as the input (the dictionary) +// - A fixed-width data block containing the indices into the dictionary. +message Dictionary { + // The compression used to store the indices data block + CompressiveEncoding indices = 1; + // The compression used to store the dictionary items data block + CompressiveEncoding items = 2; + // The number of items in the dictionary + uint32 num_dictionary_items = 3; +} + +// A compression scheme where runs of common values are encoded as a single value and a count +// +// This is an opaque encoding unless the run lengths are considered metadata. +// +// The input is a single data block of any kind. +// There are two outputs: +// - A data block of the same kind as the input (the run values) +// - A fixed-width data block containing the lengths of the runs +message Rle { + // The compression used to store the run values data block + CompressiveEncoding values = 1; + // The compression used to store the run lengths data block + CompressiveEncoding run_lengths = 2; +} + +// Converts a fixed-size-list of values into a flattened list of values +// +// This encoding does not actually compress the data, it just flattens out the FSL layers. +// +// This is a transparent encoding. +// +// The input is a single block of fixed-width data (with a wide width and few items) +// The output is a single block of fixed-width data (with a narrow width and many items) +message FixedSizeList { + // The number of items in this layer of FSL + uint64 items_per_value = 1; + // Whether or not there is a validity buffer + bool has_validity = 3; + // The compression used to store the flattened values data block + CompressiveEncoding values = 2; +} + +// Packs a struct containing only fixed-width children into a single fixed-width data block +// +// The children are concatenated row by row and stored as a single fixed-width buffer. This is +// the legacy packed struct representation and remains available for backwards compatibility. +message PackedStruct { + // The number of bits contributed by each child field in the packed row + repeated uint64 bits_per_value = 1; + // The compression used to store the packed fixed-width values + CompressiveEncoding values = 2; +} + +// Variable-width packed struct encoding (2.2 extension) +// +// Each child value is compressed independently before being transposed into +// a row-major layout. This preserves per-field compression boundaries at the +// cost of disabling mini-block compression. Readers must prefer this field +// when present and fall back to the legacy encoding otherwise. +message VariablePackedStruct { + // Per-field encoding metadata in struct order + repeated FieldEncoding fields = 1; + + // Encoding description for a single child field + message FieldEncoding { + // Compression applied to individual field values before transposition + CompressiveEncoding value = 1; + oneof layout { + // Bit width of each compressed value (when fixed width) + uint64 bits_per_value = 2; + // Bit width of the length prefix for variable-width compressed values + uint64 bits_per_length = 3; + } + } +} + +// A compression scheme that wraps the underlying data with general compression +// +// Note: The application of wrapped compression will depend on the layout of the data. +// If we apply it to mini-block data then we compress entire mini-blocks. If we apply +// it to full-zip data then we compress each value individually. +// +// Note: Wrapped compression is somewhat unique at the moment as it is applied to the +// output of the inner encoding and not the input like all other compressive encodings. +// +// Note: General compression can usually be applied in two spots. We can apply +// it to individual buffers or we can apply it here, to the entire array. +// +// For example, let's say we are storing mini-blocks of strings and we are using +// FSST and bitpacking the offsets. We have something like this... +// +// WRAPPED(†3) -> FSST -> VARIABLE -(offsets)-> INLINE_BITPACKING -(data)-> FLAT -> BUFFER (†1) +// -(data)-> BUFFER (†2) +// +// General compression can be applied at †1, †2, or †3 (or any combination of these). +// +// If we apply it at †1 then we apply it just to the bitpacked offsets +// If we apply it at †2 then we apply it just to the FSST compressed data +// If we apply it at †3 then we apply it to the entire mini-block (both offsets and data) +// +// The input is a single data block of any kind. +// The output is a single data block of the same kind as the input. +message General { + // The compression to apply to the values + BufferCompression compression = 1; + // The compression used to store the output data block + CompressiveEncoding values = 3; +} + +// A compression scheme where fixed-width values are transposed into a series of byte streams +// +// This is commonly used for floating point values where the upper bits (the mantissa) have a +// significantly different meaning than the lower bits. By splitting the values into byte streams +// we group the mantissa bits together and the exponent bits together. The end result is typically +// more compressible. +// +// Note that this encoding is mostly useful when combined with other encodings. It does not do any +// compression on its own. +// +// This is an opaque encoding. +// +// The input is a fixed-width data block +// The output is a single fixed-width data block +message ByteStreamSplit { + // The compression used to store the values + CompressiveEncoding values = 1; +} + +// An encoding that compresses a data block into buffers +message CompressiveEncoding { + oneof compression { + Flat flat = 1; + Variable variable = 2; + Constant constant = 3; + OutOfLineBitpacking out_of_line_bitpacking = 4; + InlineBitpacking inline_bitpacking = 5; + Fsst fsst = 6; + Dictionary dictionary = 7; + Rle rle = 8; + ByteStreamSplit byte_stream_split = 9; + General general = 10; + FixedSizeList fixed_size_list = 11; + PackedStruct packed_struct = 12; + VariablePackedStruct variable_packed_struct = 13; + } +} diff --git a/lance-artifact/protos/file.proto b/lance-artifact/protos/file.proto new file mode 100644 index 000000000..cbc658640 --- /dev/null +++ b/lance-artifact/protos/file.proto @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.file; + +// A file descriptor that describes the contents of a Lance file +message FileDescriptor { + // The schema of the file + Schema schema = 1; + // The number of rows in the file + uint64 length = 2; +} + +// A schema which describes the data type of each of the columns +message Schema { + // All fields in this file, including the nested fields. + repeated lance.file.Field fields = 1; + // Schema metadata. + map metadata = 5; +} + +// Metadata of one Lance file. +message Metadata { + // 4 was used for StatisticsMetadata in the past, but has been moved to + // prevent a bug in older readers. + reserved 4; + + // Position of the manifest in the file. If it is zero, the manifest is stored + // externally. + uint64 manifest_position = 1; + + // Logical offsets of each chunk group, i.e., number of the rows in each + // chunk. + repeated int32 batch_offsets = 2; + + // The file position that page table is stored. + // + // A page table is a matrix of N x M x 2, where N = num_fields, and M = + // num_batches. Each cell in the table is a pair of of the page. Both position and length are int64 values. The + // of all the pages in the same column are then + // contiguously stored. + // + // Every field that is a part of the file will have a run in the page table. + // This includes struct columns, which will have a run of length 0 since + // they don't store any actual data. + // + // For example, for the column 5 and batch 4, we have: + // ```text + // position = page_table[5][4][0]; + // length = page_table[5][4][1]; + // ``` + uint64 page_table_position = 3; + + message StatisticsMetadata { + // The schema of the statistics. + // + // This might be empty, meaning there are no statistics. It also might not + // contain statistics for every field. + repeated Field schema = 1; + + // The field ids of the statistics leaf fields. + // + // This plays a similar role to the `fields` field in the DataFile message. + // Each of these field ids corresponds to a field in the stats_schema. There + // is one per column in the stats page table. + repeated int32 fields = 2; + + // The file position of the statistics page table + // + // The page table is a matrix of N x 2, where N = length of stats_fields. + // This is the same layout as the main page table, except there is always + // only one batch. + // + // For example, to get the stats column 5, we have: + // ```text + // position = stats_page_table[5][0]; + // length = stats_page_table[5][1]; + // ``` + uint64 page_table_position = 3; + } + + StatisticsMetadata statistics = 5; +} // Metadata + +// Supported encodings. +enum Encoding { + // Invalid encoding. + NONE = 0; + // Plain encoding. + PLAIN = 1; + // Var-length binary encoding. + VAR_BINARY = 2; + // Dictionary encoding. + DICTIONARY = 3; + // Run-length encoding. + RLE = 4; +} + +// Dictionary field metadata +message Dictionary { + /// The file offset for storing the dictionary value. + /// It is only valid if encoding is DICTIONARY. + /// + /// The logic type presents the value type of the column, i.e., string value. + int64 offset = 1; + + /// The length of dictionary values. + int64 length = 2; +} + +// Field metadata for a column. +message Field { + enum Type { + PARENT = 0; + REPEATED = 1; + LEAF = 2; + } + Type type = 1; + + // Fully qualified name. + string name = 2; + /// Field Id. + /// + /// See the comment in `DataFile.fields` for how field ids are assigned. + int32 id = 3; + /// Parent Field ID. If not set, this is a top-level column. + int32 parent_id = 4; + + // Logical types, support parameterized Arrow Type. + // + // PARENT types will always have logical type "struct". + // + // REPEATED types may have logical types: + // * "list" + // * "large_list" + // * "list.struct" + // * "large_list.struct" + // The final two are used if the list values are structs, and therefore the + // field is both implicitly REPEATED and PARENT. + // + // LEAF types may have logical types: + // * "null" + // * "bool" + // * "int8" / "uint8" + // * "int16" / "uint16" + // * "int32" / "uint32" + // * "int64" / "uint64" + // * "halffloat" / "float" / "double" + // * "string" / "large_string" + // * "binary" / "large_binary" + // * "date32:day" + // * "date64:ms" + // * "decimal:128:{precision}:{scale}" / "decimal:256:{precision}:{scale}" + // * "time:{unit}" / "timestamp:{unit}" / "duration:{unit}", where unit is + // "s", "ms", "us", "ns" + // * "dict:{value_type}:{index_type}:false" + string logical_type = 5; + // If this field is nullable. + bool nullable = 6; + + // optional field metadata (e.g. extension type name/parameters) + map metadata = 10; + + bool unenforced_primary_key = 12; + + // Position of this field in the primary key (1-based). + // 0 means the field is part of the primary key but uses schema field id for ordering. + // When set to a positive value, primary key fields are ordered by this position. + uint32 unenforced_primary_key_position = 13; + + // Reserved for future use. Use unenforced_clustering_key_position instead. + bool unenforced_clustering_key = 14; + + // Position of this field in the clustering key (1-based). + // 0 means the field is not part of the clustering key. + uint32 unenforced_clustering_key_position = 15; + + // DEPRECATED ---------------------------------------------------------------- + + // Deprecated: Only used in V1 file format. V2 uses variable encodings defined + // per page. + // + // The global encoding to use for this field. + Encoding encoding = 7; + + // Deprecated: Only used in V1 file format. V2 dynamically chooses when to + // do dictionary encoding and keeps the dictionary in the data files. + // + // The file offset for storing the dictionary value. + // It is only valid if encoding is DICTIONARY. + // + // The logic type presents the value type of the column, i.e., string value. + Dictionary dictionary = 8; + + // Deprecated: optional extension type name, use metadata field + // ARROW:extension:name + string extension_name = 9; + + // Field number 11 was previously `string storage_class`. + // Keep it reserved so older manifests remain compatible while new writers + // avoid reusing the slot. + reserved 11; + reserved "storage_class"; +} diff --git a/lance-artifact/protos/file2.proto b/lance-artifact/protos/file2.proto new file mode 100644 index 000000000..650a1568d --- /dev/null +++ b/lance-artifact/protos/file2.proto @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.file.v2; + +import "google/protobuf/any.proto"; +import "google/protobuf/empty.proto"; + +// # Lance v2.X File Format +// +// The Lance file format is a barebones format for serializing columnar data +// into a file. +// +// * Each Lance file contains between 0 and 4Gi columns +// * Each column contains between 0 and 4Gi pages +// * Each page contains between 0 and 2^64 items +// * Different pages within a column can have different items counts +// * Columns may have up to 2^64 items +// * Different columns within a file can have different item counts +// +// The Lance file format does not have any notion of a type system or schemas. +// From the perspective of the file format all data is arbitrary buffers of +// bytes with an extensible metadata block to describe the data. It is up to +// the user to interpret these bytes meaningfully. +// +// Data buffers are written to the file first. These data buffers can be +// referenced from three different places in the file: +// +// * Page encodings can reference data buffers. This is the most common way +// that actual data is stored. +// * Column encodings can reference data buffers. For example, a column encoding +// may reference data buffer(s) containing statistics or dictionaries. +// * Finally, the global buffer offset table can reference data buffers. This +// is useful for storing data that is shared across multiple columns. +// This is also useful for global file metadata (e.g. a schema that describes +// the file) +// +// ## File Layout +// +// Note: the number of buffers (BN) is independent of the number of columns (CN) +// and pages. +// +// Buffers often need to be aligned. 64-byte alignment is common when +// working with SIMD operations. 4096-byte alignment is common when +// working with direct I/O. In order to ensure these buffers are aligned +// writers may need to insert padding before the buffers. +// +// If direct I/O is required then most (but not all) fields described +// below must be sector aligned. We have marked these fields with an +// asterisk for clarity. Readers should assume there will be optional +// padding inserted before these fields. +// +// All footer fields are unsigned integers written with little endian +// byte order. +// +// ├──────────────────────────────────┤ +// | Data Pages | +// | Data Buffer 0* | +// | ... | +// | Data Buffer BN* | +// ├──────────────────────────────────┤ +// | Column Metadatas | +// | |A| Column 0 Metadata* | +// | Column 1 Metadata* | +// | ... | +// | Column CN Metadata* | +// ├──────────────────────────────────┤ +// | Column Metadata Offset Table | +// | |B| Column 0 Metadata Position* | +// | Column 0 Metadata Size | +// | ... | +// | Column CN Metadata Position | +// | Column CN Metadata Size | +// ├──────────────────────────────────┤ +// | Global Buffers Offset Table | +// | |C| Global Buffer 0 Position* | +// | Global Buffer 0 Size | +// | ... | +// | Global Buffer GN Position | +// | Global Buffer GN Size | +// ├──────────────────────────────────┤ +// | Footer | +// | A u64: Offset to column meta 0 | +// | B u64: Offset to CMO table | +// | C u64: Offset to GBO table | +// | u32: Number of global bufs | +// | u32: Number of columns | +// | u16: Major version | +// | u16: Minor version | +// | "LANC" | +// ├──────────────────────────────────┤ +// +// File Layout-End +// +// ## Data Pages +// +// A lot of flexibility is provided in how data is stored. A page's buffers do +// not strictly need to be contiguous on the disk. However, it is recommended +// that buffers within a page be grouped together for best performance. +// +// Data pages should be large. The only time a page should be written to disk +// is when the writer needs to flush the page to disk because it has accumulated +// too much data. Pages are not read in sequential order and if pages are too +// small then the seek overhead (or request overhead) will be problematic. We +// generally advise that pages be at least 8MB or larger. +// +// ## Encodings +// +// Specific encodings are not part of this minimal format. They are provided +// by extensions. Readers and writers should be designed so that encodings can +// be easily added and removed. Ideally, they should allow for this without +// requiring recompilation through some kind of plugin system. + +// The deferred encoding is used to place the encoding itself in a different +// part of the file. This is most commonly used to allow encodings to be shared +// across different columns. For example, when writing a file with thousands of +// columns, where many pages have the exact same encoding, it can be useful +// to cut down on the size of the metadata by using a deferred encoding. +message DeferredEncoding { + // Location of the buffer containing the encoding. + // + // * If sharing encodings across columns then this will be in a global buffer + // * If sharing encodings across pages within a column this could be in a + // column metadata buffer. + // * This could also be a page buffer if the encoding is not shared, needs + // to be written before the file ends, and the encoding is too large to load + // unless we first determine the page needs to be read. This combination + // seems unusual. + uint64 buffer_location = 1; + uint64 buffer_length = 2; +} + +// The encoding is placed directly in the metadata section +message DirectEncoding { + // The bytes that make up the encoding embedded directly in the metadata + // + // This is the most common approach. + bytes encoding = 1; +} + +// An encoding stores the information needed to decode a column or page +// +// For example, it could describe if the page is using bit packing, and how many bits +// there are in each individual value. +// +// At the column level it can be used to wrap columns with dictionaries or statistics. +message Encoding { + oneof location { + // The encoding is stored elsewhere and not part of this protobuf message + DeferredEncoding indirect = 1; + // The encoding is stored within this protobuf message + DirectEncoding direct = 2; + // There is no encoding information + google.protobuf.Empty none = 3; + } +} + +// ## Metadata + +// Each column has a metadata block that is placed at the end of the file. +// These may be read individually to allow for column projection. +message ColumnMetadata { + + // This describes a page of column data. + message Page { + // The file offsets for each of the page buffers + // + // The number of buffers is variable and depends on the encoding. There + // may be zero buffers (e.g. constant encoded data) in which case this + // could be empty. + repeated uint64 buffer_offsets = 1; + // The size (in bytes) of each of the page buffers + // + // This field will have the same length as `buffer_offsets` and + // may be empty. + repeated uint64 buffer_sizes = 2; + // Logical length (e.g. # rows) of the page + uint64 length = 3; + // The encoding used to encode the page + Encoding encoding = 4; + // The priority of the page + // + // For tabular data this will be the top-level row number of the first row + // in the page (and top-level rows should not split across pages). + uint64 priority = 5; + } + // Encoding information about the column itself. This typically describes + // how to interpret the column metadata buffers. For example, it could + // describe how statistics or dictionaries are stored in the column metadata. + Encoding encoding = 1; + // The pages in the column + repeated Page pages = 2; + // The file offsets of each of the column metadata buffers + // + // There may be zero buffers. + repeated uint64 buffer_offsets = 3; + // The size (in bytes) of each of the column metadata buffers + // + // This field will have the same length as `buffer_offsets` and + // may be empty. + repeated uint64 buffer_sizes = 4; +} // Metadata-End + +// ## Where is the rest? +// +// This file format is extremely minimal. It is a building block for +// creating more useful readers and writers and not terribly useful by itself. +// Other protobuf files will describe how this can be extended. diff --git a/lance-artifact/protos/filtered_read.proto b/lance-artifact/protos/filtered_read.proto new file mode 100644 index 000000000..d81f6b02c --- /dev/null +++ b/lance-artifact/protos/filtered_read.proto @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.datafusion; + +import "table_identifier.proto"; + +message U64Range { + uint64 start = 1; + uint64 end = 2; +} + +message ProjectionProto { + repeated int32 field_ids = 1; + bool with_row_id = 2; + bool with_row_addr = 3; + bool with_row_last_updated_at_version = 4; + bool with_row_created_at_version = 5; + BlobHandlingProto blob_handling = 6; +} + +message BlobHandlingProto { + oneof mode { + // All blobs read as binary + bool all_binary = 1; + // Blobs as descriptions, other binary as binary (default) + bool blobs_descriptions = 2; + // All binary columns as descriptions + bool all_descriptions = 3; + // Specific blobs read as binary, rest as descriptions (non-blob binary stays binary) + FieldIdSet some_blobs_binary = 4; + // Specific columns as binary, all other binary as descriptions + FieldIdSet some_binary = 5; + } +} + +message FieldIdSet { + repeated uint32 field_ids = 1; +} + +message FilteredReadThreadingModeProto { + oneof mode { + uint64 one_partition_multiple_threads = 1; + uint64 multiple_partitions = 2; + } +} + +// Serializable form of FilteredReadOptions. +message FilteredReadOptionsProto { + optional U64Range scan_range_before_filter = 1; + optional U64Range scan_range_after_filter = 2; + bool with_deleted_rows = 3; + optional uint32 batch_size = 4; + optional uint64 fragment_readahead = 5; + repeated uint64 fragment_ids = 6; + ProjectionProto projection = 7; + optional bytes refine_filter_substrait = 8; + optional bytes full_filter_substrait = 9; + FilteredReadThreadingModeProto threading_mode = 10; + optional uint64 io_buffer_size_bytes = 11; + // Arrow IPC schema for decoding Substrait filters (may be wider than projection). + optional bytes filter_schema_ipc = 12; +} + +// Serializable form of FilteredReadPlan (planned/distributed mode). +// RowAddrTreeMap serialized via its built-in serialize_into/deserialize_from. +// Per-fragment filters are Substrait-encoded and deduplicated. +message FilteredReadPlanProto { + bytes row_addr_tree_map = 1; + optional U64Range scan_range_after_filter = 2; + // Arrow IPC schema for decoding Substrait filters (matches the schema used at encode time). + optional bytes filter_schema_ipc = 3; + // Per-fragment filter mapping. Key is fragment id, value is a list index into + // filter_expressions. Multiple fragments can share the same list index when + // they have the same filter, avoiding duplicate Substrait encoding. + map fragment_filter_ids = 4; + // Deduplicated Substrait-encoded filter expressions. Each entry is referenced + // by one or more values in fragment_filter_ids. + repeated bytes filter_expressions = 5; +} + +// Top-level wrapper for FilteredReadExec serialization. +message FilteredReadExecProto { + TableIdentifier table = 1; + FilteredReadOptionsProto options = 2; + // FilteredRead has two modes + // Plan-then-execute (distributed): The planner creates a FilteredReadPlan and sends it to a remote executor. + // Plan-and-execute (local): The executor creates the plan itself at execution time. + optional FilteredReadPlanProto plan = 3; + // Note: FilteredReadExec.index_input (child ExecutionPlan) is NOT serialized here. + // DataFusion's PhysicalExtensionCodec handles child plans automatically: it walks + // the plan tree via children() / with_new_children(), serializes each node, and + // passes deserialized children back as the `inputs` parameter in try_decode. + // This means any ExecutionPlan in the tree (including index_input) must also + // implement try_encode/try_decode in the PhysicalExtensionCodec. + // TODO: implement serialize/deserialize for lance-specific index input ExecutionPlans. +} diff --git a/lance-artifact/protos/index.proto b/lance-artifact/protos/index.proto new file mode 100644 index 000000000..a72207b59 --- /dev/null +++ b/lance-artifact/protos/index.proto @@ -0,0 +1,251 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.index.pb; + +import "google/protobuf/any.proto"; + +// The type of an index. +enum IndexType { + // Vector index + VECTOR = 0; +} + +message Index { + // The unique index name in the dataset. + string name = 1; + + // Columns to be used to build the index. + repeated string columns = 2; + + // The version of the dataset this index was built from. + uint64 dataset_version = 3; + + // The [`IndexType`] of the index. + IndexType index_type = 4; + + /// Index implementation details. + oneof implementation { + VectorIndex vector_index = 5; + } +} + +message Tensor { + enum DataType { + BFLOAT16 = 0; + FLOAT16 = 1; + FLOAT32 = 2; + FLOAT64 = 3; + UINT8 = 4; + UINT16 = 5; + UINT32 = 6; + UINT64 = 7; + } + + DataType data_type = 1; + + // Data shape, [dim1, dim2, ...] + repeated uint32 shape = 2; + + // Data buffer + bytes data = 3; +} + +// Inverted Index File Metadata. +message IVF { + // Centroids of partitions. `dimension * num_partitions` of float32s. + // + // Deprecated, use centroids_tensor instead. + repeated float centroids = 1; // [deprecated = true]; + + // File offset of each partition. + repeated uint64 offsets = 2; + + // Number of records in the partition. + repeated uint32 lengths = 3; + + // Tensor of centroids. `num_partitions * dimension` of float32s. + Tensor centroids_tensor = 4; + + // KMeans loss. + optional double loss = 5; +} + +// Product Quantization. +message PQ { + // The number of bits to present a centroid. + uint32 num_bits = 1; + + // Number of sub vectors. + uint32 num_sub_vectors = 2; + + // Vector dimension + uint32 dimension = 3; + + // Codebook. `dimension * 2 ^ num_bits` of float32s. + repeated float codebook = 4; + + // Tensor of codebook. `2 ^ num_bits * dimension` of floats. + Tensor codebook_tensor = 5; +} + +// Transform type +enum TransformType { + OPQ = 0; +} + +// A transform matrix to apply to a vector or vectors. +message Transform { + // The file offset the matrix is stored + uint64 position = 1; + + // Data shape of the matrix, [rows, cols]. + repeated uint32 shape = 2; + + // Transform type. + TransformType type = 3; +} + +// Flat Index +message Flat {} + +// DiskAnn Index +message DiskAnn { + // Graph spec version + uint32 spec = 1; + + // Graph file + string filename = 2; + + // r parameter + uint32 r = 3; + + // alpha parameter + float alpha = 4; + + // L parameter + uint32 L = 5; + + /// Entry points to the graph + repeated uint64 entries = 6; +} + +// One stage in the vector index pipeline. +message VectorIndexStage { + oneof stage { + // Flat index + Flat flat = 1; + // `IVF` - Inverted File + IVF ivf = 2; + // Product Quantization + PQ pq = 3; + // Transformer + Transform transform = 4; + // DiskANN + DiskAnn diskann = 5; + } +} + +// Metric Type for Vector Index +enum VectorMetricType { + // L2 (Euclidean) Distance + L2 = 0; + + // Cosine Distance + Cosine = 1; + + // Dot Product + Dot = 2; + + // Hamming Distance + Hamming = 3; +} + +// Vector Index Metadata +message VectorIndex { + // Index specification version. + uint32 spec_version = 1; + + // Vector dimension; + uint32 dimension = 2; + + // Composed vector index stages. + // + // For example, `IVF_PQ` index type can be expressed as: + // + // ```text + // let stages = vec![Ivf{}, PQ{num_bits: 8, num_sub_vectors: 16}] + // ``` + repeated VectorIndexStage stages = 3; + + // Vector distance metrics type + VectorMetricType metric_type = 4; +} + +// Details for vector indexes, stored in the manifest's index_details field. +message VectorIndexDetails { + VectorMetricType metric_type = 1; + + // The target number of vectors per partition. + // 0 means unset. + uint64 target_partition_size = 2; + + // Optional HNSW index configuration. If set, the index has an HNSW layer. + optional HnswParameters hnsw_index_config = 3; + + message ProductQuantization { + uint32 num_bits = 1; + uint32 num_sub_vectors = 2; + } + message ScalarQuantization { + uint32 num_bits = 1; + } + message RabitQuantization { + enum RotationType { + FAST = 0; + MATRIX = 1; + } + uint32 num_bits = 1; + RotationType rotation_type = 2; + } + + // No quantization; vectors are stored as-is. + message FlatCompression {} + + oneof compression { + ProductQuantization pq = 4; + ScalarQuantization sq = 5; + RabitQuantization rq = 6; + FlatCompression flat = 8; + } + + // Runtime hints: optional build preferences that don't affect index structure. + // Keys use reverse-DNS namespacing (e.g., "lance.ivf.max_iters", "lancedb.accelerator"). + // Unrecognized keys must be silently ignored by all runtimes. + map runtime_hints = 9; +} + +// Hierarchical Navigable Small World (HNSW) parameters, used as an optional configuration for IVF indexes. +message HnswParameters { + // The maximum number of outgoing edges per node in the HNSW graph. Higher values + // means more connections, better recall, but more memory and slower builds. + // Referred to as "M" in the HNSW literature. + uint32 max_connections = 1; + // "construction exploration factor": The size of the dynamic list used during + // index construction. + uint32 construction_ef = 2; + // The maximum number of levels in the HNSW graph. + uint32 max_level = 3; +} + +message JsonIndexDetails { + string path = 1; + google.protobuf.Any target_details = 2; +} +message BloomFilterIndexDetails {} + +message RTreeIndexDetails {} + +message FMIndexDetails {} \ No newline at end of file diff --git a/lance-artifact/protos/index_old.proto b/lance-artifact/protos/index_old.proto new file mode 100644 index 000000000..236d1f110 --- /dev/null +++ b/lance-artifact/protos/index_old.proto @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.table; + +// NOTE: Do *NOT* add new index details here. Add them to the index.proto file instead. +// This file is in the lance.table package namespace while the index.proto file is in the +// lance.index package namespace. +// +// These are only here for forward compatibility. Older versions of Lance expect btree indexes +// to have lance.table in the package namespace. +// +// If you need to modify these messages (e.g. to add new fields to btree or bitmap) then +// it is ok to modify them here. + +// Currently many of these are empty messages because all needed details are either hard-coded (e.g. +// filenames) or stored in the index itself. However, we may want to add more details in the +// future, in particular we can add details that may be useful for planning queries (e.g. don't +// force us to load the index until we know we can make use of it) + +message BTreeIndexDetails {} +message BitmapIndexDetails {} +message LabelListIndexDetails {} +message NGramIndexDetails {} +message ZoneMapIndexDetails { + // Number of rows per zone. Optional for backwards compatibility: absent on + // datasets written before this field was added. When absent, no seed writer + // is created for the index. + optional uint64 rows_per_zone = 1; + // Whether seed-based incremental updates are enabled for this index. + // On-disk semantics: absent means seeds are disabled (old datasets written + // before this field was added). Present false means explicitly disabled. + // Present true means seeds are enabled: the index will embed per-fragment + // seed buffers in data files and harvest them during incremental updates + // to skip full column scans. + // Creation-time default: index creation code sets this to true for + // variable-length types (strings, binary) and fixed-width types wider than + // 8 bytes, and to false for narrow fixed-width types (e.g. Int64, Float64). + optional bool use_seeds = 2; + // Whether this index tracks exact null row addresses in a separate bitmap. + // Absent or false means legacy format: null positions are not tracked and + // IS NULL searches fall back to approximate zone-level statistics. Present + // true means IS NULL is exact and IS NOT NULL can be answered without a + // full scan. + optional bool has_null_bitmap = 3; +} +message InvertedIndexDetails { + enum DocumentGranularity { + ROW = 0; + LIST_ELEMENT = 1; + } + + message CodeTokenizerConfig { + // Split one lexical identifier into subwords, e.g. getUserName -> + // get/user/name. + bool split_identifiers = 1; + // Split identifier subwords across letter/number boundaries, e.g. + // HTML2JSON -> html/2/json. An absent value uses the code tokenizer default; + // a present value records the explicit index-time choice. + optional bool split_on_numerics = 2; + // Keep the complete lexical identifier in addition to subwords, e.g. + // user_name plus user/name. An absent value uses the code tokenizer default; + // a present value records the explicit index-time choice. + optional bool preserve_original = 3; + // Index operator tokens such as "::", "->", and "!=". Operators are not + // indexed by default because they are often high-frequency noise. + bool index_operators = 4; + } + + // Lexical tokenizer used after document-level text extraction. This is an + // implementation component such as "simple", "icu", "ngram", or "code". + // Input-time analyzer profiles are expanded into this field and the concrete + // options below before these details are persisted. + // Marking this field as optional as old versions of the index store blank details and we + // need to make sure we have a proper optional field to detect this. + optional string base_tokenizer = 1; + string language = 2; + bool with_position = 3; + optional uint32 max_token_length = 4; + bool lower_case = 5; + bool stem = 6; + bool remove_stop_words = 7; + bool ascii_folding = 8; + uint32 min_ngram_length = 9; + uint32 max_ngram_length = 10; + bool prefix_only = 11; + // Number of documents per compressed posting block. An absent value means + // the index predates this field and must use the legacy block size of 128. + // A present value records the block size used by the index; 256 is valid + // with format versions 3 and 4. + optional uint32 block_size = 12; + // Options for base_tokenizer = "code". Presence records the code tokenizer + // configuration used to build the index; absence means there is no + // code-specific configuration to apply. + CodeTokenizerConfig code_config = 13; + // The logical FTS document boundary. The protobuf default preserves the + // legacy row-document behavior when this field is absent. + DocumentGranularity document_granularity = 14; + // The posting-list payload format. This is separate from index_version, + // which identifies the overall inverted-index layout. + optional uint32 posting_format_version = 15; +} diff --git a/lance-artifact/protos/license_header.txt b/lance-artifact/protos/license_header.txt new file mode 100644 index 000000000..893e9a808 --- /dev/null +++ b/lance-artifact/protos/license_header.txt @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors diff --git a/lance-artifact/protos/rowids.proto b/lance-artifact/protos/rowids.proto new file mode 100644 index 000000000..3039cbf2a --- /dev/null +++ b/lance-artifact/protos/rowids.proto @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.table; +// TODO: what would it take to store this in a LanceV2 file? +// Or would flatbuffers be better for this? + +/// A sequence of row IDs. This is split up into one or more segments, +/// each of which can be encoded in different ways. The encodings are optimized +/// for values that are sorted, which will often be the case with row ids. +/// They also have optimized forms depending on how sparse the values are. +message RowIdSequence { + repeated U64Segment segments = 1; +} + +/// Different ways to encode a sequence of u64 values. +message U64Segment { + /// A range of u64 values. + message Range { + /// The start of the range, inclusive. + uint64 start = 1; + /// The end of the range, exclusive. + uint64 end = 2; + } + + /// A range of u64 values with holes. + message RangeWithHoles { + /// The start of the range, inclusive. + uint64 start = 1; + /// The end of the range, exclusive. + uint64 end = 2; + /// The holes in the range, as a sorted array of values; + /// Binary search can be used to check whether a value is a hole and should + /// be skipped. This can also be used to count the number of holes before a + /// given value, if you need to find the logical offset of a value in the + /// segment. + EncodedU64Array holes = 3; + } + + /// A range of u64 values with a bitmap. + message RangeWithBitmap { + /// The start of the range, inclusive. + uint64 start = 1; + /// The end of the range, exclusive. + uint64 end = 2; + /// A bitmap of the values in the range. The bitmap is a sequence of bytes, + /// where each byte represents 8 values. The first byte represents values + /// start to start + 7, the second byte represents values start + 8 to + /// start + 15, and so on. The most significant bit of each byte represents + /// the first value in the range, and the least significant bit represents + /// the last value in the range. If the bit is set, the value is in the + /// range; if it is not set, the value is not in the range. + bytes bitmap = 3; + } + + oneof segment { + /// When the values are sorted and contiguous. + Range range = 1; + /// When the values are sorted but have a few gaps. + RangeWithHoles range_with_holes = 2; + /// When the values are sorted but have many gaps. + RangeWithBitmap range_with_bitmap = 3; + /// When the values are sorted but are sparse. + EncodedU64Array sorted_array = 4; + /// A general array of values, which is not sorted. + EncodedU64Array array = 5; + } +} // RowIdSegment + +/// A basic bitpacked array of u64 values. +message EncodedU64Array { + message U16Array { + uint64 base = 1; + /// The deltas are stored as 16-bit unsigned integers. + /// (protobuf doesn't support 16-bit integers, so we use bytes instead) + bytes offsets = 2; + } + + message U32Array { + uint64 base = 1; + /// The deltas are stored as 32-bit unsigned integers. + /// (we use bytes instead of uint32 to avoid overhead of varint encoding) + bytes offsets = 2; + } + + message U64Array { + /// (We use bytes instead of uint64 to avoid overhead of varint encoding) + bytes values = 2; + } + + oneof array { + U16Array u16_array = 1; + U32Array u32_array = 2; + U64Array u64_array = 3; + } +} + +/// A sequence of dataset versions. Similar to RowIdSequence but tracks +/// version runs. It uses RLE (Run-Length Encoding) to efficiently +// represent consecutive rows with the same version. +message RowDatasetVersionSequence { + repeated RowDatasetVersionRun runs = 1; +} + +/// A run of rows with the same version. +message RowDatasetVersionRun { + /// The number of consecutive rows with the same version. + U64Segment span = 1; + + uint64 version = 2; +} diff --git a/lance-artifact/protos/table.proto b/lance-artifact/protos/table.proto new file mode 100644 index 000000000..9a64230f4 --- /dev/null +++ b/lance-artifact/protos/table.proto @@ -0,0 +1,805 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.table; + +import "google/protobuf/any.proto"; +import "google/protobuf/timestamp.proto"; +import "file.proto"; + +/* + +Format: + ++----------------------------------------+ +| Encoded Column 0, Chunk 0 | + ... +| Encoded Column M, Chunk N - 1 | +| Encoded Column M, Chunk N | +| Indices ... | +| Chunk Position (M x N x 8) | +| Manifest (Optional) | +| Metadata | +| i64: metadata position | +| MAJOR_VERSION | MINOR_VERSION | "LANC" | ++----------------------------------------+ + */ + +// UUID type. encoded as 16 bytes. +message UUID { + bytes uuid = 1; +} + +// Manifest is a global section shared between all the files. +message Manifest { + // All fields of the dataset, including the nested fields. + repeated lance.file.Field fields = 1; + + // Schema metadata. + map schema_metadata = 5; + + // Fragments of the dataset. + repeated DataFragment fragments = 2; + + // Snapshot version number. + uint64 version = 3; + + // The file position of the version auxiliary data. + // * It is not inheritable between versions. + // * It is not loaded by default during query. + uint64 version_aux_data = 4; + + message WriterVersion { + // The name of the library that created this file. + string library = 1; + // The version of the library that created this file. Because we cannot assume + // that the library is semantically versioned, this is a string. However, if it + // is semantically versioned, it should be a valid semver string without any 'v' + // prefix. For example: `2.0.0`, `2.0.0-rc.1`. + // + // For forward compatibility with older readers, when writing new manifests this + // field should contain only the core version (major.minor.patch) without any + // prerelease or build metadata. The prerelease/build info should be stored in + // the separate prerelease and build_metadata fields instead. + string version = 2; + // Optional semver prerelease identifier. + // + // This field stores the prerelease portion of a semantic version separately + // from the core version number. For example, if the full version is "2.0.0-rc.1", + // the version field would contain "2.0.0" and prerelease would contain "rc.1". + // + // This separation ensures forward compatibility: older readers can parse the + // clean version field without errors, while newer readers can reconstruct the + // full semantic version by combining version, prerelease, and build_metadata. + // + // If absent, the version field is used as-is. + optional string prerelease = 3; + // Optional semver build metadata. + // + // This field stores the build metadata portion of a semantic version separately + // from the core version number. For example, if the full version is + // "2.0.0-rc.1+build.123", the version field would contain "2.0.0", prerelease + // would contain "rc.1", and build_metadata would contain "build.123". + // + // If absent, no build metadata is present. + optional string build_metadata = 4; + } + + // The version of the writer that created this file. + // + // This information may be used to detect whether the file may have known bugs + // associated with that writer. + WriterVersion writer_version = 13; + + // If present, the file position of the index metadata. + optional uint64 index_section = 6; + + // Version creation Timestamp, UTC timezone + google.protobuf.Timestamp timestamp = 7; + + // Optional version tag + string tag = 8; + + // Feature flags for readers. + // + // A bitmap of flags that indicate which features are required to be able to + // read the table. If a reader does not recognize a flag that is set, it + // should not attempt to read the dataset. + // + // Known flags: + // * 1 << 0: deletion files are present + // * 1 << 1: row ids are stable and stored as part of the fragment metadata. + // * 1 << 2: use v2 format (deprecated) + // * 1 << 3: table config is present + // * 1 << 4: dataset uses multiple base paths + // * 1 << 5: transaction file writes are disabled + // * 1 << 6: data overlay files are present (see DataOverlayFile). Readers that do + // not understand overlays must refuse the dataset, since ignoring an overlay + // would silently return stale base values. + uint64 reader_feature_flags = 9; + + // Feature flags for writers. + // + // A bitmap of flags that indicate which features must be used when writing to the + // dataset. If a writer does not recognize a flag that is set, it should not attempt to + // write to the dataset. + // + // The flag identities are the same as for reader_feature_flags, but the values of + // reader_feature_flags and writer_feature_flags are not required to be identical. + uint64 writer_feature_flags = 10; + + // The highest fragment ID that has been used so far. + // + // This ID is not guaranteed to be present in the current version, but it may + // have been used in previous versions. + // + // For a single fragment, will be zero. For no fragments, will be absent. + optional uint32 max_fragment_id = 11; + + // Path to the transaction file, relative to `{root}/_transactions`. The file at that + // location contains a wire-format serialized Transaction message representing the + // transaction that created this version. + // + // This string field "transaction_file" may be empty if no transaction file was written. + // + // The path format is "{read_version}-{uuid}.txn" where {read_version} is the version of + // the table the transaction read from (serialized to decimal with no padding digits), + // and {uuid} is a hyphen-separated UUID. + string transaction_file = 12; + + // The file position of the transaction content. None if transaction is empty + // This transaction content begins with the transaction content length as u32 + // If the transaction proto message has a length of `len`, the message ends at `len` + 4 + optional uint64 transaction_section = 21; + + // The next unused row id. If zero, then the table does not have any rows. + // + // This is only used if the "stable_row_ids" feature flag is set. + uint64 next_row_id = 14; + + message DataStorageFormat { + // The format of the data files (e.g. "lance") + string file_format = 1; + // The max format version of the data files. The format of the version can vary by + // file_format and is not required to follow semver. + // + // Every file in this version of the dataset has the same file_format version. + string version = 2; + } + + // The data storage format + // + // This specifies what format is used to store the data files. + DataStorageFormat data_format = 15; + + // Table config. + // + // Keys with the prefix "lance." are reserved for the Lance library. Other + // libraries may wish to similarly prefix their configuration keys + // appropriately. + map config = 16; + + // Metadata associated with the table. + // + // This is a key-value map that can be used to store arbitrary metadata + // associated with the table. + // + // This is different than configuration, which is used to tell libraries how + // to read, write, or manage the table. + // + // This is different than schema metadata, which is used to describe the + // data itself and is attached to the output schema of scans. + map table_metadata = 19; + + // Field number 17 (`blob_dataset_version`) was used for a secondary blob dataset. + reserved 17; + reserved "blob_dataset_version"; + + // The base paths of data files. + // + // This is used to determine the base path of a data file. In common cases data file paths are under current dataset base path. + // But for shallow cloning, importing file and other multi-tier storage cases, the actual data files could be outside of the current dataset. + // This field is used with the `base_id` in `lance.file.File` and `lance.file.DeletionFile`. + // + // For example, if we have a dataset with base path `s3://bucket/dataset`, we have a DataFile with base_id 0, we get the actual data file path by: + // base_paths[id = 0] + /data/ + file.path + // the key(a.k.a index) starts from 0, increased by 1 for each new base path. + repeated BasePath base_paths = 18; + + // The branch of the dataset. None means main branch. + optional string branch = 20; +} // Manifest + +// external dataset base path +message BasePath { + uint32 id = 1; + // This is an alias name of the base path, it is optional. + // When we use shallow clone and the target version is a tag, the tag name will be set here. + optional string name = 2; + // Flag indicating whether this path is a dataset root path or file directory: + // - true: Path is a dataset root (actual files under subdirectories like `data`, '_deletions') + // - false: Path is a direct file directory (scenario like importing files) + bool is_dataset_root = 3; + // Note: This absolute path will be directly used by Path:parse(), + string path = 4; +} + +// Auxiliary Data attached to a version. +// Only load on-demand. +message VersionAuxData { + // key-value metadata. + map metadata = 3; +} + +// Metadata describing an index. +message IndexMetadata { + // Unique ID of an index. It is unique across all the dataset versions. + UUID uuid = 1; + + // The columns to build the index. These refer to file.Field.id. + repeated int32 fields = 2; + + // Index name. Must be unique within one dataset version. + string name = 3; + + // The version of the dataset this index was built from. + uint64 dataset_version = 4; + + // A bitmap of the included fragment ids. + // + // This may by used to determine how much of the dataset is covered by the + // index. This information can be retrieved from the dataset by looking at + // the dataset at `dataset_version`. However, since the old version may be + // deleted while the index is still in use, this information is also stored + // in the index. + // + // The bitmap is stored as a 32-bit Roaring bitmap. + bytes fragment_bitmap = 5; + + // Details, specific to the index type, which are needed to load / interpret the index + // + // Indices should avoid putting large amounts of information in this field, as it will + // bloat the manifest. + // + // Indexes are plugins, and so the format of the details message is flexible and not fully + // defined by the table format. However, there are some conventions that should be followed: + // + // - When Lance APIs refer to indexes they will use the type URL of the index details as the + // identifier for the index type. If a user provides a simple string identifier like + // "btree" then it will be converted to "/lance.table.BTreeIndexDetails" + // - Type URLs comparisons are case-insensitive. Thereform an index must have a unique type + // URL ignoring case. + google.protobuf.Any index_details = 6; + + // The minimum lance version that this index is compatible with. + optional int32 index_version = 7; + + // Timestamp when the index was created (UTC timestamp in milliseconds since epoch) + // + // This field is optional for backward compatibility. For existing indices created before + // this field was added, this will be None/null. + optional uint64 created_at = 8; + + // The base path index of the data file. Used when the file is imported or referred from another dataset. + // Lance use it as key of the base_paths field in Manifest to determine the actual base path of the data file. + optional uint32 base_id = 9; + + // List of files and their sizes for this index segment. + // This enables skipping HEAD calls when opening indices and allows reporting + // of index sizes without extra IO. + // If this is empty, the index files sizes are unknown. + repeated IndexFile files = 10; +} + +// Metadata about a single file within an index segment. +message IndexFile { + // Path relative to the index directory (e.g., "index.idx", "auxiliary.idx") + string path = 1; + // Size of the file in bytes + uint64 size_bytes = 2; +} + +// Index Section, containing a list of index metadata for one dataset version. +message IndexSection { + repeated IndexMetadata indices = 1; +} + +// A DataFragment is a set of files which represent the different columns of the same +// rows. If column exists in the schema of a dataset, but the file for that column does +// not exist within a DataFragment of that dataset, that column consists entirely of +// nulls. +message DataFragment { + // The ID of a DataFragment is unique within a dataset. + uint64 id = 1; + + repeated DataFile files = 2; + + // Optional overlay files for this fragment, which supply new values for a + // subset of cells without rewriting the base data files. This MUST be empty + // if the data overlay files feature flag (64) is not set in the manifest. + // + // Order is significant: a later entry is newer than an earlier one. When two + // overlays cover the same (offset, field) and share a `committed_version`, the + // later entry wins. See DataOverlayFile for the full resolution rules. + repeated DataOverlayFile overlays = 11; + + // File that indicates which rows, if any, should be considered deleted. + DeletionFile deletion_file = 3; + + // TODO: What's the simplest way we can allow an inline tombstone bitmap? + + // A serialized RowIdSequence message (see rowids.proto). + // + // These are the row ids for the fragment, in order of the rows as they appear. + // That is, if a fragment has 3 rows, and the row ids are [1, 42, 3], then the + // first row is row 1, the second row is row 42, and the third row is row 3. + oneof row_id_sequence { + // If small (< 200KB), the row ids are stored inline. + bytes inline_row_ids = 5; + // Otherwise, stored as part of a file. + ExternalFile external_row_ids = 6; + } // row_id_sequence + + oneof last_updated_at_version_sequence { + // If small (< 200KB), the row latest updated versions are stored inline. + bytes inline_last_updated_at_versions = 7; + // Otherwise, stored as part of a file. + ExternalFile external_last_updated_at_versions = 8; + } // last_updated_at_version_sequence + + oneof created_at_version_sequence { + // If small (< 200KB), the row created at versions are stored inline. + bytes inline_created_at_versions = 9; + // Otherwise, stored as part of a file. + ExternalFile external_created_at_versions = 10; + } // created_at_version_sequence + + // Number of original rows in the fragment, this includes rows that are now marked with + // deletion tombstones. To compute the current number of rows, subtract + // `deletion_file.num_deleted_rows` from this value. + uint64 physical_rows = 4; +} + +message DataFile { + // Path to the root relative to the dataset's URI. + string path = 1; + // The ids of the fields/columns in this file. + // + // When a DataFile object is created in memory, every value in fields is assigned -1 by + // default. An object with a value in fields of -1 must not be stored to disk. -2 is + // used for "tombstoned", meaning a field that is no longer in use. This is often + // because the original field id was reassigned to a different data file. + // + // In Lance v1 IDs are assigned based on position in the file, offset by the max + // existing field id in the table (if any already). So when a fragment is first created + // with one file of N columns, the field ids will be 1, 2, ..., N. If a second fragment + // is created with M columns, the field ids will be N+1, N+2, ..., N+M. + // + // In Lance v1 there is one field for each field in the input schema, this includes + // nested fields (both struct and list). Fixed size list fields have only a single + // field id (these are not considered nested fields in Lance v1). + // + // This allows column indices to be calculated from field IDs and the input schema. + // + // In Lance v2 the field IDs generally follow the same pattern but there is no + // way to calculate the column index from the field ID. This is because a given + // field could be encoded in many different ways, some of which occupy a different + // number of columns. For example, a struct field could be encoded into N + 1 columns + // or it could be encoded into a single packed column. To determine column indices + // the column_indices property should be used instead. + // + // In Lance v1 these ids must be sorted but might not always be contiguous. + repeated int32 fields = 2; + // The top-level column indices for each field in the file. + // + // If the data file is version 1 then this property will be empty + // + // Otherwise there must be one entry for each field in `fields`. + // + // Some fields may not correspond to a top-level column in the file. In these cases + // the index will -1. + // + // For example, consider the schema: + // + // - dimension: packed-struct (0): + // - x: u32 (1) + // - y: u32 (2) + // - path: `list` (3) + // - embedding: `fsl<768>` (4) + // - fp64 + // - borders: `fsl<4>` (5) + // - simple-struct (6) + // - margin: fp64 (7) + // - padding: fp64 (8) + // + // One possible column indices array could be: + // [0, -1, -1, 1, 3, 4, 5, 6, 7] + // + // This reflects quite a few phenomenon: + // - The packed struct is encoded into a single column and there is no top-level column + // for the x or y fields + // - The variable sized list is encoded into two columns + // - The embedding is encoded into a single column (common for FSL of primitive) and there + // is not "FSL column" + // - The borders field actually does have an "FSL column" + // + // The column indices table may not have duplicates (other than -1) + repeated int32 column_indices = 3; + // The major file version used to create the file + uint32 file_major_version = 4; + // The minor file version used to create the file + // + // If both `file_major_version` and `file_minor_version` are set to 0, + // then this is a version 0.1 or version 0.2 file. + uint32 file_minor_version = 5; + + // The known size of the file on disk in bytes. + // + // This is used to quickly find the footer of the file. + // + // When this is zero, it should be interpreted as "unknown". + uint64 file_size_bytes = 6; + + // The base path index of the data file. Used when the file is imported or referred from another dataset. + // Lance use it as key of the base_paths field in Manifest to determine the actual base path of the data file. + optional uint32 base_id = 7; +} // DataFile + +// An overlay file supplies new values for a subset of (row offset, field) cells +// within a fragment, without rewriting the fragment's base data files. It is +// used for efficient updates when only a small fraction of rows and/or columns +// change. +// +// On read, a cell is resolved by consulting the fragment's overlays from newest +// to oldest: the first overlay that covers that (offset, field) wins; if none +// cover it, the value falls through to the base data file. Because deletions +// take precedence over overlays, an overlay value for an offset that is also +// marked deleted is dead and is ignored. +// +// The overlay's data file does NOT store a row-offset key column. Within a value +// column, the position of a covered offset's value is the rank (0-based count of +// set bits below it) of that offset within the field's coverage bitmap. Because +// fields may cover different offset sets, the value columns of a single overlay +// data file may have different lengths (which the Lance file format permits). +message DataOverlayFile { + // The data file storing the overlay's new cell values, one value column per + // field in `data_file.fields`. No row-offset key column is stored. + DataFile data_file = 1; + + // Which (offset, field) cells this overlay provides values for. + oneof coverage { + // A single 32-bit Roaring bitmap of physical row offsets that applies to + // every field in `data_file.fields` (a "dense" / rectangular overlay). + // Every covered offset has a value for every field. This is the common case + // for a plain UPDATE, where one SET list is applied to one set of rows. + bytes shared_offset_bitmap = 2; + // Per-field coverage for a "sparse" overlay, used when different fields cover + // different offset sets (e.g. a MERGE with multiple WHEN MATCHED branches). + FieldCoverage field_coverage = 4; + } + + // The dataset version at which this overlay became effective: the version of + // the commit that introduced it, NOT the version it was read from. It is + // stamped at commit time and re-stamped if the commit is retried, in the same + // way as the created-at / last-updated-at version sequences. + // + // This drives two orderings: + // * Versus index builds: an index whose `dataset_version` >= this value + // already incorporates this overlay. Otherwise the overlay's covered cells + // are excluded from index results for the affected fields and re-evaluated + // against their current values (see the Data Overlay Files specification). + // * Versus other overlays: when two overlays cover the same (offset, field), + // the one with the higher `committed_version` wins. Overlays that share a + // `committed_version` are ordered by their position in + // `DataFragment.overlays`, where a later entry is newer and wins. + uint64 committed_version = 3; +} + +// Per-field coverage for a sparse overlay. +message FieldCoverage { + // One entry per field in the overlay's `data_file.fields`, in the same order. + // Each is a 32-bit Roaring bitmap of the physical row offsets covered for that + // field. An offset present in a field's bitmap but mapped to a NULL value + // means the cell is overridden to NULL (distinct from an offset that is absent, + // which falls through to the base data file). + repeated bytes offset_bitmaps = 1; +} + +// Deletion File +// +// The path of the deletion file is constructed as: +// {root}/_deletions/{fragment_id}-{read_version}-{id}.{extension} +// where {extension} depends on DeletionFileType. +message DeletionFile { + // Type of deletion file, intended as a way to increase efficiency of the storage of deleted row + // offsets. If there are sparsely deleted rows, then ARROW_ARRAY is the most efficient. If there + // are densely deleted rows, then BITMAP is the most efficient. + enum DeletionFileType { + // A single Int32Array of deleted row offsets, stored as an Arrow IPC file with one batch and + // one column. Has a .arrow extension. + ARROW_ARRAY = 0; + // A Roaring Bitmap of deleted row offsets. Has a .bin extension. + BITMAP = 1; + } + + // Type of deletion file. + DeletionFileType file_type = 1; + // The version of the dataset this deletion file was built from. + uint64 read_version = 2; + // An opaque id used to differentiate this file from others written by concurrent + // writers. + uint64 id = 3; + // The number of rows that are marked as deleted. + uint64 num_deleted_rows = 4; + // The base path index of the deletion file. Used when the file is imported or referred from another + // dataset. Lance uses it as key of the base_paths field in Manifest to determine the actual base + // path of the deletion file. + optional uint32 base_id = 7; +} // DeletionFile + +message ExternalFile { + // Path to the file, relative to the root of the table. + string path = 1; + // The byte offset in the file where the data starts. + uint64 offset = 2; + // The size of the data in the file, in bytes. + uint64 size = 3; +} + +// VectorIndexDetails and HnswParameters (formerly HnswIndexDetails) moved to index.proto + +message FragmentReuseIndexDetails { + + oneof content { + // if < 200KB, store the content inline, otherwise store the InlineContent bytes in external file + InlineContent inline = 1; + ExternalFile external = 2; + } + + message InlineContent { + repeated Version versions = 1; + } + + message FragmentDigest { + uint64 id = 1; + + uint64 physical_rows = 2; + + uint64 num_deleted_rows = 3; + } + + // A summarized version of the RewriteGroup information in a Rewrite transaction + message Group { + // A roaring treemap of the changed row addresses. + // When combined with the old fragment IDs and new fragment IDs, + // it can recover the full mapping of old row addresses to either new row addresses or deleted. + // this mapping can then be used to remap indexes or satisfy index queries for the new unindexed fragments. + bytes changed_row_addrs = 1; + + repeated FragmentDigest old_fragments = 2; + + repeated FragmentDigest new_fragments = 3; + } + + message Version { + // The dataset_version at the time the index adds this version entry + uint64 dataset_version = 1; + + repeated Group groups = 3; + } +} + +// ============================================================================ +// MemWAL Index Types +// ============================================================================ + +// Lifecycle status of a WAL shard. Drives drop-table two-phase commit: +// a SEALED shard refuses new writer claims (reversible) until the drop +// commits (the shard dir is deleted) or rolls back (status -> ACTIVE). +enum ShardStatus { + // Normal: the shard accepts writer claims. + ACTIVE = 0; + // A drop is in flight: claims are refused. Reversible to ACTIVE. + SEALED = 1; +} + +// Shard manifest containing epoch-based fencing and WAL state. +// Each shard has exactly one active writer at any time. +message ShardManifest { + // Shard identifier (UUID v4). + UUID shard_id = 11; + + // Manifest version number. + // Matches the version encoded in the filename. + uint64 version = 1; + + // Shard spec ID this shard was created with. + // Set at shard creation and immutable thereafter. + // A value of 0 indicates a manually-created shard not governed by any spec. + uint32 shard_spec_id = 10; + + // Computed shard field values as raw Arrow scalar bytes, keyed by shard + // field id. The byte encoding follows Arrow's little-endian convention: + // int32 is 4 LE bytes, utf8 is raw UTF-8 bytes, etc. The receiver looks + // up the result_type from the ShardingSpec to interpret each value. + repeated ShardFieldEntry shard_field_entries = 14; + + // Writer fencing token - monotonically increasing. + // A writer must increment this when claiming the shard. + uint64 writer_epoch = 2; + + // The most recent WAL entry position that has been flushed to a MemTable. + // During recovery, replay starts from replay_after_wal_entry_position + 1. + // WAL positions are 1-based, so the default value 0 unambiguously means + // "no flush has ever stamped this shard" and recovery replays from 1. + uint64 replay_after_wal_entry_position = 3; + + // The most recent WAL entry position observed at the time the manifest was + // updated. WAL positions are 1-based; default 0 means no entry has been + // written yet. This is a hint, not authoritative - recovery must list + // files to find actual state. + uint64 wal_entry_position_last_seen = 4; + + // Generation to assign to the next SSTable (incremented after each MemTable flush). + uint64 current_generation = 6; + + // Field 7 removed: compaction progress lives in + // MemWalIndexDetails.compacted_sstables. + + // List of SSTables created by flushing MemTables and their directory paths. + repeated SsTable sstables = 8; + + // Lifecycle status. Default ACTIVE; SEALED marks an in-flight drop + // (drop-table 2PC). A SEALED manifest refuses claims at claim_epoch. + ShardStatus status = 15; +} + +// A shard field value stored as raw Arrow scalar bytes. +message ShardFieldEntry { + // Shard field id (matches ShardingField.field_id in the ShardingSpec). + string field_id = 1; + + // Raw Arrow scalar value bytes in little-endian encoding. + // The data type is determined by the result_type of the matching ShardingField. + bytes value = 2; +} + +// An SSTable: the immutable result of flushing a MemTable, stored as a Lance dataset. +message SsTable { + // Generation number identifying this SSTable. + uint64 generation = 1; + + // Directory name relative to the shard directory. + string path = 2; +} + +// A pointer to the latest SSTable compacted for a shard. +message CompactedSsTable { + // Shard identifier (UUID v4). + UUID shard_id = 1; + + // Generation of the latest SSTable compacted into the base table for this shard. + uint64 generation = 2; +} + +// Tracks which compacted SSTable generation a base table index has been rebuilt to cover. +// Used to determine whether to read from SSTable indexes or base table. +message IndexCatchupProgress { + // Name of the base table index (must match an entry in maintained_indexes). + string index_name = 1; + + // Per-shard progress: the generation up to which this index covers. + // If a shard is not present, the index is assumed to be fully caught up + // (i.e., caught_up_generation >= compacted_generation for that shard). + repeated CompactedSsTable caught_up_generations = 2; +} + +// Index details for MemWAL Index, stored in IndexMetadata.index_details. +// This is the centralized structure for all MemWAL metadata: +// - Configuration (sharding specs, indexes to maintain) +// - SSTable compaction progress +// - Shard state snapshots +// +// Writers read this index to get configuration before writing. +// Readers may use shard snapshots in this index as a point-in-time +// optimization. Readers that need the latest shard set should list shard +// directories in storage and read each shard's latest manifest. +// A background process updates the index periodically to keep shard snapshots current. +// +// Shard snapshots are stored as a Lance file with one row per shard. +// The schema records shard discovery fields. Full mutable shard state remains +// authoritative in the shard manifest files. +// shard_id: utf8 +// shard_spec_id: uint32 +// shard_field_{field_id}: typed per the matching ShardingField.result_type +message MemWalIndexDetails { + // Snapshot timestamp (Unix timestamp in milliseconds). + int64 snapshot_ts_millis = 1; + + // Number of shards in the snapshot. + // Used to determine storage format without reading the snapshot data. + uint32 num_shards = 2; + + // Inline shard snapshots for small shard counts. + // When num_shards <= threshold (implementation-defined, e.g., 100), + // snapshots are stored inline as serialized bytes. + // Format: Lance file bytes with the shard snapshot schema. + optional bytes inline_snapshots = 3; + + // Sharding specs defining how to derive shard identifiers. + // This configuration determines how rows are partitioned into shards. + repeated ShardingSpec sharding_specs = 7; + + // Indexes from the base table to maintain in MemTables. + // These are index names referencing indexes defined on the base table. + // The primary key btree index is always maintained implicitly and + // should not be listed here. + // + // For vector indexes, MemTables inherit quantization parameters (PQ codebook, + // SQ params) from the base table index to ensure distance comparability. + repeated string maintained_indexes = 8; + + // Latest SSTable compacted into the base table for each shard. + // This is updated atomically with merge-insert data commits, enabling + // conflict resolution when multiple compactors operate concurrently. + // + // Note: This is separate from shard snapshots because: + // 1. compacted_sstables is updated by compactors (atomic with data commit) + // 2. shard snapshots are updated by background index builder + repeated CompactedSsTable compacted_sstables = 9; + + // Per-index catchup progress tracking. + // When data is compacted into the base table, base table indexes are rebuilt + // asynchronously. This field tracks which generation each index covers. + // + // For indexed queries, if an index's caught_up_generation < compacted_generation, + // readers should use SSTable indexes for the gap instead of + // scanning unindexed data in the base table. + // + // If an index is not present in this list, it is assumed to be fully caught up. + repeated IndexCatchupProgress index_catchup = 10; + + // Default ShardWriter configuration values for this MemWAL index. + // + // A free-form string map persisted so that every writer — across + // processes and restarts — starts from the same default writer + // configuration. These are defaults only: an individual writer may + // still override any value at runtime in its own ShardWriterConfig + // (which is not persisted). + map writer_config_defaults = 11; +} + +// Sharding spec definition. +message ShardingSpec { + // Unique identifier for this spec within the index. + // IDs are never reused. + uint32 spec_id = 1; + + // Sharding field definitions that determine how to compute shard identifiers. + repeated ShardingField fields = 2; +} + +// Sharding field definition. +message ShardingField { + // Unique string identifier for this shard field. + string field_id = 1; + + // Field IDs referencing source columns in the schema. + repeated int32 source_ids = 2; + + // Well-known shard transform name (e.g., "identity", "year", "bucket"). + // Mutually exclusive with expression. + optional string transform = 3; + + // DataFusion SQL expression for custom logic. + // Mutually exclusive with transform. + optional string expression = 4; + + // Output type of the shard value (Arrow type name). + string result_type = 5; + + // Transform parameters (e.g., num_buckets for bucket transform). + map parameters = 6; +} diff --git a/lance-artifact/protos/table_identifier.proto b/lance-artifact/protos/table_identifier.proto new file mode 100644 index 000000000..3a4714552 --- /dev/null +++ b/lance-artifact/protos/table_identifier.proto @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +package lance.datafusion; + +// Identifies a Lance dataset for remote reconstruction. +// +// Two modes: +// 1. uri + serialized_manifest (fast): remote executor skips manifest read. +// 2. uri + version + etag (lightweight): remote executor loads manifest from storage. +message TableIdentifier { + string uri = 1; + uint64 version = 2; + optional string manifest_etag = 3; + optional bytes serialized_manifest = 4; + map storage_options = 5; +} diff --git a/lance-artifact/protos/transaction.proto b/lance-artifact/protos/transaction.proto new file mode 100644 index 000000000..c3c5e3819 --- /dev/null +++ b/lance-artifact/protos/transaction.proto @@ -0,0 +1,377 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +syntax = "proto3"; + +import "file.proto"; +import "table.proto"; +import "google/protobuf/any.proto"; + +package lance.table; + +// A transaction represents the changes to a dataset. +// +// This has two purposes: +// 1. When retrying a commit, the transaction can be used to re-build an updated +// manifest. +// 2. When there's a conflict, this can be used to determine whether the other +// transaction is compatible with this one. +message Transaction { + // The version of the dataset this transaction was built from. + // + // For example, for a delete transaction this means the version of the dataset + // that was read from while evaluating the deletion predicate. + uint64 read_version = 1; + + // The UUID that unique identifies a transaction. + string uuid = 2; + + // Optional version tag. + string tag = 3; + + // Optional properties for the transaction + // __lance_commit_message is a reserved key + map transaction_properties = 4; + + // Add new rows to the dataset. + message Append { + // The new fragments to append. + // + // Fragment IDs are not yet assigned. + repeated DataFragment fragments = 1; + } + + // Mark rows as deleted. + message Delete { + // The fragments to update + // + // The fragment IDs will match existing fragments in the dataset. + repeated DataFragment updated_fragments = 1; + // The fragments to delete entirely. + repeated uint64 deleted_fragment_ids = 2; + // The predicate that was evaluated + // + // This may be used to determine whether the delete would have affected + // files written by a concurrent transaction. + string predicate = 3; + } + + // Create or overwrite the entire dataset. + message Overwrite { + // The new fragments + // + // Fragment IDs are not yet assigned. + repeated DataFragment fragments = 1; + // The new schema + repeated lance.file.Field schema = 2; + // Schema metadata. + map schema_metadata = 3; + // Key-value pairs to merge with existing config. + map config_upsert_values = 4; + // The base paths to be added for the initial dataset creation + repeated BasePath initial_bases = 5; + } + + // Add or replace a new secondary index. + // + // This is also used to remove an index (we are replacing it with nothing) + // + // - new_indices: the modified indices, empty if dropping indices only + // - removed_indices: the indices that are being replaced + message CreateIndex { + repeated IndexMetadata new_indices = 1; + repeated IndexMetadata removed_indices = 2; + } + + // An operation that rewrites but does not change the data in the table. These + // kinds of operations just rearrange data. + message Rewrite { + // The old fragments that are being replaced + // + // DEPRECATED: use groups instead. + // + // These should all have existing fragment IDs. + repeated DataFragment old_fragments = 1; + // The new fragments + // + // DEPRECATED: use groups instead. + // + // These fragments IDs are not yet assigned. + repeated DataFragment new_fragments = 2; + + // During a rewrite an index may be rewritten. We only serialize the UUID + // since a rewrite should not change the other index parameters. + message RewrittenIndex { + // The id of the index that will be replaced + UUID old_id = 1; + // the id of the new index + UUID new_id = 2; + // the new index details + google.protobuf.Any new_index_details = 3; + // the version of the new index + uint32 new_index_version = 4; + // Files in the new index with their sizes. + // Empty if file sizes are not available (e.g. older writers). + repeated IndexFile new_index_files = 5; + } + + // A group of rewrite files that are all part of the same rewrite. + message RewriteGroup { + // The old fragment that is being replaced + // + // This should have an existing fragment ID. + repeated DataFragment old_fragments = 1; + // The new fragment + // + // The ID should have been reserved by an earlier + // reserve operation + repeated DataFragment new_fragments = 2; + } + + // Groups of files that have been rewritten + repeated RewriteGroup groups = 3; + // Indices that have been rewritten + repeated RewrittenIndex rewritten_indices = 4; + } + + // An operation that merges in a new column, altering the schema. + message Merge { + // The updated fragments + // + // These should all have existing fragment IDs. + repeated DataFragment fragments = 1; + // The new schema + repeated lance.file.Field schema = 2; + // Schema metadata. + map schema_metadata = 3; + } + + // An operation that projects a subset of columns, altering the schema. + message Project { + // The new schema + repeated lance.file.Field schema = 1; + } + + // An operation that restores a dataset to a previous version. + message Restore { + // The version to restore to + uint64 version = 1; + } + + // An operation that reserves fragment ids for future use in + // a rewrite operation. + message ReserveFragments { + uint32 num_fragments = 1; + } + + // An operation that clones a dataset. + message Clone { + // - true: Performs a metadata-only clone (copies manifest without data files). + // The cloned dataset references original data through `base_paths`, + // suitable for experimental scenarios or rapid metadata migration. + // - false: Performs a full deep clone using the underlying object storage's native + // copy API (e.g., S3 CopyObject, GCS rewrite). This leverages server-side + // bulk copy operations to bypass download/upload bottlenecks, achieving + // near-linear speedup for large datasets (typically 3-10x faster than + // manual file transfers). The operation maintains atomicity and data + // integrity guarantees provided by the storage backend. + bool is_shallow = 1; + // the reference name in the source dataset + // in most cases it should be the branch or tag name in the source dataset + optional string ref_name = 2; + // the version of the source dataset for cloning + uint64 ref_version = 3; + // the absolute base path of the source dataset for cloning + string ref_path = 4; + // if the target dataset is a branch, this is the branch name of the target dataset + optional string branch_name = 5; + } + + // Exact set of key hashes for conflict detection. + // Used when the number of inserted rows is small. + message ExactKeySetFilter { + // 64-bit hashes of the inserted row keys. + repeated uint64 key_hashes = 1; + } + + // Bloom filter for key existence tests. + // Used when the number of rows is large. + message BloomFilter { + // Bitset backing the bloom filter (SBBF format). + bytes bitmap = 1; + // Number of bits in the bitmap. + uint32 num_bits = 2; + // Number of items the filter was sized for. + // Used for intersection validation (filters with different sizes cannot be compared). + // Default: 8192 + uint64 number_of_items = 3; + // False positive probability the filter was sized for. + // Used for intersection validation (filters with different parameters cannot be compared). + // Default: 0.00057 + double probability = 4; + } + + // A filter for checking key existence in set of rows inserted by a merge insert operation. + // Only created when the merge insert's ON columns match the schema's unenforced primary key. + // The presence of this filter indicates strict primary key conflict detection should be used. + // Can use either an exact set (for small row counts) or a Bloom filter (for large row counts). + message KeyExistenceFilter { + // Field IDs of columns participating in the key (must match unenforced primary key). + repeated int32 field_ids = 1; + // The underlying data structure storing the key hashes. + oneof data { + // Exact set of key hashes (used for small number of rows). + ExactKeySetFilter exact = 2; + // Bloom filter (used for large number of rows). + BloomFilter bloom = 3; + } + } + + // Serialized as sorted distinct local physical row offsets within the fragment (0-based). + message UInt32List { + repeated uint32 values = 1; + } + + // An operation that updates rows but does not add or remove rows. + message Update { + // The fragments that have been removed. These are fragments where all rows + // have been updated and moved to a new fragment. + repeated uint64 removed_fragment_ids = 1; + // The fragments that have been updated. + repeated DataFragment updated_fragments = 2; + // The new fragments where updated rows have been moved to. + repeated DataFragment new_fragments = 3; + // The ids of the fields that have been modified. + repeated uint32 fields_modified = 4; + /// SSTables to mark as compacted after this transaction. + repeated CompactedSsTable compacted_sstables = 5; + /// The fields that used to judge whether to preserve the new frag's id into + /// the frag bitmap of the specified indices. + repeated uint32 fields_for_preserving_frag_bitmap = 6; + // The mode of update + UpdateMode update_mode = 7; + // Filter for checking existence of keys in newly inserted rows, used for conflict detection. + // Only tracks keys from INSERT operations during merge insert, not updates. + optional KeyExistenceFilter inserted_rows = 8; + // Per-fragment physical row offsets that matched an update_columns hash join (RewriteColumns). + map updated_fragment_offsets = 9; + } + + // The mode of update operation + enum UpdateMode { + + /// rows are deleted in current fragments and rewritten in new fragments. + /// This is most optimal when the majority of columns are being rewritten + /// or only a few rows are being updated. + REWRITE_ROWS = 0; + + /// within each fragment, columns are fully rewritten and inserted as new data files. + /// Old versions of columns are tombstoned. This is most optimal when most rows are affected + /// but a small subset of columns are affected. + REWRITE_COLUMNS = 1; + } + + // An entry for a map update. If value is not set, the key will be removed from the map. + message UpdateMapEntry { + // The key of the map entry to update. + string key = 1; + // The value to set for the key. + optional string value = 2; + } + + message UpdateMap { + repeated UpdateMapEntry update_entries = 1; + // If true, the map will be replaced entirely with the new entries. + // If false, the new entries will be merged with the existing map. + bool replace = 2; + } + + // An operation that updates the table config, table metadata, schema metadata, + // or field metadata. + message UpdateConfig { + UpdateMap config_updates = 6; + UpdateMap table_metadata_updates = 7; + UpdateMap schema_metadata_updates = 8; + map field_metadata_updates = 9; + + // Deprecated ------------------------------- + map upsert_values = 1; + repeated string delete_keys = 2; + map schema_metadata = 3; + map field_metadata = 4; + + message FieldMetadataUpdate { + map metadata = 5; + } + } + + message DataReplacementGroup { + uint64 fragment_id = 1; + DataFile new_file = 2; + } + + // An operation that replaces the data in a region of the table with new data. + message DataReplacement { + repeated DataReplacementGroup replacements = 1; + } + + // Overlay files to append to a single fragment, in order (the last entry is + // newest). The overlays are appended to the fragment's existing `overlays` + // list; they do not replace it, so overlays written by concurrent commits are + // preserved. + message DataOverlayGroup { + uint64 fragment_id = 1; + // Each DataOverlayFile.committed_version is left 0 by the writer and stamped + // to the new dataset version at commit time (re-stamped on retry), in the + // same way as the created-at / last-updated-at version sequences. The fields + // touched are read from each overlay's `data_file.fields`. + repeated DataOverlayFile overlays = 2; + } + + // Attach overlay files to fragments, supplying new values for a subset of + // (row offset, field) cells without rewriting the fragments' base data files. + // See the DataOverlayFile message in table.proto for resolution, coverage, and + // versioning rules, and the Data Overlay Files and Transactions specifications + // for the (intentionally permissive) conflict semantics. + message DataOverlay { + repeated DataOverlayGroup groups = 1; + } + + // Update SSTable compaction progress in the MemWAL index. + // This operation is used during merge-insert to atomically record which + // SSTables have been compacted into the base table. + message UpdateMemWalState { + // SSTables being marked as compacted. + repeated CompactedSsTable compacted_sstables = 1; + } + + // An operation that updates base paths in the dataset. + message UpdateBases { + // The new base paths to add to the manifest. + repeated BasePath new_bases = 1; + } + + // The operation of this transaction. + oneof operation { + Append append = 100; + Delete delete = 101; + Overwrite overwrite = 102; + CreateIndex create_index = 103; + Rewrite rewrite = 104; + Merge merge = 105; + Restore restore = 106; + ReserveFragments reserve_fragments = 107; + Update update = 108; + Project project = 109; + UpdateConfig update_config = 110; + DataReplacement data_replacement = 111; + UpdateMemWalState update_mem_wal_state = 112; + Clone clone = 113; + UpdateBases update_bases = 114; + DataOverlay data_overlay = 115; + } + + // Fields 200/202 (`blob_append` / `blob_overwrite`) previously represented blob dataset ops. + reserved 200, 202; + reserved "blob_append", "blob_overwrite"; +} diff --git a/lance-artifact/rust/.gitignore b/lance-artifact/rust/.gitignore new file mode 100644 index 000000000..2eea525d8 --- /dev/null +++ b/lance-artifact/rust/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/lance-artifact/rust/AGENTS.md b/lance-artifact/rust/AGENTS.md new file mode 100644 index 000000000..70a803c6c --- /dev/null +++ b/lance-artifact/rust/AGENTS.md @@ -0,0 +1,90 @@ +# Rust Guidelines + +Also see [root AGENTS.md](../AGENTS.md) for cross-language standards. + +## Code Style + +- Use `Vec::with_capacity()` when size is known or estimable — prefer over-estimating capacity to multiple reallocations. +- Wrap large or expensive-to-clone struct fields (maps, protobuf metadata, schemas) in `Arc` to avoid deep copies. +- Use `Box::pin(...)` or `.boxed()` but never both — `.boxed()` already returns `Pin>`. +- Remove dead code instead of adding `#[allow(dead_code)]`. Delete unused constants instead of reducing visibility. +- Use `column_by_name()` for `RecordBatch` column access in production code; use `batch["column_name"]` in tests. +- Use `PrimitiveArray::::from(vec)` (zero-copy) instead of `from_iter_values(vec)` for Vec-to-PrimitiveArray conversion. +- Implement `Default` trait on config/options structs instead of standalone `default_*()` helpers. +- Place `#[cfg(test)] mod tests` as a single block at the bottom of each file — no production code after it. +- Place `use` imports at the top of the file, not inline within function bodies. +- Extract substantial new logic (bin packing, scheduling) into dedicated submodules instead of inlining into large files. +- Delete obsolete internal (`pub(crate)` / private) methods in the same PR that introduces their replacements. For public API methods, follow the deprecation path in root AGENTS.md instead. +- Choose log levels by audience: `debug!` for routine/high-frequency ops, `info!` for infrequent operator-visible state changes, `warn!` for unexpected conditions. + +## Concurrency + +- The closure passed to `spawn_cpu()` must only consume CPU and return — it must **never** wait on anything: **no channels** (blocking send/recv), **no I/O**, **no locks**, and no `block_on`/`.blocking_*`. The CPU pool can collapse to a single worker in resource-constrained environments (`<= 3` CPUs), so a parked closure can deadlock the whole pool with a silent 0% hang. Keep the waiting in surrounding async code and hand only the pure-CPU work to `spawn_cpu()`. Only dispatch substantial work (rule of thumb: ~100µs+ of CPU); below that the pool overhead outweighs the benefit and the work is better left inline. See the doc comment on `spawn_cpu` for the rationale. + +## API Design + +- Use `with_`-prefixed builder methods for optional config (e.g., `MyStruct::new(required).with_option(v)`) — don't create separate constructor variants. +- For public APIs, prefer `Into` or `AsRef` trait bounds for flexible inputs. +- Prefer `pub(crate)` over `pub` for crate-internal items. Use `pub use` re-exports for the actual public API surface. +- Use enums instead of magic numbers for format versions, variant types, and discriminators — leverage exhaustive `match`. +- Use strongly-typed structs instead of `HashMap` in APIs — convert to strings only at serialization boundaries. +- Keep `RowAddr` (physical fragment+offset) and `RowId` (stable logical identifier) as distinct types — never raw `u64` for both. +- Use `RowAddress` from `lance-core/src/utils/address.rs` instead of raw bitwise operations on row addresses. +- Use `RowAddrTreeMap`/`RoaringBitmap` instead of `Vec>` for physical row selections. +- Use logical row counts (`num_rows()`) instead of `physical_rows` for user-facing metrics — subtract deletions. +- Keep traits minimal — only core abstraction methods. Move helpers to standalone functions and config to struct fields. +- Get column/field types from schema metadata — never materialize data rows just to inspect types. +- Use stable, versioned serialization formats for persistent storage (e.g., index files) — avoid unstable cross-version formats. +- Use Arrow's type-safe access (`ArrayAccessor` trait bounds, `as_*_array` helpers) instead of `arrow::compute::cast` + `downcast_ref`. Prefer `_opt` variants (e.g., `as_string_opt`) unless the data type has already been verified. +- In `lance-io/`, use single-syscall writes for local filesystem I/O — don't reuse cloud multipart upload machinery. + +## Error Handling + +- Never use `.unwrap()`, `.expect()`, `panic!()`, or `assert!()` in library code for fallible operations — use `?` with `Result` and proper error types. Reserve `.unwrap()` for tests only. +- Avoid bare `.unwrap()`; use `if let`, `match`, `let ... else`, `?`, or combinators. Never `.is_none()` followed by `.unwrap()`. If unavoidable, use `.expect("reason")`. +- Return `LanceError::NotSupported` instead of `todo!()` or `unimplemented!()` for unsupported code paths. Test with `Result::Err` assertions, not `#[should_panic]`. +- Match `Error` variant to root cause: `Error::invalid_input` for caller data issues, `Error::corrupt_file` for format/integrity issues, `Error::not_found` for missing resources, `Error::io` for I/O failures. +- Include full context in error messages — variable names, values, sizes, types, indices. Not generic messages like `"Invalid chunk size"`. +- Use `checked_add`/`checked_mul` instead of `wrapping_add`/`wrapping_mul` for counters and IDs — return an error on overflow. +- Prefer `debug_assert!` over `assert!` for non-safety invariants; reserve `assert!` for conditions preventing data corruption. Always include descriptive messages. +- Don't silently guard against impossible conditions — use `debug_assert!`, return an explicit error, or remove the check. +- Log warnings on best-effort/cleanup failures instead of silently swallowing or propagating errors. +- Log warnings for silent no-ops (skipped operations); omit warnings before errors since the error message is sufficient. +- Avoid `unwrap_or(default)` on map lookups for required config params — use `.ok_or_else(|| Error::...)` and verify key names match between serialization and deserialization. +- Advance all parallel iterators before any `continue` branches — early exits that skip `.next()` calls cause misalignment. +- Bind `iter.next()` with `let Some(x) = iter.next() else { ... }` — never call `.next()` twice to check-then-use. + +## Naming + +- Reserve `_`-prefixed names for truly unused bindings — if a variable is read, drop the underscore. +- Prefix boolean variables with `is_` or `has_` instead of ambiguous `with_` or bare adjectives. +- Name booleans so `false` (zero/`Default::default()`) is the desired default — use `disable_*` instead of `enable_*` when the feature should be on by default. +- Name functions to match their actual scope — e.g., `handle_partition_system_columns` not `handle_system_columns` if only a subset is handled. + +## Testing + +- Use `record_batch!()` from `arrow_array` to construct `RecordBatch` in tests instead of manual Schema/Arc/try_new boilerplate. +- Use `gen_batch()` builder API (`.col()`, `.into_reader_rows()`) for test data setup instead of manual Arrow construction. +- Use `.try_into_batch()` instead of `.try_into_stream().try_collect()` for scanner results in tests. +- Use plain `"memory://"` URIs in tests — no atomic counters or unique suffixes needed. +- Assert on both error variant (`assert!(matches!(error, ErrorType::Variant { .. }))`) and message content — don't just check `is_err()`. + +## Documentation + +- Add doc comments to public API elements that convey semantic meaning, valid values, and effects — don't restate type signatures. +- Document enum variant doc comments with behavioral semantics, not just labels. For numeric parameters, state whether it's an id, count, index, etc. +- Add doc comments to magic constants, thresholds, and non-obvious transformation functions — explain what the value represents and why it was chosen. +- Comment fallback/guard code paths with when they trigger and why they exist. +- Ensure doc comments match actual semantics — distinguish mutates-in-place (`&mut self`) from returns-new-value. +- Use explicit forward-looking language (`TODO`, `FIXME`) in comments to distinguish current behavior from planned changes. +- Document the semantic meaning of both present and absent states for `Option` fields. +- Use precise domain terminology — avoid ambiguous abbreviations (e.g., "FIXED" vs "fixed-width") or incorrect terms (e.g., "fields" when meaning "fragments"). + +## lance-encoding + +Performance-critical encoding/decoding paths have additional requirements: + +- Hoist loop-invariant conditionals out of hot loops — branch once outside, then use separate loop bodies or monomorphized variants. +- Pre-allocate single contiguous buffers. Default to `buf.resize(len, 0)` for safe initialization; reserve `Vec::with_capacity` + `unsafe { set_len() }` for measured hot paths only, with a `// SAFETY:` comment explaining why the buffer will be fully initialized before read (e.g., immediately followed by `read_exact`). +- Use `spawn_cpu()` only at the async-to-CPU boundary (e.g., FSST, decompression, batch materialization) — never nest redundant `spawn_cpu()` calls. +- Use `expect_next()` and similar utility methods instead of inlining `None`-checks with error returns. diff --git a/lance-artifact/rust/CLAUDE.md b/lance-artifact/rust/CLAUDE.md new file mode 120000 index 000000000..47dc3e3d8 --- /dev/null +++ b/lance-artifact/rust/CLAUDE.md @@ -0,0 +1 @@ +AGENTS.md \ No newline at end of file diff --git a/lance-artifact/rust/CONTRIBUTING.md b/lance-artifact/rust/CONTRIBUTING.md new file mode 100644 index 000000000..31898b3be --- /dev/null +++ b/lance-artifact/rust/CONTRIBUTING.md @@ -0,0 +1,41 @@ +# Contributing to Rust + +To format and lint Rust code: + +```bash +cargo fmt --all +cargo clippy --all-features --tests --benches +``` + +## Core Format + +The core format is implemented in Rust under the `rust` directory. Once you've setup Rust you can build the core format with: + +```bash +cargo build +``` + +This builds the debug build. For the optimized release build: + +```bash +cargo build -r +``` + +To run the Rust unit tests: + +```bash +cargo test +``` + +If you're working on a performance related feature, benchmarks can be run via: + +```bash +cargo bench +``` + +If you want detailed logging and full backtraces, set the following environment variables. +More details can be found [here](../docs/src/guide/performance.md#logging). + +```bash +LANCE_LOG=info RUST_BACKTRACE=FULL +``` diff --git a/lance-artifact/rust/README.md b/lance-artifact/rust/README.md new file mode 100644 index 000000000..11ea8cb4e --- /dev/null +++ b/lance-artifact/rust/README.md @@ -0,0 +1,2 @@ +# Lance Rust Workspace +Where core rust code lance lives diff --git a/lance-artifact/rust/arrow-scalar/Cargo.toml b/lance-artifact/rust/arrow-scalar/Cargo.toml new file mode 100644 index 000000000..b5e968faf --- /dev/null +++ b/lance-artifact/rust/arrow-scalar/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "lance-arrow-scalar" +version = "58.0.0" +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +description = "Arrow scalar type with Ord, Hash, and Eq support" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true +readme = "README.md" + +[dependencies] +# Note: this is a core crate and we should aim to keep this dependency list +# as minimal as possible. +arrow-array = { workspace = true } +arrow-buffer = { workspace = true } +arrow-cast = { workspace = true } +arrow-data = { workspace = true } +arrow-row = { workspace = true } +arrow-schema = { workspace = true } +half = { workspace = true } + +[dev-dependencies] +arrow-ord = { workspace = true } +proptest = { workspace = true } +rstest = { workspace = true } + +[lints] +workspace = true diff --git a/lance-artifact/rust/arrow-scalar/README.md b/lance-artifact/rust/arrow-scalar/README.md new file mode 100644 index 000000000..7173a1162 --- /dev/null +++ b/lance-artifact/rust/arrow-scalar/README.md @@ -0,0 +1,57 @@ +# lance-arrow-scalar + +A scalar type backed by Apache Arrow arrays with `Ord`, `Hash`, and `Eq` support. + +## Overview + +`ArrowScalar` wraps a single-element Arrow array and provides comparison and hashing operations by leveraging Apache Arrow's `OwnedRow` representation. This ensures: + +- **Correct total ordering** for all Arrow types +- **Proper NaN handling** for floating-point values +- **Consistent null ordering** +- **O(1) comparisons** via cached row bytes + +## Features + +- `Eq`, `Ord`, and `Hash` traits for Arrow scalar values +- Support for all Arrow data types +- Serde serialization/deserialization support +- Zero-copy conversion from Arrow arrays + +## Usage + +Add to your `Cargo.toml`: + +```toml +[dependencies] +lance-arrow-scalar = "57.0.0" +``` + +Then use in your code: + +```rust +use lance_arrow_scalar::ArrowScalar; + +// Create from primitive types +let a = ArrowScalar::from(42i32); +let b = ArrowScalar::from(100i32); +assert!(a < b); + +// Create from strings +let s1 = ArrowScalar::from("hello"); +let s2 = ArrowScalar::from("world"); +assert!(s1 < s2); + +// Use in collections +use std::collections::HashMap; +let mut map = HashMap::new(); +map.insert(ArrowScalar::from("key"), ArrowScalar::from(123)); +``` + +## Cross-Type Comparison + +Comparing scalars of different data types produces an arbitrary but consistent ordering based on the underlying row bytes. This allows scalars to be used as keys in sorted collections regardless of type, though the ordering across types is not semantically meaningful. + +## Implementation Details + +Comparisons and hashing are delegated to [`arrow_row::OwnedRow`], which provides efficient byte-level operations. The row representation is cached at construction time, making all comparison and hashing operations O(1). diff --git a/lance-artifact/rust/arrow-scalar/src/convert.rs b/lance-artifact/rust/arrow-scalar/src/convert.rs new file mode 100644 index 000000000..de783a3a6 --- /dev/null +++ b/lance-artifact/rust/arrow-scalar/src/convert.rs @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_array::*; +use half::f16; + +use crate::ArrowScalar; + +macro_rules! impl_from_primitive { + ($native_ty:ty, $array_ty:ty) => { + impl From<$native_ty> for ArrowScalar { + fn from(value: $native_ty) -> Self { + let array: ArrayRef = Arc::new(<$array_ty>::from(vec![value])); + Self::try_from_array(array).expect("single-element primitive array is always valid") + } + } + }; +} + +impl_from_primitive!(i8, Int8Array); +impl_from_primitive!(i16, Int16Array); +impl_from_primitive!(i32, Int32Array); +impl_from_primitive!(i64, Int64Array); +impl_from_primitive!(u8, UInt8Array); +impl_from_primitive!(u16, UInt16Array); +impl_from_primitive!(u32, UInt32Array); +impl_from_primitive!(u64, UInt64Array); +impl_from_primitive!(f32, Float32Array); +impl_from_primitive!(f64, Float64Array); + +impl From for ArrowScalar { + fn from(value: bool) -> Self { + let array: ArrayRef = Arc::new(BooleanArray::from(vec![value])); + Self::try_from_array(array).expect("single-element boolean array is always valid") + } +} + +impl From for ArrowScalar { + fn from(value: f16) -> Self { + let array: ArrayRef = Arc::new(Float16Array::from(vec![value])); + Self::try_from_array(array).expect("single-element f16 array is always valid") + } +} + +impl From<&str> for ArrowScalar { + fn from(value: &str) -> Self { + let array: ArrayRef = Arc::new(StringArray::from(vec![value])); + Self::try_from_array(array).expect("single-element string array is always valid") + } +} + +impl From for ArrowScalar { + fn from(value: String) -> Self { + Self::from(value.as_str()) + } +} + +impl From<&[u8]> for ArrowScalar { + fn from(value: &[u8]) -> Self { + let array: ArrayRef = Arc::new(BinaryArray::from_vec(vec![value])); + Self::try_from_array(array).expect("single-element binary array is always valid") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_from_primitives() { + let s = ArrowScalar::from(42i32); + assert!(!s.is_null()); + assert_eq!(format!("{s}"), "42"); + + let s = ArrowScalar::from(1.5f64); + assert!(!s.is_null()); + + let s = ArrowScalar::from(true); + assert_eq!(format!("{s}"), "true"); + } + + #[test] + fn test_from_string_types() { + let s = ArrowScalar::from("hello"); + assert_eq!(format!("{s}"), "hello"); + + let s = ArrowScalar::from(String::from("world")); + assert_eq!(format!("{s}"), "world"); + } + + #[test] + fn test_from_binary() { + let bytes: &[u8] = &[0xDE, 0xAD]; + let s = ArrowScalar::from(bytes); + assert!(!s.is_null()); + } + + #[test] + fn test_from_f16() { + let s = ArrowScalar::from(f16::from_f32(1.5)); + assert!(!s.is_null()); + } +} diff --git a/lance-artifact/rust/arrow-scalar/src/lib.rs b/lance-artifact/rust/arrow-scalar/src/lib.rs new file mode 100644 index 000000000..70c468ebb --- /dev/null +++ b/lance-artifact/rust/arrow-scalar/src/lib.rs @@ -0,0 +1,621 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! A scalar type backed by a single-element Arrow array with [`Ord`], [`Hash`], +//! and [`Eq`] support. +//! +//! Comparisons and hashing are delegated to [`arrow_row::OwnedRow`], which +//! provides a correct total ordering for all Arrow types (including proper NaN +//! handling for floats and null ordering). + +mod convert; +pub mod serde; + +use std::cmp::Ordering; +use std::fmt; +use std::hash::{Hash, Hasher}; +use std::sync::Arc; + +use arrow_array::cast::AsArray; +use arrow_array::types::{Float16Type, Float32Type, Float64Type}; +use arrow_array::{ArrayRef, make_array, new_null_array}; +use arrow_cast::display::ArrayFormatter; +use arrow_data::transform::MutableArrayData; +use arrow_row::{OwnedRow, RowConverter, SortField}; +use arrow_schema::{ArrowError, DataType}; + +type Result = std::result::Result; + +/// A scalar value backed by a length-1 Arrow array. +/// +/// `ArrowScalar` provides [`Eq`], [`Ord`], and [`Hash`] by caching an +/// [`OwnedRow`] at construction time. This means comparisons and hashing are +/// O(1) row-byte operations rather than per-type dispatch. +/// +/// # Cross-type comparison +/// +/// Comparing scalars of different data types produces an arbitrary but +/// consistent ordering based on the underlying row bytes. This is intentional +/// — it allows scalars to be used as keys in sorted collections regardless of +/// type, but the ordering across types is not semantically meaningful. +/// +/// # Examples +/// +/// ``` +/// use lance_arrow_scalar::ArrowScalar; +/// +/// let a = ArrowScalar::from(1i32); +/// let b = ArrowScalar::from(2i32); +/// assert!(a < b); +/// +/// let c = ArrowScalar::from("hello"); +/// assert_eq!(c, ArrowScalar::from("hello")); +/// ``` +pub struct ArrowScalar { + array: ArrayRef, + row: OwnedRow, +} + +impl ArrowScalar { + /// Create a scalar by extracting the element at `offset` from `array`. + pub fn try_new(array: &ArrayRef, offset: usize) -> Result { + if offset >= array.len() { + return Err(ArrowError::InvalidArgumentError( + "Scalar index out of bounds".to_string(), + )); + } + + let data = array.to_data(); + let mut mutable = MutableArrayData::new(vec![&data], true, 1); + mutable.extend(0, offset, offset + 1); + let single = make_array(mutable.freeze()); + Self::try_from_array(single) + } + + /// Create a scalar from a length-1 array. + pub fn try_from_array(array: ArrayRef) -> Result { + if array.len() != 1 { + return Err(ArrowError::InvalidArgumentError(format!( + "ArrowScalar requires a length-1 array, got length {}", + array.len() + ))); + } + + let row = Self::compute_row(&array)?; + Ok(Self { array, row }) + } + + /// Create a null scalar of the given data type. + pub fn new_null(data_type: &DataType) -> Result { + Self::try_from_array(new_null_array(data_type, 1)) + } + + fn compute_row(array: &ArrayRef) -> Result { + let sort_field = SortField::new(array.data_type().clone()); + let converter = RowConverter::new(vec![sort_field])?; + let rows = converter.convert_columns(&[Arc::clone(array)])?; + Ok(rows.row(0).owned()) + } + + /// Returns a reference to the underlying length-1 array. + pub fn as_array(&self) -> &ArrayRef { + &self.array + } + + /// Returns the data type of this scalar. + pub fn data_type(&self) -> &DataType { + self.array.data_type() + } + + /// Returns `true` if this scalar is null. + pub fn is_null(&self) -> bool { + self.array.null_count() == 1 + } + + /// Returns `true` if this scalar is a non-null floating-point NaN. + /// + /// ``` + /// use lance_arrow_scalar::ArrowScalar; + /// + /// assert!(ArrowScalar::from(f32::NAN).is_nan()); + /// assert!(!ArrowScalar::from(1.0f32).is_nan()); + /// assert!(!ArrowScalar::from(1i32).is_nan()); + /// ``` + pub fn is_nan(&self) -> bool { + if self.is_null() { + return false; + } + + match self.data_type() { + DataType::Float16 => self.array.as_primitive::().value(0).is_nan(), + DataType::Float32 => self.array.as_primitive::().value(0).is_nan(), + DataType::Float64 => self.array.as_primitive::().value(0).is_nan(), + _ => false, + } + } +} + +impl PartialEq for ArrowScalar { + fn eq(&self, other: &Self) -> bool { + self.row == other.row + } +} + +impl Eq for ArrowScalar {} + +impl PartialOrd for ArrowScalar { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for ArrowScalar { + fn cmp(&self, other: &Self) -> Ordering { + self.row.cmp(&other.row) + } +} + +impl Hash for ArrowScalar { + fn hash(&self, state: &mut H) { + self.row.hash(state); + } +} + +impl fmt::Display for ArrowScalar { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_null() { + return write!(f, "null"); + } + let formatter = + ArrayFormatter::try_new(&self.array, &Default::default()).map_err(|_| fmt::Error)?; + write!(f, "{}", formatter.value(0)) + } +} + +impl fmt::Debug for ArrowScalar { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ArrowScalar({}: {})", self.data_type(), self) + } +} + +impl Clone for ArrowScalar { + fn clone(&self) -> Self { + Self { + array: Arc::clone(&self.array), + row: self.row.clone(), + } + } +} + +#[cfg(test)] +mod tests { + use std::collections::{BTreeSet, HashSet}; + use std::sync::Arc; + + use arrow_array::*; + use rstest::rstest; + + use super::*; + + #[test] + fn test_try_new_extracts_element() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![10, 20, 30])); + let s = ArrowScalar::try_new(&array, 1).unwrap(); + assert_eq!(format!("{s}"), "20"); + } + + #[test] + fn test_try_new_out_of_bounds() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![1])); + assert!(ArrowScalar::try_new(&array, 5).is_err()); + } + + #[test] + fn test_try_from_array_wrong_length() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2])); + assert!(ArrowScalar::try_from_array(array).is_err()); + } + + #[test] + fn test_equality() { + let a = ArrowScalar::from(42i32); + let b = ArrowScalar::from(42i32); + let c = ArrowScalar::from(99i32); + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn test_ordering() { + let a = ArrowScalar::from(1i32); + let b = ArrowScalar::from(2i32); + let c = ArrowScalar::from(3i32); + assert!(a < b); + assert!(b < c); + assert_eq!(a.cmp(&a), Ordering::Equal); + } + + #[test] + fn test_hash_consistent_with_eq() { + use std::hash::DefaultHasher; + + let a = ArrowScalar::from(42i32); + let b = ArrowScalar::from(42i32); + let hash_a = { + let mut h = DefaultHasher::new(); + a.hash(&mut h); + h.finish() + }; + let hash_b = { + let mut h = DefaultHasher::new(); + b.hash(&mut h); + h.finish() + }; + assert_eq!(hash_a, hash_b); + } + + #[test] + fn test_in_hashset() { + let mut set = HashSet::new(); + set.insert(ArrowScalar::from(1i32)); + set.insert(ArrowScalar::from(2i32)); + set.insert(ArrowScalar::from(1i32)); + assert_eq!(set.len(), 2); + } + + #[test] + fn test_in_btreeset() { + let mut set = BTreeSet::new(); + set.insert(ArrowScalar::from(3i32)); + set.insert(ArrowScalar::from(1i32)); + set.insert(ArrowScalar::from(2i32)); + let values: Vec<_> = set.iter().map(|s| format!("{s}")).collect(); + assert_eq!(values, vec!["1", "2", "3"]); + } + + #[test] + fn test_null_scalar() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![None])); + let s = ArrowScalar::try_from_array(array).unwrap(); + assert!(s.is_null()); + assert_eq!(format!("{s}"), "null"); + } + + #[test] + fn test_null_sorts_first() { + let null_scalar = { + let array: ArrayRef = Arc::new(Int32Array::from(vec![None])); + ArrowScalar::try_from_array(array).unwrap() + }; + let value_scalar = ArrowScalar::from(0i32); + assert!(null_scalar < value_scalar); + } + + #[rstest] + #[case::float_nan( + ArrowScalar::from(f64::NAN), + ArrowScalar::from(f64::INFINITY), + Ordering::Greater + )] + #[case::float_normal(ArrowScalar::from(1.0f64), ArrowScalar::from(2.0f64), Ordering::Less)] + fn test_float_ordering( + #[case] a: ArrowScalar, + #[case] b: ArrowScalar, + #[case] expected: Ordering, + ) { + assert_eq!(a.cmp(&b), expected); + } + + #[rstest] + #[case::float16_nan(ArrowScalar::from(half::f16::NAN), true)] + #[case::float32_nan(ArrowScalar::from(f32::NAN), true)] + #[case::float64_nan(ArrowScalar::from(f64::NAN), true)] + #[case::float64_finite(ArrowScalar::from(1.0f64), false)] + #[case::int32(ArrowScalar::from(1i32), false)] + fn test_is_nan(#[case] scalar: ArrowScalar, #[case] expected: bool) { + assert_eq!(scalar.is_nan(), expected); + } + + #[test] + fn test_null_is_not_nan() { + let array: ArrayRef = Arc::new(Float64Array::from(vec![None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert!(!scalar.is_nan()); + } + + #[test] + fn test_display_string() { + let s = ArrowScalar::from("hello world"); + assert_eq!(format!("{s}"), "hello world"); + } + + #[test] + fn test_debug() { + let s = ArrowScalar::from(42i32); + let debug = format!("{s:?}"); + assert!(debug.contains("ArrowScalar")); + assert!(debug.contains("42")); + } + + #[test] + fn test_clone() { + let a = ArrowScalar::from(42i32); + let b = a.clone(); + assert_eq!(a, b); + } + + #[test] + fn test_data_type() { + let s = ArrowScalar::from(42i32); + assert_eq!(s.data_type(), &DataType::Int32); + } + + #[test] + fn test_boolean_roundtrip() { + let t = ArrowScalar::from(true); + let f = ArrowScalar::from(false); + assert_eq!(t.data_type(), &DataType::Boolean); + assert!(!t.is_null()); + assert_eq!(format!("{t}"), "true"); + assert_eq!(format!("{f}"), "false"); + + // Extract from multi-element array + let array: ArrayRef = Arc::new(BooleanArray::from(vec![true, false, true])); + let s = ArrowScalar::try_new(&array, 1).unwrap(); + assert_eq!(format!("{s}"), "false"); + assert_eq!(s.data_type(), &DataType::Boolean); + } + + #[test] + fn test_boolean_equality_and_ordering() { + let t1 = ArrowScalar::from(true); + let t2 = ArrowScalar::from(true); + let f1 = ArrowScalar::from(false); + assert_eq!(t1, t2); + assert_ne!(t1, f1); + // false < true in arrow row encoding + assert!(f1 < t1); + } + + #[test] + fn test_boolean_null() { + let array: ArrayRef = Arc::new(BooleanArray::from(vec![None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert!(scalar.is_null()); + assert_eq!(scalar.data_type(), &DataType::Boolean); + assert_eq!(format!("{scalar}"), "null"); + + // null sorts before false + let f = ArrowScalar::from(false); + assert!(scalar < f); + } + + #[test] + fn test_string_view_roundtrip() { + let array: ArrayRef = Arc::new(StringViewArray::from(vec![ + "hello world, this is a long string view", + ])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert_eq!(scalar.data_type(), &DataType::Utf8View); + assert!(!scalar.is_null()); + assert_eq!( + format!("{scalar}"), + "hello world, this is a long string view" + ); + + // Extract from multi-element array + let array: ArrayRef = Arc::new(StringViewArray::from(vec!["alpha", "beta", "gamma"])); + let s = ArrowScalar::try_new(&array, 1).unwrap(); + assert_eq!(format!("{s}"), "beta"); + assert_eq!(s.data_type(), &DataType::Utf8View); + } + + #[test] + fn test_binary_view_roundtrip() { + let values: Vec<&[u8]> = vec![b"\xDE\xAD\xBE\xEF"]; + let array: ArrayRef = Arc::new(BinaryViewArray::from(values)); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert_eq!(scalar.data_type(), &DataType::BinaryView); + assert!(!scalar.is_null()); + + // Extract from multi-element array + let values: Vec<&[u8]> = vec![b"aaa", b"bbb", b"ccc"]; + let array: ArrayRef = Arc::new(BinaryViewArray::from(values)); + let s = ArrowScalar::try_new(&array, 2).unwrap(); + assert_eq!(s.data_type(), &DataType::BinaryView); + } + + #[test] + fn test_string_view_equality_and_ordering() { + let mk = |s: &str| { + let array: ArrayRef = Arc::new(StringViewArray::from(vec![s])); + ArrowScalar::try_from_array(array).unwrap() + }; + let a = mk("apple"); + let b = mk("apple"); + let c = mk("banana"); + assert_eq!(a, b); + assert_ne!(a, c); + assert!(a < c); + } + + #[test] + fn test_binary_view_equality_and_ordering() { + let mk = |b: &[u8]| { + let values: Vec<&[u8]> = vec![b]; + let array: ArrayRef = Arc::new(BinaryViewArray::from(values)); + ArrowScalar::try_from_array(array).unwrap() + }; + let a = mk(b"\x01\x02"); + let b = mk(b"\x01\x02"); + let c = mk(b"\x01\x03"); + assert_eq!(a, b); + assert_ne!(a, c); + assert!(a < c); + } + + #[test] + fn test_string_view_in_collections() { + let mk = |s: &str| { + let array: ArrayRef = Arc::new(StringViewArray::from(vec![s])); + ArrowScalar::try_from_array(array).unwrap() + }; + + let mut hset = HashSet::new(); + hset.insert(mk("foo")); + hset.insert(mk("bar")); + hset.insert(mk("foo")); + assert_eq!(hset.len(), 2); + + let mut bset = BTreeSet::new(); + bset.insert(mk("cherry")); + bset.insert(mk("apple")); + bset.insert(mk("banana")); + let sorted: Vec<_> = bset.iter().map(|s| format!("{s}")).collect(); + assert_eq!(sorted, vec!["apple", "banana", "cherry"]); + } + + #[test] + fn test_string_view_null() { + let array: ArrayRef = Arc::new(StringViewArray::from(vec![Option::<&str>::None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert!(scalar.is_null()); + assert_eq!(scalar.data_type(), &DataType::Utf8View); + assert_eq!(format!("{scalar}"), "null"); + } + + #[test] + fn test_binary_view_null() { + let array: ArrayRef = Arc::new(BinaryViewArray::from(vec![Option::<&[u8]>::None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert!(scalar.is_null()); + assert_eq!(scalar.data_type(), &DataType::BinaryView); + } + + #[test] + fn test_cross_type_comparison_is_consistent() { + let int_scalar = ArrowScalar::from(42i32); + let str_scalar = ArrowScalar::from("hello"); + // The ordering is arbitrary but must be consistent + let ord1 = int_scalar.cmp(&str_scalar); + let ord2 = int_scalar.cmp(&str_scalar); + assert_eq!(ord1, ord2); + // And the reverse should be opposite + assert_eq!(str_scalar.cmp(&int_scalar), ord1.reverse()); + } +} + +#[cfg(test)] +mod prop_tests { + use std::sync::Arc; + + use arrow_array::*; + use arrow_ord::sort::sort; + use arrow_schema::SortOptions; + use proptest::prelude::*; + + use super::ArrowScalar; + + /// Generate an arbitrary Arrow array of a randomly chosen type, including + /// nulls. Covers primitives, booleans, string/binary types and their view + /// variants. + fn arbitrary_array() -> BoxedStrategy { + let len = 0..=100usize; + + prop_oneof![ + // --- integer types --- + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Int8Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Int16Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Int32Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Int64Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(UInt8Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(UInt16Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(UInt32Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(UInt64Array::from(v)) as ArrayRef), + // --- float types --- + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Float32Array::from(v)) as ArrayRef), + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(Float64Array::from(v)) as ArrayRef), + // --- boolean --- + proptest::collection::vec(proptest::option::of(any::()), len.clone()) + .prop_map(|v| Arc::new(BooleanArray::from(v)) as ArrayRef), + // --- string types --- + proptest::collection::vec(proptest::option::of(any::()), len.clone()).prop_map( + |v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(StringArray::from(refs)) as ArrayRef + } + ), + proptest::collection::vec(proptest::option::of(any::()), len.clone()).prop_map( + |v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(LargeStringArray::from(refs)) as ArrayRef + } + ), + proptest::collection::vec(proptest::option::of(any::()), len.clone()).prop_map( + |v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(StringViewArray::from(refs)) as ArrayRef + } + ), + // --- binary types --- + proptest::collection::vec( + proptest::option::of(proptest::collection::vec(any::(), 0..50)), + len.clone(), + ) + .prop_map(|v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(BinaryArray::from(refs)) as ArrayRef + }), + proptest::collection::vec( + proptest::option::of(proptest::collection::vec(any::(), 0..50)), + len.clone(), + ) + .prop_map(|v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(LargeBinaryArray::from(refs)) as ArrayRef + }), + proptest::collection::vec( + proptest::option::of(proptest::collection::vec(any::(), 0..50)), + len, + ) + .prop_map(|v| { + let refs: Vec> = v.iter().map(|o| o.as_deref()).collect(); + Arc::new(BinaryViewArray::from(refs)) as ArrayRef + }), + ] + .boxed() + } + + proptest::proptest! { + #[test] + fn sorted_array_produces_sorted_scalars(array in arbitrary_array()) { + let sorted = sort( + &array, + Some(SortOptions { descending: false, nulls_first: true }), + ) + .unwrap(); + + let scalars: Vec = (0..sorted.len()) + .map(|i| ArrowScalar::try_new(&sorted, i).unwrap()) + .collect(); + + for i in 1..scalars.len() { + prop_assert!( + scalars[i - 1] <= scalars[i], + "scalar[{}] ({:?}) should be <= scalar[{}] ({:?})", + i - 1, scalars[i - 1], i, scalars[i], + ); + } + } + } +} diff --git a/lance-artifact/rust/arrow-scalar/src/serde.rs b/lance-artifact/rust/arrow-scalar/src/serde.rs new file mode 100644 index 000000000..7a458d138 --- /dev/null +++ b/lance-artifact/rust/arrow-scalar/src/serde.rs @@ -0,0 +1,558 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Binary serialization for [`ArrowScalar`]. +//! +//! Default format (with type prefix): +//! ```text +//! | varint: format_string_len | raw: format_string_bytes | +//! | varint: null_flag (0 = non-null, 1 = null) | +//! | varint: num_buffers | (only if non-null) +//! | varint: buffer_0_len | ... | varint: buffer_{n-1}_len | (only if non-null) +//! | raw: buffer_0 bytes | ... | raw: buffer_{n-1} bytes | (only if non-null) +//! ``` +//! +//! The format string uses the +//! [Arrow C Data Interface](https://arrow.apache.org/docs/format/CDataInterface.html#data-type-description-format-strings) +//! encoding. Use [`EncodeOptions`] / [`DecodeOptions`] to omit the type prefix +//! when the caller already knows the data type. + +use std::borrow::Cow; +use std::sync::Arc; + +use arrow_array::make_array; +use arrow_buffer::Buffer; +use arrow_data::ArrayDataBuilder; +use arrow_schema::{ArrowError, DataType, IntervalUnit, TimeUnit}; + +use crate::ArrowScalar; + +type Result = std::result::Result; + +/// Options for [`ArrowScalar::encode_with_options`]. +pub struct EncodeOptions { + /// When `true` (the default), the Arrow C Data Interface format string + /// for the scalar's data type is prepended as a varint-length-prefixed + /// UTF-8 string. Set to `false` to omit the type prefix (the caller + /// must then supply the `DataType` at decode time). + pub include_data_type: bool, +} + +impl Default for EncodeOptions { + fn default() -> Self { + Self { + include_data_type: true, + } + } +} + +/// Options for [`ArrowScalar::decode_with_options`]. +#[derive(Default)] +pub struct DecodeOptions<'a> { + /// When `Some`, the data type is taken from this value and the encoded + /// bytes are assumed to contain no type prefix. When `None` (the + /// default), the data type is read from the encoded format-string prefix. + pub data_type: Option<&'a DataType>, +} + +/// Encode a `u64` as a variable-length integer (LEB128). +/// +/// Values below 128 use a single byte; the maximum encoding is 10 bytes. +pub fn encode_varint(out: &mut Vec, mut value: u64) { + loop { + let byte = (value & 0x7F) as u8; + value >>= 7; + if value == 0 { + out.push(byte); + return; + } + out.push(byte | 0x80); + } +} + +/// Decode a variable-length integer (LEB128) from `buf` at the given `offset`. +/// +/// On success, `offset` is advanced past the consumed bytes. +pub fn decode_varint(buf: &[u8], offset: &mut usize) -> Result { + let mut result: u64 = 0; + let mut shift = 0u32; + loop { + if *offset >= buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid varint: unexpected EOF".to_string(), + )); + } + let byte = buf[*offset]; + *offset += 1; + + result |= u64::from(byte & 0x7F) << shift; + if byte & 0x80 == 0 { + return Ok(result); + } + shift += 7; + if shift >= 64 { + return Err(ArrowError::InvalidArgumentError( + "Invalid varint: too many bytes".to_string(), + )); + } + } +} + +/// Convert a [`DataType`] to its Arrow C Data Interface format string. +/// +/// Only non-nested types are supported (nested types are already rejected by +/// [`ArrowScalar::encode`]). +fn data_type_to_format_string(dtype: &DataType) -> Result> { + match dtype { + DataType::Null => Ok("n".into()), + DataType::Boolean => Ok("b".into()), + DataType::Int8 => Ok("c".into()), + DataType::UInt8 => Ok("C".into()), + DataType::Int16 => Ok("s".into()), + DataType::UInt16 => Ok("S".into()), + DataType::Int32 => Ok("i".into()), + DataType::UInt32 => Ok("I".into()), + DataType::Int64 => Ok("l".into()), + DataType::UInt64 => Ok("L".into()), + DataType::Float16 => Ok("e".into()), + DataType::Float32 => Ok("f".into()), + DataType::Float64 => Ok("g".into()), + DataType::Binary => Ok("z".into()), + DataType::LargeBinary => Ok("Z".into()), + DataType::Utf8 => Ok("u".into()), + DataType::LargeUtf8 => Ok("U".into()), + DataType::BinaryView => Ok("vz".into()), + DataType::Utf8View => Ok("vu".into()), + DataType::FixedSizeBinary(n) => Ok(Cow::Owned(format!("w:{n}"))), + DataType::Decimal32(p, s) => Ok(Cow::Owned(format!("d:{p},{s},32"))), + DataType::Decimal64(p, s) => Ok(Cow::Owned(format!("d:{p},{s},64"))), + DataType::Decimal128(p, s) => Ok(Cow::Owned(format!("d:{p},{s}"))), + DataType::Decimal256(p, s) => Ok(Cow::Owned(format!("d:{p},{s},256"))), + DataType::Date32 => Ok("tdD".into()), + DataType::Date64 => Ok("tdm".into()), + DataType::Time32(TimeUnit::Second) => Ok("tts".into()), + DataType::Time32(TimeUnit::Millisecond) => Ok("ttm".into()), + DataType::Time64(TimeUnit::Microsecond) => Ok("ttu".into()), + DataType::Time64(TimeUnit::Nanosecond) => Ok("ttn".into()), + DataType::Timestamp(TimeUnit::Second, None) => Ok("tss:".into()), + DataType::Timestamp(TimeUnit::Millisecond, None) => Ok("tsm:".into()), + DataType::Timestamp(TimeUnit::Microsecond, None) => Ok("tsu:".into()), + DataType::Timestamp(TimeUnit::Nanosecond, None) => Ok("tsn:".into()), + DataType::Timestamp(TimeUnit::Second, Some(tz)) => Ok(Cow::Owned(format!("tss:{tz}"))), + DataType::Timestamp(TimeUnit::Millisecond, Some(tz)) => Ok(Cow::Owned(format!("tsm:{tz}"))), + DataType::Timestamp(TimeUnit::Microsecond, Some(tz)) => Ok(Cow::Owned(format!("tsu:{tz}"))), + DataType::Timestamp(TimeUnit::Nanosecond, Some(tz)) => Ok(Cow::Owned(format!("tsn:{tz}"))), + DataType::Duration(TimeUnit::Second) => Ok("tDs".into()), + DataType::Duration(TimeUnit::Millisecond) => Ok("tDm".into()), + DataType::Duration(TimeUnit::Microsecond) => Ok("tDu".into()), + DataType::Duration(TimeUnit::Nanosecond) => Ok("tDn".into()), + DataType::Interval(IntervalUnit::YearMonth) => Ok("tiM".into()), + DataType::Interval(IntervalUnit::DayTime) => Ok("tiD".into()), + DataType::Interval(IntervalUnit::MonthDayNano) => Ok("tin".into()), + other => Err(ArrowError::InvalidArgumentError(format!( + "Cannot encode data type as format string: {other:?}" + ))), + } +} + +/// Parse an Arrow C Data Interface format string back to a [`DataType`]. +/// +/// Only non-nested types are supported. +fn format_string_to_data_type(fmt: &str) -> Result { + match fmt { + "n" => Ok(DataType::Null), + "b" => Ok(DataType::Boolean), + "c" => Ok(DataType::Int8), + "C" => Ok(DataType::UInt8), + "s" => Ok(DataType::Int16), + "S" => Ok(DataType::UInt16), + "i" => Ok(DataType::Int32), + "I" => Ok(DataType::UInt32), + "l" => Ok(DataType::Int64), + "L" => Ok(DataType::UInt64), + "e" => Ok(DataType::Float16), + "f" => Ok(DataType::Float32), + "g" => Ok(DataType::Float64), + "z" => Ok(DataType::Binary), + "Z" => Ok(DataType::LargeBinary), + "u" => Ok(DataType::Utf8), + "U" => Ok(DataType::LargeUtf8), + "vz" => Ok(DataType::BinaryView), + "vu" => Ok(DataType::Utf8View), + "tdD" => Ok(DataType::Date32), + "tdm" => Ok(DataType::Date64), + "tts" => Ok(DataType::Time32(TimeUnit::Second)), + "ttm" => Ok(DataType::Time32(TimeUnit::Millisecond)), + "ttu" => Ok(DataType::Time64(TimeUnit::Microsecond)), + "ttn" => Ok(DataType::Time64(TimeUnit::Nanosecond)), + "tDs" => Ok(DataType::Duration(TimeUnit::Second)), + "tDm" => Ok(DataType::Duration(TimeUnit::Millisecond)), + "tDu" => Ok(DataType::Duration(TimeUnit::Microsecond)), + "tDn" => Ok(DataType::Duration(TimeUnit::Nanosecond)), + "tiM" => Ok(DataType::Interval(IntervalUnit::YearMonth)), + "tiD" => Ok(DataType::Interval(IntervalUnit::DayTime)), + "tin" => Ok(DataType::Interval(IntervalUnit::MonthDayNano)), + other => { + let parts: Vec<&str> = other.splitn(2, ':').collect(); + match parts.as_slice() { + ["w", num_bytes] => { + let n = num_bytes.parse::().map_err(|_| { + ArrowError::InvalidArgumentError( + "FixedSizeBinary requires an integer byte count".to_string(), + ) + })?; + Ok(DataType::FixedSizeBinary(n)) + } + ["d", extra] => { + let dec_parts: Vec<&str> = extra.splitn(3, ',').collect(); + match dec_parts.as_slice() { + [precision, scale] => { + let p = precision.parse::().map_err(|_| { + ArrowError::InvalidArgumentError( + "Decimal requires an integer precision".to_string(), + ) + })?; + let s = scale.parse::().map_err(|_| { + ArrowError::InvalidArgumentError( + "Decimal requires an integer scale".to_string(), + ) + })?; + Ok(DataType::Decimal128(p, s)) + } + [precision, scale, bits] => { + let p = precision.parse::().map_err(|_| { + ArrowError::InvalidArgumentError( + "Decimal requires an integer precision".to_string(), + ) + })?; + let s = scale.parse::().map_err(|_| { + ArrowError::InvalidArgumentError( + "Decimal requires an integer scale".to_string(), + ) + })?; + match *bits { + "32" => Ok(DataType::Decimal32(p, s)), + "64" => Ok(DataType::Decimal64(p, s)), + "128" => Ok(DataType::Decimal128(p, s)), + "256" => Ok(DataType::Decimal256(p, s)), + _ => Err(ArrowError::InvalidArgumentError(format!( + "Unsupported decimal bit width: {bits}" + ))), + } + } + _ => Err(ArrowError::InvalidArgumentError(format!( + "Invalid decimal format string: d:{extra}" + ))), + } + } + ["tss", ""] => Ok(DataType::Timestamp(TimeUnit::Second, None)), + ["tsm", ""] => Ok(DataType::Timestamp(TimeUnit::Millisecond, None)), + ["tsu", ""] => Ok(DataType::Timestamp(TimeUnit::Microsecond, None)), + ["tsn", ""] => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)), + ["tss", tz] => Ok(DataType::Timestamp(TimeUnit::Second, Some(Arc::from(*tz)))), + ["tsm", tz] => Ok(DataType::Timestamp( + TimeUnit::Millisecond, + Some(Arc::from(*tz)), + )), + ["tsu", tz] => Ok(DataType::Timestamp( + TimeUnit::Microsecond, + Some(Arc::from(*tz)), + )), + ["tsn", tz] => Ok(DataType::Timestamp( + TimeUnit::Nanosecond, + Some(Arc::from(*tz)), + )), + _ => Err(ArrowError::InvalidArgumentError(format!( + "Unsupported format string: {other:?}" + ))), + } + } + } +} + +impl ArrowScalar { + /// Serialize this scalar to a self-describing binary representation. + /// + /// The data type is encoded as a format-string prefix so that + /// [`decode`](Self::decode) can reconstruct the scalar without external + /// type information. Use [`encode_with_options`](Self::encode_with_options) + /// to omit the prefix when the caller already knows the type. + /// + /// Only non-nested scalars are supported. Null scalars are encoded as a + /// null flag with no buffer data. + pub fn encode(&self) -> Result> { + self.encode_with_options(&EncodeOptions::default()) + } + + /// Serialize this scalar with the given [`EncodeOptions`]. + pub fn encode_with_options(&self, options: &EncodeOptions) -> Result> { + let array = self.as_array(); + let data = array.to_data(); + if !data.child_data().is_empty() { + return Err(ArrowError::InvalidArgumentError( + "Cannot encode nested scalar".to_string(), + )); + } + + let mut out = Vec::with_capacity(64); + + if options.include_data_type { + let fmt = data_type_to_format_string(array.data_type())?; + encode_varint(&mut out, fmt.len() as u64); + out.extend_from_slice(fmt.as_bytes()); + } + + if self.is_null() { + encode_varint(&mut out, 1); // null_flag = 1 + } else { + encode_varint(&mut out, 0); // null_flag = 0 + let buffers = data.buffers(); + encode_varint(&mut out, buffers.len() as u64); + for b in buffers { + encode_varint(&mut out, b.len() as u64); + } + for b in buffers { + out.extend_from_slice(b.as_slice()); + } + } + Ok(out) + } + + /// Deserialize a scalar from the self-describing binary representation + /// produced by [`encode`](Self::encode). + /// + /// The data type is read from the format-string prefix in the encoded + /// bytes. Use [`decode_with_options`](Self::decode_with_options) to supply + /// the type externally when the prefix was omitted at encode time. + pub fn decode(buf: &[u8]) -> Result { + Self::decode_with_options(buf, &DecodeOptions::default()) + } + + /// Deserialize a scalar with the given [`DecodeOptions`]. + pub fn decode_with_options(buf: &[u8], options: &DecodeOptions) -> Result { + let mut offset = 0; + + let data_type = match options.data_type { + Some(dt) => dt.clone(), + None => { + let fmt_len = decode_varint(buf, &mut offset)? as usize; + if offset + fmt_len > buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar buffer: unexpected EOF reading format string".to_string(), + )); + } + let fmt_str = std::str::from_utf8(&buf[offset..offset + fmt_len]).map_err(|e| { + ArrowError::InvalidArgumentError(format!( + "Invalid format string: not valid UTF-8: {e}" + )) + })?; + offset += fmt_len; + format_string_to_data_type(fmt_str)? + } + }; + + let null_flag = decode_varint(buf, &mut offset)?; + if null_flag == 1 { + if offset != buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar buffer: trailing bytes after null flag".to_string(), + )); + } + return Self::new_null(&data_type); + } + + let num_buffers = decode_varint(buf, &mut offset)? as usize; + + let mut buffer_lens = Vec::with_capacity(num_buffers); + for _ in 0..num_buffers { + buffer_lens.push(decode_varint(buf, &mut offset)? as usize); + } + + let mut buffers = Vec::with_capacity(num_buffers); + for len in &buffer_lens { + if offset + len > buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar buffer: unexpected EOF".to_string(), + )); + } + buffers.push(Buffer::from_vec(buf[offset..offset + len].to_vec())); + offset += len; + } + + if offset != buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar buffer: trailing bytes".to_string(), + )); + } + + let mut builder = ArrayDataBuilder::new(data_type).len(1).null_count(0); + for b in buffers { + builder = builder.add_buffer(b); + } + let array = make_array(builder.build()?); + Self::try_from_array(array) + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{ + ArrayRef, BinaryViewArray, Int32Array, StringArray, StringViewArray, + TimestampMicrosecondArray, + }; + use arrow_schema::DataType; + use rstest::rstest; + + use super::*; + use crate::ArrowScalar; + + #[test] + fn test_varint_roundtrip() { + for value in [0u64, 1, 127, 128, 16383, 16384, u64::MAX] { + let mut buf = Vec::new(); + encode_varint(&mut buf, value); + let mut offset = 0; + let decoded = decode_varint(&buf, &mut offset).unwrap(); + assert_eq!(decoded, value); + assert_eq!(offset, buf.len()); + } + } + + #[test] + fn test_varint_small_is_one_byte() { + let mut buf = Vec::new(); + encode_varint(&mut buf, 42); + assert_eq!(buf.len(), 1); + assert_eq!(buf[0], 42); + } + + #[rstest] + #[case::int32(Arc::new(Int32Array::from(vec![42])) as ArrayRef)] + #[case::string(Arc::new(StringArray::from(vec!["hello"])) as ArrayRef)] + #[case::string_view(Arc::new(StringViewArray::from(vec!["hello world, long string view"])) as ArrayRef)] + #[case::binary_view(Arc::new(BinaryViewArray::from(vec![b"\xDE\xAD\xBE\xEF".as_ref()])) as ArrayRef)] + fn test_encode_decode_roundtrip(#[case] array: ArrayRef) { + let scalar = ArrowScalar::try_from_array(array).unwrap(); + let encoded = scalar.encode().unwrap(); + let decoded = ArrowScalar::decode(&encoded).unwrap(); + assert_eq!(scalar, decoded); + assert_eq!(scalar.data_type(), decoded.data_type()); + } + + #[rstest] + #[case::int32(Arc::new(Int32Array::from(vec![42])) as ArrayRef, DataType::Int32)] + #[case::string(Arc::new(StringArray::from(vec!["hello"])) as ArrayRef, DataType::Utf8)] + #[case::string_view(Arc::new(StringViewArray::from(vec!["hello view"])) as ArrayRef, DataType::Utf8View)] + #[case::binary_view(Arc::new(BinaryViewArray::from(vec![b"\xCA\xFE".as_ref()])) as ArrayRef, DataType::BinaryView)] + fn test_encode_decode_without_type_prefix(#[case] array: ArrayRef, #[case] dt: DataType) { + let scalar = ArrowScalar::try_from_array(array).unwrap(); + let opts = EncodeOptions { + include_data_type: false, + }; + let encoded = scalar.encode_with_options(&opts).unwrap(); + let decode_opts = DecodeOptions { + data_type: Some(&dt), + }; + let decoded = ArrowScalar::decode_with_options(&encoded, &decode_opts).unwrap(); + assert_eq!(scalar, decoded); + } + + #[test] + fn test_null_encode_decode_roundtrip() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + assert!(scalar.is_null()); + let encoded = scalar.encode().unwrap(); + let decoded = ArrowScalar::decode(&encoded).unwrap(); + assert!(decoded.is_null()); + assert_eq!(decoded.data_type(), &DataType::Int32); + assert_eq!(scalar, decoded); + } + + #[test] + fn test_null_encode_decode_without_type_prefix() { + let array: ArrayRef = Arc::new(StringArray::from(vec![Option::<&str>::None])); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + let opts = EncodeOptions { + include_data_type: false, + }; + let encoded = scalar.encode_with_options(&opts).unwrap(); + let decode_opts = DecodeOptions { + data_type: Some(&DataType::Utf8), + }; + let decoded = ArrowScalar::decode_with_options(&encoded, &decode_opts).unwrap(); + assert!(decoded.is_null()); + assert_eq!(decoded.data_type(), &DataType::Utf8); + } + + #[test] + fn test_decode_trailing_bytes() { + let scalar = ArrowScalar::from(42i32); + let mut encoded = scalar.encode().unwrap(); + encoded.push(0xFF); + assert!(ArrowScalar::decode(&encoded).is_err()); + } + + #[test] + fn test_encoded_bytes_contain_format_prefix() { + let scalar = ArrowScalar::from(42i32); + let encoded = scalar.encode().unwrap(); + // First byte is varint length of format string "i" (length 1) + assert_eq!(encoded[0], 1); + // Second byte is the format string itself + assert_eq!(encoded[1], b'i'); + } + + #[rstest] + #[case::null(DataType::Null, "n")] + #[case::boolean(DataType::Boolean, "b")] + #[case::int8(DataType::Int8, "c")] + #[case::uint8(DataType::UInt8, "C")] + #[case::int16(DataType::Int16, "s")] + #[case::uint16(DataType::UInt16, "S")] + #[case::int32(DataType::Int32, "i")] + #[case::uint32(DataType::UInt32, "I")] + #[case::int64(DataType::Int64, "l")] + #[case::uint64(DataType::UInt64, "L")] + #[case::float16(DataType::Float16, "e")] + #[case::float32(DataType::Float32, "f")] + #[case::float64(DataType::Float64, "g")] + #[case::binary(DataType::Binary, "z")] + #[case::large_binary(DataType::LargeBinary, "Z")] + #[case::utf8(DataType::Utf8, "u")] + #[case::large_utf8(DataType::LargeUtf8, "U")] + #[case::binary_view(DataType::BinaryView, "vz")] + #[case::utf8_view(DataType::Utf8View, "vu")] + #[case::date32(DataType::Date32, "tdD")] + #[case::date64(DataType::Date64, "tdm")] + #[case::fixed_size_binary(DataType::FixedSizeBinary(16), "w:16")] + #[case::decimal128(DataType::Decimal128(10, 2), "d:10,2")] + #[case::decimal256(DataType::Decimal256(38, 10), "d:38,10,256")] + #[case::timestamp_us_utc( + DataType::Timestamp(TimeUnit::Microsecond, Some(Arc::from("UTC"))), + "tsu:UTC" + )] + #[case::timestamp_ns_none(DataType::Timestamp(TimeUnit::Nanosecond, None), "tsn:")] + #[case::duration_s(DataType::Duration(TimeUnit::Second), "tDs")] + #[case::interval_ym(DataType::Interval(IntervalUnit::YearMonth), "tiM")] + fn test_format_string_roundtrip(#[case] dt: DataType, #[case] expected_fmt: &str) { + let fmt = data_type_to_format_string(&dt).unwrap(); + assert_eq!(fmt.as_ref(), expected_fmt); + let roundtripped = format_string_to_data_type(&fmt).unwrap(); + assert_eq!(roundtripped, dt); + } + + #[test] + fn test_timestamp_with_tz_roundtrip() { + let array: ArrayRef = Arc::new( + TimestampMicrosecondArray::from(vec![1_000_000]).with_timezone("America/New_York"), + ); + let scalar = ArrowScalar::try_from_array(array).unwrap(); + let encoded = scalar.encode().unwrap(); + let decoded = ArrowScalar::decode(&encoded).unwrap(); + assert_eq!(scalar, decoded); + assert_eq!(scalar.data_type(), decoded.data_type()); + } +} diff --git a/lance-artifact/rust/arrow-stats/Cargo.toml b/lance-artifact/rust/arrow-stats/Cargo.toml new file mode 100644 index 000000000..dc5a0616e --- /dev/null +++ b/lance-artifact/rust/arrow-stats/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "lance-arrow-stats" +version = "58.0.0" +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +description = "Statistics accumulator for Arrow arrays (min, max, null_count, nan_count)" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true +readme = "README.md" + +[dependencies] +arrow-array = { workspace = true } +arrow-schema = { workspace = true } +lance-arrow-scalar = { workspace = true } + +[dev-dependencies] +arrow-select = { workspace = true } +proptest = { workspace = true } +rstest = { workspace = true } + +[lints] +workspace = true diff --git a/lance-artifact/rust/arrow-stats/README.md b/lance-artifact/rust/arrow-stats/README.md new file mode 100644 index 000000000..553f6de2e --- /dev/null +++ b/lance-artifact/rust/arrow-stats/README.md @@ -0,0 +1,62 @@ +# lance-arrow-stats + +Statistics accumulator for [Apache Arrow](https://arrow.apache.org/) arrays. + +Computes min, max, null count, NaN count, and buffer memory usage over one or +more batches of Arrow data. Designed for use in Lance's columnar storage layer +where page-level statistics drive predicate pushdown and query planning. + +## Usage + +```rust +use arrow_array::{Int32Array, ArrayRef}; +use lance_arrow_stats::StatisticsAccumulator; +use arrow_schema::DataType; +use std::sync::Arc; + +let mut acc = StatisticsAccumulator::new(&DataType::Int32); + +let batch: ArrayRef = Arc::new(Int32Array::from(vec![Some(3), None, Some(1), Some(4)])); +acc.update(&batch).unwrap(); + +let stats = acc.finish(); +assert_eq!(stats.null_count, 1); +``` + +## Tracked Statistics + +| Statistic | Description | +| --------------- | -------------------------------------------------------- | +| `min` | Minimum non-null, non-NaN value (`ArrowScalar`) | +| `max` | Maximum non-null, non-NaN value (`ArrowScalar`) | +| `null_count` | Total number of null values | +| `nan_count` | Total NaN values (float and float-list types only) | +| `item_nulls` | Null items inside list entries (list types only) | +| `buffer_memory` | Total Arrow buffer memory in bytes | + +## Supported Types + +- **Numeric** — Int8–Int64, UInt8–UInt64, Float16/32/64 +- **Temporal** — Date32/64, Time32/64, Timestamp, Duration +- **Boolean** +- **String** — Utf8, LargeUtf8 +- **Binary** — Binary, LargeBinary +- **List** — List, LargeList, FixedSizeList (computes stats over items) + +Dictionary, run-end encoded, and view types are accepted but min/max will be +`None`. + +## Merging + +Accumulators of the same data type can be merged, which is useful for combining +statistics computed in parallel across different pages or files: + +```rust +use lance_arrow_stats::StatisticsAccumulator; +use arrow_schema::DataType; + +let mut a = StatisticsAccumulator::new(&DataType::Float32); +let mut b = StatisticsAccumulator::new(&DataType::Float32); +// ... update each with different batches ... +a.merge(&b).unwrap(); +``` diff --git a/lance-artifact/rust/arrow-stats/proptest-regressions/lib.txt b/lance-artifact/rust/arrow-stats/proptest-regressions/lib.txt new file mode 100644 index 000000000..8794b465e --- /dev/null +++ b/lance-artifact/rust/arrow-stats/proptest-regressions/lib.txt @@ -0,0 +1,8 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 81b0445f36fa8f491c1fb3162f51b61c8be140d5b2a1e792c42b4bdb7f1b6a62 # shrinks to values = [0.0, -0.0] +cc 8651fce939497f33c6dafd842937d95965af97833bfbbd10df30d5ea00dbd07d # shrinks to values = [Some(0.0), Some(-0.0)] diff --git a/lance-artifact/rust/arrow-stats/src/lib.rs b/lance-artifact/rust/arrow-stats/src/lib.rs new file mode 100644 index 000000000..5c00a0157 --- /dev/null +++ b/lance-artifact/rust/arrow-stats/src/lib.rs @@ -0,0 +1,1294 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Statistics accumulator for streams of Arrow arrays. +//! +//! Tracks min, max, null_count, and optional nan_count across batches of arrays sharing +//! the same [`DataType`]. Uses [`ArrowScalar`] for extrema tracking. +//! +//! # Example +//! +//! ``` +//! use std::sync::Arc; +//! use arrow_array::{ArrayRef, Int32Array}; +//! use arrow_schema::DataType; +//! use lance_arrow_stats::StatisticsAccumulator; +//! +//! let mut acc = StatisticsAccumulator::new(&DataType::Int32); +//! let array: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), None, Some(3)])); +//! acc.update(&array).unwrap(); +//! +//! let stats = acc.finish(); +//! assert_eq!(stats.null_count, 1); +//! assert!(stats.nan_count.is_none()); +//! ``` +//! +//! # Data Type Support +//! +//! All basic types are supported. Every data type supports `null_count` and `buffer_memory`. +//! The `nan_count` field is `Some` only for floating-point types (including lists of floats) +//! and `None` for all other types. +//! +//! # List Types +//! +//! List types are supported. The `item_nulls` field will be set to the number of null items within list entries. +//! This will be `Some` only for list types. +//! +//! # String Types / Binary Types +//! +//! String & binary types are supported. Binary comparison will be used for string types to calculate the min and the +//! max. This works for ASCII but may be surprising for special characters. For example, "é" would sort after "z". +//! +//! In addition, string view and binary view types are supported. +//! +//! # Unsupported Types +//! +//! Special encodings (dictionary, run end encoded, view types) are not currently fully supported. The min and max will +//! be set to `None` for these types. The `nan_count` will also be `None` unless the underlying +//! type is a floating-point type. +//! +//! Structs are not currently supported. + +mod nan; + +use arrow_array::cast::AsArray; +use arrow_array::types::*; +use arrow_array::{Array, ArrayRef}; +use arrow_schema::{ArrowError, DataType}; +use lance_arrow_scalar::ArrowScalar; + +use nan::count_nans; + +type Result = std::result::Result; + +/// Returns true if the data type can contain NaN values (float primitives +/// or list types whose items are floats). +fn can_have_nan(data_type: &DataType) -> bool { + match data_type { + DataType::Float16 | DataType::Float32 | DataType::Float64 => true, + DataType::List(f) | DataType::LargeList(f) => can_have_nan(f.data_type()), + DataType::FixedSizeList(f, _) => can_have_nan(f.data_type()), + _ => false, + } +} + +/// Accumulated statistics for a stream of Arrow arrays of a single [`DataType`]. +#[derive(Debug, Clone)] +pub struct StatisticsAccumulator { + data_type: DataType, + min: Option, + max: Option, + null_count: u64, + /// Count of NaN values. `Some` only for floating-point types (or lists of floats). + nan_count: Option, + /// Number of null items within list entries. `Some` only for list types. + item_nulls: Option, + buffer_memory: u64, +} + +/// Snapshot of accumulated statistics. +#[derive(Debug, Clone)] +pub struct Statistics { + pub min: Option, + pub max: Option, + pub null_count: u64, + /// Count of NaN values. `None` for non-floating-point types. + pub nan_count: Option, + /// Number of null items within list entries. `None` for non-list types. + pub item_nulls: Option, + /// Total buffer memory in bytes across all arrays seen by this accumulator. + pub buffer_memory: u64, +} + +impl StatisticsAccumulator { + /// Create a new accumulator for arrays of the given data type. + pub fn new(data_type: &DataType) -> Self { + let item_nulls = match data_type { + DataType::List(_) | DataType::LargeList(_) | DataType::FixedSizeList(_, _) => Some(0), + _ => None, + }; + let nan_count = if can_have_nan(data_type) { + Some(0) + } else { + None + }; + Self { + data_type: data_type.clone(), + min: None, + max: None, + null_count: 0, + nan_count, + item_nulls, + buffer_memory: 0, + } + } + + /// Returns the data type this accumulator expects. + pub fn data_type(&self) -> &DataType { + &self.data_type + } + + /// Update with a new batch of values. + pub fn update(&mut self, array: &ArrayRef) -> Result<()> { + if array.data_type() != &self.data_type { + return Err(ArrowError::InvalidArgumentError(format!( + "Type mismatch: expected {:?}, got {:?}", + self.data_type, + array.data_type() + ))); + } + + if array.is_empty() { + return Ok(()); + } + + self.buffer_memory += array.get_buffer_memory_size() as u64; + self.null_count += array.null_count() as u64; + + match array.data_type() { + DataType::List(_) => { + let list = array.as_list::(); + self.update_items( + (0..list.len()) + .filter(|&i| !list.is_null(i)) + .map(|i| list.value(i)), + ) + } + DataType::LargeList(_) => { + let list = array.as_list::(); + self.update_items( + (0..list.len()) + .filter(|&i| !list.is_null(i)) + .map(|i| list.value(i)), + ) + } + DataType::FixedSizeList(_, _) => { + let list = array.as_fixed_size_list(); + self.update_items( + (0..list.len()) + .filter(|&i| !list.is_null(i)) + .map(|i| list.value(i)), + ) + } + _ => { + if let Some(ref mut nan_count) = self.nan_count { + *nan_count += count_nans(array); + } + let (batch_min, batch_max) = find_min_max(array)?; + self.update_min(batch_min); + self.update_max(batch_max); + Ok(()) + } + } + } + + /// Process items from list entries, updating min/max, nan_count, and item_nulls. + fn update_items(&mut self, items: impl Iterator) -> Result<()> { + for item_array in items { + self.update_item(&item_array)?; + } + Ok(()) + } + + /// Process a single item array. If it is itself a list type, recurse into + /// its non-null entries; otherwise treat it as a leaf and compute min/max. + fn update_item(&mut self, item_array: &ArrayRef) -> Result<()> { + if item_array.is_empty() { + return Ok(()); + } + if let Some(ref mut item_nulls) = self.item_nulls { + *item_nulls += item_array.null_count() as u64; + } + match item_array.data_type() { + DataType::List(_) => { + let list = item_array.as_list::(); + for i in 0..list.len() { + if !list.is_null(i) { + self.update_item(&list.value(i))?; + } + } + } + DataType::LargeList(_) => { + let list = item_array.as_list::(); + for i in 0..list.len() { + if !list.is_null(i) { + self.update_item(&list.value(i))?; + } + } + } + DataType::FixedSizeList(_, _) => { + let list = item_array.as_fixed_size_list(); + for i in 0..list.len() { + if !list.is_null(i) { + self.update_item(&list.value(i))?; + } + } + } + _ => { + if let Some(ref mut nan_count) = self.nan_count { + *nan_count += count_nans(item_array); + } + let (batch_min, batch_max) = find_min_max(item_array)?; + self.update_min(batch_min); + self.update_max(batch_max); + } + } + Ok(()) + } + + fn update_min(&mut self, batch_min: Option) { + if let Some(new_min) = batch_min { + self.min = Some(match self.min.take() { + Some(cur) if cur <= new_min => cur, + _ => new_min, + }); + } + } + + fn update_max(&mut self, batch_max: Option) { + if let Some(new_max) = batch_max { + self.max = Some(match self.max.take() { + Some(cur) if cur >= new_max => cur, + _ => new_max, + }); + } + } + + /// Merge another accumulator into this one. + pub fn merge(&mut self, other: &Self) -> Result<()> { + if self.data_type != other.data_type { + return Err(ArrowError::InvalidArgumentError(format!( + "Type mismatch: expected {:?}, got {:?}", + self.data_type, other.data_type + ))); + } + + self.null_count += other.null_count; + if let (Some(a), Some(b)) = (&mut self.nan_count, other.nan_count) { + *a += b; + } + self.buffer_memory += other.buffer_memory; + + if let (Some(a), Some(b)) = (&mut self.item_nulls, other.item_nulls) { + *a += b; + } + + if let Some(ref other_min) = other.min { + self.min = Some(match self.min.take() { + Some(cur) if cur <= *other_min => cur, + _ => other_min.clone(), + }); + } + + if let Some(ref other_max) = other.max { + self.max = Some(match self.max.take() { + Some(cur) if cur >= *other_max => cur, + _ => other_max.clone(), + }); + } + + Ok(()) + } + + /// Consume the accumulator and return a statistics snapshot. + pub fn finish(self) -> Statistics { + Statistics { + min: self.min, + max: self.max, + null_count: self.null_count, + nan_count: self.nan_count, + item_nulls: self.item_nulls, + buffer_memory: self.buffer_memory, + } + } + + /// Return a snapshot of the current statistics without consuming the accumulator. + pub fn statistics(&self) -> Statistics { + Statistics { + min: self.min.clone(), + max: self.max.clone(), + null_count: self.null_count, + nan_count: self.nan_count, + item_nulls: self.item_nulls, + buffer_memory: self.buffer_memory, + } + } + + /// Reset all statistics back to initial state. + pub fn reset(&mut self) { + self.min = None; + self.max = None; + self.null_count = 0; + if let Some(ref mut nan_count) = self.nan_count { + *nan_count = 0; + } + if let Some(ref mut item_nulls) = self.item_nulls { + *item_nulls = 0; + } + self.buffer_memory = 0; + } +} + +macro_rules! find_extrema_primitive { + ($array:expr, $arrow_type:ty) => {{ + let typed = $array.as_primitive::<$arrow_type>(); + let mut min_idx: Option = None; + let mut max_idx: Option = None; + let mut min_val = None; + let mut max_val = None; + for i in 0..typed.len() { + if typed.is_null(i) { + continue; + } + let v = typed.value(i); + if min_val.is_none() || v < *min_val.as_ref().unwrap() { + min_val = Some(v); + min_idx = Some(i); + } + if max_val.is_none() || v > *max_val.as_ref().unwrap() { + max_val = Some(v); + max_idx = Some(i); + } + } + (min_idx, max_idx) + }}; +} + +macro_rules! find_extrema_float { + ($array:expr, $arrow_type:ty) => {{ + let typed = $array.as_primitive::<$arrow_type>(); + let mut min_idx: Option = None; + let mut max_idx: Option = None; + let mut min_val = None; + let mut max_val = None; + for i in 0..typed.len() { + if typed.is_null(i) { + continue; + } + let v = typed.value(i); + if v.is_nan() { + continue; + } + // Use total_cmp for a consistent total ordering that + // distinguishes -0.0 from 0.0 (matching ArrowScalar's Ord). + if min_val.is_none() + || v.total_cmp(min_val.as_ref().unwrap()) == std::cmp::Ordering::Less + { + min_val = Some(v); + min_idx = Some(i); + } + if max_val.is_none() + || v.total_cmp(max_val.as_ref().unwrap()) == std::cmp::Ordering::Greater + { + max_val = Some(v); + max_idx = Some(i); + } + } + (min_idx, max_idx) + }}; +} + +macro_rules! find_extrema_bytes { + ($array:expr, $cast:ident :: < $offset:ty >) => {{ + let typed = $array.$cast::<$offset>(); + let mut min_idx: Option = None; + let mut max_idx: Option = None; + let mut min_val = None; + let mut max_val = None; + for i in 0..typed.len() { + if typed.is_null(i) { + continue; + } + let v = typed.value(i); + if min_val.is_none() || v < min_val.unwrap() { + min_val = Some(v); + min_idx = Some(i); + } + if max_val.is_none() || v > max_val.unwrap() { + max_val = Some(v); + max_idx = Some(i); + } + } + (min_idx, max_idx) + }}; +} + +fn find_min_max(array: &ArrayRef) -> Result<(Option, Option)> { + let (min_idx, max_idx) = find_min_max_indices(array)?; + + let min_scalar = min_idx + .map(|i| ArrowScalar::try_new(array, i)) + .transpose()?; + let max_scalar = max_idx + .map(|i| ArrowScalar::try_new(array, i)) + .transpose()?; + + Ok((min_scalar, max_scalar)) +} + +fn find_min_max_indices(array: &ArrayRef) -> Result<(Option, Option)> { + use DataType::*; + + let result = match array.data_type() { + // Integer types + Int8 => find_extrema_primitive!(array, Int8Type), + Int16 => find_extrema_primitive!(array, Int16Type), + Int32 => find_extrema_primitive!(array, Int32Type), + Int64 => find_extrema_primitive!(array, Int64Type), + UInt8 => find_extrema_primitive!(array, UInt8Type), + UInt16 => find_extrema_primitive!(array, UInt16Type), + UInt32 => find_extrema_primitive!(array, UInt32Type), + UInt64 => find_extrema_primitive!(array, UInt64Type), + + // Float types (skip NaN) + Float16 => find_extrema_float!(array, Float16Type), + Float32 => find_extrema_float!(array, Float32Type), + Float64 => find_extrema_float!(array, Float64Type), + + // Temporal types + Date32 => find_extrema_primitive!(array, Date32Type), + Date64 => find_extrema_primitive!(array, Date64Type), + Time32(arrow_schema::TimeUnit::Second) => { + find_extrema_primitive!(array, Time32SecondType) + } + Time32(arrow_schema::TimeUnit::Millisecond) => { + find_extrema_primitive!(array, Time32MillisecondType) + } + Time64(arrow_schema::TimeUnit::Microsecond) => { + find_extrema_primitive!(array, Time64MicrosecondType) + } + Time64(arrow_schema::TimeUnit::Nanosecond) => { + find_extrema_primitive!(array, Time64NanosecondType) + } + Timestamp(arrow_schema::TimeUnit::Second, _) => { + find_extrema_primitive!(array, TimestampSecondType) + } + Timestamp(arrow_schema::TimeUnit::Millisecond, _) => { + find_extrema_primitive!(array, TimestampMillisecondType) + } + Timestamp(arrow_schema::TimeUnit::Microsecond, _) => { + find_extrema_primitive!(array, TimestampMicrosecondType) + } + Timestamp(arrow_schema::TimeUnit::Nanosecond, _) => { + find_extrema_primitive!(array, TimestampNanosecondType) + } + Duration(arrow_schema::TimeUnit::Second) => { + find_extrema_primitive!(array, DurationSecondType) + } + Duration(arrow_schema::TimeUnit::Millisecond) => { + find_extrema_primitive!(array, DurationMillisecondType) + } + Duration(arrow_schema::TimeUnit::Microsecond) => { + find_extrema_primitive!(array, DurationMicrosecondType) + } + Duration(arrow_schema::TimeUnit::Nanosecond) => { + find_extrema_primitive!(array, DurationNanosecondType) + } + + // Boolean + Boolean => { + let typed = array.as_boolean(); + let mut min_idx: Option = None; + let mut max_idx: Option = None; + let mut min_val: Option = None; + let mut max_val: Option = None; + for i in 0..typed.len() { + if typed.is_null(i) { + continue; + } + let v = typed.value(i); + if min_val.is_none() || (!v && min_val.unwrap()) { + min_val = Some(v); + min_idx = Some(i); + } + if max_val.is_none() || (v && !max_val.unwrap()) { + max_val = Some(v); + max_idx = Some(i); + } + } + (min_idx, max_idx) + } + + // String types + Utf8 => find_extrema_bytes!(array, as_string::), + LargeUtf8 => find_extrema_bytes!(array, as_string::), + + // Binary types + Binary => find_extrema_bytes!(array, as_binary::), + LargeBinary => find_extrema_bytes!(array, as_binary::), + + // For unsupported types we skip min/max (and nan_count). + // null_count and buffer_memory are already tracked above. + _ => return Ok((None, None)), + }; + + Ok(result) +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::*; + use arrow_schema::DataType; + use rstest::rstest; + + use super::*; + + #[test] + fn test_empty_array() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(Vec::::new())); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.nan_count, None); + } + + #[test] + fn test_all_nulls() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![None, None, None])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 3); + } + + #[test] + fn test_single_value() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![42])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(stats.min.as_ref().unwrap(), stats.max.as_ref().unwrap()); + assert_eq!(format!("{}", stats.min.unwrap()), "42"); + } + + #[test] + fn test_basic_int_stats() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 5, 3, 2, 4])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "5"); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.nan_count, None); + } + + #[test] + fn test_with_nulls() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), None, Some(3)])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "3"); + assert_eq!(stats.null_count, 1); + } + + #[test] + fn test_float_nan_excluded() { + let mut acc = StatisticsAccumulator::new(&DataType::Float64); + let array: ArrayRef = Arc::new(Float64Array::from(vec![1.0, f64::NAN, 3.0])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1.0"); + assert_eq!(format!("{}", stats.max.unwrap()), "3.0"); + assert_eq!(stats.nan_count, Some(1)); + } + + #[test] + fn test_all_nan() { + let mut acc = StatisticsAccumulator::new(&DataType::Float64); + let array: ArrayRef = Arc::new(Float64Array::from(vec![f64::NAN, f64::NAN])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.nan_count, Some(2)); + } + + #[test] + fn test_null_and_nan() { + let mut acc = StatisticsAccumulator::new(&DataType::Float64); + let array: ArrayRef = Arc::new(Float64Array::from(vec![None, Some(f64::NAN)])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.nan_count, Some(1)); + } + + #[test] + fn test_multiple_updates() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let a1: ArrayRef = Arc::new(Int32Array::from(vec![5, 3])); + let a2: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), None, Some(7)])); + acc.update(&a1).unwrap(); + acc.update(&a2).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "7"); + assert_eq!(stats.null_count, 1); + } + + #[test] + fn test_merge() { + let mut acc1 = StatisticsAccumulator::new(&DataType::Int32); + let a1: ArrayRef = Arc::new(Int32Array::from(vec![1, 5])); + acc1.update(&a1).unwrap(); + + let mut acc2 = StatisticsAccumulator::new(&DataType::Int32); + let a2: ArrayRef = Arc::new(Int32Array::from(vec![Some(3), None, Some(10)])); + acc2.update(&a2).unwrap(); + + acc1.merge(&acc2).unwrap(); + let stats = acc1.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "10"); + assert_eq!(stats.null_count, 1); + } + + #[test] + fn test_merge_type_mismatch() { + let acc1 = StatisticsAccumulator::new(&DataType::Int32); + let acc2 = StatisticsAccumulator::new(&DataType::Float64); + let mut acc1 = acc1; + assert!(acc1.merge(&acc2).is_err()); + } + + #[test] + fn test_type_mismatch_error() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Float64Array::from(vec![1.0])); + assert!(acc.update(&array).is_err()); + } + + #[test] + fn test_reset() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), None, Some(3)])); + acc.update(&array).unwrap(); + acc.reset(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.nan_count, None); + } + + #[test] + fn test_string_stats() { + let mut acc = StatisticsAccumulator::new(&DataType::Utf8); + let array: ArrayRef = Arc::new(StringArray::from(vec!["apple", "cherry", "banana"])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "apple"); + assert_eq!(format!("{}", stats.max.unwrap()), "cherry"); + } + + #[test] + fn test_boolean_stats() { + let mut acc = StatisticsAccumulator::new(&DataType::Boolean); + let array: ArrayRef = Arc::new(BooleanArray::from(vec![true, false])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "false"); + assert_eq!(format!("{}", stats.max.unwrap()), "true"); + } + + #[rstest] + #[case::i32( + DataType::Int32, + Arc::new(Int32Array::from(vec![3, 1, 2])) as ArrayRef, + "1", "3" + )] + #[case::i64( + DataType::Int64, + Arc::new(Int64Array::from(vec![30, 10, 20])) as ArrayRef, + "10", "30" + )] + #[case::u32( + DataType::UInt32, + Arc::new(UInt32Array::from(vec![3, 1, 2])) as ArrayRef, + "1", "3" + )] + #[case::u64( + DataType::UInt64, + Arc::new(UInt64Array::from(vec![30, 10, 20])) as ArrayRef, + "10", "30" + )] + #[case::f32( + DataType::Float32, + Arc::new(Float32Array::from(vec![3.0f32, 1.0, 2.0])) as ArrayRef, + "1.0", "3.0" + )] + #[case::f64( + DataType::Float64, + Arc::new(Float64Array::from(vec![3.0f64, 1.0, 2.0])) as ArrayRef, + "1.0", "3.0" + )] + fn test_rstest_primitives( + #[case] dt: DataType, + #[case] array: ArrayRef, + #[case] expected_min: &str, + #[case] expected_max: &str, + ) { + let mut acc = StatisticsAccumulator::new(&dt); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), expected_min); + assert_eq!(format!("{}", stats.max.unwrap()), expected_max); + } + + #[test] + fn test_statistics_does_not_consume() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3])); + acc.update(&array).unwrap(); + let s1 = acc.statistics(); + let s2 = acc.statistics(); + assert_eq!(format!("{}", s1.min.unwrap()), "1"); + assert_eq!(format!("{}", s2.max.unwrap()), "3"); + } + + #[test] + fn test_merge_into_empty() { + let mut acc1 = StatisticsAccumulator::new(&DataType::Int32); + let mut acc2 = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![5, 10])); + acc2.update(&array).unwrap(); + + acc1.merge(&acc2).unwrap(); + let stats = acc1.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "5"); + assert_eq!(format!("{}", stats.max.unwrap()), "10"); + } + + #[test] + fn test_buffer_memory() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let a1: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3])); + let a2: ArrayRef = Arc::new(Int32Array::from(vec![4, 5])); + let expected = a1.get_buffer_memory_size() + a2.get_buffer_memory_size(); + acc.update(&a1).unwrap(); + acc.update(&a2).unwrap(); + let stats = acc.finish(); + assert_eq!(stats.buffer_memory, expected as u64); + assert!(stats.buffer_memory > 0); + } + + #[test] + fn test_buffer_memory_reset() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3])); + acc.update(&array).unwrap(); + assert!(acc.statistics().buffer_memory > 0); + acc.reset(); + assert_eq!(acc.statistics().buffer_memory, 0); + } + + #[test] + fn test_non_list_item_nulls_is_none() { + let mut acc = StatisticsAccumulator::new(&DataType::Int32); + let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3])); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(stats.item_nulls, None); + } + + mod list_tests { + use super::*; + use arrow_array::builder::{Int32Builder, LargeListBuilder, ListBuilder}; + use arrow_schema::Field; + + fn list_data_type() -> DataType { + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))) + } + + fn large_list_data_type() -> DataType { + DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true))) + } + + /// Build a ListArray from a slice of optional lists of optional i32. + fn build_list_array(rows: &[Option<&[Option]>]) -> ArrayRef { + let mut builder = ListBuilder::new(Int32Builder::new()); + for row in rows { + match row { + Some(items) => { + for item in *items { + match item { + Some(v) => builder.values().append_value(*v), + None => builder.values().append_null(), + } + } + builder.append(true); + } + None => builder.append(false), + } + } + Arc::new(builder.finish()) + } + + /// Build a LargeListArray from a slice of optional lists of optional i32. + fn build_large_list_array(rows: &[Option<&[Option]>]) -> ArrayRef { + let mut builder = LargeListBuilder::new(Int32Builder::new()); + for row in rows { + match row { + Some(items) => { + for item in *items { + match item { + Some(v) => builder.values().append_value(*v), + None => builder.values().append_null(), + } + } + builder.append(true); + } + None => builder.append(false), + } + } + Arc::new(builder.finish()) + } + + #[test] + fn test_list_basic() { + // [[1, 5], [3, 2, 4]] + let array = build_list_array(&[ + Some(&[Some(1), Some(5)]), + Some(&[Some(3), Some(2), Some(4)]), + ]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "5"); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.item_nulls, Some(0)); + assert_eq!(stats.nan_count, None); + } + + #[test] + fn test_list_with_null_items() { + // [[1, null, 5], [null, 3]] + let array = + build_list_array(&[Some(&[Some(1), None, Some(5)]), Some(&[None, Some(3)])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "5"); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.item_nulls, Some(2)); + } + + #[test] + fn test_list_with_null_lists() { + // [[1, 2], null, [3]] + let array = build_list_array(&[Some(&[Some(1), Some(2)]), None, Some(&[Some(3)])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "3"); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.item_nulls, Some(0)); + } + + #[test] + fn test_list_with_null_lists_and_null_items() { + // [[1, null], null, [null, 3]] + let array = build_list_array(&[Some(&[Some(1), None]), None, Some(&[None, Some(3)])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "3"); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.item_nulls, Some(2)); + } + + #[test] + fn test_list_all_null_lists() { + let array = build_list_array(&[None, None]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 2); + assert_eq!(stats.item_nulls, Some(0)); + } + + #[test] + fn test_list_empty_lists() { + // [[], [1], []] + let array = build_list_array(&[Some(&[]), Some(&[Some(1)]), Some(&[])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "1"); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.item_nulls, Some(0)); + } + + #[test] + fn test_list_all_items_null() { + // [[null, null]] + let array = build_list_array(&[Some(&[None, None])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.item_nulls, Some(2)); + } + + #[test] + fn test_list_multiple_updates() { + let a1 = build_list_array(&[Some(&[Some(5), Some(3)])]); + let a2 = build_list_array(&[Some(&[Some(1), None]), None, Some(&[Some(7)])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&a1).unwrap(); + acc.update(&a2).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "7"); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.item_nulls, Some(1)); + } + + #[test] + fn test_list_merge() { + let a1 = build_list_array(&[Some(&[Some(1), Some(5)])]); + let a2 = build_list_array(&[Some(&[Some(3), None]), None, Some(&[Some(10)])]); + + let mut acc1 = StatisticsAccumulator::new(&list_data_type()); + acc1.update(&a1).unwrap(); + let mut acc2 = StatisticsAccumulator::new(&list_data_type()); + acc2.update(&a2).unwrap(); + + acc1.merge(&acc2).unwrap(); + let stats = acc1.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "10"); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.item_nulls, Some(1)); + } + + #[test] + fn test_list_reset() { + let array = build_list_array(&[Some(&[Some(1), None])]); + let mut acc = StatisticsAccumulator::new(&list_data_type()); + acc.update(&array).unwrap(); + acc.reset(); + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 0); + assert_eq!(stats.item_nulls, Some(0)); + } + + #[test] + fn test_large_list() { + let array = + build_large_list_array(&[Some(&[Some(10), None, Some(1)]), None, Some(&[Some(5)])]); + let mut acc = StatisticsAccumulator::new(&large_list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "10"); + assert_eq!(stats.null_count, 1); + assert_eq!(stats.item_nulls, Some(1)); + } + + /// Build a List> array from nested slices. + /// + /// Each outer Option represents an outer list entry (None = null outer list). + /// Each inner Option<&[Option]> represents an inner list entry + /// (None = null inner list). + #[allow(clippy::type_complexity)] + fn build_nested_list_array(rows: &[Option<&[Option<&[Option]>]>]) -> ArrayRef { + let inner_builder = ListBuilder::new(Int32Builder::new()); + let mut builder = ListBuilder::new(inner_builder); + for row in rows { + match row { + Some(inner_lists) => { + let inner_builder = builder.values(); + for inner_list in *inner_lists { + match inner_list { + Some(items) => { + for item in *items { + match item { + Some(v) => { + inner_builder.values().append_value(*v); + } + None => { + inner_builder.values().append_null(); + } + } + } + inner_builder.append(true); + } + None => { + inner_builder.append(false); + } + } + } + builder.append(true); + } + None => builder.append(false), + } + } + Arc::new(builder.finish()) + } + + fn nested_list_data_type() -> DataType { + DataType::List(Arc::new(Field::new( + "item", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + ))) + } + + #[test] + fn test_nested_list() { + // [[[1, 2], [3]], null, [[null, 5], null, [6]]] + let array = build_nested_list_array(&[ + Some(&[Some(&[Some(1), Some(2)][..]), Some(&[Some(3)])]), + None, + Some(&[Some(&[None, Some(5)]), None, Some(&[Some(6)])]), + ]); + + let mut acc = StatisticsAccumulator::new(&nested_list_data_type()); + acc.update(&array).unwrap(); + let stats = acc.finish(); + + // min/max should be computed across all leaf int32 values + assert_eq!(format!("{}", stats.min.unwrap()), "1"); + assert_eq!(format!("{}", stats.max.unwrap()), "6"); + // null_count: only the one null outer list + assert_eq!(stats.null_count, 1); + // item_nulls: 1 null int32 + 1 null inner list = 2 + assert_eq!(stats.item_nulls, Some(2)); + } + } + + mod proptests { + use super::*; + use arrow_select::take::take; + use proptest::prelude::*; + + /// Shuffle an array by applying a random permutation via the `take` kernel. + fn shuffle(array: &ArrayRef, permutation: &[usize]) -> ArrayRef { + let indices = + UInt32Array::from(permutation.iter().map(|&i| i as u32).collect::>()); + take(array.as_ref(), &indices, None).unwrap() + } + + /// Compute stats for an array, returning (min, max) as Option. + fn compute_stats(array: &ArrayRef) -> (Option, Option) { + let mut acc = StatisticsAccumulator::new(array.data_type()); + acc.update(array).unwrap(); + let stats = acc.finish(); + (stats.min, stats.max) + } + + macro_rules! prop_test_full { + ($name:ident, $array_ty:ty, $elem_strategy:expr) => { + proptest! { + #[test] + fn $name( + values in proptest::collection::vec($elem_strategy, 1..100usize), + ) { + let len = values.len(); + let array: ArrayRef = Arc::new(<$array_ty>::from(values)); + let (orig_min, orig_max) = compute_stats(&array); + + // min <= max when both exist + if let (Some(mn), Some(mx)) = (&orig_min, &orig_max) { + prop_assert!(mn <= mx, "min {:?} > max {:?}", mn, mx); + } + + // Reverse the array as a simple permutation + let rev_indices: Vec = (0..len).rev().collect(); + let reversed = shuffle(&array, &rev_indices); + let (rev_min, rev_max) = compute_stats(&reversed); + prop_assert_eq!(&orig_min, &rev_min, "min changed after reverse"); + prop_assert_eq!(&orig_max, &rev_max, "max changed after reverse"); + } + } + }; + } + + macro_rules! prop_test_nullable_full { + ($name:ident, $array_ty:ty, $elem_strategy:expr) => { + proptest! { + #[test] + fn $name( + values in proptest::collection::vec( + proptest::option::of($elem_strategy), 1..100usize + ), + ) { + let len = values.len(); + let array: ArrayRef = Arc::new(<$array_ty>::from(values)); + let (orig_min, orig_max) = compute_stats(&array); + + if let (Some(mn), Some(mx)) = (&orig_min, &orig_max) { + prop_assert!(mn <= mx, "min {:?} > max {:?}", mn, mx); + } + + let rev_indices: Vec = (0..len).rev().collect(); + let reversed = shuffle(&array, &rev_indices); + let (rev_min, rev_max) = compute_stats(&reversed); + prop_assert_eq!(&orig_min, &rev_min, "min changed after reverse"); + prop_assert_eq!(&orig_max, &rev_max, "max changed after reverse"); + + // Also verify null_count and nan_count are invariant + let mut acc_orig = StatisticsAccumulator::new(array.data_type()); + acc_orig.update(&array).unwrap(); + let mut acc_rev = StatisticsAccumulator::new(array.data_type()); + acc_rev.update(&reversed).unwrap(); + prop_assert_eq!( + acc_orig.statistics().null_count, + acc_rev.statistics().null_count, + "null_count changed after shuffle" + ); + prop_assert_eq!( + acc_orig.statistics().nan_count, + acc_rev.statistics().nan_count, + "nan_count changed after shuffle" + ); + } + } + }; + } + + // --- Integer types --- + prop_test_full!(prop_i32, Int32Array, any::()); + prop_test_full!(prop_i64, Int64Array, any::()); + prop_test_full!(prop_u32, UInt32Array, any::()); + prop_test_full!(prop_u64, UInt64Array, any::()); + prop_test_full!(prop_i8, Int8Array, any::()); + prop_test_full!(prop_i16, Int16Array, any::()); + prop_test_full!(prop_u8, UInt8Array, any::()); + prop_test_full!(prop_u16, UInt16Array, any::()); + + // --- Nullable integer types --- + prop_test_nullable_full!(prop_i32_nullable, Int32Array, any::()); + prop_test_nullable_full!(prop_i64_nullable, Int64Array, any::()); + prop_test_nullable_full!(prop_u32_nullable, UInt32Array, any::()); + + // --- Float types (with NaN) --- + prop_test_full!(prop_f32, Float32Array, any::()); + prop_test_full!(prop_f64, Float64Array, any::()); + prop_test_nullable_full!(prop_f64_nullable, Float64Array, any::()); + + // --- String type --- + prop_test_full!(prop_string, StringArray, "[a-z]{0,20}"); + prop_test_nullable_full!(prop_string_nullable, StringArray, "[a-z]{0,20}"); + + // --- Boolean type --- + prop_test_full!(prop_bool, BooleanArray, any::()); + prop_test_nullable_full!(prop_bool_nullable, BooleanArray, any::()); + + // --- Random permutation shuffle test (uses prop_shuffle) --- + proptest! { + #[test] + fn prop_random_permutation_i32( + values in proptest::collection::vec( + proptest::option::of(any::()), 1..100usize + ), + ) { + let len = values.len(); + let array: ArrayRef = Arc::new(Int32Array::from(values)); + let (orig_min, orig_max) = compute_stats(&array); + + if let (Some(mn), Some(mx)) = (&orig_min, &orig_max) { + prop_assert!(mn <= mx); + } + + // Create and shuffle a permutation + let mut perm: Vec = (0..len).collect(); + // Deterministic "shuffle" using a reversal + rotation + perm.reverse(); + if len > 1 { + perm.rotate_left(len / 2); + } + + let shuffled = shuffle(&array, &perm); + let (shuf_min, shuf_max) = compute_stats(&shuffled); + prop_assert_eq!(&orig_min, &shuf_min); + prop_assert_eq!(&orig_max, &shuf_max); + } + } + + proptest! { + /// Verify that splitting an array into two chunks and merging + /// the accumulators gives the same result as processing the + /// whole array at once. + #[test] + fn prop_merge_consistent_i32( + values in proptest::collection::vec( + proptest::option::of(any::()), 2..100usize + ), + ) { + let array: ArrayRef = Arc::new(Int32Array::from(values.clone())); + let split = values.len() / 2; + + let mut full_acc = StatisticsAccumulator::new(&DataType::Int32); + full_acc.update(&array).unwrap(); + + let left: ArrayRef = Arc::new(Int32Array::from(values[..split].to_vec())); + let right: ArrayRef = Arc::new(Int32Array::from(values[split..].to_vec())); + let mut left_acc = StatisticsAccumulator::new(&DataType::Int32); + left_acc.update(&left).unwrap(); + let mut right_acc = StatisticsAccumulator::new(&DataType::Int32); + right_acc.update(&right).unwrap(); + left_acc.merge(&right_acc).unwrap(); + + let full_stats = full_acc.finish(); + let merged_stats = left_acc.finish(); + + prop_assert_eq!(&full_stats.min, &merged_stats.min); + prop_assert_eq!(&full_stats.max, &merged_stats.max); + prop_assert_eq!(full_stats.null_count, merged_stats.null_count); + } + } + } + + #[test] + fn test_unsupported_type_tracks_null_count_and_memory() { + use arrow_array::builder::{Int32Builder, StructBuilder}; + use arrow_schema::Field; + + let fields = vec![Field::new("a", DataType::Int32, true)]; + let mut builder = StructBuilder::new(fields, vec![Box::new(Int32Builder::new()) as _]); + for _ in 0..3 { + builder + .field_builder::(0) + .unwrap() + .append_null(); + builder.append_null(); + } + let struct_array: ArrayRef = Arc::new(builder.finish()); + + let dt = struct_array.data_type().clone(); + let mut acc = StatisticsAccumulator::new(&dt); + acc.update(&struct_array).unwrap(); + + let stats = acc.finish(); + assert!(stats.min.is_none()); + assert!(stats.max.is_none()); + assert_eq!(stats.null_count, 3); + assert_eq!(stats.nan_count, None); + assert!(stats.buffer_memory > 0); + } +} diff --git a/lance-artifact/rust/arrow-stats/src/nan.rs b/lance-artifact/rust/arrow-stats/src/nan.rs new file mode 100644 index 000000000..b8b2fd3e8 --- /dev/null +++ b/lance-artifact/rust/arrow-stats/src/nan.rs @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_array::cast::AsArray; +use arrow_array::types::{Float16Type, Float32Type, Float64Type}; +use arrow_array::{Array, ArrayRef}; + +macro_rules! count_nans_typed { + ($array:expr, $arrow_type:ty) => {{ + let typed = $array.as_primitive::<$arrow_type>(); + let mut count = 0u64; + for i in 0..typed.len() { + if !typed.is_null(i) && typed.value(i).is_nan() { + count += 1; + } + } + count + }}; +} + +/// Count the number of non-null NaN values in an array. +/// +/// Returns 0 for non-float types. +pub fn count_nans(array: &ArrayRef) -> u64 { + use arrow_schema::DataType::*; + match array.data_type() { + Float16 => count_nans_typed!(array, Float16Type), + Float32 => count_nans_typed!(array, Float32Type), + Float64 => count_nans_typed!(array, Float64Type), + _ => 0, + } +} diff --git a/lance-artifact/rust/compression/bitpacking/Cargo.toml b/lance-artifact/rust/compression/bitpacking/Cargo.toml new file mode 100644 index 000000000..56db321ac --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "lance-bitpacking" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +description = "Vendored copy of https://github.com/spiraldb/fastlanes for use in Lance" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[dependencies] +arrayref = "0.3" +crunchy = "0.2" +paste = "1" +seq-macro = "0.3" + +[dev-dependencies] +bitpacking = { workspace = true, features = ["bitpacker4x"] } + +[lints] +workspace = true diff --git a/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker4x.rs b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker4x.rs new file mode 100644 index 000000000..ca3c68908 --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker4x.rs @@ -0,0 +1,820 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use super::BitPacker; + +use crate::bitpacker_internal::{Available, UnsafeBitPacker}; + +const BLOCK_LEN: usize = 32 * 4; + +#[cfg(target_arch = "x86_64")] +mod sse3 { + + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + + use std::arch::x86_64::__m128i as DataType; + use std::arch::x86_64::_mm_and_si128 as op_and; + use std::arch::x86_64::_mm_lddqu_si128 as load_unaligned; + use std::arch::x86_64::_mm_or_si128 as op_or; + use std::arch::x86_64::_mm_set1_epi32 as set1; + use std::arch::x86_64::_mm_slli_epi32 as left_shift_32; + use std::arch::x86_64::_mm_srli_epi32 as right_shift_32; + use std::arch::x86_64::_mm_storeu_si128 as store_unaligned; + use std::arch::x86_64::{ + _mm_add_epi32, _mm_cvtsi128_si32, _mm_shuffle_epi32, _mm_slli_si128, _mm_srli_si128, + _mm_sub_epi32, + }; + + #[allow(non_snake_case)] + #[inline] + unsafe fn or_collapse_to_u32(accumulator: DataType) -> u32 { + let a__b__c__d_ = accumulator; + let ______a__b_ = _mm_srli_si128(a__b__c__d_, 8); + let a__b__ca_db = op_or(a__b__c__d_, ______a__b_); + let ___a__b__ca = _mm_srli_si128(a__b__ca_db, 4); + let _______cadb = op_or(a__b__ca_db, ___a__b__ca); + _mm_cvtsi128_si32(_______cadb) as u32 + } + + #[target_feature(enable = "sse3")] + unsafe fn compute_delta(curr: DataType, prev: DataType) -> DataType { + _mm_sub_epi32( + curr, + op_or(_mm_slli_si128(curr, 4), _mm_srli_si128(prev, 12)), + ) + } + + #[target_feature(enable = "sse3")] + #[allow(non_snake_case)] + #[inline] + unsafe fn integrate_delta(prev: DataType, delta: DataType) -> DataType { + let offset = _mm_shuffle_epi32(prev, 0xff); + let a__b__c__d_ = delta; + let ______a__b_ = _mm_slli_si128(delta, 8); + let a__b__ca_db = _mm_add_epi32(______a__b_, a__b__c__d_); + let ___a__b__ca = _mm_slli_si128(a__b__ca_db, 4); + let a_ab_abc_abcd: DataType = _mm_add_epi32(___a__b__ca, a__b__ca_db); + _mm_add_epi32(offset, a_ab_abc_abcd) + } + + #[target_feature(enable = "sse3")] + #[inline] + unsafe fn add(left: DataType, right: DataType) -> DataType { + _mm_add_epi32(left, right) + } + + unsafe fn sub(left: DataType, right: DataType) -> DataType { + _mm_sub_epi32(left, right) + } + + declare_bitpacker!(target_feature(enable = "sse3")); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + is_x86_feature_detected!("sse3") + } + } +} + +#[cfg(all(target_arch = "aarch64", target_endian = "little"))] +mod neon { + + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + use std::arch::aarch64::{ + uint32x4_t, vaddq_u32, vandq_u32, vdupq_n_u32, vextq_u32, vgetq_lane_u32, vld1q_u32, + vorrq_u32, vshlq_n_u32, vshrq_n_u32, vst1q_u32, vsubq_u32, + }; + + pub(crate) type DataType = uint32x4_t; + + #[inline] + /// Creates a vector with all elements set to `el`. + unsafe fn set1(el: i32) -> DataType { + vdupq_n_u32(el as u32) + } + + #[inline] + unsafe fn right_shift_32(el: DataType) -> DataType { + const { + assert!(N >= 0); + assert!(N <= 32); + } + + // We unroll here because vshrq_n_u32 only accepts constants from 1 to 32. + match N { + 0 => el, + 1 => vshrq_n_u32::<1>(el), + 2 => vshrq_n_u32::<2>(el), + 3 => vshrq_n_u32::<3>(el), + 4 => vshrq_n_u32::<4>(el), + 5 => vshrq_n_u32::<5>(el), + 6 => vshrq_n_u32::<6>(el), + 7 => vshrq_n_u32::<7>(el), + 8 => vshrq_n_u32::<8>(el), + 9 => vshrq_n_u32::<9>(el), + 10 => vshrq_n_u32::<10>(el), + 11 => vshrq_n_u32::<11>(el), + 12 => vshrq_n_u32::<12>(el), + 13 => vshrq_n_u32::<13>(el), + 14 => vshrq_n_u32::<14>(el), + 15 => vshrq_n_u32::<15>(el), + 16 => vshrq_n_u32::<16>(el), + 17 => vshrq_n_u32::<17>(el), + 18 => vshrq_n_u32::<18>(el), + 19 => vshrq_n_u32::<19>(el), + 20 => vshrq_n_u32::<20>(el), + 21 => vshrq_n_u32::<21>(el), + 22 => vshrq_n_u32::<22>(el), + 23 => vshrq_n_u32::<23>(el), + 24 => vshrq_n_u32::<24>(el), + 25 => vshrq_n_u32::<25>(el), + 26 => vshrq_n_u32::<26>(el), + 27 => vshrq_n_u32::<27>(el), + 28 => vshrq_n_u32::<28>(el), + 29 => vshrq_n_u32::<29>(el), + 30 => vshrq_n_u32::<30>(el), + 31 => vshrq_n_u32::<31>(el), + 32 => vdupq_n_u32(0), + _ => core::hint::unreachable_unchecked(), + } + } + + #[inline] + unsafe fn left_shift_32(el: DataType) -> DataType { + const { + assert!(N >= 0); + assert!(N <= 32); + } + + // We unroll here because vshlq_n_u32 only accepts constants from 0 to 31. + match N { + 0 => el, + 1 => vshlq_n_u32::<1>(el), + 2 => vshlq_n_u32::<2>(el), + 3 => vshlq_n_u32::<3>(el), + 4 => vshlq_n_u32::<4>(el), + 5 => vshlq_n_u32::<5>(el), + 6 => vshlq_n_u32::<6>(el), + 7 => vshlq_n_u32::<7>(el), + 8 => vshlq_n_u32::<8>(el), + 9 => vshlq_n_u32::<9>(el), + 10 => vshlq_n_u32::<10>(el), + 11 => vshlq_n_u32::<11>(el), + 12 => vshlq_n_u32::<12>(el), + 13 => vshlq_n_u32::<13>(el), + 14 => vshlq_n_u32::<14>(el), + 15 => vshlq_n_u32::<15>(el), + 16 => vshlq_n_u32::<16>(el), + 17 => vshlq_n_u32::<17>(el), + 18 => vshlq_n_u32::<18>(el), + 19 => vshlq_n_u32::<19>(el), + 20 => vshlq_n_u32::<20>(el), + 21 => vshlq_n_u32::<21>(el), + 22 => vshlq_n_u32::<22>(el), + 23 => vshlq_n_u32::<23>(el), + 24 => vshlq_n_u32::<24>(el), + 25 => vshlq_n_u32::<25>(el), + 26 => vshlq_n_u32::<26>(el), + 27 => vshlq_n_u32::<27>(el), + 28 => vshlq_n_u32::<28>(el), + 29 => vshlq_n_u32::<29>(el), + 30 => vshlq_n_u32::<30>(el), + 31 => vshlq_n_u32::<31>(el), + 32 => vdupq_n_u32(0), + _ => core::hint::unreachable_unchecked(), + } + } + + use vorrq_u32 as op_or; + + #[inline] + unsafe fn op_and(left: DataType, right: DataType) -> DataType { + vandq_u32(left, right) + } + + #[inline] + unsafe fn load_unaligned(addr: *const DataType) -> DataType { + vld1q_u32(addr.cast::()) + } + + #[inline] + unsafe fn store_unaligned(addr: *mut DataType, data: DataType) { + vst1q_u32(addr.cast::(), data); + } + + #[inline] + /// Collapses the vector by performing a bitwise OR across all lanes + unsafe fn or_collapse_to_u32(acc: DataType) -> u32 { + vgetq_lane_u32(acc, 0) + | vgetq_lane_u32(acc, 1) + | vgetq_lane_u32(acc, 2) + | vgetq_lane_u32(acc, 3) + } + + unsafe fn compute_delta(curr: DataType, prev: DataType) -> DataType { + // Build a vector with [prev[3], curr[0], curr[1], curr[2]] + let prev_shifted = vextq_u32(prev, curr, 3); + vsubq_u32(curr, prev_shifted) + } + + #[allow(non_snake_case)] + #[inline] + unsafe fn integrate_delta(prev: DataType, delta: DataType) -> DataType { + let base = vdupq_n_u32(vgetq_lane_u32(prev, 3)); + let zero = vdupq_n_u32(0); + let a__b__c__d_ = delta; + let ______a__b_ = vextq_u32(zero, a__b__c__d_, 2); + let a__b__ca_db = vaddq_u32(______a__b_, a__b__c__d_); + let ___a__b__ca = vextq_u32(zero, a__b__ca_db, 3); + let a_ab_abc_abcd = vaddq_u32(___a__b__ca, a__b__ca_db); + vaddq_u32(base, a_ab_abc_abcd) + } + + #[inline] + unsafe fn add(left: DataType, right: DataType) -> DataType { + vaddq_u32(left, right) + } + + #[inline] + unsafe fn sub(left: DataType, right: DataType) -> DataType { + vsubq_u32(left, right) + } + + declare_bitpacker!(target_feature(enable = "neon")); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + std::arch::is_aarch64_feature_detected!("neon") + } + } +} + +mod scalar { + + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + use std::ptr; + + pub(crate) type DataType = [u32; 4]; + + pub(crate) fn set1(el: i32) -> DataType { + [el as u32; 4] + } + + pub(crate) fn right_shift_32(el: DataType) -> DataType { + [el[0] >> N, el[1] >> N, el[2] >> N, el[3] >> N] + } + + pub(crate) fn left_shift_32(el: DataType) -> DataType { + [el[0] << N, el[1] << N, el[2] << N, el[3] << N] + } + + pub(crate) fn op_or(left: DataType, right: DataType) -> DataType { + [ + left[0] | right[0], + left[1] | right[1], + left[2] | right[2], + left[3] | right[3], + ] + } + + pub(crate) fn op_and(left: DataType, right: DataType) -> DataType { + [ + left[0] & right[0], + left[1] & right[1], + left[2] & right[2], + left[3] & right[3], + ] + } + + pub(crate) unsafe fn load_unaligned(addr: *const DataType) -> DataType { + ptr::read_unaligned(addr) + } + + pub(crate) unsafe fn store_unaligned(addr: *mut DataType, data: DataType) { + ptr::write_unaligned(addr, data); + } + + pub(crate) fn or_collapse_to_u32(accumulator: DataType) -> u32 { + (accumulator[0] | accumulator[1]) | (accumulator[2] | accumulator[3]) + } + + fn compute_delta(curr: DataType, prev: DataType) -> DataType { + [ + curr[0].wrapping_sub(prev[3]), + curr[1].wrapping_sub(curr[0]), + curr[2].wrapping_sub(curr[1]), + curr[3].wrapping_sub(curr[2]), + ] + } + + fn integrate_delta(offset: DataType, delta: DataType) -> DataType { + let el0 = offset[3].wrapping_add(delta[0]); + let el1 = el0.wrapping_add(delta[1]); + let el2 = el1.wrapping_add(delta[2]); + let el3 = el2.wrapping_add(delta[3]); + [el0, el1, el2, el3] + } + + pub(crate) fn add(left: DataType, right: DataType) -> DataType { + [ + left[0].wrapping_add(right[0]), + left[1].wrapping_add(right[1]), + left[2].wrapping_add(right[2]), + left[3].wrapping_add(right[3]), + ] + } + + pub(crate) fn sub(left: DataType, right: DataType) -> DataType { + [ + left[0].wrapping_sub(right[0]), + left[1].wrapping_sub(right[1]), + left[2].wrapping_sub(right[2]), + left[3].wrapping_sub(right[3]), + ] + } + + // The `allow(unused)` is here to put an attribute that has no effect. + // + // For other bitpacker, we enable specific CPU instruction set, but for the + // scalar bitpacker none is required. + declare_bitpacker!(allow(unused)); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + true + } + } +} + +#[derive(Clone, Copy)] +enum InstructionSet { + #[cfg(target_arch = "x86_64")] + SSE3, + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + NEON, + Scalar, +} + +/// `BitPacker4x` packs integers in groups of 4. This gives an opportunity +/// to leverage `SSE3` instructions to encode and decode the stream. +/// +/// One block must contain `128 integers`. +#[derive(Clone, Copy)] +pub struct BitPacker4x(InstructionSet); + +impl BitPacker4x { + #[cfg(target_arch = "x86_64")] + pub(crate) fn new_sse() -> Option { + sse3::UnsafeBitPackerImpl::available().then_some(BitPacker4x(InstructionSet::SSE3)) + } + + #[cfg(not(target_arch = "x86_64"))] + pub(crate) fn new_sse() -> Option { + None + } + + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + pub(crate) fn new_neon() -> Option { + neon::UnsafeBitPackerImpl::available().then_some(BitPacker4x(InstructionSet::NEON)) + } + + #[cfg(not(all(target_arch = "aarch64", target_endian = "little")))] + pub(crate) fn new_neon() -> Option { + None + } + + pub(crate) fn new_scalar() -> Self { + BitPacker4x(InstructionSet::Scalar) + } +} + +impl BitPacker for BitPacker4x { + const BLOCK_LEN: usize = BLOCK_LEN; + + fn new() -> Self { + Self::new_sse() + .or_else(Self::new_neon) + .unwrap_or_else(Self::new_scalar) + } + + fn compress(&self, decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => { + sse3::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + } + } + } + + fn compress_sorted( + &self, + initial: u32, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => sse3::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + } + } + } + + fn compress_strictly_sorted( + &self, + initial: Option, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => sse3::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + } + } + } + + fn decompress(&self, compressed: &[u8], decompressed: &mut [u32], num_bits: u8) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => { + sse3::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + } + } + } + + fn decompress_strictly_sorted( + &self, + initial: Option, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => sse3::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + } + } + } + + fn decompress_sorted( + &self, + initial: u32, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => sse3::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + } + } + } + + fn num_bits(&self, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => sse3::UnsafeBitPackerImpl::num_bits(decompressed), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::num_bits(decompressed), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::num_bits(decompressed), + } + } + } + + fn num_bits_sorted(&self, initial: u32, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => { + sse3::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + } + } + } + + fn num_bits_strictly_sorted(&self, initial: Option, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::SSE3 => { + sse3::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + } + } + } +} + +#[cfg(any( + target_arch = "x86_64", + all(target_arch = "aarch64", target_endian = "little") +))] +#[cfg(any())] +mod tests { + use super::BLOCK_LEN; + use super::scalar; + use crate::bitpacker_internal::Available; + use crate::tests::test_util_compatible; + use crate::{BitPacker, BitPacker4x}; + + #[cfg(target_arch = "x86_64")] + #[test] + fn test_compatible_sse3() { + use super::sse3; + if sse3::UnsafeBitPackerImpl::available() { + test_util_compatible::( + BLOCK_LEN, + ); + } + } + + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + #[test] + fn test_compatible_neon() { + use super::neon; + if neon::UnsafeBitPackerImpl::available() { + test_util_compatible::( + BLOCK_LEN, + ); + } + } + + #[test] + fn test_delta_bit_width_32() { + let values = vec![i32::max_value() as u32 + 1; BitPacker4x::BLOCK_LEN]; + let bit_packer = BitPacker4x::new(); + let bit_width = bit_packer.num_bits_sorted(0, &values); + assert_eq!(bit_width, 32); + + let mut block = vec![0u8; BitPacker4x::compressed_block_size(bit_width)]; + bit_packer.compress_sorted(0, &values, &mut block, bit_width); + + let mut decoded_values = vec![0x10101010; BitPacker4x::BLOCK_LEN]; + bit_packer.decompress_sorted(0, &block, &mut decoded_values, bit_width); + + assert_eq!(values, decoded_values); + } + + #[test] + fn test_bit_width_32() { + let mut values = vec![i32::max_value() as u32 + 1; BitPacker4x::BLOCK_LEN]; + values[0] = 0; + let bit_packer = BitPacker4x::new(); + let bit_width = bit_packer.num_bits(&values); + assert_eq!(bit_width, 32); + + let mut block = vec![0u8; BitPacker4x::compressed_block_size(bit_width)]; + bit_packer.compress(&values, &mut block, bit_width); + + let mut decoded_values = vec![0x10101010; BitPacker4x::BLOCK_LEN]; + bit_packer.decompress(&block, &mut decoded_values, bit_width); + + assert_eq!(values, decoded_values); + } +} + +#[cfg(test)] +mod tests { + use super::{BLOCK_LEN, BitPacker4x}; + use crate::bitpacker_internal::BitPacker; + use bitpacking::{BitPacker as ExternalBitPacker, BitPacker4x as ExternalBitPacker4x}; + + fn mask_for_width(width: u8) -> u32 { + match width { + 0 => 0, + 32 => u32::MAX, + _ => (1u32 << width) - 1, + } + } + + fn raw_values(width: u8) -> Vec { + let mask = mask_for_width(width); + (0..BLOCK_LEN) + .map(|idx| ((idx * 17 + 3) as u32) & mask) + .collect() + } + + fn sorted_values(width: u8) -> (u32, Vec) { + if width == 0 { + return (11, vec![11; BLOCK_LEN]); + } + if width == 32 { + return (0, vec![u32::MAX; BLOCK_LEN]); + } + + let mask = mask_for_width(width).min(127); + let initial = 11u32; + let mut current = initial; + let values = (0..BLOCK_LEN) + .map(|idx| { + current += (idx as u32 * 7 + 1) & mask; + current + }) + .collect(); + (initial, values) + } + + fn strictly_sorted_values(width: u8) -> (Option, Vec) { + let mask = mask_for_width(width).min(127); + let mut current = 0u32; + let values = (0..BLOCK_LEN) + .map(|idx| { + if idx == 0 { + current = 0; + } else { + current += 1 + ((idx as u32 * 5) & mask); + } + current + }) + .collect(); + (None, values) + } + + #[test] + fn scalar_backend_matches_external_bitpacker4x() { + let scalar = BitPacker4x::new_scalar(); + let external = ExternalBitPacker4x::new(); + + for width in 0..=32 { + let values = raw_values(width); + assert_eq!(scalar.num_bits(&values), external.num_bits(&values)); + + let mut actual = vec![0u8; BitPacker4x::compressed_block_size(width)]; + let actual_len = scalar.compress(&values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker4x::compressed_block_size(width)]; + let expected_len = external.compress(&values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "raw width {width}"); + + let mut decoded = vec![0u32; BLOCK_LEN]; + assert_eq!(scalar.decompress(&actual, &mut decoded, width), actual_len); + assert_eq!(decoded, values); + + let (initial, values) = sorted_values(width); + assert_eq!( + scalar.num_bits_sorted(initial, &values), + external.num_bits_sorted(initial, &values) + ); + + let mut actual = vec![0u8; BitPacker4x::compressed_block_size(width)]; + let actual_len = scalar.compress_sorted(initial, &values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker4x::compressed_block_size(width)]; + let expected_len = external.compress_sorted(initial, &values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "sorted width {width}"); + + let mut decoded = vec![0u32; BLOCK_LEN]; + assert_eq!( + scalar.decompress_sorted(initial, &actual, &mut decoded, width), + actual_len + ); + assert_eq!(decoded, values); + } + } + + #[test] + fn scalar_backend_matches_external_strictly_sorted_bitpacker4x() { + let scalar = BitPacker4x::new_scalar(); + let external = ExternalBitPacker4x::new(); + + for width in 0..=16 { + let (initial, values) = strictly_sorted_values(width); + let num_bits = external.num_bits_strictly_sorted(initial, &values); + assert_eq!(scalar.num_bits_strictly_sorted(initial, &values), num_bits); + + let mut actual = vec![0u8; BitPacker4x::compressed_block_size(num_bits)]; + let actual_len = + scalar.compress_strictly_sorted(initial, &values, &mut actual, num_bits); + + let mut expected = vec![0u8; ExternalBitPacker4x::compressed_block_size(num_bits)]; + let expected_len = + external.compress_strictly_sorted(initial, &values, &mut expected, num_bits); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "strict width {width}"); + + let mut decoded = vec![0u32; BLOCK_LEN]; + assert_eq!( + scalar.decompress_strictly_sorted(initial, &actual, &mut decoded, num_bits), + actual_len + ); + assert_eq!(decoded, values); + } + } +} diff --git a/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker8x.rs b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker8x.rs new file mode 100644 index 000000000..b17edacab --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/bitpacker8x.rs @@ -0,0 +1,872 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use super::BitPacker; + +use crate::bitpacker_internal::{Available, UnsafeBitPacker}; + +const BLOCK_LEN: usize = 32 * 8; + +#[cfg(target_arch = "x86_64")] +mod avx2 { + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + + use std::arch::x86_64::__m256i as DataType; + use std::arch::x86_64::_mm256_and_si256 as op_and; + use std::arch::x86_64::_mm256_lddqu_si256 as load_unaligned; + use std::arch::x86_64::_mm256_or_si256 as op_or; + use std::arch::x86_64::_mm256_set1_epi32 as set1; + use std::arch::x86_64::_mm256_slli_epi32 as left_shift_32; + use std::arch::x86_64::_mm256_srli_epi32 as right_shift_32; + use std::arch::x86_64::_mm256_storeu_si256 as store_unaligned; + + use std::arch::x86_64::{ + _mm256_add_epi32, _mm256_extract_epi32, _mm256_permute2f128_si256, _mm256_shuffle_epi32, + _mm256_slli_si256, _mm256_srli_si256, _mm256_sub_epi32, + }; + + #[allow(non_snake_case)] + unsafe fn or_collapse_to_u32(accumulator: DataType) -> u32 { + let a__b__c__d__e__f__g__h_ = accumulator; + let ______a__b________e__f = _mm256_srli_si256(a__b__c__d__e__f__g__h_, 8); + let a__b__ca_db_e__f__ge_hf = op_or(a__b__c__d__e__f__g__h_, ______a__b________e__f); + let ___a__b__ca____e__f__ge = _mm256_srli_si256(a__b__ca_db_e__f__ge_hf, 4); + let _________cadb______gehf = op_or(a__b__ca_db_e__f__ge_hf, ___a__b__ca____e__f__ge); + let cadb = _mm256_extract_epi32(_________cadb______gehf, 0); + let gehf = _mm256_extract_epi32(_________cadb______gehf, 4); + (cadb | gehf) as u32 + } + + unsafe fn compute_delta(curr: DataType, prev: DataType) -> DataType { + let left_shift = _mm256_slli_si256(curr, 4); + let curr_shift = _mm256_srli_si256(curr, 12); + let curr_right_only = _mm256_permute2f128_si256(curr_shift, curr_shift, 8); + let prev_shift = _mm256_srli_si256(prev, 12); + let sub_left = _mm256_permute2f128_si256(prev_shift, prev_shift, 3 | (8 << 4)); + let diff = op_or(left_shift, op_or(curr_right_only, sub_left)); + _mm256_sub_epi32(curr, diff) + } + + #[allow(non_snake_case)] + unsafe fn integrate_delta(prev: DataType, delta: DataType) -> DataType { + let offset_repeat = _mm256_shuffle_epi32(prev, 0xff); + let offset = _mm256_permute2f128_si256(offset_repeat, offset_repeat, 3 | (8 << 4)); + let a__b__c__d__e__f__g__h__ = delta; + let ______a__b________e__f__ = _mm256_slli_si256(delta, 8); + let a__b__ca_db_e__f__ge_fh_ = + _mm256_add_epi32(a__b__c__d__e__f__g__h__, ______a__b________e__f__); + let ___a__b__ca____e__f__ge_ = _mm256_slli_si256(a__b__ca_db_e__f__ge_fh_, 4); + let halved_prefix_sum = + _mm256_add_epi32(___a__b__ca____e__f__ge_, a__b__ca_db_e__f__ge_fh_); + let offsetted_halved_prefix_sum = _mm256_add_epi32(halved_prefix_sum, offset); + let select_last_low = _mm256_shuffle_epi32(offsetted_halved_prefix_sum, 0xff); + let high_offset = _mm256_permute2f128_si256(select_last_low, select_last_low, 8); + _mm256_add_epi32(high_offset, offsetted_halved_prefix_sum) + } + + unsafe fn add(left: DataType, right: DataType) -> DataType { + _mm256_add_epi32(left, right) + } + + unsafe fn sub(left: DataType, right: DataType) -> DataType { + _mm256_sub_epi32(left, right) + } + + declare_bitpacker!(target_feature(enable = "avx2")); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + is_x86_feature_detected!("avx2") + } + } +} + +#[cfg(all(target_arch = "aarch64", target_endian = "little"))] +mod neon { + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + use std::arch::aarch64::{ + uint32x4_t, vaddq_u32, vandq_u32, vdupq_n_u32, vextq_u32, vgetq_lane_u32, vld1q_u32, + vorrq_u32, vshlq_n_u32, vshrq_n_u32, vst1q_u32, vsubq_u32, + }; + + pub(crate) type DataType = [uint32x4_t; 2]; + + #[inline] + unsafe fn set1(el: i32) -> DataType { + let lanes = vdupq_n_u32(el as u32); + [lanes, lanes] + } + + #[inline] + unsafe fn right_shift_32(el: DataType) -> DataType { + const { + assert!(N >= 0); + assert!(N <= 32); + } + + match N { + 0 => el, + 1 => [vshrq_n_u32::<1>(el[0]), vshrq_n_u32::<1>(el[1])], + 2 => [vshrq_n_u32::<2>(el[0]), vshrq_n_u32::<2>(el[1])], + 3 => [vshrq_n_u32::<3>(el[0]), vshrq_n_u32::<3>(el[1])], + 4 => [vshrq_n_u32::<4>(el[0]), vshrq_n_u32::<4>(el[1])], + 5 => [vshrq_n_u32::<5>(el[0]), vshrq_n_u32::<5>(el[1])], + 6 => [vshrq_n_u32::<6>(el[0]), vshrq_n_u32::<6>(el[1])], + 7 => [vshrq_n_u32::<7>(el[0]), vshrq_n_u32::<7>(el[1])], + 8 => [vshrq_n_u32::<8>(el[0]), vshrq_n_u32::<8>(el[1])], + 9 => [vshrq_n_u32::<9>(el[0]), vshrq_n_u32::<9>(el[1])], + 10 => [vshrq_n_u32::<10>(el[0]), vshrq_n_u32::<10>(el[1])], + 11 => [vshrq_n_u32::<11>(el[0]), vshrq_n_u32::<11>(el[1])], + 12 => [vshrq_n_u32::<12>(el[0]), vshrq_n_u32::<12>(el[1])], + 13 => [vshrq_n_u32::<13>(el[0]), vshrq_n_u32::<13>(el[1])], + 14 => [vshrq_n_u32::<14>(el[0]), vshrq_n_u32::<14>(el[1])], + 15 => [vshrq_n_u32::<15>(el[0]), vshrq_n_u32::<15>(el[1])], + 16 => [vshrq_n_u32::<16>(el[0]), vshrq_n_u32::<16>(el[1])], + 17 => [vshrq_n_u32::<17>(el[0]), vshrq_n_u32::<17>(el[1])], + 18 => [vshrq_n_u32::<18>(el[0]), vshrq_n_u32::<18>(el[1])], + 19 => [vshrq_n_u32::<19>(el[0]), vshrq_n_u32::<19>(el[1])], + 20 => [vshrq_n_u32::<20>(el[0]), vshrq_n_u32::<20>(el[1])], + 21 => [vshrq_n_u32::<21>(el[0]), vshrq_n_u32::<21>(el[1])], + 22 => [vshrq_n_u32::<22>(el[0]), vshrq_n_u32::<22>(el[1])], + 23 => [vshrq_n_u32::<23>(el[0]), vshrq_n_u32::<23>(el[1])], + 24 => [vshrq_n_u32::<24>(el[0]), vshrq_n_u32::<24>(el[1])], + 25 => [vshrq_n_u32::<25>(el[0]), vshrq_n_u32::<25>(el[1])], + 26 => [vshrq_n_u32::<26>(el[0]), vshrq_n_u32::<26>(el[1])], + 27 => [vshrq_n_u32::<27>(el[0]), vshrq_n_u32::<27>(el[1])], + 28 => [vshrq_n_u32::<28>(el[0]), vshrq_n_u32::<28>(el[1])], + 29 => [vshrq_n_u32::<29>(el[0]), vshrq_n_u32::<29>(el[1])], + 30 => [vshrq_n_u32::<30>(el[0]), vshrq_n_u32::<30>(el[1])], + 31 => [vshrq_n_u32::<31>(el[0]), vshrq_n_u32::<31>(el[1])], + 32 => set1(0), + _ => core::hint::unreachable_unchecked(), + } + } + + #[inline] + unsafe fn left_shift_32(el: DataType) -> DataType { + const { + assert!(N >= 0); + assert!(N <= 32); + } + + match N { + 0 => el, + 1 => [vshlq_n_u32::<1>(el[0]), vshlq_n_u32::<1>(el[1])], + 2 => [vshlq_n_u32::<2>(el[0]), vshlq_n_u32::<2>(el[1])], + 3 => [vshlq_n_u32::<3>(el[0]), vshlq_n_u32::<3>(el[1])], + 4 => [vshlq_n_u32::<4>(el[0]), vshlq_n_u32::<4>(el[1])], + 5 => [vshlq_n_u32::<5>(el[0]), vshlq_n_u32::<5>(el[1])], + 6 => [vshlq_n_u32::<6>(el[0]), vshlq_n_u32::<6>(el[1])], + 7 => [vshlq_n_u32::<7>(el[0]), vshlq_n_u32::<7>(el[1])], + 8 => [vshlq_n_u32::<8>(el[0]), vshlq_n_u32::<8>(el[1])], + 9 => [vshlq_n_u32::<9>(el[0]), vshlq_n_u32::<9>(el[1])], + 10 => [vshlq_n_u32::<10>(el[0]), vshlq_n_u32::<10>(el[1])], + 11 => [vshlq_n_u32::<11>(el[0]), vshlq_n_u32::<11>(el[1])], + 12 => [vshlq_n_u32::<12>(el[0]), vshlq_n_u32::<12>(el[1])], + 13 => [vshlq_n_u32::<13>(el[0]), vshlq_n_u32::<13>(el[1])], + 14 => [vshlq_n_u32::<14>(el[0]), vshlq_n_u32::<14>(el[1])], + 15 => [vshlq_n_u32::<15>(el[0]), vshlq_n_u32::<15>(el[1])], + 16 => [vshlq_n_u32::<16>(el[0]), vshlq_n_u32::<16>(el[1])], + 17 => [vshlq_n_u32::<17>(el[0]), vshlq_n_u32::<17>(el[1])], + 18 => [vshlq_n_u32::<18>(el[0]), vshlq_n_u32::<18>(el[1])], + 19 => [vshlq_n_u32::<19>(el[0]), vshlq_n_u32::<19>(el[1])], + 20 => [vshlq_n_u32::<20>(el[0]), vshlq_n_u32::<20>(el[1])], + 21 => [vshlq_n_u32::<21>(el[0]), vshlq_n_u32::<21>(el[1])], + 22 => [vshlq_n_u32::<22>(el[0]), vshlq_n_u32::<22>(el[1])], + 23 => [vshlq_n_u32::<23>(el[0]), vshlq_n_u32::<23>(el[1])], + 24 => [vshlq_n_u32::<24>(el[0]), vshlq_n_u32::<24>(el[1])], + 25 => [vshlq_n_u32::<25>(el[0]), vshlq_n_u32::<25>(el[1])], + 26 => [vshlq_n_u32::<26>(el[0]), vshlq_n_u32::<26>(el[1])], + 27 => [vshlq_n_u32::<27>(el[0]), vshlq_n_u32::<27>(el[1])], + 28 => [vshlq_n_u32::<28>(el[0]), vshlq_n_u32::<28>(el[1])], + 29 => [vshlq_n_u32::<29>(el[0]), vshlq_n_u32::<29>(el[1])], + 30 => [vshlq_n_u32::<30>(el[0]), vshlq_n_u32::<30>(el[1])], + 31 => [vshlq_n_u32::<31>(el[0]), vshlq_n_u32::<31>(el[1])], + 32 => set1(0), + _ => core::hint::unreachable_unchecked(), + } + } + + #[inline] + unsafe fn op_or(left: DataType, right: DataType) -> DataType { + [vorrq_u32(left[0], right[0]), vorrq_u32(left[1], right[1])] + } + + #[inline] + unsafe fn op_and(left: DataType, right: DataType) -> DataType { + [vandq_u32(left[0], right[0]), vandq_u32(left[1], right[1])] + } + + #[inline] + unsafe fn load_unaligned(addr: *const DataType) -> DataType { + let ptr = addr.cast::(); + [vld1q_u32(ptr), vld1q_u32(ptr.add(4))] + } + + #[inline] + unsafe fn store_unaligned(addr: *mut DataType, data: DataType) { + let ptr = addr.cast::(); + vst1q_u32(ptr, data[0]); + vst1q_u32(ptr.add(4), data[1]); + } + + #[inline] + unsafe fn or_collapse_to_u32(accumulator: DataType) -> u32 { + vgetq_lane_u32(accumulator[0], 0) + | vgetq_lane_u32(accumulator[0], 1) + | vgetq_lane_u32(accumulator[0], 2) + | vgetq_lane_u32(accumulator[0], 3) + | vgetq_lane_u32(accumulator[1], 0) + | vgetq_lane_u32(accumulator[1], 1) + | vgetq_lane_u32(accumulator[1], 2) + | vgetq_lane_u32(accumulator[1], 3) + } + + #[inline] + unsafe fn compute_delta(curr: DataType, prev: DataType) -> DataType { + [ + vsubq_u32(curr[0], vextq_u32(prev[1], curr[0], 3)), + vsubq_u32(curr[1], vextq_u32(curr[0], curr[1], 3)), + ] + } + + #[allow(non_snake_case)] + #[inline] + unsafe fn integrate_half(base: u32, delta: uint32x4_t) -> uint32x4_t { + let base = vdupq_n_u32(base); + let zero = vdupq_n_u32(0); + let a__b__c__d_ = delta; + let ______a__b_ = vextq_u32(zero, a__b__c__d_, 2); + let a__b__ca_db = vaddq_u32(______a__b_, a__b__c__d_); + let ___a__b__ca = vextq_u32(zero, a__b__ca_db, 3); + let a_ab_abc_abcd = vaddq_u32(___a__b__ca, a__b__ca_db); + vaddq_u32(base, a_ab_abc_abcd) + } + + #[inline] + unsafe fn integrate_delta(prev: DataType, delta: DataType) -> DataType { + let low = integrate_half(vgetq_lane_u32(prev[1], 3), delta[0]); + let high = integrate_half(vgetq_lane_u32(low, 3), delta[1]); + [low, high] + } + + #[inline] + unsafe fn add(left: DataType, right: DataType) -> DataType { + [vaddq_u32(left[0], right[0]), vaddq_u32(left[1], right[1])] + } + + #[inline] + unsafe fn sub(left: DataType, right: DataType) -> DataType { + [vsubq_u32(left[0], right[0]), vsubq_u32(left[1], right[1])] + } + + declare_bitpacker!(target_feature(enable = "neon")); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + std::arch::is_aarch64_feature_detected!("neon") + } + } +} + +mod scalar { + use super::BLOCK_LEN; + use crate::bitpacker_internal::Available; + use std::ptr; + + pub(crate) type DataType = [u32; 8]; + + pub(crate) fn set1(el: i32) -> DataType { + [el as u32; 8] + } + + pub(crate) fn right_shift_32(el: DataType) -> DataType { + [ + el[0] >> N, + el[1] >> N, + el[2] >> N, + el[3] >> N, + el[4] >> N, + el[5] >> N, + el[6] >> N, + el[7] >> N, + ] + } + + pub(crate) fn left_shift_32(el: DataType) -> DataType { + [ + el[0] << N, + el[1] << N, + el[2] << N, + el[3] << N, + el[4] << N, + el[5] << N, + el[6] << N, + el[7] << N, + ] + } + + pub(crate) fn op_or(left: DataType, right: DataType) -> DataType { + [ + left[0] | right[0], + left[1] | right[1], + left[2] | right[2], + left[3] | right[3], + left[4] | right[4], + left[5] | right[5], + left[6] | right[6], + left[7] | right[7], + ] + } + + pub(crate) fn op_and(left: DataType, right: DataType) -> DataType { + [ + left[0] & right[0], + left[1] & right[1], + left[2] & right[2], + left[3] & right[3], + left[4] & right[4], + left[5] & right[5], + left[6] & right[6], + left[7] & right[7], + ] + } + + pub(crate) unsafe fn load_unaligned(addr: *const DataType) -> DataType { + ptr::read_unaligned(addr) + } + + pub(crate) unsafe fn store_unaligned(addr: *mut DataType, data: DataType) { + ptr::write_unaligned(addr, data); + } + + pub(crate) fn or_collapse_to_u32(accumulator: DataType) -> u32 { + ((accumulator[0] | accumulator[1]) | (accumulator[2] | accumulator[3])) + | ((accumulator[4] | accumulator[5]) | (accumulator[6] | accumulator[7])) + } + + fn compute_delta(curr: DataType, prev: DataType) -> DataType { + [ + curr[0].wrapping_sub(prev[7]), + curr[1].wrapping_sub(curr[0]), + curr[2].wrapping_sub(curr[1]), + curr[3].wrapping_sub(curr[2]), + curr[4].wrapping_sub(curr[3]), + curr[5].wrapping_sub(curr[4]), + curr[6].wrapping_sub(curr[5]), + curr[7].wrapping_sub(curr[6]), + ] + } + + fn integrate_delta(offset: DataType, delta: DataType) -> DataType { + let el0 = offset[7].wrapping_add(delta[0]); + let el1 = el0.wrapping_add(delta[1]); + let el2 = el1.wrapping_add(delta[2]); + let el3 = el2.wrapping_add(delta[3]); + let el4 = el3.wrapping_add(delta[4]); + let el5 = el4.wrapping_add(delta[5]); + let el6 = el5.wrapping_add(delta[6]); + let el7 = el6.wrapping_add(delta[7]); + [el0, el1, el2, el3, el4, el5, el6, el7] + } + + pub(crate) fn add(left: DataType, right: DataType) -> DataType { + [ + left[0].wrapping_add(right[0]), + left[1].wrapping_add(right[1]), + left[2].wrapping_add(right[2]), + left[3].wrapping_add(right[3]), + left[4].wrapping_add(right[4]), + left[5].wrapping_add(right[5]), + left[6].wrapping_add(right[6]), + left[7].wrapping_add(right[7]), + ] + } + + pub(crate) fn sub(left: DataType, right: DataType) -> DataType { + [ + left[0].wrapping_sub(right[0]), + left[1].wrapping_sub(right[1]), + left[2].wrapping_sub(right[2]), + left[3].wrapping_sub(right[3]), + left[4].wrapping_sub(right[4]), + left[5].wrapping_sub(right[5]), + left[6].wrapping_sub(right[6]), + left[7].wrapping_sub(right[7]), + ] + } + + // The `allow(unused)` is here to put an attribute that has no effect. + // + // For other bitpackers, we enable a specific CPU instruction set, but for + // the scalar bitpacker none is required. + declare_bitpacker!(allow(unused)); + + impl Available for UnsafeBitPackerImpl { + fn available() -> bool { + true + } + } +} + +#[derive(Clone, Copy)] +enum InstructionSet { + #[cfg(target_arch = "x86_64")] + AVX2, + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + NEON, + Scalar, +} + +/// 8-wide bitpacker implementation. +/// +/// One block contains 256 integers. +#[derive(Clone, Copy)] +pub struct BitPacker8x(InstructionSet); + +impl BitPacker8x { + #[cfg(target_arch = "x86_64")] + pub(crate) fn new_avx2() -> Option { + avx2::UnsafeBitPackerImpl::available().then_some(BitPacker8x(InstructionSet::AVX2)) + } + + #[cfg(not(target_arch = "x86_64"))] + pub(crate) fn new_avx2() -> Option { + None + } + + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + pub(crate) fn new_neon() -> Option { + neon::UnsafeBitPackerImpl::available().then_some(BitPacker8x(InstructionSet::NEON)) + } + + #[cfg(not(all(target_arch = "aarch64", target_endian = "little")))] + pub(crate) fn new_neon() -> Option { + None + } + + pub(crate) fn new_scalar() -> Self { + BitPacker8x(InstructionSet::Scalar) + } +} + +impl BitPacker for BitPacker8x { + const BLOCK_LEN: usize = BLOCK_LEN; + + fn new() -> Self { + Self::new_avx2() + .or_else(Self::new_neon) + .unwrap_or_else(Self::new_scalar) + } + + fn compress(&self, decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => { + avx2::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::compress(decompressed, compressed, num_bits) + } + } + } + } + + fn compress_sorted( + &self, + initial: u32, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => avx2::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::compress_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + } + } + } + + fn compress_strictly_sorted( + &self, + initial: Option, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => avx2::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::compress_strictly_sorted( + initial, + decompressed, + compressed, + num_bits, + ), + } + } + } + + fn decompress(&self, compressed: &[u8], decompressed: &mut [u32], num_bits: u8) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => { + avx2::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::decompress(compressed, decompressed, num_bits) + } + } + } + } + + fn decompress_sorted( + &self, + initial: u32, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => avx2::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::decompress_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + } + } + } + + fn decompress_strictly_sorted( + &self, + initial: Option, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => avx2::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::decompress_strictly_sorted( + initial, + compressed, + decompressed, + num_bits, + ), + } + } + } + + fn num_bits(&self, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => avx2::UnsafeBitPackerImpl::num_bits(decompressed), + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => neon::UnsafeBitPackerImpl::num_bits(decompressed), + InstructionSet::Scalar => scalar::UnsafeBitPackerImpl::num_bits(decompressed), + } + } + } + + fn num_bits_sorted(&self, initial: u32, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => { + avx2::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::num_bits_sorted(initial, decompressed) + } + } + } + } + + fn num_bits_strictly_sorted(&self, initial: Option, decompressed: &[u32]) -> u8 { + unsafe { + match self.0 { + #[cfg(target_arch = "x86_64")] + InstructionSet::AVX2 => { + avx2::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + InstructionSet::NEON => { + neon::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + InstructionSet::Scalar => { + scalar::UnsafeBitPackerImpl::num_bits_strictly_sorted(initial, decompressed) + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::BitPacker8x; + use crate::bitpacker_internal::BitPacker; + use bitpacking::{BitPacker as ExternalBitPacker, BitPacker8x as ExternalBitPacker8x}; + + fn mask_for_width(width: u8) -> u32 { + match width { + 0 => 0, + 32 => u32::MAX, + _ => (1u32 << width) - 1, + } + } + + fn raw_values(width: u8, seed: u64) -> Vec { + let mask = mask_for_width(width); + let mut state = seed; + (0..BitPacker8x::BLOCK_LEN) + .map(|idx| { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + match seed % 4 { + 0 => 0, + 1 => mask, + 2 => idx as u32 & mask, + _ => state as u32 & mask, + } + }) + .collect() + } + + fn sorted_values(width: u8, seed: u64) -> (u32, Vec) { + if width == 0 { + return (17, vec![17; BitPacker8x::BLOCK_LEN]); + } + if width == 32 { + return (0, vec![u32::MAX; BitPacker8x::BLOCK_LEN]); + } + + let mask = mask_for_width(width).min(127); + let mut state = seed; + let mut current = 17u32; + let values = (0..BitPacker8x::BLOCK_LEN) + .map(|_| { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + current += state as u32 & mask; + current + }) + .collect(); + (17, values) + } + + fn strictly_sorted_values(width: u8, seed: u64) -> (Option, Vec) { + let mask = mask_for_width(width).min(127); + let mut state = seed; + let mut current = 0u32; + let values = (0..BitPacker8x::BLOCK_LEN) + .map(|idx| { + if idx == 0 { + current = 0; + } else { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + current += 1 + (state as u32 & mask); + } + current + }) + .collect(); + (None, values) + } + + fn assert_raw_compatible(ours: BitPacker8x, external: ExternalBitPacker8x) { + for width in 0..=32 { + for seed in [0, 1, 2, 123456789] { + let values = raw_values(width, seed); + assert_eq!(ours.num_bits(&values), external.num_bits(&values)); + + let mut actual = vec![0u8; BitPacker8x::compressed_block_size(width)]; + let actual_len = ours.compress(&values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker8x::compressed_block_size(width)]; + let expected_len = external.compress(&values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "raw width {width} seed {seed}"); + + let mut decoded = vec![0u32; BitPacker8x::BLOCK_LEN]; + assert_eq!(ours.decompress(&actual, &mut decoded, width), actual_len); + assert_eq!(decoded, values); + } + } + } + + fn assert_sorted_compatible(ours: BitPacker8x, external: ExternalBitPacker8x) { + for width in 0..=32 { + for seed in [0, 1, 2, 123456789] { + let (initial, values) = sorted_values(width, seed); + assert_eq!( + ours.num_bits_sorted(initial, &values), + external.num_bits_sorted(initial, &values) + ); + + let mut actual = vec![0u8; BitPacker8x::compressed_block_size(width)]; + let actual_len = ours.compress_sorted(initial, &values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker8x::compressed_block_size(width)]; + let expected_len = external.compress_sorted(initial, &values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "sorted width {width} seed {seed}"); + + let mut decoded = vec![0u32; BitPacker8x::BLOCK_LEN]; + assert_eq!( + ours.decompress_sorted(initial, &actual, &mut decoded, width), + actual_len + ); + assert_eq!(decoded, values); + } + } + } + + fn assert_strictly_sorted_compatible(ours: BitPacker8x, external: ExternalBitPacker8x) { + for width in 0..=16 { + for seed in [0, 1, 2, 123456789] { + let (initial, values) = strictly_sorted_values(width, seed); + let num_bits = external.num_bits_strictly_sorted(initial, &values); + assert_eq!(ours.num_bits_strictly_sorted(initial, &values), num_bits); + + let mut actual = vec![0u8; BitPacker8x::compressed_block_size(num_bits)]; + let actual_len = + ours.compress_strictly_sorted(initial, &values, &mut actual, num_bits); + + let mut expected = vec![0u8; ExternalBitPacker8x::compressed_block_size(num_bits)]; + let expected_len = + external.compress_strictly_sorted(initial, &values, &mut expected, num_bits); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "strict width {width} seed {seed}"); + + let mut decoded = vec![0u32; BitPacker8x::BLOCK_LEN]; + assert_eq!( + ours.decompress_strictly_sorted(initial, &actual, &mut decoded, num_bits), + actual_len + ); + assert_eq!(decoded, values); + } + } + } + + #[test] + fn bitpacker8x_raw_compatible_with_external_bitpacking() { + assert_raw_compatible(BitPacker8x::new(), ExternalBitPacker8x::new()); + } + + #[test] + fn bitpacker8x_sorted_compatible_with_external_bitpacking() { + assert_sorted_compatible(BitPacker8x::new(), ExternalBitPacker8x::new()); + } + + #[test] + fn scalar_backend_matches_external_bitpacker8x() { + let scalar = BitPacker8x::new_scalar(); + let external = ExternalBitPacker8x::new(); + + assert_raw_compatible(scalar, external); + assert_sorted_compatible(scalar, external); + } + + #[test] + fn scalar_backend_matches_external_strictly_sorted_bitpacker8x() { + let scalar = BitPacker8x::new_scalar(); + let external = ExternalBitPacker8x::new(); + + assert_strictly_sorted_compatible(scalar, external); + } + + #[cfg(all(target_arch = "aarch64", target_endian = "little"))] + #[test] + fn neon_backend_matches_external_bitpacker8x() { + if let Some(neon) = BitPacker8x::new_neon() { + let external = ExternalBitPacker8x::new(); + + assert_raw_compatible(neon, external); + assert_sorted_compatible(neon, external); + assert_strictly_sorted_compatible(neon, external); + } + } +} diff --git a/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/macros.rs b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/macros.rs new file mode 100644 index 000000000..e79686fe9 --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/macros.rs @@ -0,0 +1,669 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +macro_rules! pack_unpack_with_bits { + + ($name:ident, $n:expr, $cpufeature:meta) => { + + + mod $name { + + use crunchy::unroll; + use super::BLOCK_LEN; + use super::{Sink, Transformer}; + use super::{DataType, + set1, + right_shift_32, + left_shift_32, + op_or, + op_and, + load_unaligned, + store_unaligned}; + + const NUM_BITS: usize = $n; + const NUM_BYTES_PER_BLOCK: usize = NUM_BITS * BLOCK_LEN / 8; + + #[$cpufeature] + pub(crate) unsafe fn pack(input_arr: &[u32], output_arr: &mut [u8], mut delta_computer: TDeltaComputer) -> usize { + assert_eq!(input_arr.len(), BLOCK_LEN, "Input block too small {}, (expected {})", input_arr.len(), BLOCK_LEN); + assert!(output_arr.len() >= NUM_BYTES_PER_BLOCK, "Output array too small (numbits {}). {} <= {}", NUM_BITS, output_arr.len(), NUM_BYTES_PER_BLOCK); + + let input_ptr = input_arr.as_ptr().cast::(); + let mut output_ptr = output_arr.as_mut_ptr().cast::(); + let mut out_register: DataType = delta_computer.transform(load_unaligned(input_ptr)); + + unroll! { + for iter in 0..30 { + const i: usize = 1 + iter; + + const bits_filled: usize = i * NUM_BITS; + const inner_cursor: usize = bits_filled % 32; + const remaining: usize = 32 - inner_cursor; + + let offset_ptr = input_ptr.add(i); + let in_register: DataType = delta_computer.transform(load_unaligned(offset_ptr)); + + out_register = + if inner_cursor > 0 { + op_or(out_register, left_shift_32::<{inner_cursor as i32}>(in_register)) + } else { + in_register + }; + + if remaining <= NUM_BITS { + store_unaligned(output_ptr, out_register); + output_ptr = output_ptr.add(1); + if 0 < remaining && remaining < NUM_BITS { + out_register = right_shift_32::<{remaining as i32}>(in_register); + } + } + } + } + let in_register: DataType = delta_computer.transform(load_unaligned(input_ptr.add(31))); + out_register = if 32 - NUM_BITS > 0 { + op_or(out_register, left_shift_32::<{32 - NUM_BITS as i32}>(in_register)) + } else { + op_or(out_register, in_register) + }; + store_unaligned(output_ptr, out_register); + + NUM_BYTES_PER_BLOCK + } + + #[$cpufeature] + pub(crate) unsafe fn unpack(compressed: &[u8], mut output: Output) -> usize { + + assert!(compressed.len() >= NUM_BYTES_PER_BLOCK, "Compressed array seems too small. ({} < {}) ", compressed.len(), NUM_BYTES_PER_BLOCK); + + let mut input_ptr = compressed.as_ptr().cast::(); + + let mask_scalar: u32 = ((1u64 << NUM_BITS) - 1u64) as u32; + let mask = set1(mask_scalar as i32); + + let mut in_register: DataType = load_unaligned(input_ptr); + + let out_register = op_and(in_register, mask); + output.process(out_register); + + unroll! { + for iter in 0..31 { + const i: usize = iter + 1; + + const inner_cursor: usize = (i * NUM_BITS) % 32; + const inner_capacity: usize = 32 - inner_cursor; + + let shifted_in_register = if inner_cursor != 0 { + right_shift_32::<{inner_cursor as i32}>(in_register) + } else { + in_register + }; + let mut out_register: DataType = op_and(shifted_in_register, mask); + + // We consumed our current quadruplets entirely. + // We therefore read another one. + if inner_capacity <= NUM_BITS && i != 31 { + input_ptr = input_ptr.add(1); + in_register = load_unaligned(input_ptr); + + // This quadruplets is actually cutting one of + // our `DataType`. We need to read the next one. + if inner_capacity < NUM_BITS { + let shifted = if inner_capacity != 0 { + left_shift_32::<{inner_capacity as i32}>(in_register) + } else { + in_register + }; + let masked = op_and(shifted, mask); + out_register = op_or(out_register, masked); + } + } + + output.process(out_register); + } + } + + + NUM_BYTES_PER_BLOCK + } + } + } +} + +macro_rules! pack_unpack_with_bits_32 { + ($cpufeature:meta) => { + mod pack_unpack_with_bits_32 { + use super::BLOCK_LEN; + use super::{DataType, load_unaligned, store_unaligned}; + use super::{Sink, Transformer}; + use crunchy::unroll; + + const NUM_BITS: usize = 32; + const NUM_BYTES_PER_BLOCK: usize = NUM_BITS * BLOCK_LEN / 8; + + #[$cpufeature] + pub(crate) unsafe fn pack( + input_arr: &[u32], + output_arr: &mut [u8], + mut delta_computer: TDeltaComputer, + ) -> usize { + assert_eq!( + input_arr.len(), + BLOCK_LEN, + "Input block too small {}, (expected {})", + input_arr.len(), + BLOCK_LEN + ); + assert!( + output_arr.len() >= NUM_BYTES_PER_BLOCK, + "Output array too small (numbits {}). {} <= {}", + NUM_BITS, + output_arr.len(), + NUM_BYTES_PER_BLOCK + ); + + let input_ptr: *const DataType = input_arr.as_ptr().cast::(); + let output_ptr = output_arr.as_mut_ptr().cast::(); + unroll! { + for i in 0..32 { + let input_offset_ptr = input_ptr.add(i); + let output_offset_ptr = output_ptr.add(i); + let input_register = load_unaligned(input_offset_ptr); + let output_register = delta_computer.transform(input_register); + store_unaligned(output_offset_ptr, output_register); + } + } + NUM_BYTES_PER_BLOCK + } + + #[$cpufeature] + pub(crate) unsafe fn unpack( + compressed: &[u8], + mut output: Output, + ) -> usize { + assert!( + compressed.len() >= NUM_BYTES_PER_BLOCK, + "Compressed array seems too small. ({} < {}) ", + compressed.len(), + NUM_BYTES_PER_BLOCK + ); + let input_ptr = compressed.as_ptr().cast::(); + for i in 0..32 { + let input_offset_ptr = input_ptr.add(i); + let in_register: DataType = load_unaligned(input_offset_ptr); + output.process(in_register); + } + NUM_BYTES_PER_BLOCK + } + } + }; +} + +macro_rules! declare_bitpacker { + ($cpufeature:meta) => { + use super::super::UnsafeBitPacker; + use super::super::most_significant_bit; + use crunchy::unroll; + + pack_unpack_with_bits!(pack_unpack_with_bits_1, 1, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_2, 2, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_3, 3, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_4, 4, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_5, 5, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_6, 6, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_7, 7, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_8, 8, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_9, 9, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_10, 10, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_11, 11, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_12, 12, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_13, 13, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_14, 14, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_15, 15, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_16, 16, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_17, 17, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_18, 18, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_19, 19, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_20, 20, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_21, 21, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_22, 22, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_23, 23, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_24, 24, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_25, 25, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_26, 26, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_27, 27, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_28, 28, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_29, 29, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_30, 30, $cpufeature); + pack_unpack_with_bits!(pack_unpack_with_bits_31, 31, $cpufeature); + pack_unpack_with_bits_32!($cpufeature); + + unsafe fn compress_generic( + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + delta_computer: DeltaComputer, + ) -> usize { + match num_bits { + 0 => 0, + 1 => pack_unpack_with_bits_1::pack(decompressed, compressed, delta_computer), + 2 => pack_unpack_with_bits_2::pack(decompressed, compressed, delta_computer), + 3 => pack_unpack_with_bits_3::pack(decompressed, compressed, delta_computer), + 4 => pack_unpack_with_bits_4::pack(decompressed, compressed, delta_computer), + 5 => pack_unpack_with_bits_5::pack(decompressed, compressed, delta_computer), + 6 => pack_unpack_with_bits_6::pack(decompressed, compressed, delta_computer), + 7 => pack_unpack_with_bits_7::pack(decompressed, compressed, delta_computer), + 8 => pack_unpack_with_bits_8::pack(decompressed, compressed, delta_computer), + 9 => pack_unpack_with_bits_9::pack(decompressed, compressed, delta_computer), + 10 => pack_unpack_with_bits_10::pack(decompressed, compressed, delta_computer), + 11 => pack_unpack_with_bits_11::pack(decompressed, compressed, delta_computer), + 12 => pack_unpack_with_bits_12::pack(decompressed, compressed, delta_computer), + 13 => pack_unpack_with_bits_13::pack(decompressed, compressed, delta_computer), + 14 => pack_unpack_with_bits_14::pack(decompressed, compressed, delta_computer), + 15 => pack_unpack_with_bits_15::pack(decompressed, compressed, delta_computer), + 16 => pack_unpack_with_bits_16::pack(decompressed, compressed, delta_computer), + 17 => pack_unpack_with_bits_17::pack(decompressed, compressed, delta_computer), + 18 => pack_unpack_with_bits_18::pack(decompressed, compressed, delta_computer), + 19 => pack_unpack_with_bits_19::pack(decompressed, compressed, delta_computer), + 20 => pack_unpack_with_bits_20::pack(decompressed, compressed, delta_computer), + 21 => pack_unpack_with_bits_21::pack(decompressed, compressed, delta_computer), + 22 => pack_unpack_with_bits_22::pack(decompressed, compressed, delta_computer), + 23 => pack_unpack_with_bits_23::pack(decompressed, compressed, delta_computer), + 24 => pack_unpack_with_bits_24::pack(decompressed, compressed, delta_computer), + 25 => pack_unpack_with_bits_25::pack(decompressed, compressed, delta_computer), + 26 => pack_unpack_with_bits_26::pack(decompressed, compressed, delta_computer), + 27 => pack_unpack_with_bits_27::pack(decompressed, compressed, delta_computer), + 28 => pack_unpack_with_bits_28::pack(decompressed, compressed, delta_computer), + 29 => pack_unpack_with_bits_29::pack(decompressed, compressed, delta_computer), + 30 => pack_unpack_with_bits_30::pack(decompressed, compressed, delta_computer), + 31 => pack_unpack_with_bits_31::pack(decompressed, compressed, delta_computer), + 32 => pack_unpack_with_bits_32::pack(decompressed, compressed, delta_computer), + _ => { + panic!("Num bits must be <= 32. Was {}.", num_bits); + } + } + } + + pub trait Transformer { + unsafe fn transform(&mut self, data: DataType) -> DataType; + } + + struct NoDelta; + + impl Transformer for NoDelta { + #[inline] + unsafe fn transform(&mut self, current: DataType) -> DataType { + current + } + } + + struct DeltaComputer { + pub previous: DataType, + } + + impl Transformer for DeltaComputer { + #[inline] + unsafe fn transform(&mut self, current: DataType) -> DataType { + let result = compute_delta(current, self.previous); + self.previous = current; + result + } + } + + struct StrictDeltaComputer { + pub previous: DataType, + } + + impl Transformer for StrictDeltaComputer { + #[inline] + unsafe fn transform(&mut self, current: DataType) -> DataType { + let result = compute_delta(current, self.previous); + self.previous = current; + sub(result, set1(1)) + } + } + + pub trait Sink { + unsafe fn process(&mut self, data_type: DataType); + } + + struct Store { + output_ptr: *mut DataType, + } + + impl Store { + fn new(output_ptr: *mut DataType) -> Store { + Store { output_ptr } + } + } + + struct DeltaIntegrate { + current: DataType, + output_ptr: *mut DataType, + } + + impl DeltaIntegrate { + unsafe fn new(initial: u32, output_ptr: *mut DataType) -> DeltaIntegrate { + DeltaIntegrate { + current: set1(initial as i32), + output_ptr, + } + } + } + + impl Sink for DeltaIntegrate { + #[inline] + unsafe fn process(&mut self, delta: DataType) { + self.current = integrate_delta(self.current, delta); + store_unaligned(self.output_ptr, self.current); + self.output_ptr = self.output_ptr.add(1); + } + } + + struct StrictDeltaIntegrate { + current: DataType, + output_ptr: *mut DataType, + } + + impl StrictDeltaIntegrate { + unsafe fn new(initial: u32, output_ptr: *mut DataType) -> StrictDeltaIntegrate { + StrictDeltaIntegrate { + current: set1(initial as i32), + output_ptr, + } + } + } + + impl Sink for StrictDeltaIntegrate { + #[inline] + unsafe fn process(&mut self, delta: DataType) { + self.current = integrate_delta(self.current, add(delta, set1(1))); + store_unaligned(self.output_ptr, self.current); + self.output_ptr = self.output_ptr.add(1); + } + } + + impl Sink for Store { + #[inline] + unsafe fn process(&mut self, out_register: DataType) { + store_unaligned(self.output_ptr, out_register); + self.output_ptr = self.output_ptr.add(1); + } + } + + #[inline] + unsafe fn decompress_to( + compressed: &[u8], + mut sink: Output, + num_bits: u8, + ) -> usize { + match num_bits { + 0 => { + let zero = set1(0i32); + for _ in 0..32 { + sink.process(zero); + } + 0 + } + 1 => pack_unpack_with_bits_1::unpack(compressed, sink), + 2 => pack_unpack_with_bits_2::unpack(compressed, sink), + 3 => pack_unpack_with_bits_3::unpack(compressed, sink), + 4 => pack_unpack_with_bits_4::unpack(compressed, sink), + 5 => pack_unpack_with_bits_5::unpack(compressed, sink), + 6 => pack_unpack_with_bits_6::unpack(compressed, sink), + 7 => pack_unpack_with_bits_7::unpack(compressed, sink), + 8 => pack_unpack_with_bits_8::unpack(compressed, sink), + 9 => pack_unpack_with_bits_9::unpack(compressed, sink), + 10 => pack_unpack_with_bits_10::unpack(compressed, sink), + 11 => pack_unpack_with_bits_11::unpack(compressed, sink), + 12 => pack_unpack_with_bits_12::unpack(compressed, sink), + 13 => pack_unpack_with_bits_13::unpack(compressed, sink), + 14 => pack_unpack_with_bits_14::unpack(compressed, sink), + 15 => pack_unpack_with_bits_15::unpack(compressed, sink), + 16 => pack_unpack_with_bits_16::unpack(compressed, sink), + 17 => pack_unpack_with_bits_17::unpack(compressed, sink), + 18 => pack_unpack_with_bits_18::unpack(compressed, sink), + 19 => pack_unpack_with_bits_19::unpack(compressed, sink), + 20 => pack_unpack_with_bits_20::unpack(compressed, sink), + 21 => pack_unpack_with_bits_21::unpack(compressed, sink), + 22 => pack_unpack_with_bits_22::unpack(compressed, sink), + 23 => pack_unpack_with_bits_23::unpack(compressed, sink), + 24 => pack_unpack_with_bits_24::unpack(compressed, sink), + 25 => pack_unpack_with_bits_25::unpack(compressed, sink), + 26 => pack_unpack_with_bits_26::unpack(compressed, sink), + 27 => pack_unpack_with_bits_27::unpack(compressed, sink), + 28 => pack_unpack_with_bits_28::unpack(compressed, sink), + 29 => pack_unpack_with_bits_29::unpack(compressed, sink), + 30 => pack_unpack_with_bits_30::unpack(compressed, sink), + 31 => pack_unpack_with_bits_31::unpack(compressed, sink), + 32 => pack_unpack_with_bits_32::unpack(compressed, sink), + _ => { + panic!("Num bits must be <= 32. Was {}.", num_bits); + } + } + } + + pub struct UnsafeBitPackerImpl; + + impl UnsafeBitPacker for UnsafeBitPackerImpl { + const BLOCK_LEN: usize = BLOCK_LEN; + + #[$cpufeature] + unsafe fn compress(decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize { + compress_generic(decompressed, compressed, num_bits, NoDelta) + } + + #[$cpufeature] + unsafe fn compress_sorted( + initial: u32, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + let delta_computer = DeltaComputer { + previous: set1(initial as i32), + }; + compress_generic(decompressed, compressed, num_bits, delta_computer) + } + + #[$cpufeature] + unsafe fn compress_strictly_sorted( + initial: Option, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize { + // to allow encoding [0, 1, 2, ..], we need to permit an initial value "lower" than + // zero. To get a clean api, that value is None, but in practice, as we work on + // wrapping integers, u32::MAX/-1 does the job just fine. + let initial = initial.unwrap_or(u32::MAX); + let delta_computer = StrictDeltaComputer { + previous: set1(initial as i32), + }; + compress_generic(decompressed, compressed, num_bits, delta_computer) + } + + #[$cpufeature] + unsafe fn decompress( + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + assert!( + decompressed.len() >= BLOCK_LEN, + "The output array is not large enough : ({} >= {})", + decompressed.len(), + BLOCK_LEN + ); + let output_ptr = decompressed.as_mut_ptr().cast::(); + let output = Store::new(output_ptr); + decompress_to(compressed, output, num_bits) + } + + #[$cpufeature] + unsafe fn decompress_sorted( + initial: u32, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + assert!( + decompressed.len() >= BLOCK_LEN, + "The output array is not large enough : ({} >= {})", + decompressed.len(), + BLOCK_LEN + ); + let output_ptr = decompressed.as_mut_ptr().cast::(); + let output = DeltaIntegrate::new(initial, output_ptr); + decompress_to(compressed, output, num_bits) + } + + #[$cpufeature] + unsafe fn decompress_strictly_sorted( + initial: Option, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize { + assert!( + decompressed.len() >= BLOCK_LEN, + "The output array is not large enough : ({} >= {})", + decompressed.len(), + BLOCK_LEN + ); + let initial = initial.unwrap_or(u32::MAX); + let output_ptr = decompressed.as_mut_ptr().cast::(); + let output = StrictDeltaIntegrate::new(initial, output_ptr); + decompress_to(compressed, output, num_bits) + } + + #[$cpufeature] + unsafe fn num_bits(decompressed: &[u32]) -> u8 { + assert_eq!( + decompressed.len(), + BLOCK_LEN, + "`decompressed`'s len is not `BLOCK_LEN={}`", + BLOCK_LEN + ); + let data: *const DataType = decompressed.as_ptr().cast::(); + let mut accumulator = load_unaligned(data); + unroll! { + for iter in 0..31 { + let i = iter + 1; + let newvec = load_unaligned(data.add(i)); + accumulator = op_or(accumulator, newvec); + } + } + most_significant_bit(or_collapse_to_u32(accumulator)) + } + + #[$cpufeature] + unsafe fn num_bits_sorted(initial: u32, decompressed: &[u32]) -> u8 { + assert_eq!( + decompressed.len(), + BLOCK_LEN, + "`decompressed`'s len is not `BLOCK_LEN={}`", + BLOCK_LEN + ); + let initial_vec = set1(initial as i32); + let data: *const DataType = decompressed.as_ptr().cast::(); + + let first = load_unaligned(data); + let mut accumulator = compute_delta(load_unaligned(data), initial_vec); + let mut previous = first; + + unroll! { + for iter in 0..30 { + let i = iter + 1; + let current = load_unaligned(data.add(i)); + let delta = compute_delta(current, previous); + accumulator = op_or(accumulator, delta); + previous = current; + } + } + let current = load_unaligned(data.add(31)); + let delta = compute_delta(current, previous); + accumulator = op_or(accumulator, delta); + most_significant_bit(or_collapse_to_u32(accumulator)) + } + + #[$cpufeature] + unsafe fn num_bits_strictly_sorted(initial: Option, decompressed: &[u32]) -> u8 { + assert_eq!( + decompressed.len(), + BLOCK_LEN, + "`decompressed`'s len is not `BLOCK_LEN={}`", + BLOCK_LEN + ); + let initial = initial.unwrap_or(u32::MAX); + let initial_vec = set1(initial as i32); + let one = set1(1); + let data: *const DataType = decompressed.as_ptr().cast::(); + + let first = load_unaligned(data); + let mut accumulator = sub(compute_delta(load_unaligned(data), initial_vec), one); + let mut previous = first; + + unroll! { + for iter in 0..30 { + let i = iter + 1; + let current = load_unaligned(data.add(i)); + let delta = sub(compute_delta(current, previous), one); + accumulator = op_or(accumulator, delta); + previous = current; + } + } + let current = load_unaligned(data.add(31)); + let delta = sub(compute_delta(current, previous), one); + accumulator = op_or(accumulator, delta); + most_significant_bit(or_collapse_to_u32(accumulator)) + } + } + + #[cfg(any())] + mod tests { + use super::super::UnsafeBitPacker; + use super::UnsafeBitPackerImpl; + use crate::Available; + use crate::tests::{DeltaKind, test_suite_compress_decompress}; + + #[test] + fn test_num_bits() { + if UnsafeBitPackerImpl::available() { + for num_bits in 0..32 { + for pos in 0..32 { + let mut vals = [0u32; UnsafeBitPackerImpl::BLOCK_LEN]; + if num_bits > 0 { + vals[pos] = 1 << (num_bits - 1); + } + assert_eq!( + unsafe { UnsafeBitPackerImpl::num_bits(&vals[..]) }, + num_bits + ); + } + } + } + } + + #[test] + fn test_bitpacker() { + if UnsafeBitPackerImpl::available() { + test_suite_compress_decompress::(DeltaKind::NoDelta); + } + } + + #[test] + fn test_bitpacker_delta() { + if UnsafeBitPackerImpl::available() { + test_suite_compress_decompress::(DeltaKind::Delta); + } + } + + #[test] + fn test_bitpacker_strict_delta() { + if UnsafeBitPackerImpl::available() { + test_suite_compress_decompress::(DeltaKind::StrictDelta); + } + } + } + }; +} diff --git a/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/mod.rs b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/mod.rs new file mode 100644 index 000000000..80803e50e --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/src/bitpacker_internal/mod.rs @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +// Lance-owned u32 SIMD bitpacking kernels. +// +// This is adapted from the MIT-licensed `bitpacking` crate so Lance can keep the +// hot FTS posting-list bitpacking implementation inside lance-bitpacking while +// preserving byte compatibility with the existing 4x format. + +#![allow(dead_code)] +#![allow(unsafe_op_in_unsafe_fn)] +#![allow(clippy::redundant_pub_crate)] +#![allow(clippy::upper_case_acronyms)] +#![allow(clippy::use_self)] + +#[macro_use] +mod macros; + +mod bitpacker4x; +mod bitpacker8x; + +pub use bitpacker4x::BitPacker4x; +pub use bitpacker8x::BitPacker8x; + +pub(crate) trait Available { + fn available() -> bool; +} + +pub(crate) trait UnsafeBitPacker { + const BLOCK_LEN: usize; + + unsafe fn compress(decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize; + + unsafe fn compress_sorted( + initial: u32, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize; + + unsafe fn compress_strictly_sorted( + initial: Option, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize; + + unsafe fn decompress(compressed: &[u8], decompressed: &mut [u32], num_bits: u8) -> usize; + + unsafe fn decompress_sorted( + initial: u32, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize; + + unsafe fn decompress_strictly_sorted( + initial: Option, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize; + + unsafe fn num_bits(decompressed: &[u32]) -> u8; + + unsafe fn num_bits_sorted(initial: u32, decompressed: &[u32]) -> u8; + + unsafe fn num_bits_strictly_sorted(initial: Option, decompressed: &[u32]) -> u8; +} + +/// Block bitpacker for fixed-size `u32` blocks. +/// +/// Implementations own runtime SIMD dispatch and use caller-provided buffers. +/// Packed bytes are stable for a given implementation and bit width. +pub trait BitPacker: Sized + Clone + Copy { + /// Number of `u32` values in one physical block. + const BLOCK_LEN: usize; + + /// Select the best supported implementation for the current CPU. + /// + /// Lance uses SIMD backends when available and falls back to a scalar + /// backend otherwise, matching the existing allocation-free call shape used + /// by the upstream `bitpacking` crate. + fn new() -> Self; + + /// Compress one full block of raw values into `compressed`. + fn compress(&self, decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize; + + /// Delta-compress one full non-decreasing block into `compressed`. + fn compress_sorted( + &self, + initial: u32, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize; + + /// Delta-compress one full strictly increasing block into `compressed`. + fn compress_strictly_sorted( + &self, + initial: Option, + decompressed: &[u32], + compressed: &mut [u8], + num_bits: u8, + ) -> usize; + + /// Decompress one raw block into `decompressed`. + fn decompress(&self, compressed: &[u8], decompressed: &mut [u32], num_bits: u8) -> usize; + + /// Decompress one delta-compressed non-decreasing block. + fn decompress_sorted( + &self, + initial: u32, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize; + + /// Decompress one delta-compressed strictly increasing block. + fn decompress_strictly_sorted( + &self, + initial: Option, + compressed: &[u8], + decompressed: &mut [u32], + num_bits: u8, + ) -> usize; + + /// Return the minimum bit width needed to represent a full raw block. + fn num_bits(&self, decompressed: &[u32]) -> u8; + + /// Return the minimum bit width needed to represent deltas in a full block. + fn num_bits_sorted(&self, initial: u32, decompressed: &[u32]) -> u8; + + /// Return the minimum bit width needed to represent strict deltas in a full block. + fn num_bits_strictly_sorted(&self, initial: Option, decompressed: &[u32]) -> u8; + + /// Return the byte size of one compressed block at `num_bits`. + #[must_use] + fn compressed_block_size(num_bits: u8) -> usize { + Self::BLOCK_LEN * num_bits as usize / 8 + } +} + +#[inline] +fn most_significant_bit(value: u32) -> u8 { + (u32::BITS - value.leading_zeros()) as u8 +} diff --git a/lance-artifact/rust/compression/bitpacking/src/lib.rs b/lance-artifact/rust/compression/bitpacking/src/lib.rs new file mode 100644 index 000000000..c6aa6d75a --- /dev/null +++ b/lance-artifact/rust/compression/bitpacking/src/lib.rs @@ -0,0 +1,2278 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +// NOTICE: +// This file is a modification of the `fastlanes` crate: https://github.com/spiraldb/fastlanes +// It is modified to allow a rust stable build +// +// The original code can be accessed at +// https://github.com/spiraldb/fastlanes/blob/8e0ff374f815d919d0c0ebdccf5ffd9e6dc7d663/src/bitpacking.rs +// https://github.com/spiraldb/fastlanes/blob/8e0ff374f815d919d0c0ebdccf5ffd9e6dc7d663/src/lib.rs +// https://github.com/spiraldb/fastlanes/blob/8e0ff374f815d919d0c0ebdccf5ffd9e6dc7d663/src/macros.rs +// +// The original code is licensed under the Apache Software License: +// https://github.com/spiraldb/fastlanes/blob/8e0ff374f815d919d0c0ebdccf5ffd9e6dc7d663/LICENSE + +use arrayref::{array_mut_ref, array_ref}; +use core::mem::size_of; + +mod bitpacker_internal; + +pub use bitpacker_internal::{BitPacker, BitPacker4x, BitPacker8x}; + +pub const FL_ORDER: [usize; 8] = [0, 4, 2, 6, 1, 5, 3, 7]; + +pub trait FastLanes: Sized + Copy { + const T: usize = size_of::() * 8; + const LANES: usize = 1024 / Self::T; +} + +// Implement the trait for basic unsigned integer types +impl FastLanes for u8 {} +impl FastLanes for u16 {} +impl FastLanes for u32 {} +impl FastLanes for u64 {} + +macro_rules! pack { + ($T:ty, $W:expr, $packed:expr, $lane:expr, | $_1:tt $idx:ident | $($body:tt)*) => { + macro_rules! __kernel__ {( $_1 $idx:ident ) => ( $($body)* )} + { + use paste::paste; + + // The number of bits of T. + const T: usize = <$T>::T; + + #[inline(always)] + fn index(row: usize, lane: usize) -> usize { + let o = row / 8; + let s = row % 8; + (FL_ORDER[o] * 16) + (s * 128) + lane + } + + if $W == 0 { + // Nothing to do if W is 0, since the packed array is zero bytes. + } else if $W == T { + // Special case for W=T, we can just copy the input value directly to the packed value. + paste!(seq_t!(row in $T { + let idx = index(row, $lane); + $packed[<$T>::LANES * row + $lane] = __kernel__!(idx); + })); + } else { + // A mask of W bits. + let mask: $T = (1 << $W) - 1; + + // First we loop over each lane in the virtual 1024 bit word. + let mut tmp: $T = 0; + + // Loop over each of the rows of the lane. + // Inlining this loop means all branches are known at compile time and + // the code is auto-vectorized for SIMD execution. + paste!(seq_t!(row in $T { + let idx = index(row, $lane); + let src = __kernel__!(idx); + let src = src & mask; + + // Shift the src bits into their position in the tmp output variable. + if row == 0 { + tmp = src; + } else { + tmp |= src << (row * $W) % T; + } + + // If the next packed position is after our current one, then we have filled + // the current output and we can write the packed value. + let curr_word: usize = (row * $W) / T; + let next_word: usize = ((row + 1) * $W) / T; + + #[allow(unused_assignments)] + if next_word > curr_word { + $packed[<$T>::LANES * curr_word + $lane] = tmp; + let remaining_bits: usize = ((row + 1) * $W) % T; + // Keep the remaining bits for the next packed value. + tmp = src >> $W - remaining_bits; + } + })); + } + } + }; +} + +macro_rules! unpack { + ($T:ty, $W:expr, $packed:expr, $lane:expr, | $_1:tt $idx:ident, $_2:tt $elem:ident | $($body:tt)*) => { + macro_rules! __kernel__ {( $_1 $idx:ident, $_2 $elem:ident ) => ( $($body)* )} + { + use paste::paste; + + // The number of bits of T. + const T: usize = <$T>::T; + + #[inline(always)] + fn index(row: usize, lane: usize) -> usize { + let o = row / 8; + let s = row % 8; + (FL_ORDER[o] * 16) + (s * 128) + lane + } + + if $W == 0 { + // Special case for W=0, we just need to zero the output. + // We'll still respect the iteration order in case the kernel has side effects. + paste!(seq_t!(row in $T { + let idx = index(row, $lane); + let zero: $T = 0; + __kernel__!(idx, zero); + })); + } else if $W == T { + // Special case for W=T, we can just copy the packed value directly to the output. + paste!(seq_t!(row in $T { + let idx = index(row, $lane); + let src = $packed[<$T>::LANES * row + $lane]; + __kernel__!(idx, src); + })); + } else { + #[inline] + fn mask(width: usize) -> $T { + if width == T { <$T>::MAX } else { (1 << (width % T)) - 1 } + } + + let mut src: $T = $packed[$lane]; + let mut tmp: $T; + + paste!(seq_t!(row in $T { + // Figure out the packed positions + let curr_word: usize = (row * $W) / T; + let next_word = ((row + 1) * $W) / T; + + let shift = (row * $W) % T; + + if next_word > curr_word { + // Consume some bits from the curr packed input, the remainder are in the next + // packed input value + let remaining_bits = ((row + 1) * $W) % T; + let current_bits = $W - remaining_bits; + tmp = (src >> shift) & mask(current_bits); + + if next_word < $W { + // Load the next packed value + src = $packed[<$T>::LANES * next_word + $lane]; + // Consume the remaining bits from the next input value. + tmp |= (src & mask(remaining_bits)) << current_bits; + } + } else { + // Otherwise, just grab W bits from the src value + tmp = (src >> shift) & mask($W); + } + + // Write out the unpacked value + let idx = index(row, $lane); + __kernel__!(idx, tmp); + })); + } + } + }; +} + +// Macro for repeating a code block bit_size_of:: times. +macro_rules! seq_t { + ($ident:ident in u8 $body:tt) => {seq_macro::seq!($ident in 0..8 $body)}; + ($ident:ident in u16 $body:tt) => {seq_macro::seq!($ident in 0..16 $body)}; + ($ident:ident in u32 $body:tt) => {seq_macro::seq!($ident in 0..32 $body)}; + ($ident:ident in u64 $body:tt) => {seq_macro::seq!($ident in 0..64 $body)}; +} + +/// `BitPack` into a compile-time known bit-width. +pub trait BitPacking: FastLanes { + /// Packs 1024 elements into `W` bits each, where `W` is runtime-known instead of + /// compile-time known. + /// + /// # Safety + /// The input slice must be of exactly length 1024. The output slice must be of length + /// `1024 * W / T`, where `T` is the bit-width of Self and `W` is the packed width. + /// These lengths are checked only with `debug_assert` (i.e., not checked on release builds). + unsafe fn unchecked_pack(width: usize, input: &[Self], output: &mut [Self]); + + /// Unpacks 1024 elements from `W` bits each, where `W` is runtime-known instead of + /// compile-time known. + /// + /// # Safety + /// The input slice must be of length `1024 * W / T`, where `T` is the bit-width of Self and `W` + /// is the packed width. The output slice must be of exactly length 1024. + /// These lengths are checked only with `debug_assert` (i.e., not checked on release builds). + unsafe fn unchecked_unpack(width: usize, input: &[Self], output: &mut [Self]); +} + +impl BitPacking for u8 { + unsafe fn unchecked_pack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + output.len(), + packed_len, + "Output buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(input.len(), 1024, "Input buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + // Nothing to write when width is zero. + } + 1 => pack_8_1( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 / 8], + ), + 2 => pack_8_2( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 2 / 8], + ), + 3 => pack_8_3( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 3 / 8], + ), + 4 => pack_8_4( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 4 / 8], + ), + 5 => pack_8_5( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 5 / 8], + ), + 6 => pack_8_6( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 6 / 8], + ), + 7 => pack_8_7( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 7 / 8], + ), + 8 => pack_8_8( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 8 / 8], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } + + unsafe fn unchecked_unpack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + input.len(), + packed_len, + "Input buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(output.len(), 1024, "Output buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + // A zero-width packed chunk implies all zeros. + output.fill(0); + } + 1 => unpack_8_1( + array_ref![input, 0, 1024 / 8], + array_mut_ref![output, 0, 1024], + ), + 2 => unpack_8_2( + array_ref![input, 0, 1024 * 2 / 8], + array_mut_ref![output, 0, 1024], + ), + 3 => unpack_8_3( + array_ref![input, 0, 1024 * 3 / 8], + array_mut_ref![output, 0, 1024], + ), + 4 => unpack_8_4( + array_ref![input, 0, 1024 * 4 / 8], + array_mut_ref![output, 0, 1024], + ), + 5 => unpack_8_5( + array_ref![input, 0, 1024 * 5 / 8], + array_mut_ref![output, 0, 1024], + ), + 6 => unpack_8_6( + array_ref![input, 0, 1024 * 6 / 8], + array_mut_ref![output, 0, 1024], + ), + 7 => unpack_8_7( + array_ref![input, 0, 1024 * 7 / 8], + array_mut_ref![output, 0, 1024], + ), + 8 => unpack_8_8( + array_ref![input, 0, 1024 * 8 / 8], + array_mut_ref![output, 0, 1024], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } +} + +impl BitPacking for u16 { + unsafe fn unchecked_pack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + output.len(), + packed_len, + "Output buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(input.len(), 1024, "Input buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + // Nothing to write when width is zero. + } + 1 => pack_16_1( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 / 16], + ), + 2 => pack_16_2( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 2 / 16], + ), + 3 => pack_16_3( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 3 / 16], + ), + 4 => pack_16_4( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 4 / 16], + ), + 5 => pack_16_5( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 5 / 16], + ), + 6 => pack_16_6( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 6 / 16], + ), + 7 => pack_16_7( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 7 / 16], + ), + 8 => pack_16_8( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 8 / 16], + ), + 9 => pack_16_9( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 9 / 16], + ), + + 10 => pack_16_10( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 10 / 16], + ), + 11 => pack_16_11( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 11 / 16], + ), + 12 => pack_16_12( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 12 / 16], + ), + 13 => pack_16_13( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 13 / 16], + ), + 14 => pack_16_14( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 14 / 16], + ), + 15 => pack_16_15( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 15 / 16], + ), + 16 => pack_16_16( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 16 / 16], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } + + unsafe fn unchecked_unpack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + input.len(), + packed_len, + "Input buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(output.len(), 1024, "Output buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + output.fill(0); + } + 1 => unpack_16_1( + array_ref![input, 0, 1024 / 16], + array_mut_ref![output, 0, 1024], + ), + 2 => unpack_16_2( + array_ref![input, 0, 1024 * 2 / 16], + array_mut_ref![output, 0, 1024], + ), + 3 => unpack_16_3( + array_ref![input, 0, 1024 * 3 / 16], + array_mut_ref![output, 0, 1024], + ), + 4 => unpack_16_4( + array_ref![input, 0, 1024 * 4 / 16], + array_mut_ref![output, 0, 1024], + ), + 5 => unpack_16_5( + array_ref![input, 0, 1024 * 5 / 16], + array_mut_ref![output, 0, 1024], + ), + 6 => unpack_16_6( + array_ref![input, 0, 1024 * 6 / 16], + array_mut_ref![output, 0, 1024], + ), + 7 => unpack_16_7( + array_ref![input, 0, 1024 * 7 / 16], + array_mut_ref![output, 0, 1024], + ), + 8 => unpack_16_8( + array_ref![input, 0, 1024 * 8 / 16], + array_mut_ref![output, 0, 1024], + ), + 9 => unpack_16_9( + array_ref![input, 0, 1024 * 9 / 16], + array_mut_ref![output, 0, 1024], + ), + + 10 => unpack_16_10( + array_ref![input, 0, 1024 * 10 / 16], + array_mut_ref![output, 0, 1024], + ), + 11 => unpack_16_11( + array_ref![input, 0, 1024 * 11 / 16], + array_mut_ref![output, 0, 1024], + ), + 12 => unpack_16_12( + array_ref![input, 0, 1024 * 12 / 16], + array_mut_ref![output, 0, 1024], + ), + 13 => unpack_16_13( + array_ref![input, 0, 1024 * 13 / 16], + array_mut_ref![output, 0, 1024], + ), + 14 => unpack_16_14( + array_ref![input, 0, 1024 * 14 / 16], + array_mut_ref![output, 0, 1024], + ), + 15 => unpack_16_15( + array_ref![input, 0, 1024 * 15 / 16], + array_mut_ref![output, 0, 1024], + ), + 16 => unpack_16_16( + array_ref![input, 0, 1024 * 16 / 16], + array_mut_ref![output, 0, 1024], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } +} + +impl BitPacking for u32 { + unsafe fn unchecked_pack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + output.len(), + packed_len, + "Output buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(input.len(), 1024, "Input buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + // Nothing to write when width is zero. + } + 1 => pack_32_1( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 / 32], + ), + 2 => pack_32_2( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 2 / 32], + ), + 3 => pack_32_3( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 3 / 32], + ), + 4 => pack_32_4( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 4 / 32], + ), + 5 => pack_32_5( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 5 / 32], + ), + 6 => pack_32_6( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 6 / 32], + ), + 7 => pack_32_7( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 7 / 32], + ), + 8 => pack_32_8( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 8 / 32], + ), + 9 => pack_32_9( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 9 / 32], + ), + + 10 => pack_32_10( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 10 / 32], + ), + 11 => pack_32_11( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 11 / 32], + ), + 12 => pack_32_12( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 12 / 32], + ), + 13 => pack_32_13( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 13 / 32], + ), + 14 => pack_32_14( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 14 / 32], + ), + 15 => pack_32_15( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 15 / 32], + ), + 16 => pack_32_16( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 16 / 32], + ), + 17 => pack_32_17( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 17 / 32], + ), + 18 => pack_32_18( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 18 / 32], + ), + 19 => pack_32_19( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 19 / 32], + ), + + 20 => pack_32_20( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 20 / 32], + ), + 21 => pack_32_21( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 21 / 32], + ), + 22 => pack_32_22( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 22 / 32], + ), + 23 => pack_32_23( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 23 / 32], + ), + 24 => pack_32_24( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 24 / 32], + ), + 25 => pack_32_25( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 25 / 32], + ), + 26 => pack_32_26( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 26 / 32], + ), + 27 => pack_32_27( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 27 / 32], + ), + 28 => pack_32_28( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 28 / 32], + ), + 29 => pack_32_29( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 29 / 32], + ), + + 30 => pack_32_30( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 30 / 32], + ), + 31 => pack_32_31( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 31 / 32], + ), + 32 => pack_32_32( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 32 / 32], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } + + unsafe fn unchecked_unpack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + input.len(), + packed_len, + "Input buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(output.len(), 1024, "Output buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + output.fill(0); + } + 1 => unpack_32_1( + array_ref![input, 0, 1024 / 32], + array_mut_ref![output, 0, 1024], + ), + 2 => unpack_32_2( + array_ref![input, 0, 1024 * 2 / 32], + array_mut_ref![output, 0, 1024], + ), + 3 => unpack_32_3( + array_ref![input, 0, 1024 * 3 / 32], + array_mut_ref![output, 0, 1024], + ), + 4 => unpack_32_4( + array_ref![input, 0, 1024 * 4 / 32], + array_mut_ref![output, 0, 1024], + ), + 5 => unpack_32_5( + array_ref![input, 0, 1024 * 5 / 32], + array_mut_ref![output, 0, 1024], + ), + 6 => unpack_32_6( + array_ref![input, 0, 1024 * 6 / 32], + array_mut_ref![output, 0, 1024], + ), + 7 => unpack_32_7( + array_ref![input, 0, 1024 * 7 / 32], + array_mut_ref![output, 0, 1024], + ), + 8 => unpack_32_8( + array_ref![input, 0, 1024 * 8 / 32], + array_mut_ref![output, 0, 1024], + ), + 9 => unpack_32_9( + array_ref![input, 0, 1024 * 9 / 32], + array_mut_ref![output, 0, 1024], + ), + + 10 => unpack_32_10( + array_ref![input, 0, 1024 * 10 / 32], + array_mut_ref![output, 0, 1024], + ), + 11 => unpack_32_11( + array_ref![input, 0, 1024 * 11 / 32], + array_mut_ref![output, 0, 1024], + ), + 12 => unpack_32_12( + array_ref![input, 0, 1024 * 12 / 32], + array_mut_ref![output, 0, 1024], + ), + 13 => unpack_32_13( + array_ref![input, 0, 1024 * 13 / 32], + array_mut_ref![output, 0, 1024], + ), + 14 => unpack_32_14( + array_ref![input, 0, 1024 * 14 / 32], + array_mut_ref![output, 0, 1024], + ), + 15 => unpack_32_15( + array_ref![input, 0, 1024 * 15 / 32], + array_mut_ref![output, 0, 1024], + ), + 16 => unpack_32_16( + array_ref![input, 0, 1024 * 16 / 32], + array_mut_ref![output, 0, 1024], + ), + 17 => unpack_32_17( + array_ref![input, 0, 1024 * 17 / 32], + array_mut_ref![output, 0, 1024], + ), + 18 => unpack_32_18( + array_ref![input, 0, 1024 * 18 / 32], + array_mut_ref![output, 0, 1024], + ), + 19 => unpack_32_19( + array_ref![input, 0, 1024 * 19 / 32], + array_mut_ref![output, 0, 1024], + ), + + 20 => unpack_32_20( + array_ref![input, 0, 1024 * 20 / 32], + array_mut_ref![output, 0, 1024], + ), + 21 => unpack_32_21( + array_ref![input, 0, 1024 * 21 / 32], + array_mut_ref![output, 0, 1024], + ), + 22 => unpack_32_22( + array_ref![input, 0, 1024 * 22 / 32], + array_mut_ref![output, 0, 1024], + ), + 23 => unpack_32_23( + array_ref![input, 0, 1024 * 23 / 32], + array_mut_ref![output, 0, 1024], + ), + 24 => unpack_32_24( + array_ref![input, 0, 1024 * 24 / 32], + array_mut_ref![output, 0, 1024], + ), + 25 => unpack_32_25( + array_ref![input, 0, 1024 * 25 / 32], + array_mut_ref![output, 0, 1024], + ), + 26 => unpack_32_26( + array_ref![input, 0, 1024 * 26 / 32], + array_mut_ref![output, 0, 1024], + ), + 27 => unpack_32_27( + array_ref![input, 0, 1024 * 27 / 32], + array_mut_ref![output, 0, 1024], + ), + 28 => unpack_32_28( + array_ref![input, 0, 1024 * 28 / 32], + array_mut_ref![output, 0, 1024], + ), + 29 => unpack_32_29( + array_ref![input, 0, 1024 * 29 / 32], + array_mut_ref![output, 0, 1024], + ), + + 30 => unpack_32_30( + array_ref![input, 0, 1024 * 30 / 32], + array_mut_ref![output, 0, 1024], + ), + 31 => unpack_32_31( + array_ref![input, 0, 1024 * 31 / 32], + array_mut_ref![output, 0, 1024], + ), + 32 => unpack_32_32( + array_ref![input, 0, 1024 * 32 / 32], + array_mut_ref![output, 0, 1024], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } +} + +impl BitPacking for u64 { + unsafe fn unchecked_pack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + output.len(), + packed_len, + "Output buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(input.len(), 1024, "Input buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + // Nothing to write when width is zero. + } + 1 => pack_64_1( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 / 64], + ), + 2 => pack_64_2( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 2 / 64], + ), + 3 => pack_64_3( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 3 / 64], + ), + 4 => pack_64_4( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 4 / 64], + ), + 5 => pack_64_5( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 5 / 64], + ), + 6 => pack_64_6( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 6 / 64], + ), + 7 => pack_64_7( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 7 / 64], + ), + 8 => pack_64_8( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 8 / 64], + ), + 9 => pack_64_9( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 9 / 64], + ), + + 10 => pack_64_10( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 10 / 64], + ), + 11 => pack_64_11( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 11 / 64], + ), + 12 => pack_64_12( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 12 / 64], + ), + 13 => pack_64_13( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 13 / 64], + ), + 14 => pack_64_14( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 14 / 64], + ), + 15 => pack_64_15( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 15 / 64], + ), + 16 => pack_64_16( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 16 / 64], + ), + 17 => pack_64_17( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 17 / 64], + ), + 18 => pack_64_18( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 18 / 64], + ), + 19 => pack_64_19( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 19 / 64], + ), + + 20 => pack_64_20( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 20 / 64], + ), + 21 => pack_64_21( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 21 / 64], + ), + 22 => pack_64_22( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 22 / 64], + ), + 23 => pack_64_23( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 23 / 64], + ), + 24 => pack_64_24( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 24 / 64], + ), + 25 => pack_64_25( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 25 / 64], + ), + 26 => pack_64_26( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 26 / 64], + ), + 27 => pack_64_27( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 27 / 64], + ), + 28 => pack_64_28( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 28 / 64], + ), + 29 => pack_64_29( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 29 / 64], + ), + + 30 => pack_64_30( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 30 / 64], + ), + 31 => pack_64_31( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 31 / 64], + ), + 32 => pack_64_32( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 32 / 64], + ), + 33 => pack_64_33( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 33 / 64], + ), + 34 => pack_64_34( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 34 / 64], + ), + 35 => pack_64_35( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 35 / 64], + ), + 36 => pack_64_36( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 36 / 64], + ), + 37 => pack_64_37( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 37 / 64], + ), + 38 => pack_64_38( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 38 / 64], + ), + 39 => pack_64_39( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 39 / 64], + ), + + 40 => pack_64_40( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 40 / 64], + ), + 41 => pack_64_41( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 41 / 64], + ), + 42 => pack_64_42( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 42 / 64], + ), + 43 => pack_64_43( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 43 / 64], + ), + 44 => pack_64_44( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 44 / 64], + ), + 45 => pack_64_45( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 45 / 64], + ), + 46 => pack_64_46( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 46 / 64], + ), + 47 => pack_64_47( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 47 / 64], + ), + 48 => pack_64_48( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 48 / 64], + ), + 49 => pack_64_49( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 49 / 64], + ), + + 50 => pack_64_50( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 50 / 64], + ), + 51 => pack_64_51( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 51 / 64], + ), + 52 => pack_64_52( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 52 / 64], + ), + 53 => pack_64_53( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 53 / 64], + ), + 54 => pack_64_54( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 54 / 64], + ), + 55 => pack_64_55( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 55 / 64], + ), + 56 => pack_64_56( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 56 / 64], + ), + 57 => pack_64_57( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 57 / 64], + ), + 58 => pack_64_58( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 58 / 64], + ), + 59 => pack_64_59( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 59 / 64], + ), + + 60 => pack_64_60( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 60 / 64], + ), + 61 => pack_64_61( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 61 / 64], + ), + 62 => pack_64_62( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 62 / 64], + ), + 63 => pack_64_63( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 63 / 64], + ), + 64 => pack_64_64( + array_ref![input, 0, 1024], + array_mut_ref![output, 0, 1024 * 64 / 64], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } + + unsafe fn unchecked_unpack(width: usize, input: &[Self], output: &mut [Self]) { + let packed_len = 128 * width / size_of::(); + debug_assert_eq!( + input.len(), + packed_len, + "Input buffer must be of size 1024 * W / T" + ); + debug_assert_eq!(output.len(), 1024, "Output buffer must be of size 1024"); + debug_assert!( + width <= Self::T, + "Width must be less than or equal to {}", + Self::T + ); + + match width { + 0 => { + output.fill(0); + } + 1 => unpack_64_1( + array_ref![input, 0, 1024 / 64], + array_mut_ref![output, 0, 1024], + ), + 2 => unpack_64_2( + array_ref![input, 0, 1024 * 2 / 64], + array_mut_ref![output, 0, 1024], + ), + 3 => unpack_64_3( + array_ref![input, 0, 1024 * 3 / 64], + array_mut_ref![output, 0, 1024], + ), + 4 => unpack_64_4( + array_ref![input, 0, 1024 * 4 / 64], + array_mut_ref![output, 0, 1024], + ), + 5 => unpack_64_5( + array_ref![input, 0, 1024 * 5 / 64], + array_mut_ref![output, 0, 1024], + ), + 6 => unpack_64_6( + array_ref![input, 0, 1024 * 6 / 64], + array_mut_ref![output, 0, 1024], + ), + 7 => unpack_64_7( + array_ref![input, 0, 1024 * 7 / 64], + array_mut_ref![output, 0, 1024], + ), + 8 => unpack_64_8( + array_ref![input, 0, 1024 * 8 / 64], + array_mut_ref![output, 0, 1024], + ), + 9 => unpack_64_9( + array_ref![input, 0, 1024 * 9 / 64], + array_mut_ref![output, 0, 1024], + ), + + 10 => unpack_64_10( + array_ref![input, 0, 1024 * 10 / 64], + array_mut_ref![output, 0, 1024], + ), + 11 => unpack_64_11( + array_ref![input, 0, 1024 * 11 / 64], + array_mut_ref![output, 0, 1024], + ), + 12 => unpack_64_12( + array_ref![input, 0, 1024 * 12 / 64], + array_mut_ref![output, 0, 1024], + ), + 13 => unpack_64_13( + array_ref![input, 0, 1024 * 13 / 64], + array_mut_ref![output, 0, 1024], + ), + 14 => unpack_64_14( + array_ref![input, 0, 1024 * 14 / 64], + array_mut_ref![output, 0, 1024], + ), + 15 => unpack_64_15( + array_ref![input, 0, 1024 * 15 / 64], + array_mut_ref![output, 0, 1024], + ), + 16 => unpack_64_16( + array_ref![input, 0, 1024 * 16 / 64], + array_mut_ref![output, 0, 1024], + ), + 17 => unpack_64_17( + array_ref![input, 0, 1024 * 17 / 64], + array_mut_ref![output, 0, 1024], + ), + 18 => unpack_64_18( + array_ref![input, 0, 1024 * 18 / 64], + array_mut_ref![output, 0, 1024], + ), + 19 => unpack_64_19( + array_ref![input, 0, 1024 * 19 / 64], + array_mut_ref![output, 0, 1024], + ), + + 20 => unpack_64_20( + array_ref![input, 0, 1024 * 20 / 64], + array_mut_ref![output, 0, 1024], + ), + 21 => unpack_64_21( + array_ref![input, 0, 1024 * 21 / 64], + array_mut_ref![output, 0, 1024], + ), + 22 => unpack_64_22( + array_ref![input, 0, 1024 * 22 / 64], + array_mut_ref![output, 0, 1024], + ), + 23 => unpack_64_23( + array_ref![input, 0, 1024 * 23 / 64], + array_mut_ref![output, 0, 1024], + ), + 24 => unpack_64_24( + array_ref![input, 0, 1024 * 24 / 64], + array_mut_ref![output, 0, 1024], + ), + 25 => unpack_64_25( + array_ref![input, 0, 1024 * 25 / 64], + array_mut_ref![output, 0, 1024], + ), + 26 => unpack_64_26( + array_ref![input, 0, 1024 * 26 / 64], + array_mut_ref![output, 0, 1024], + ), + 27 => unpack_64_27( + array_ref![input, 0, 1024 * 27 / 64], + array_mut_ref![output, 0, 1024], + ), + 28 => unpack_64_28( + array_ref![input, 0, 1024 * 28 / 64], + array_mut_ref![output, 0, 1024], + ), + 29 => unpack_64_29( + array_ref![input, 0, 1024 * 29 / 64], + array_mut_ref![output, 0, 1024], + ), + + 30 => unpack_64_30( + array_ref![input, 0, 1024 * 30 / 64], + array_mut_ref![output, 0, 1024], + ), + 31 => unpack_64_31( + array_ref![input, 0, 1024 * 31 / 64], + array_mut_ref![output, 0, 1024], + ), + 32 => unpack_64_32( + array_ref![input, 0, 1024 * 32 / 64], + array_mut_ref![output, 0, 1024], + ), + 33 => unpack_64_33( + array_ref![input, 0, 1024 * 33 / 64], + array_mut_ref![output, 0, 1024], + ), + 34 => unpack_64_34( + array_ref![input, 0, 1024 * 34 / 64], + array_mut_ref![output, 0, 1024], + ), + 35 => unpack_64_35( + array_ref![input, 0, 1024 * 35 / 64], + array_mut_ref![output, 0, 1024], + ), + 36 => unpack_64_36( + array_ref![input, 0, 1024 * 36 / 64], + array_mut_ref![output, 0, 1024], + ), + 37 => unpack_64_37( + array_ref![input, 0, 1024 * 37 / 64], + array_mut_ref![output, 0, 1024], + ), + 38 => unpack_64_38( + array_ref![input, 0, 1024 * 38 / 64], + array_mut_ref![output, 0, 1024], + ), + 39 => unpack_64_39( + array_ref![input, 0, 1024 * 39 / 64], + array_mut_ref![output, 0, 1024], + ), + + 40 => unpack_64_40( + array_ref![input, 0, 1024 * 40 / 64], + array_mut_ref![output, 0, 1024], + ), + 41 => unpack_64_41( + array_ref![input, 0, 1024 * 41 / 64], + array_mut_ref![output, 0, 1024], + ), + 42 => unpack_64_42( + array_ref![input, 0, 1024 * 42 / 64], + array_mut_ref![output, 0, 1024], + ), + 43 => unpack_64_43( + array_ref![input, 0, 1024 * 43 / 64], + array_mut_ref![output, 0, 1024], + ), + 44 => unpack_64_44( + array_ref![input, 0, 1024 * 44 / 64], + array_mut_ref![output, 0, 1024], + ), + 45 => unpack_64_45( + array_ref![input, 0, 1024 * 45 / 64], + array_mut_ref![output, 0, 1024], + ), + 46 => unpack_64_46( + array_ref![input, 0, 1024 * 46 / 64], + array_mut_ref![output, 0, 1024], + ), + 47 => unpack_64_47( + array_ref![input, 0, 1024 * 47 / 64], + array_mut_ref![output, 0, 1024], + ), + 48 => unpack_64_48( + array_ref![input, 0, 1024 * 48 / 64], + array_mut_ref![output, 0, 1024], + ), + 49 => unpack_64_49( + array_ref![input, 0, 1024 * 49 / 64], + array_mut_ref![output, 0, 1024], + ), + + 50 => unpack_64_50( + array_ref![input, 0, 1024 * 50 / 64], + array_mut_ref![output, 0, 1024], + ), + 51 => unpack_64_51( + array_ref![input, 0, 1024 * 51 / 64], + array_mut_ref![output, 0, 1024], + ), + 52 => unpack_64_52( + array_ref![input, 0, 1024 * 52 / 64], + array_mut_ref![output, 0, 1024], + ), + 53 => unpack_64_53( + array_ref![input, 0, 1024 * 53 / 64], + array_mut_ref![output, 0, 1024], + ), + 54 => unpack_64_54( + array_ref![input, 0, 1024 * 54 / 64], + array_mut_ref![output, 0, 1024], + ), + 55 => unpack_64_55( + array_ref![input, 0, 1024 * 55 / 64], + array_mut_ref![output, 0, 1024], + ), + 56 => unpack_64_56( + array_ref![input, 0, 1024 * 56 / 64], + array_mut_ref![output, 0, 1024], + ), + 57 => unpack_64_57( + array_ref![input, 0, 1024 * 57 / 64], + array_mut_ref![output, 0, 1024], + ), + 58 => unpack_64_58( + array_ref![input, 0, 1024 * 58 / 64], + array_mut_ref![output, 0, 1024], + ), + 59 => unpack_64_59( + array_ref![input, 0, 1024 * 59 / 64], + array_mut_ref![output, 0, 1024], + ), + + 60 => unpack_64_60( + array_ref![input, 0, 1024 * 60 / 64], + array_mut_ref![output, 0, 1024], + ), + 61 => unpack_64_61( + array_ref![input, 0, 1024 * 61 / 64], + array_mut_ref![output, 0, 1024], + ), + 62 => unpack_64_62( + array_ref![input, 0, 1024 * 62 / 64], + array_mut_ref![output, 0, 1024], + ), + 63 => unpack_64_63( + array_ref![input, 0, 1024 * 63 / 64], + array_mut_ref![output, 0, 1024], + ), + 64 => unpack_64_64( + array_ref![input, 0, 1024 * 64 / 64], + array_mut_ref![output, 0, 1024], + ), + + _ => unreachable!("Unsupported width: {}", width), + } + } +} + +macro_rules! unpack_8 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u8; 1024 * $bits / u8::T], output: &mut [u8; 1024]) { + for lane in 0..u8::LANES { + unpack!(u8, $bits, input, lane, |$idx, $elem| { + output[$idx] = $elem; + }); + } + } + }; +} + +unpack_8!(unpack_8_1, 1); +unpack_8!(unpack_8_2, 2); +unpack_8!(unpack_8_3, 3); +unpack_8!(unpack_8_4, 4); +unpack_8!(unpack_8_5, 5); +unpack_8!(unpack_8_6, 6); +unpack_8!(unpack_8_7, 7); +unpack_8!(unpack_8_8, 8); + +macro_rules! pack_8 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u8; 1024], output: &mut [u8; 1024 * $bits / u8::T]) { + for lane in 0..u8::LANES { + pack!(u8, $bits, output, lane, |$idx| { input[$idx] }); + } + } + }; +} +pack_8!(pack_8_1, 1); +pack_8!(pack_8_2, 2); +pack_8!(pack_8_3, 3); +pack_8!(pack_8_4, 4); +pack_8!(pack_8_5, 5); +pack_8!(pack_8_6, 6); +pack_8!(pack_8_7, 7); +pack_8!(pack_8_8, 8); + +macro_rules! unpack_16 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u16; 1024 * $bits / u16::T], output: &mut [u16; 1024]) { + for lane in 0..u16::LANES { + unpack!(u16, $bits, input, lane, |$idx, $elem| { + output[$idx] = $elem; + }); + } + } + }; +} + +unpack_16!(unpack_16_1, 1); +unpack_16!(unpack_16_2, 2); +unpack_16!(unpack_16_3, 3); +unpack_16!(unpack_16_4, 4); +unpack_16!(unpack_16_5, 5); +unpack_16!(unpack_16_6, 6); +unpack_16!(unpack_16_7, 7); +unpack_16!(unpack_16_8, 8); +unpack_16!(unpack_16_9, 9); +unpack_16!(unpack_16_10, 10); +unpack_16!(unpack_16_11, 11); +unpack_16!(unpack_16_12, 12); +unpack_16!(unpack_16_13, 13); +unpack_16!(unpack_16_14, 14); +unpack_16!(unpack_16_15, 15); +unpack_16!(unpack_16_16, 16); + +macro_rules! pack_16 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u16; 1024], output: &mut [u16; 1024 * $bits / u16::T]) { + for lane in 0..u16::LANES { + pack!(u16, $bits, output, lane, |$idx| { input[$idx] }); + } + } + }; +} + +pack_16!(pack_16_1, 1); +pack_16!(pack_16_2, 2); +pack_16!(pack_16_3, 3); +pack_16!(pack_16_4, 4); +pack_16!(pack_16_5, 5); +pack_16!(pack_16_6, 6); +pack_16!(pack_16_7, 7); +pack_16!(pack_16_8, 8); +pack_16!(pack_16_9, 9); +pack_16!(pack_16_10, 10); +pack_16!(pack_16_11, 11); +pack_16!(pack_16_12, 12); +pack_16!(pack_16_13, 13); +pack_16!(pack_16_14, 14); +pack_16!(pack_16_15, 15); +pack_16!(pack_16_16, 16); + +macro_rules! unpack_32 { + ($name:ident, $bit_width:expr) => { + fn $name(input: &[u32; 1024 * $bit_width / u32::T], output: &mut [u32; 1024]) { + for lane in 0..u32::LANES { + unpack!(u32, $bit_width, input, lane, |$idx, $elem| { + output[$idx] = $elem + }); + } + } + }; +} + +unpack_32!(unpack_32_1, 1); +unpack_32!(unpack_32_2, 2); +unpack_32!(unpack_32_3, 3); +unpack_32!(unpack_32_4, 4); +unpack_32!(unpack_32_5, 5); +unpack_32!(unpack_32_6, 6); +unpack_32!(unpack_32_7, 7); +unpack_32!(unpack_32_8, 8); +unpack_32!(unpack_32_9, 9); +unpack_32!(unpack_32_10, 10); +unpack_32!(unpack_32_11, 11); +unpack_32!(unpack_32_12, 12); +unpack_32!(unpack_32_13, 13); +unpack_32!(unpack_32_14, 14); +unpack_32!(unpack_32_15, 15); +unpack_32!(unpack_32_16, 16); +unpack_32!(unpack_32_17, 17); +unpack_32!(unpack_32_18, 18); +unpack_32!(unpack_32_19, 19); +unpack_32!(unpack_32_20, 20); +unpack_32!(unpack_32_21, 21); +unpack_32!(unpack_32_22, 22); +unpack_32!(unpack_32_23, 23); +unpack_32!(unpack_32_24, 24); +unpack_32!(unpack_32_25, 25); +unpack_32!(unpack_32_26, 26); +unpack_32!(unpack_32_27, 27); +unpack_32!(unpack_32_28, 28); +unpack_32!(unpack_32_29, 29); +unpack_32!(unpack_32_30, 30); +unpack_32!(unpack_32_31, 31); +unpack_32!(unpack_32_32, 32); + +macro_rules! pack_32 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u32; 1024], output: &mut [u32; 1024 * $bits / u32::BITS as usize]) { + for lane in 0..u32::LANES { + pack!(u32, $bits, output, lane, |$idx| { input[$idx] }); + } + } + }; +} + +pack_32!(pack_32_1, 1); +pack_32!(pack_32_2, 2); +pack_32!(pack_32_3, 3); +pack_32!(pack_32_4, 4); +pack_32!(pack_32_5, 5); +pack_32!(pack_32_6, 6); +pack_32!(pack_32_7, 7); +pack_32!(pack_32_8, 8); +pack_32!(pack_32_9, 9); +pack_32!(pack_32_10, 10); +pack_32!(pack_32_11, 11); +pack_32!(pack_32_12, 12); +pack_32!(pack_32_13, 13); +pack_32!(pack_32_14, 14); +pack_32!(pack_32_15, 15); +pack_32!(pack_32_16, 16); +pack_32!(pack_32_17, 17); +pack_32!(pack_32_18, 18); +pack_32!(pack_32_19, 19); +pack_32!(pack_32_20, 20); +pack_32!(pack_32_21, 21); +pack_32!(pack_32_22, 22); +pack_32!(pack_32_23, 23); +pack_32!(pack_32_24, 24); +pack_32!(pack_32_25, 25); +pack_32!(pack_32_26, 26); +pack_32!(pack_32_27, 27); +pack_32!(pack_32_28, 28); +pack_32!(pack_32_29, 29); +pack_32!(pack_32_30, 30); +pack_32!(pack_32_31, 31); +pack_32!(pack_32_32, 32); + +macro_rules! unpack_64 { + ($name:ident, $bit_width:expr) => { + fn $name(input: &[u64; 1024 * $bit_width / u64::T], output: &mut [u64; 1024]) { + for lane in 0..u64::LANES { + unpack!(u64, $bit_width, input, lane, |$idx, $elem| { + output[$idx] = $elem + }); + } + } + }; +} + +unpack_64!(unpack_64_1, 1); +unpack_64!(unpack_64_2, 2); +unpack_64!(unpack_64_3, 3); +unpack_64!(unpack_64_4, 4); +unpack_64!(unpack_64_5, 5); +unpack_64!(unpack_64_6, 6); +unpack_64!(unpack_64_7, 7); +unpack_64!(unpack_64_8, 8); +unpack_64!(unpack_64_9, 9); +unpack_64!(unpack_64_10, 10); +unpack_64!(unpack_64_11, 11); +unpack_64!(unpack_64_12, 12); +unpack_64!(unpack_64_13, 13); +unpack_64!(unpack_64_14, 14); +unpack_64!(unpack_64_15, 15); +unpack_64!(unpack_64_16, 16); +unpack_64!(unpack_64_17, 17); +unpack_64!(unpack_64_18, 18); +unpack_64!(unpack_64_19, 19); +unpack_64!(unpack_64_20, 20); +unpack_64!(unpack_64_21, 21); +unpack_64!(unpack_64_22, 22); +unpack_64!(unpack_64_23, 23); +unpack_64!(unpack_64_24, 24); +unpack_64!(unpack_64_25, 25); +unpack_64!(unpack_64_26, 26); +unpack_64!(unpack_64_27, 27); +unpack_64!(unpack_64_28, 28); +unpack_64!(unpack_64_29, 29); +unpack_64!(unpack_64_30, 30); +unpack_64!(unpack_64_31, 31); +unpack_64!(unpack_64_32, 32); + +unpack_64!(unpack_64_33, 33); +unpack_64!(unpack_64_34, 34); +unpack_64!(unpack_64_35, 35); +unpack_64!(unpack_64_36, 36); +unpack_64!(unpack_64_37, 37); +unpack_64!(unpack_64_38, 38); +unpack_64!(unpack_64_39, 39); +unpack_64!(unpack_64_40, 40); +unpack_64!(unpack_64_41, 41); +unpack_64!(unpack_64_42, 42); +unpack_64!(unpack_64_43, 43); +unpack_64!(unpack_64_44, 44); +unpack_64!(unpack_64_45, 45); +unpack_64!(unpack_64_46, 46); +unpack_64!(unpack_64_47, 47); +unpack_64!(unpack_64_48, 48); +unpack_64!(unpack_64_49, 49); +unpack_64!(unpack_64_50, 50); +unpack_64!(unpack_64_51, 51); +unpack_64!(unpack_64_52, 52); +unpack_64!(unpack_64_53, 53); +unpack_64!(unpack_64_54, 54); +unpack_64!(unpack_64_55, 55); +unpack_64!(unpack_64_56, 56); +unpack_64!(unpack_64_57, 57); +unpack_64!(unpack_64_58, 58); +unpack_64!(unpack_64_59, 59); +unpack_64!(unpack_64_60, 60); +unpack_64!(unpack_64_61, 61); +unpack_64!(unpack_64_62, 62); +unpack_64!(unpack_64_63, 63); +unpack_64!(unpack_64_64, 64); + +macro_rules! pack_64 { + ($name:ident, $bits:expr) => { + fn $name(input: &[u64; 1024], output: &mut [u64; 1024 * $bits / u64::BITS as usize]) { + for lane in 0..u64::LANES { + pack!(u64, $bits, output, lane, |$idx| { input[$idx] }); + } + } + }; +} + +pack_64!(pack_64_1, 1); +pack_64!(pack_64_2, 2); +pack_64!(pack_64_3, 3); +pack_64!(pack_64_4, 4); +pack_64!(pack_64_5, 5); +pack_64!(pack_64_6, 6); +pack_64!(pack_64_7, 7); +pack_64!(pack_64_8, 8); +pack_64!(pack_64_9, 9); +pack_64!(pack_64_10, 10); +pack_64!(pack_64_11, 11); +pack_64!(pack_64_12, 12); +pack_64!(pack_64_13, 13); +pack_64!(pack_64_14, 14); +pack_64!(pack_64_15, 15); +pack_64!(pack_64_16, 16); +pack_64!(pack_64_17, 17); +pack_64!(pack_64_18, 18); +pack_64!(pack_64_19, 19); +pack_64!(pack_64_20, 20); +pack_64!(pack_64_21, 21); +pack_64!(pack_64_22, 22); +pack_64!(pack_64_23, 23); +pack_64!(pack_64_24, 24); +pack_64!(pack_64_25, 25); +pack_64!(pack_64_26, 26); +pack_64!(pack_64_27, 27); +pack_64!(pack_64_28, 28); +pack_64!(pack_64_29, 29); +pack_64!(pack_64_30, 30); +pack_64!(pack_64_31, 31); +pack_64!(pack_64_32, 32); + +pack_64!(pack_64_33, 33); +pack_64!(pack_64_34, 34); +pack_64!(pack_64_35, 35); +pack_64!(pack_64_36, 36); +pack_64!(pack_64_37, 37); +pack_64!(pack_64_38, 38); +pack_64!(pack_64_39, 39); +pack_64!(pack_64_40, 40); +pack_64!(pack_64_41, 41); +pack_64!(pack_64_42, 42); +pack_64!(pack_64_43, 43); +pack_64!(pack_64_44, 44); +pack_64!(pack_64_45, 45); +pack_64!(pack_64_46, 46); +pack_64!(pack_64_47, 47); +pack_64!(pack_64_48, 48); +pack_64!(pack_64_49, 49); +pack_64!(pack_64_50, 50); +pack_64!(pack_64_51, 51); +pack_64!(pack_64_52, 52); +pack_64!(pack_64_53, 53); +pack_64!(pack_64_54, 54); +pack_64!(pack_64_55, 55); +pack_64!(pack_64_56, 56); +pack_64!(pack_64_57, 57); +pack_64!(pack_64_58, 58); +pack_64!(pack_64_59, 59); +pack_64!(pack_64_60, 60); +pack_64!(pack_64_61, 61); +pack_64!(pack_64_62, 62); +pack_64!(pack_64_63, 63); +pack_64!(pack_64_64, 64); + +#[cfg(test)] +mod test { + use super::*; + use bitpacking::{BitPacker as ExternalBitPacker, BitPacker4x as ExternalBitPacker4x}; + use core::array; + // a fast random number generator + pub struct XorShift { + state: u64, + } + + impl XorShift { + pub fn new(seed: u64) -> Self { + Self { state: seed } + } + + pub fn next(&mut self) -> u64 { + let mut x = self.state; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + self.state = x; + x + } + } + + fn mask_for_width(width: u8) -> u32 { + match width { + 0 => 0, + 32 => u32::MAX, + _ => (1u32 << width) - 1, + } + } + + fn raw_bitpacker4x_case(width: u8, seed: u64) -> Vec { + let mask = mask_for_width(width); + let mut rng = XorShift::new(seed); + (0..BitPacker4x::BLOCK_LEN) + .map(|idx| match seed % 4 { + 0 => 0, + 1 => mask, + 2 => idx as u32 & mask, + _ => (rng.next() as u32) & mask, + }) + .collect() + } + + fn sorted_bitpacker4x_case(width: u8, seed: u64) -> (u32, Vec) { + if width == 0 { + return (17, vec![17; BitPacker4x::BLOCK_LEN]); + } + if width == 32 { + return (0, vec![u32::MAX; BitPacker4x::BLOCK_LEN]); + } + + let mask = mask_for_width(width).min(127); + let mut rng = XorShift::new(seed); + let mut current = 17u32; + let values = (0..BitPacker4x::BLOCK_LEN) + .map(|_| { + current += (rng.next() as u32) & mask; + current + }) + .collect(); + (17, values) + } + + #[test] + fn test_bitpacker4x_raw_compatible_with_external_bitpacking() { + let ours = BitPacker4x::new(); + let external = ExternalBitPacker4x::new(); + + for width in 0..=32 { + for seed in [0, 1, 2, 123456789] { + let values = raw_bitpacker4x_case(width, seed); + assert_eq!(ours.num_bits(&values), external.num_bits(&values)); + + let mut actual = vec![0u8; BitPacker4x::compressed_block_size(width)]; + let actual_len = ours.compress(&values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker4x::compressed_block_size(width)]; + let expected_len = external.compress(&values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "width {width} seed {seed}"); + + let mut decoded = vec![0u32; BitPacker4x::BLOCK_LEN]; + let consumed = ours.decompress(&actual, &mut decoded, width); + assert_eq!(consumed, actual_len); + assert_eq!(decoded, values); + } + } + } + + #[test] + fn test_bitpacker4x_sorted_compatible_with_external_bitpacking() { + let ours = BitPacker4x::new(); + let external = ExternalBitPacker4x::new(); + + for width in 0..=32 { + for seed in [0, 1, 2, 123456789] { + let (initial, values) = sorted_bitpacker4x_case(width, seed); + assert_eq!( + ours.num_bits_sorted(initial, &values), + external.num_bits_sorted(initial, &values) + ); + + let mut actual = vec![0u8; BitPacker4x::compressed_block_size(width)]; + let actual_len = ours.compress_sorted(initial, &values, &mut actual, width); + + let mut expected = vec![0u8; ExternalBitPacker4x::compressed_block_size(width)]; + let expected_len = external.compress_sorted(initial, &values, &mut expected, width); + + assert_eq!(actual_len, expected_len); + assert_eq!(actual, expected, "width {width} seed {seed}"); + + let mut decoded = vec![0u32; BitPacker4x::BLOCK_LEN]; + let consumed = ours.decompress_sorted(initial, &actual, &mut decoded, width); + assert_eq!(consumed, actual_len); + assert_eq!(decoded, values); + } + } + } + + // a macro version of this function generalize u8, u16, u32, u64 takes very long time for a test build, so I + // write it for each type separately + fn pack_unpack_u8(bit_width: usize) { + let mut values: [u8; 1024] = [0; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u8; + } + + let mut packed = vec![0; 1024 * bit_width / 8]; + for lane in 0..u8::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + pack!(u8, bit_width, packed, lane, |$pos| { + values[$pos] + }); + } + + let mut unpacked: [u8; 1024] = [0; 1024]; + for lane in 0..u8::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + unpack!(u8, bit_width, packed, lane, |$idx, $elem| { + unpacked[$idx] = $elem; + }); + } + + assert_eq!(values, unpacked); + } + + fn pack_unpack_u16(bit_width: usize) { + let mut values: [u16; 1024] = [0; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u16; + } + + let mut packed = vec![0; 1024 * bit_width / 16]; + for lane in 0..u16::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + pack!(u16, bit_width, packed, lane, |$pos| { + values[$pos] + }); + } + + let mut unpacked: [u16; 1024] = [0; 1024]; + for lane in 0..u16::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + unpack!(u16, bit_width, packed, lane, |$idx, $elem| { + unpacked[$idx] = $elem; + }); + } + + assert_eq!(values, unpacked); + } + + fn pack_unpack_u32(bit_width: usize) { + let mut values: [u32; 1024] = [0; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u32; + } + + let mut packed = vec![0; 1024 * bit_width / 32]; + for lane in 0..u32::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + pack!(u32, bit_width, packed, lane, |$pos| { + values[$pos] + }); + } + + let mut unpacked: [u32; 1024] = [0; 1024]; + for lane in 0..u32::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + unpack!(u32, bit_width, packed, lane, |$idx, $elem| { + unpacked[$idx] = $elem; + }); + } + + assert_eq!(values, unpacked); + } + + fn pack_unpack_u64(bit_width: usize) { + let mut values: [u64; 1024] = [0; 1024]; + let mut rng = XorShift::new(123456789); + if bit_width == 64 { + for value in &mut values { + *value = rng.next(); + } + } else { + for value in &mut values { + *value = rng.next() % (1 << bit_width); + } + } + + let mut packed = vec![0; 1024 * bit_width / 64]; + for lane in 0..u64::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + pack!(u64, bit_width, packed, lane, |$pos| { + values[$pos] + }); + } + + let mut unpacked: [u64; 1024] = [0; 1024]; + for lane in 0..u64::LANES { + // Always loop over lanes first. This is what the compiler vectorizes. + unpack!(u64, bit_width, packed, lane, |$idx, $elem| { + unpacked[$idx] = $elem; + }); + } + + assert_eq!(values, unpacked); + } + + #[test] + fn test_pack() { + pack_unpack_u8(0); + pack_unpack_u8(1); + pack_unpack_u8(2); + pack_unpack_u8(3); + pack_unpack_u8(4); + pack_unpack_u8(5); + pack_unpack_u8(6); + pack_unpack_u8(7); + pack_unpack_u8(8); + + pack_unpack_u16(0); + pack_unpack_u16(1); + pack_unpack_u16(2); + pack_unpack_u16(3); + pack_unpack_u16(4); + pack_unpack_u16(5); + pack_unpack_u16(6); + pack_unpack_u16(7); + pack_unpack_u16(8); + pack_unpack_u16(9); + pack_unpack_u16(10); + pack_unpack_u16(11); + pack_unpack_u16(12); + pack_unpack_u16(13); + pack_unpack_u16(14); + pack_unpack_u16(15); + pack_unpack_u16(16); + + pack_unpack_u32(0); + pack_unpack_u32(1); + pack_unpack_u32(2); + pack_unpack_u32(3); + pack_unpack_u32(4); + pack_unpack_u32(5); + pack_unpack_u32(6); + pack_unpack_u32(7); + pack_unpack_u32(8); + pack_unpack_u32(9); + pack_unpack_u32(10); + pack_unpack_u32(11); + pack_unpack_u32(12); + pack_unpack_u32(13); + pack_unpack_u32(14); + pack_unpack_u32(15); + pack_unpack_u32(16); + pack_unpack_u32(17); + pack_unpack_u32(18); + pack_unpack_u32(19); + pack_unpack_u32(20); + pack_unpack_u32(21); + pack_unpack_u32(22); + pack_unpack_u32(23); + pack_unpack_u32(24); + pack_unpack_u32(25); + pack_unpack_u32(26); + pack_unpack_u32(27); + pack_unpack_u32(28); + pack_unpack_u32(29); + pack_unpack_u32(30); + pack_unpack_u32(31); + pack_unpack_u32(32); + + pack_unpack_u64(0); + pack_unpack_u64(1); + pack_unpack_u64(2); + pack_unpack_u64(3); + pack_unpack_u64(4); + pack_unpack_u64(5); + pack_unpack_u64(6); + pack_unpack_u64(7); + pack_unpack_u64(8); + pack_unpack_u64(9); + pack_unpack_u64(10); + pack_unpack_u64(11); + pack_unpack_u64(12); + pack_unpack_u64(13); + pack_unpack_u64(14); + pack_unpack_u64(15); + pack_unpack_u64(16); + pack_unpack_u64(17); + pack_unpack_u64(18); + pack_unpack_u64(19); + pack_unpack_u64(20); + pack_unpack_u64(21); + pack_unpack_u64(22); + pack_unpack_u64(23); + pack_unpack_u64(24); + pack_unpack_u64(25); + pack_unpack_u64(26); + pack_unpack_u64(27); + pack_unpack_u64(28); + pack_unpack_u64(29); + pack_unpack_u64(30); + pack_unpack_u64(31); + pack_unpack_u64(32); + pack_unpack_u64(33); + pack_unpack_u64(34); + pack_unpack_u64(35); + pack_unpack_u64(36); + pack_unpack_u64(37); + pack_unpack_u64(38); + pack_unpack_u64(39); + pack_unpack_u64(40); + pack_unpack_u64(41); + pack_unpack_u64(42); + pack_unpack_u64(43); + pack_unpack_u64(44); + pack_unpack_u64(45); + pack_unpack_u64(46); + pack_unpack_u64(47); + pack_unpack_u64(48); + pack_unpack_u64(49); + pack_unpack_u64(50); + pack_unpack_u64(51); + pack_unpack_u64(52); + pack_unpack_u64(53); + pack_unpack_u64(54); + pack_unpack_u64(55); + pack_unpack_u64(56); + pack_unpack_u64(57); + pack_unpack_u64(58); + pack_unpack_u64(59); + pack_unpack_u64(60); + pack_unpack_u64(61); + pack_unpack_u64(62); + pack_unpack_u64(63); + pack_unpack_u64(64); + } + + fn unchecked_pack_unpack_u8(bit_width: usize) { + let mut values = [0u8; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u8; + } + let mut packed = vec![0; 1024 * bit_width / 8]; + unsafe { + BitPacking::unchecked_pack(bit_width, &values, &mut packed); + } + let mut output = [0; 1024]; + unsafe { BitPacking::unchecked_unpack(bit_width, &packed, &mut output) }; + assert_eq!(values, output); + } + + fn unchecked_pack_unpack_u16(bit_width: usize) { + let mut values = [0u16; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u16; + } + let mut packed = vec![0; 1024 * bit_width / u16::T]; + unsafe { + BitPacking::unchecked_pack(bit_width, &values, &mut packed); + } + let mut output = [0; 1024]; + unsafe { BitPacking::unchecked_unpack(bit_width, &packed, &mut output) }; + assert_eq!(values, output); + } + + fn unchecked_pack_unpack_u32(bit_width: usize) { + let mut values = [0u32; 1024]; + let mut rng = XorShift::new(123456789); + for value in &mut values { + *value = (rng.next() % (1 << bit_width)) as u32; + } + let mut packed = vec![0; 1024 * bit_width / u32::T]; + unsafe { + BitPacking::unchecked_pack(bit_width, &values, &mut packed); + } + let mut output = [0; 1024]; + unsafe { BitPacking::unchecked_unpack(bit_width, &packed, &mut output) }; + assert_eq!(values, output); + } + + fn unchecked_pack_unpack_u64(bit_width: usize) { + let mut values = [0u64; 1024]; + let mut rng = XorShift::new(123456789); + if bit_width == 64 { + for value in &mut values { + *value = rng.next(); + } + } else { + for value in &mut values { + *value = rng.next() % (1 << bit_width); + } + } + let mut packed = vec![0; 1024 * bit_width / u64::T]; + unsafe { + BitPacking::unchecked_pack(bit_width, &values, &mut packed); + } + let mut output = [0; 1024]; + unsafe { BitPacking::unchecked_unpack(bit_width, &packed, &mut output) }; + assert_eq!(values, output); + } + + #[test] + fn test_unchecked_pack() { + let input = array::from_fn(|i| i as u32); + let mut packed = [0; 320]; + unsafe { BitPacking::unchecked_pack(10, &input, &mut packed) }; + let mut output = [0; 1024]; + unsafe { BitPacking::unchecked_unpack(10, &packed, &mut output) }; + assert_eq!(input, output); + + unchecked_pack_unpack_u8(1); + unchecked_pack_unpack_u8(2); + unchecked_pack_unpack_u8(3); + unchecked_pack_unpack_u8(4); + unchecked_pack_unpack_u8(5); + unchecked_pack_unpack_u8(6); + unchecked_pack_unpack_u8(7); + unchecked_pack_unpack_u8(8); + + unchecked_pack_unpack_u16(1); + unchecked_pack_unpack_u16(2); + unchecked_pack_unpack_u16(3); + unchecked_pack_unpack_u16(4); + unchecked_pack_unpack_u16(5); + unchecked_pack_unpack_u16(6); + unchecked_pack_unpack_u16(7); + unchecked_pack_unpack_u16(8); + unchecked_pack_unpack_u16(9); + unchecked_pack_unpack_u16(10); + unchecked_pack_unpack_u16(11); + unchecked_pack_unpack_u16(12); + unchecked_pack_unpack_u16(13); + unchecked_pack_unpack_u16(14); + unchecked_pack_unpack_u16(15); + unchecked_pack_unpack_u16(16); + + unchecked_pack_unpack_u32(1); + unchecked_pack_unpack_u32(2); + unchecked_pack_unpack_u32(3); + unchecked_pack_unpack_u32(4); + unchecked_pack_unpack_u32(5); + unchecked_pack_unpack_u32(6); + unchecked_pack_unpack_u32(7); + unchecked_pack_unpack_u32(8); + unchecked_pack_unpack_u32(9); + unchecked_pack_unpack_u32(10); + unchecked_pack_unpack_u32(11); + unchecked_pack_unpack_u32(12); + unchecked_pack_unpack_u32(13); + unchecked_pack_unpack_u32(14); + unchecked_pack_unpack_u32(15); + unchecked_pack_unpack_u32(16); + unchecked_pack_unpack_u32(17); + unchecked_pack_unpack_u32(18); + unchecked_pack_unpack_u32(19); + unchecked_pack_unpack_u32(20); + unchecked_pack_unpack_u32(21); + unchecked_pack_unpack_u32(22); + unchecked_pack_unpack_u32(23); + unchecked_pack_unpack_u32(24); + unchecked_pack_unpack_u32(25); + unchecked_pack_unpack_u32(26); + unchecked_pack_unpack_u32(27); + unchecked_pack_unpack_u32(28); + unchecked_pack_unpack_u32(29); + unchecked_pack_unpack_u32(30); + unchecked_pack_unpack_u32(31); + unchecked_pack_unpack_u32(32); + + unchecked_pack_unpack_u64(1); + unchecked_pack_unpack_u64(2); + unchecked_pack_unpack_u64(3); + unchecked_pack_unpack_u64(4); + unchecked_pack_unpack_u64(5); + unchecked_pack_unpack_u64(6); + unchecked_pack_unpack_u64(7); + unchecked_pack_unpack_u64(8); + unchecked_pack_unpack_u64(9); + unchecked_pack_unpack_u64(10); + unchecked_pack_unpack_u64(11); + unchecked_pack_unpack_u64(12); + unchecked_pack_unpack_u64(13); + unchecked_pack_unpack_u64(14); + unchecked_pack_unpack_u64(15); + unchecked_pack_unpack_u64(16); + unchecked_pack_unpack_u64(17); + unchecked_pack_unpack_u64(18); + unchecked_pack_unpack_u64(19); + unchecked_pack_unpack_u64(20); + unchecked_pack_unpack_u64(21); + unchecked_pack_unpack_u64(22); + unchecked_pack_unpack_u64(23); + unchecked_pack_unpack_u64(24); + unchecked_pack_unpack_u64(25); + unchecked_pack_unpack_u64(26); + unchecked_pack_unpack_u64(27); + unchecked_pack_unpack_u64(28); + unchecked_pack_unpack_u64(29); + unchecked_pack_unpack_u64(30); + unchecked_pack_unpack_u64(31); + unchecked_pack_unpack_u64(32); + unchecked_pack_unpack_u64(33); + unchecked_pack_unpack_u64(34); + unchecked_pack_unpack_u64(35); + unchecked_pack_unpack_u64(36); + unchecked_pack_unpack_u64(37); + unchecked_pack_unpack_u64(38); + unchecked_pack_unpack_u64(39); + unchecked_pack_unpack_u64(40); + unchecked_pack_unpack_u64(41); + unchecked_pack_unpack_u64(42); + unchecked_pack_unpack_u64(43); + unchecked_pack_unpack_u64(44); + unchecked_pack_unpack_u64(45); + unchecked_pack_unpack_u64(46); + unchecked_pack_unpack_u64(47); + unchecked_pack_unpack_u64(48); + unchecked_pack_unpack_u64(49); + unchecked_pack_unpack_u64(50); + unchecked_pack_unpack_u64(51); + unchecked_pack_unpack_u64(52); + unchecked_pack_unpack_u64(53); + unchecked_pack_unpack_u64(54); + unchecked_pack_unpack_u64(55); + unchecked_pack_unpack_u64(56); + unchecked_pack_unpack_u64(57); + unchecked_pack_unpack_u64(58); + unchecked_pack_unpack_u64(59); + unchecked_pack_unpack_u64(60); + unchecked_pack_unpack_u64(61); + unchecked_pack_unpack_u64(62); + unchecked_pack_unpack_u64(63); + unchecked_pack_unpack_u64(64); + } +} diff --git a/lance-artifact/rust/compression/fsst/Cargo.toml b/lance-artifact/rust/compression/fsst/Cargo.toml new file mode 100644 index 000000000..7056896e3 --- /dev/null +++ b/lance-artifact/rust/compression/fsst/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "fsst" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +description = "FSST string compression for Lance" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[dependencies] +arrow-array.workspace = true +rand.workspace = true + +[dev-dependencies] +test-log.workspace = true +tokio.workspace = true + +[[example]] +name = "benchmark" +path = "examples/benchmark.rs" + +[lints] +workspace = true diff --git a/lance-artifact/rust/compression/fsst/examples/benchmark.rs b/lance-artifact/rust/compression/fsst/examples/benchmark.rs new file mode 100644 index 000000000..f71abeefe --- /dev/null +++ b/lance-artifact/rust/compression/fsst/examples/benchmark.rs @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::fs::File; +use std::io::{BufRead, BufReader}; + +use arrow_array::StringArray; +use fsst::fsst::{FSST_SYMBOL_TABLE_SIZE, compress, decompress}; +use rand::Rng; + +const TEST_NUM: usize = 20; +const BUFFER_SIZE: usize = 8 * 1024 * 1024; + +fn read_random_8_m_chunk(file_path: &str) -> Result { + let file = File::open(file_path)?; + let reader = BufReader::new(file); + + let lines: Vec = reader.lines().collect::>()?; + let num_lines = lines.len(); + + let mut rng = rand::rng(); + let mut curr_line = rng.random_range(0..num_lines); + + let chunk_size = BUFFER_SIZE; + let mut size = 0; + let mut result_lines = vec![]; + while size + lines[curr_line].len() < chunk_size { + result_lines.push(lines[curr_line].clone()); + size += lines[curr_line].len(); + curr_line += 1; + curr_line %= num_lines; + } + + Ok(StringArray::from(result_lines)) +} + +fn benchmark(file_path: &str) { + // Step 1: load data in memory + let mut inputs: Vec = vec![]; + let mut symbol_tables: Vec<[u8; FSST_SYMBOL_TABLE_SIZE]> = vec![]; + for _ in 0..TEST_NUM { + let this_input = read_random_8_m_chunk(file_path).unwrap(); + inputs.push(this_input); + symbol_tables.push([0u8; FSST_SYMBOL_TABLE_SIZE]); + } + + // Step 2: allocate memory for compression and decompression outputs + let mut compression_out_bufs = vec![]; + let mut compression_out_offsets_bufs = vec![]; + for _ in 0..TEST_NUM { + let this_com_out_buf = vec![0u8; BUFFER_SIZE]; + let this_com_out_offsets_buf = vec![0i32; BUFFER_SIZE]; + compression_out_bufs.push(this_com_out_buf); + compression_out_offsets_bufs.push(this_com_out_offsets_buf); + } + let mut decompression_out_bufs = vec![]; + let mut decompression_out_offsets_bufs = vec![]; + for _ in 0..TEST_NUM { + // `decompress` requires the output buffer to be at least 8x the compressed input (a 1-byte + // code can expand to an 8-byte symbol). The compressed buffer is at most `BUFFER_SIZE`, so + // `BUFFER_SIZE * 8` is a safe upper bound. + let this_decom_out_buf = vec![0u8; BUFFER_SIZE * 8]; + let this_decom_out_offsets_buf = vec![0i32; BUFFER_SIZE * 3]; + decompression_out_bufs.push(this_decom_out_buf); + decompression_out_offsets_bufs.push(this_decom_out_offsets_buf); + } + + let original_total_size: usize = inputs.iter().map(|input| input.values().len()).sum(); + + // Step 3: compress data + let start = std::time::Instant::now(); + for i in 0..TEST_NUM { + compress( + symbol_tables[i].as_mut(), + inputs[i].values(), + inputs[i].value_offsets(), + &mut compression_out_bufs[i], + &mut compression_out_offsets_bufs[i], + ) + .unwrap(); + } + let compression_finish_time = std::time::Instant::now(); + + for i in 0..TEST_NUM { + decompress( + &symbol_tables[i], + &compression_out_bufs[i], + &compression_out_offsets_bufs[i], + &mut decompression_out_bufs[i], + &mut decompression_out_offsets_bufs[i], + ) + .unwrap(); + } + let decompression_finish_time = std::time::Instant::now(); + let compression_total_size: usize = compression_out_bufs.iter().map(|buf| buf.len()).sum(); + let compression_ratio = original_total_size as f64 / compression_total_size as f64; + let compress_time = compression_finish_time - start; + let decompress_time = decompression_finish_time - compression_finish_time; + + let compress_seconds = + compress_time.as_secs() as f64 + compress_time.subsec_nanos() as f64 * 1e-9; + + let decompress_seconds = + decompress_time.as_secs() as f64 + decompress_time.subsec_nanos() as f64 * 1e-9; + + let com_speed = (original_total_size as f64 / compress_seconds) / 1024f64 / 1024f64; + + let d_speed = (original_total_size as f64 / decompress_seconds) / 1024f64 / 1024f64; + for i in 0..TEST_NUM { + assert_eq!( + inputs[i].value_offsets().len(), + decompression_out_offsets_bufs[i].len() + ); + } + + // Print tsv headers + #[allow(clippy::print_stdout)] + { + println!("for file: {}", file_path); + println!("Compression ratio\tCompression speed\tDecompression speed"); + println!( + "{:.3}\t\t\t\t{:.2}MB/s\t\t\t{:.2}MB/s", + compression_ratio, com_speed, d_speed + ); + } + for i in 0..TEST_NUM { + assert_eq!(inputs[i].value_data(), decompression_out_bufs[i]); + assert_eq!(inputs[i].value_offsets(), decompression_out_offsets_bufs[i]); + } +} + +// to run this test, download MS Marco dataset from https://msmarco.z22.web.core.windows.net/msmarcoranking/fulldocs.tsv.gz +// and use a script like this to get each column +/* +import csv +import sys +def write_second_column(input_path, output_path): + csv.field_size_limit(sys.maxsize) + with open(input_path, 'r') as input_file, open(output_path, 'w') as output_file: + tsv_reader = csv.reader(input_file, delimiter='\t') + tsv_writer = csv.writer(output_file, delimiter='\t') + for row in tsv_reader: + tsv_writer.writerow([row[2]]) +#write_second_column('/Users/x/fulldocs.tsv', '/Users/x/first_column_fulldocs.tsv') +#write_second_column('/Users/x/fulldocs.tsv', '/Users/x/second_column_fulldocs.tsv') +write_second_column('/Users/x/fulldocs.tsv', '/Users/x/third_column_fulldocs.tsv') +*/ +fn main() { + let file_paths = [ + "/home/x/first_column_fulldocs.tsv", + "/home/x/second_column_fulldocs.tsv", + "/home/x/third_column_fulldocs_chunk_0.tsv", + ]; + for file_path in file_paths { + benchmark(file_path); + } +} diff --git a/lance-artifact/rust/compression/fsst/src/fsst.rs b/lance-artifact/rust/compression/fsst/src/fsst.rs new file mode 100644 index 000000000..d00a6ed80 --- /dev/null +++ b/lance-artifact/rust/compression/fsst/src/fsst.rs @@ -0,0 +1,1744 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +// the first 32-bits of a FSST compressed file is the FSST magic number +const FSST_MAGIC: u64 = 0x46535354 << 32; // "FSST" +// when the code is FSST_ESC, the next byte should be interpreted as is +const FSST_ESC: u8 = 255; +// when building symbol table, we have a maximum of 512 symbols, so we can use 9 bits to represent the code +const FSST_CODE_BITS: u16 = 9; +// when building symbol table, we use the first 256 codes to represent the index itself, for example, code 0 represents byte 0 +const FSST_CODE_BASE: u16 = 256; + +// code 512, which we can never reach(maximum code is 511) +const FSST_CODE_MAX: u16 = 1 << FSST_CODE_BITS; +// all code bits set +const FSST_CODE_MASK: u16 = FSST_CODE_MAX - 1; +// we construct FSST symbol tables using a random sample of about 16KB (1<<14) +const FSST_SAMPLETARGET: usize = 1 << 14; +const FSST_SAMPLEMAXSZ: usize = 2 * FSST_SAMPLETARGET; + +// if the input size is less than 32 KB, we mark the file header and copy the input to the output as is +pub const FSST_LEAST_INPUT_SIZE: usize = 32 * 1024; + +// if the max length of the input strings are less than `FSST_LEAST_INPUT_MAX_LENGTH`, we shouldn't use FSST. +pub const FSST_LEAST_INPUT_MAX_LENGTH: u64 = 5; + +// we only use the lower 32 bits in icl, so we can use 1 << 32 to represent a free slot in the hash table +const FSST_ICL_FREE: u64 = 1 << 32; +// in the icl field of a symbol, the symbol length is stored in 4 bits starting from the 28th bit +const CODE_LEN_SHIFT_IN_ICL: u64 = 28; +// in the icl field of a symbol, the symbol code is stored in the 12 bits starting from the 16th bit +const CODE_SHIFT_IN_ICL: u64 = 16; + +const CODE_LEN_SHIFT_IN_CODE: u64 = 12; + +const FSST_HASH_TAB_SIZE: usize = 1024; +const FSST_HASH_PRIME: u64 = 2971215073; +const FSST_SHIFT: usize = 15; +#[inline] +fn fsst_hash(w: u64) -> u64 { + w.wrapping_mul(FSST_HASH_PRIME) ^ ((w.wrapping_mul(FSST_HASH_PRIME)) >> FSST_SHIFT) +} + +const MAX_SYMBOL_LENGTH: usize = 8; + +pub const FSST_SYMBOL_TABLE_SIZE: usize = 8 + 256 * 8 + 256; // 8 bytes for the header, 256 symbols(8 bytes each), 256 bytes for lens + +use arrow_array::OffsetSizeTrait; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; +use std::cmp::Ordering; +use std::collections::BinaryHeap; +use std::collections::HashSet; +use std::io; +use std::marker::PhantomData; +use std::ptr; + +#[inline] +fn fsst_unaligned_load_unchecked(v: *const u8) -> u64 { + // SAFETY: the caller must guarantee that `v` points to at least 8 readable bytes. All callers + // uphold this: `compress_bulk` loads from a 520-byte stack buffer at an offset < 511 (leaving + // >= 8 bytes), `build_symbol_table` guards the load with `word.len() > 7 && curr < word.len() - 7`, + // `find_longest_symbol_from_char_slice` copies into a stack `[u8; 8]` before loading, and + // `FsstDecoder::init` reads symbols from a `symbol_table` buffer already validated to be + // exactly `FSST_SYMBOL_TABLE_SIZE` bytes. + unsafe { ptr::read_unaligned(v as *const u64) } +} + +#[derive(Default, Copy, Clone, PartialEq, Eq)] +struct Symbol { + // the byte sequence that this symbol stands for + val: u64, + + // icl = u64 ignoredBits:16,code:12,length:4,unused:32 -- but we avoid exposing this bit-field notation + // use a single u64 to be sure "code" is accessed with one load and can be compared with one comparison + icl: u64, +} + +use std::fmt; + +impl fmt::Display for Symbol { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let bytes = self.val.to_ne_bytes(); + for i in 0..self.symbol_len() { + write!(f, "{}", bytes[i as usize] as char)?; + } + write!(f, "\t")?; + write!( + f, + "ignoredBits: {}, code: {}, length: {}", + self.ignored_bits(), + self.code(), + self.symbol_len() + )?; + Ok(()) + } +} + +impl fmt::Debug for Symbol { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let bytes = self.val.to_ne_bytes(); + for i in 0..self.symbol_len() { + write!(f, "{}", bytes[i as usize] as char)?; + } + write!(f, "\t")?; + write!( + f, + "ignoredBits: {}, code: {}, length: {}", + self.ignored_bits(), + self.code(), + self.symbol_len() + )?; + Ok(()) + } +} + +impl Symbol { + fn new() -> Self { + Self { + val: 0, + icl: FSST_ICL_FREE, + } + } + + fn from_char(c: u8, code: u16) -> Self { + Self { + val: c as u64, + // in a symbol which represents a single character, 56 bits(7 bytes) are ignored, code length is 1 + icl: (1 << CODE_LEN_SHIFT_IN_ICL) | ((code as u64) << CODE_SHIFT_IN_ICL) | 56, + } + } + + fn set_code_len(&mut self, code: u16, len: u32) { + self.icl = ((len as u64) << CODE_LEN_SHIFT_IN_ICL) + | ((code as u64) << CODE_SHIFT_IN_ICL) + | ((8u64.saturating_sub(len as u64)) * 8); + } + + #[inline] + fn symbol_len(&self) -> u32 { + (self.icl >> CODE_LEN_SHIFT_IN_ICL) as u32 + } + + #[inline] + fn code(&self) -> u16 { + ((self.icl >> CODE_SHIFT_IN_ICL) & FSST_CODE_MASK as u64) as u16 + } + + // ignoredBits is (8-length)*8, which is the amount of high bits to zero in the input word before comparing with the hashtable key + // it could of course be computed from len during lookup, but storing it precomputed in some loose bits is faster + #[inline] + fn ignored_bits(&self) -> u32 { + (self.icl & u16::MAX as u64) as u32 + } + + #[inline] + fn first(&self) -> u8 { + assert!(self.symbol_len() >= 1); + (0xFF & self.val) as u8 + } + + #[inline] + fn first2(&self) -> u16 { + assert!(self.symbol_len() >= 2); + (0xFFFF & self.val) as u16 + } + + #[inline] + fn hash(&self) -> u64 { + let v = 0xFFFFFF & self.val; + fsst_hash(v) + } + + // right is the substring follows left + // for example, in "hello", + // "llo" is the substring that follows "he" + fn concat(left: Self, right: Self) -> Self { + let mut s = Self::new(); + let mut length = left.symbol_len() + right.symbol_len(); + if length > MAX_SYMBOL_LENGTH as u32 { + length = MAX_SYMBOL_LENGTH as u32; + } + s.set_code_len(FSST_CODE_MASK, length); + s.val = (right.val << (8 * left.symbol_len())) | left.val; + s + } +} + +// Symbol that can be put in a queue, ordered on gain +#[derive(Clone)] +struct QSymbol { + symbol: Symbol, + // the gain field is only used in the symbol queue that sorts symbols on gain + gain: u32, +} + +impl PartialEq for QSymbol { + fn eq(&self, other: &Self) -> bool { + self.symbol.val == other.symbol.val && self.symbol.icl == other.symbol.icl + } +} + +impl Ord for QSymbol { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.gain + .cmp(&other.gain) + .then_with(|| other.symbol.val.cmp(&self.symbol.val)) + } +} + +impl PartialOrd for QSymbol { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Eq for QSymbol {} + +use std::hash::{Hash, Hasher}; + +impl Hash for QSymbol { + // this hash algorithm follows the C++ implementation of the FSST in the paper + fn hash(&self, state: &mut H) { + let mut k = self.symbol.val; + const M: u64 = 0xc6a4a7935bd1e995; + const R: u32 = 47; + let mut h: u64 = 0x8445d61a4e774912 ^ (8u64.wrapping_mul(M)); + k = k.wrapping_mul(M); + k ^= k >> R; + k = k.wrapping_mul(M); + h ^= k; + h = h.wrapping_mul(M); + h ^= h >> R; + h = h.wrapping_mul(M); + h ^= h >> R; + h.hash(state); + } +} + +#[derive(Clone)] +struct SymbolTable { + short_codes: [u16; 65536], + byte_codes: [u16; 256], + symbols: [Symbol; FSST_CODE_MAX as usize], + hash_tab: [Symbol; FSST_HASH_TAB_SIZE], + n_symbols: u16, + terminator: u16, + // in a finalized symbol table, symbols are arranged by their symbol length, + // in the order of 2, 3, 4, 5, 6, 7, 8, 1, codes < suffix_lim are 2 bytes codes that don't have a longer suffix + suffix_lim: u16, + len_histo: [u8; FSST_CODE_BITS as usize], +} + +impl std::fmt::Display for SymbolTable { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "A FSST SymbolTable after finalize():")?; + writeln!(f, "n_symbols: {}", self.n_symbols)?; + for i in 0_usize..self.n_symbols as usize { + writeln!(f, "symbols[{}]: {}", i, self.symbols[i])?; + } + writeln!(f, "suffix_lim: {}", self.suffix_lim)?; + for i in 0..FSST_CODE_BITS { + writeln!(f, "len_histo[{}]: {}", i, self.len_histo[i as usize])?; + } + Ok(()) + } +} + +impl std::fmt::Debug for SymbolTable { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "A FSST SymbolTable before finalize():")?; + writeln!(f, "n_symbols: {}", self.n_symbols)?; + for i in FSST_CODE_BASE as usize..FSST_CODE_BASE as usize + self.n_symbols as usize { + writeln!(f, "symbols[{}]: {}", i, self.symbols[i])?; + } + writeln!(f, "suffix_lim: {}", self.suffix_lim)?; + for i in 0..FSST_CODE_BITS { + writeln!(f, "len_histo[{}]: {}\n", i, self.len_histo[i as usize])?; + } + Ok(()) + } +} + +impl SymbolTable { + fn new() -> Self { + let mut symbols = [Symbol::new(); FSST_CODE_MAX as usize]; + for (i, symbol) in symbols.iter_mut().enumerate().take(256) { + *symbol = Symbol::from_char(i as u8, i as u16); + } + let unused = Symbol::from_char(0, FSST_CODE_MASK); + for i in 256..FSST_CODE_MAX { + symbols[i as usize] = unused; + } + let s = Symbol::new(); + let hash_tab = [s; FSST_HASH_TAB_SIZE]; + let mut byte_codes = [0; 256]; + for (i, byte_code) in byte_codes.iter_mut().enumerate() { + *byte_code = i as u16; + } + let mut short_codes = [FSST_CODE_MASK; 65536]; + for i in 0..=65535_u16 { + short_codes[i as usize] = i & 0xFF; + } + Self { + short_codes, + byte_codes, + symbols, + hash_tab, + n_symbols: 0, + terminator: 256, + suffix_lim: FSST_CODE_MAX, + len_histo: [0; FSST_CODE_BITS as usize], + } + } + + fn clear(&mut self) { + for i in 0..256 { + self.symbols[i] = Symbol::from_char(i as u8, i as u16); + } + let unused = Symbol::from_char(0, FSST_CODE_MASK); + for i in 256..FSST_CODE_MAX { + self.symbols[i as usize] = unused; + } + for i in 0..256 { + self.byte_codes[i] = i as u16; + } + for i in 0..=65535_u16 { + self.short_codes[i as usize] = i & 0xFF; + } + let s = Symbol::new(); + for i in 0..FSST_HASH_TAB_SIZE { + self.hash_tab[i] = s; + } + for i in 0..FSST_CODE_BITS as usize { + self.len_histo[i] = 0; + } + self.n_symbols = 0; + } + + fn hash_insert(&mut self, s: Symbol) -> bool { + let idx = (s.hash() & (FSST_HASH_TAB_SIZE as u64 - 1)) as usize; + let taken = self.hash_tab[idx].icl < FSST_ICL_FREE; + if taken { + return false; // collision in hash table + } + self.hash_tab[idx].icl = s.icl; + self.hash_tab[idx].val = s.val & (u64::MAX >> (s.ignored_bits())); + true + } + + fn add(&mut self, mut s: Symbol) -> bool { + assert!(FSST_CODE_BASE + self.n_symbols < FSST_CODE_MAX); + let len = s.symbol_len(); + s.set_code_len(FSST_CODE_BASE + self.n_symbols, len); + if len == 1 { + self.byte_codes[s.first() as usize] = FSST_CODE_BASE + self.n_symbols; + } else if len == 2 { + self.short_codes[s.first2() as usize] = FSST_CODE_BASE + self.n_symbols; + } else if !self.hash_insert(s) { + return false; + } + self.symbols[(FSST_CODE_BASE + self.n_symbols) as usize] = s; + self.n_symbols += 1; + self.len_histo[(len - 1) as usize] += 1; + true + } + + fn find_longest_symbol_from_char_slice(&self, input: &[u8]) -> u16 { + let len = if input.len() >= MAX_SYMBOL_LENGTH { + MAX_SYMBOL_LENGTH + } else { + input.len() + }; + if len < 2 { + return self.byte_codes[input[0] as usize] & FSST_CODE_MASK; + } + if len == 2 { + let short_code = ((input[1] as usize) << 8) | input[0] as usize; + if self.short_codes[short_code] >= FSST_CODE_BASE { + return self.short_codes[short_code] & FSST_CODE_MASK; + } else { + return self.byte_codes[input[0] as usize] & FSST_CODE_MASK; + } + } + let mut input_in_1_word = [0; 8]; + input_in_1_word[..len].copy_from_slice(&input[..len]); + let input_in_u64 = fsst_unaligned_load_unchecked(input_in_1_word.as_ptr()); + let hash_idx = fsst_hash(input_in_u64) as usize & (FSST_HASH_TAB_SIZE - 1); + let s_in_hash_tab = self.hash_tab[hash_idx]; + if s_in_hash_tab.icl < FSST_ICL_FREE + && s_in_hash_tab.val == (input_in_u64 & (u64::MAX >> s_in_hash_tab.ignored_bits())) + { + return s_in_hash_tab.code(); + } + self.byte_codes[input[0] as usize] & FSST_CODE_MASK + } + + // rationale for finalize: + // - during symbol table construction, we may create more than 256 codes, but bring it down to max 255 in the last makeTable() + // consequently we needed more than 8 bits during symbol table construction, but can simplify the codes to single bytes in finalize() + // (this feature is in fact lo longer used, but could still be exploited: symbol construction creates no more than 255 symbols in each pass) + // - we not only reduce the amount of codes to <255, but also *reorder* the symbols and renumber their codes, for higher compression perf. + // we renumber codes so they are grouped by length, to allow optimized scalar string compression (byteLim and suffixLim optimizations). + // - we make the use of byteCode[] no longer necessary by inserting single-byte codes in the free spots of shortCodes[] + // Using shortCodes[] only makes compression faster. When creating the symbolTable, however, using shortCodes[] for the single-byte + // symbols is slow, as each insert touches 256 positions in it. This optimization was added when optimizing symbolTable construction time. + // + // In all, we change the layout and coding, as follows.. + // + // before finalize(): + // - The real symbols are symbols[256..256+nSymbols>. As we may have nSymbols > 255 + // - The first 256 codes are pseudo symbols (all escaped bytes) + // + // after finalize(): + // - table layout is symbols[0..nSymbols>, with nSymbols < 256. + // - Real codes are [0,nSymbols>. 8-th bit not set. + // - Escapes in shortCodes have the 8th bit set (value: 256+255=511). 255 because the code to be emitted is the escape byte 255 + // - symbols are grouped by length: 2,3,4,5,6,7,8, then 1 (single-byte codes last) + // the two-byte codes are split in two sections: + // - first section contains codes for symbols for which there is no longer symbol (no suffix). It allows an early-out during compression + // + // finally, shortCodes[] is modified to also encode all single-byte symbols (hence byteCodes[] is not required on a critical path anymore). + fn finalize(&mut self) { + assert!(self.n_symbols < FSST_CODE_BASE); + let mut new_code: [u16; 256] = [0; 256]; + let mut rsum: [u8; 8] = [0; 8]; + let byte_lim = self.n_symbols - self.len_histo[0] as u16; + + rsum[0] = byte_lim as u8; // 1-byte codes are highest + for i in 1..7 { + rsum[i + 1] = rsum[i] + self.len_histo[i]; + } + + let mut suffix_lim = 0; + let mut j = rsum[2]; + for i in 0..self.n_symbols { + let mut s1 = self.symbols[(FSST_CODE_BASE + i) as usize]; + let len = s1.symbol_len(); + let opt = if len == 2 { self.n_symbols } else { 0 }; + if opt != 0 { + let mut has_suffix = false; + let first2 = s1.first2(); + for k in 0..opt { + let s2 = self.symbols[(FSST_CODE_BASE + k) as usize]; + if k != i && s2.symbol_len() > 2 && first2 == s2.first2() { + has_suffix = true; + } + } + new_code[i as usize] = if has_suffix { + suffix_lim += 1; + suffix_lim - 1 + } else { + j -= 1; + j as u16 + }; + } else { + new_code[i as usize] = rsum[(len - 1) as usize] as u16; + rsum[(len - 1) as usize] += 1; + } + s1.set_code_len(new_code[i as usize], len); + self.symbols[new_code[i as usize] as usize] = s1; + } + + for i in 0..256 { + if (self.byte_codes[i] & FSST_CODE_MASK) >= FSST_CODE_BASE { + self.byte_codes[i] = + new_code[(self.byte_codes[i] & 0xFF) as usize] | (1 << CODE_LEN_SHIFT_IN_CODE); + } else { + self.byte_codes[i] = 511 | (1 << CODE_LEN_SHIFT_IN_CODE); + } + } + + for i in 0..65536 { + if (self.short_codes[i] & FSST_CODE_MASK) > FSST_CODE_BASE { + self.short_codes[i] = + new_code[(self.short_codes[i] & 0xFF) as usize] | (2 << CODE_LEN_SHIFT_IN_CODE); + } else { + self.short_codes[i] = self.byte_codes[i & 0xFF] | (1 << CODE_LEN_SHIFT_IN_CODE); + } + } + + for i in 0..FSST_HASH_TAB_SIZE { + if self.hash_tab[i].icl < FSST_ICL_FREE { + self.hash_tab[i] = + self.symbols[new_code[(self.hash_tab[i].code() & 0xFF) as usize] as usize]; + } + } + self.suffix_lim = suffix_lim; + } +} + +#[derive(Clone)] +struct Counters { + count1: Vec, + count2: Vec>, +} + +impl Counters { + fn new() -> Self { + Self { + count1: vec![0; FSST_CODE_MAX as usize], + count2: vec![vec![0; FSST_CODE_MAX as usize]; FSST_CODE_MAX as usize], + } + } + + #[inline] + fn count1_set(&mut self, pos1: usize, val: u16) { + self.count1[pos1] = val; + } + + #[inline] + fn count1_inc(&mut self, pos1: u16) { + self.count1[pos1 as usize] = self.count1[pos1 as usize].saturating_add(1); + } + + #[inline] + fn count2_inc(&mut self, pos1: usize, pos2: usize) { + self.count2[pos1][pos2] = self.count2[pos1][pos2].saturating_add(1); + } + + #[inline] + fn count1_get(&self, pos1: usize) -> u16 { + self.count1[pos1] + } + + #[inline] + fn count2_get(&self, pos1: usize, pos2: usize) -> u16 { + self.count2[pos1][pos2] + } +} + +#[inline] +fn is_escape_code(pos: u16) -> bool { + pos < FSST_CODE_BASE +} + +// make_sample selects strings randoms from the input, and returns a set of strings of size around FSST_SAMPLETARGET +fn make_sample(in_buf: &[u8], offsets: &[T]) -> (Vec, Vec) { + let total_size = in_buf.len(); + if total_size <= FSST_SAMPLETARGET { + return (in_buf.to_vec(), offsets.to_vec()); + } + let mut sample_buf = Vec::with_capacity(FSST_SAMPLEMAXSZ); + let mut sample_offsets: Vec = Vec::new(); + + sample_offsets.push(T::from_usize(0).unwrap()); + let mut rng = StdRng::from_os_rng(); + while sample_buf.len() < FSST_SAMPLETARGET { + let rand_num = rng.random_range(0..offsets.len()) % (offsets.len() - 1); + sample_buf.extend_from_slice( + &in_buf[offsets[rand_num].as_usize()..offsets[rand_num + 1].as_usize()], + ); + sample_offsets.push(T::from_usize(sample_buf.len()).unwrap()); + } + sample_offsets.push(T::from_usize(sample_buf.len()).unwrap()); + (sample_buf, sample_offsets) +} + +// build_symbol_table constructs a symbol table from a sample of the input +fn build_symbol_table( + sample_buf: Vec, + sample_offsets: Vec, +) -> io::Result> { + let mut st = SymbolTable::new(); + let mut best_table = SymbolTable::new(); + // worst case (everything exception), will be updated later + let mut best_gain = T::zero() - T::from_usize(FSST_SAMPLEMAXSZ).unwrap(); + + let mut byte_histo = [0; 256]; + for c in &sample_buf { + byte_histo[*c as usize] += 1; + } + let mut curr_min_histo = FSST_SAMPLEMAXSZ; + + for (i, this_byte_histo) in byte_histo.iter().enumerate() { + if *this_byte_histo < curr_min_histo { + curr_min_histo = *this_byte_histo; + st.terminator = i as u16; + } + } + + // Compress sample, and compute (pair-)frequencies + let compress_count = |st: &mut SymbolTable, sample_frac: usize| -> (Box, T) { + let mut gain = T::from_usize(0).unwrap(); + let mut counters = Counters::new(); + + for i in 1..sample_offsets.len() { + if sample_offsets[i] == sample_offsets[i - 1] { + continue; + } + let word = &sample_buf[sample_offsets[i - 1].as_usize()..sample_offsets[i].as_usize()]; + + let mut curr = 0; + let mut curr_code; + let mut prev_code = st.find_longest_symbol_from_char_slice(&word[curr..]); + curr += st.symbols[prev_code as usize].symbol_len() as usize; + + // Avoid arithmetic on Option + let symbol_len = st.symbols[prev_code as usize].symbol_len() as usize; + let escape_cost = if is_escape_code(prev_code) { 1 } else { 0 }; + let gain_contribution = symbol_len.saturating_sub(1 + escape_cost); + gain += T::from_usize(gain_contribution).unwrap(); + + while curr < word.len() { + counters.count1_inc(prev_code); + let symbol_len; + + if st.symbols[prev_code as usize].symbol_len() != 1 { + counters.count1_inc(word[curr] as u16); + } + + if word.len() > 7 && curr < word.len() - 7 { + let mut this_64_bit_word: u64 = + fsst_unaligned_load_unchecked(word[curr..].as_ptr()); + let code = this_64_bit_word & 0xFFFFFF; + let idx = fsst_hash(code) as usize & (FSST_HASH_TAB_SIZE - 1); + let s: Symbol = st.hash_tab[idx]; + let short_code = + st.short_codes[(this_64_bit_word & 0xFFFF) as usize] & FSST_CODE_MASK; + this_64_bit_word &= 0xFFFFFFFFFFFFFFFF >> s.icl as u8; + if (s.icl < FSST_ICL_FREE) & (s.val == this_64_bit_word) { + curr_code = s.code(); + symbol_len = s.symbol_len(); + } else if short_code >= FSST_CODE_BASE { + curr_code = short_code; + symbol_len = 2; + } else { + curr_code = + st.byte_codes[(this_64_bit_word & 0xFF) as usize] & FSST_CODE_MASK; + symbol_len = 1; + } + } else { + curr_code = st.find_longest_symbol_from_char_slice(&word[curr..]); + symbol_len = st.symbols[curr_code as usize].symbol_len(); + } + + // Avoid arithmetic on Option + let symbol_len_usize = symbol_len as usize; + let escape_cost = if is_escape_code(curr_code) { 1 } else { 0 }; + let gain_contribution = symbol_len_usize.saturating_sub(1 + escape_cost); + gain += T::from_usize(gain_contribution).unwrap(); + + // no need to count pairs in final round + if sample_frac < 128 { + // consider the symbol that is the concatenation of the last two symbols + counters.count2_inc(prev_code as usize, curr_code as usize); + if symbol_len > 1 { + counters.count2_inc(prev_code as usize, word[curr] as usize); + } + } + curr += symbol_len as usize; + prev_code = curr_code; + } + counters.count1_inc(prev_code); + } + (Box::new(counters), gain) + }; + + let make_table = |st: &mut SymbolTable, counters: &mut Counters, sample_frac: usize| { + let mut candidates: HashSet = HashSet::new(); + + counters.count1_set(st.terminator as usize, u16::MAX); + + let add_or_inc = |cands: &mut HashSet, s: Symbol, count: u64| { + if count < (5 * sample_frac as u64) / 128 { + return; + } + let mut q = QSymbol { + symbol: s, + gain: (count * s.symbol_len() as u64) as u32, + }; + if let Some(old_q) = cands.get(&q) { + q.gain += old_q.gain; + cands.remove(&old_q.clone()); + } + cands.insert(q); + }; + + // add candidate symbols based on counted frequencies + for pos1 in 0..FSST_CODE_BASE as usize + st.n_symbols as usize { + let cnt1 = counters.count1_get(pos1); + if cnt1 == 0 { + continue; + } + // heuristic: promoting single-byte symbols (*8) helps reduce exception rates and increases [de]compression speed + let s1 = st.symbols[pos1]; + add_or_inc( + &mut candidates, + s1, + if s1.symbol_len() == 1 { 8 } else { 1 } * cnt1 as u64, + ); + if s1.first() == st.terminator as u8 { + continue; + } + if sample_frac >= 128 + || s1.symbol_len() == MAX_SYMBOL_LENGTH as u32 + || s1.first() == st.terminator as u8 + { + continue; + } + for pos2 in 0..FSST_CODE_BASE as usize + st.n_symbols as usize { + let cnt2 = counters.count2_get(pos1, pos2); + if cnt2 == 0 { + continue; + } + + // create a new symbol + let s2 = st.symbols[pos2]; + let s3 = Symbol::concat(s1, s2); + // multi-byte symbols cannot contain the terminator byte + if s2.first() != st.terminator as u8 { + add_or_inc(&mut candidates, s3, cnt2 as u64); + } + } + } + let mut pq: BinaryHeap = BinaryHeap::new(); + for q in &candidates { + pq.push(q.clone()); + } + + // Create new symbol map using best candidates + st.clear(); + while st.n_symbols < 255 && !pq.is_empty() { + let q = pq.pop().unwrap(); + st.add(q.symbol); + } + }; + + for frac in [8, 38, 68, 98, 108, 128] { + // we do 5 rounds (sampleFrac=8,38,68,98,128) + let (mut this_counter, gain) = compress_count(&mut st, frac); + if gain >= best_gain { + // a new best solution + best_gain = gain; + best_table = st.clone(); + } + make_table(&mut st, &mut this_counter, frac); + } + best_table.finalize(); // renumber codes for more efficient compression + if best_table.n_symbols == 0 { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "Fsst failed to build symbol table, input len: {}, input_offsets len: {}", + sample_buf.len(), + sample_offsets.len() + ), + )); + } + Ok(Box::new(best_table)) +} + +fn compress_bulk( + st: &SymbolTable, + strs: &[u8], + offsets: &[T], + out: &mut Vec, + out_offsets: &mut Vec, + out_pos: &mut usize, + out_offsets_len: &mut usize, +) -> io::Result<()> { + let mut out_curr = *out_pos; + + let mut compress = |buf: &[u8], in_end: usize, out_curr: &mut usize| { + let mut in_curr = 0; + while in_curr < in_end { + let word = fsst_unaligned_load_unchecked(buf[in_curr..].as_ptr()); + let short_code = st.short_codes[(word & 0xFFFF) as usize]; + let word_first_3_byte = word & 0xFFFFFF; + let idx = fsst_hash(word_first_3_byte) as usize & (FSST_HASH_TAB_SIZE - 1); + let s = st.hash_tab[idx]; + out[*out_curr + 1] = word as u8; // speculatively write out escaped byte + let code = if s.icl < FSST_ICL_FREE && s.val == (word & (u64::MAX >> (s.icl & 0xFFFF))) + { + (s.icl >> 16) as u16 + } else { + short_code + }; + out[*out_curr] = code as u8; + in_curr += (code >> 12) as usize; + *out_curr += 1 + ((code & 256) >> 8) as usize; + } + }; + + out_offsets[0] = T::from_usize(*out_pos).unwrap(); + for i in 1..offsets.len() { + let mut in_curr = offsets[i - 1].as_usize(); + let end_curr = offsets[i].as_usize(); + let mut buf: [u8; 520] = [0; 520]; // +8 sentinel is to avoid 8-byte unaligned-loads going beyond 511 out-of-bounds + while in_curr < end_curr { + let in_end = std::cmp::min(in_curr + 511, end_curr); + { + let this_len = in_end - in_curr; + buf[..this_len].copy_from_slice(&strs[in_curr..in_end]); + buf[this_len] = st.terminator as u8; // sentinel + } + compress(&buf, in_end - in_curr, &mut out_curr); + in_curr = in_end; + } + out_offsets[i] = T::from_usize(out_curr).unwrap(); + } + + out.resize(out_curr, 0); // shrink to actual size + out_offsets.resize(offsets.len(), T::from_usize(0).unwrap()); // shrink to actual size + *out_pos = out_curr; + *out_offsets_len = offsets.len(); + Ok(()) +} + +fn decompress_bulk( + decoder: &FsstDecoder, + compressed_strs: &[u8], + offsets: &[T], + out: &mut Vec, + out_offsets: &mut Vec, + out_pos: &mut usize, + out_offsets_len: &mut usize, +) -> io::Result<()> { + let symbols = decoder.symbols; + let lens = decoder.lens; + // SAFETY invariant shared by every `unsafe` block in this closure: + // - `out` is sized to at least 8x `compressed_strs` (checked in `FsstDecoder::init`, which the + // sole public entry point always runs before reaching this function). Each code advances + // `out_curr` by `lens[code]`, which is 1..=8 for a well-formed symbol table, and each + // consumed input byte yields at most 8 output bytes, so `out_curr + 8 <= out.len()` at every + // 8-byte write, including the final one. This is why we can `write_unaligned` a full 8-byte + // word per code and advance by only the length. + // NOTE: `lens` is loaded verbatim from the (untrusted) symbol table and is NOT re-validated + // to be <= 8 on decode, and offsets (below) are likewise trusted. A corrupted table or + // offset buffer can violate these bounds; callers must supply structures produced by + // `compress` (or otherwise trusted). Hardening the decoder against corrupt input is a + // separate concern, not addressed here. + // - The only unchecked read is `read_unaligned::`, gated by `in_curr + 4 <= in_end`; the + // scalar paths use bounds-checked indexing. `in_end` is a caller-provided offset into + // `compressed_strs`; the read is sound only if `in_end <= compressed_strs.len()`, which is a + // trusted precondition (holds for encoder-produced offsets; not validated here). + let mut decompress = |mut in_curr: usize, in_end: usize, out_curr: &mut usize| { + // Do SIMD operation here by 4 bytes + while in_curr + 4 <= in_end { + let next_block; + let mut code; + let mut len; + // SAFETY: the loop guard proves `in_curr + 4 <= in_end`. Per the closure-level + // invariant, `in_end <= compressed_strs.len()` is a trusted precondition (not checked + // here), so the 4-byte read is in bounds for well-formed input. + unsafe { + next_block = + ptr::read_unaligned(compressed_strs.as_ptr().add(in_curr) as *const u32); + } + let escape_mask = (next_block & 0x80808080u32) + & ((((!next_block) & 0x7F7F7F7Fu32) + 0x7F7F7F7Fu32) ^ 0x80808080u32); + if escape_mask == 0 { + // 0th byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 1st byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 2nd byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 3rd byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + } else { + let first_escape_pos = escape_mask.trailing_zeros() >> 3; + if first_escape_pos == 3 { + // 0th byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 1st byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 2nd byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // escape byte + in_curr += 2; + out[*out_curr] = compressed_strs[in_curr - 1]; + *out_curr += 1; + } else if first_escape_pos == 2 { + // 0th byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // 1st byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // escape byte + in_curr += 2; + out[*out_curr] = compressed_strs[in_curr - 1]; + *out_curr += 1; + } else if first_escape_pos == 1 { + // 0th byte + code = compressed_strs[in_curr] as usize; + len = lens[code] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += len; + + // escape byte + in_curr += 2; + out[*out_curr] = compressed_strs[in_curr - 1]; + *out_curr += 1; + } else { + // escape byte + in_curr += 2; + out[*out_curr] = compressed_strs[in_curr - 1]; + *out_curr += 1; + } + } + } + + // handle the remaining bytes + if in_curr + 2 <= in_end { + out[*out_curr] = compressed_strs[in_curr + 1]; + if compressed_strs[in_curr] != FSST_ESC { + let code = compressed_strs[in_curr] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += lens[code] as usize; + if compressed_strs[in_curr] != FSST_ESC { + let code = compressed_strs[in_curr] as usize; + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + in_curr += 1; + *out_curr += lens[code] as usize; + } else { + in_curr += 2; + out[*out_curr] = compressed_strs[in_curr - 1]; + *out_curr += 1; + } + } else { + in_curr += 2; + *out_curr += 1; + } + } + + if in_curr < in_end { + // last code cannot be an escape code + let code = compressed_strs[in_curr] as usize; + // SAFETY: see the closure-level invariant. This is the final write and has no + // subsequent write to cover its slack, so it is the tightest case: `out_curr` is at + // most 8*(consumed_input_bytes - 1), and the 8-byte store lands within `out.len()` + // precisely because the caller sized `out` to 8x the input. + unsafe { + let src = symbols[code]; + ptr::write_unaligned(out.as_mut_ptr().add(*out_curr) as *mut u64, src); + } + *out_curr += lens[code] as usize; + } + }; + + let mut out_curr = *out_pos; + out_offsets[0] = T::from_usize(*out_pos).unwrap(); + for i in 1..offsets.len() { + let in_curr = offsets[i - 1].as_usize(); + let in_end = offsets[i].as_usize(); + decompress(in_curr, in_end, &mut out_curr); + out_offsets[i] = T::from_usize(out_curr).unwrap(); + } + out.resize(out_curr, 0); + out_offsets.resize(offsets.len(), T::from_usize(0).unwrap()); + *out_pos = out_curr; + *out_offsets_len = offsets.len(); + Ok(()) +} + +struct FsstEncoder { + symbol_table: Box, + // when in_buf is less than FSST_LEAST_INPUT_SIZE, we simply copy the input to the output + encoder_switch: bool, + _phantom: PhantomData, +} + +impl FsstEncoder { + fn new() -> Self { + Self { + symbol_table: Box::new(SymbolTable::new()), + encoder_switch: false, + _phantom: PhantomData, + } + } + + fn init( + &mut self, + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &[u8], + out_offsets_buf: &[T], + symbol_table: &[u8], + ) -> io::Result<()> { + // should we have a symbol_table MAGIC footer here? + if symbol_table.len() != FSST_SYMBOL_TABLE_SIZE { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "the symbol table buffer for FSST encoder must have size {}", + FSST_SYMBOL_TABLE_SIZE + ), + )); + } + + if in_buf.len() < FSST_LEAST_INPUT_SIZE { + return Ok(()); + } + + // currently, we make sure the compression output buffer has at least the same size as the input buffer, + if in_buf.len() > out_buf.len() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "output buffer ({}) too small for FSST encoder (need at least {})", + out_buf.len(), + in_buf.len() + ), + )); + } + if in_offsets_buf.len() > out_offsets_buf.len() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "output offsets buffer ({}) too small for FSST encoder (need at least {})", + out_offsets_buf.len(), + in_offsets_buf.len() + ), + )); + } + + self.encoder_switch = true; + let (sample, sample_offsets) = make_sample(in_buf, in_offsets_buf); + let st = build_symbol_table(sample, sample_offsets)?; + self.symbol_table = st; + Ok(()) + } + + fn export(&self, symbol_table_buf: &mut [u8]) -> io::Result<()> { + let st = &self.symbol_table; + + let st_info: u64 = FSST_MAGIC + | ((self.encoder_switch as u64) << 24) + | (((st.suffix_lim & 255) as u64) << 16) + | (((st.terminator & 255) as u64) << 8) + | ((st.n_symbols & 255) as u64); + + let st_info_bytes = st_info.to_ne_bytes(); + let mut pos = 0; + symbol_table_buf[pos..pos + st_info_bytes.len()].copy_from_slice(&st_info_bytes); + + pos += st_info_bytes.len(); + + for i in 0..st.n_symbols as usize { + let s = st.symbols[i]; + let s_bytes = s.val.to_ne_bytes(); + symbol_table_buf[pos..pos + s_bytes.len()].copy_from_slice(&s_bytes); + pos += s_bytes.len(); + } + for i in 0..st.n_symbols as usize { + let this_len = st.symbols[i].symbol_len(); + symbol_table_buf[pos] = this_len as u8; + pos += 1; + } + Ok(()) + } + + fn compress( + &mut self, + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &mut Vec, + out_offsets_buf: &mut Vec, + symbol_table_buf: &mut [u8], + ) -> io::Result<()> { + self.init( + in_buf, + in_offsets_buf, + out_buf, + out_offsets_buf, + symbol_table_buf, + )?; + self.export(symbol_table_buf)?; + + // if the input buffer is less than FSST_LEAST_INPUT_SIZE, we simply copy the input to the output + if !self.encoder_switch { + out_buf.resize(in_buf.len(), 0); + out_buf.copy_from_slice(in_buf); + out_offsets_buf.resize(in_offsets_buf.len(), T::from_usize(0).unwrap()); + out_offsets_buf.copy_from_slice(in_offsets_buf); + return Ok(()); + } + let mut out_pos = 0; + let mut out_offsets_len = 0; + compress_bulk( + &self.symbol_table, + in_buf, + in_offsets_buf, + out_buf, + out_offsets_buf, + &mut out_pos, + &mut out_offsets_len, + )?; + Ok(()) + } +} + +const FSST_CORRUPT: u64 = 32774747032022883; // 7-byte number in little endian containing "corrupt" +struct FsstDecoder { + lens: [u8; 256], + symbols: [u64; 256], + decoder_switch_on: bool, + _phantom: PhantomData, +} + +impl FsstDecoder { + fn new() -> Self { + Self { + lens: [0; 256], + symbols: [FSST_CORRUPT; 256], + decoder_switch_on: false, + _phantom: PhantomData, + } + } + + fn init( + &mut self, + symbol_table: &[u8], + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &[u8], + out_offsets_buf: &[T], + ) -> io::Result<()> { + let st_info = u64::from_ne_bytes(symbol_table[..8].try_into().unwrap()); + if st_info & FSST_MAGIC != FSST_MAGIC { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "the input buffer is not a valid FSST compressed data", + )); + } + + if symbol_table.len() != FSST_SYMBOL_TABLE_SIZE { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "the symbol table buffer for FSST decoder must have size {}", + FSST_SYMBOL_TABLE_SIZE + ), + )); + } + + self.decoder_switch_on = (st_info & (1 << 24)) != 0; + // A single 1-byte code can decode to a symbol of up to MAX_SYMBOL_LENGTH (8) bytes, so the + // decoded output can be up to 8x the input. `decompress_bulk` also relies on this bound: it + // writes a full 8-byte word per code (advancing only by the symbol length), so the output + // buffer must be large enough that even the final write stays in bounds. Require out_buf to + // be at least 8x in_buf. `checked_mul` guards against `in_buf.len() * 8` wrapping on 32-bit + // targets (an input >= 512 MiB would otherwise bypass the check); treat overflow as too + // small. + if self.decoder_switch_on + && in_buf + .len() + .checked_mul(8) + .is_none_or(|needed| needed > out_buf.len()) + { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "output buffer too small for FSST decoder", + )); + } + + // when decoder_switch_on is false, we make sure the out_buf is at least the same size of the in_buf, + if !self.decoder_switch_on && in_buf.len() > out_buf.len() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "output buffer too small for FSST decoder", + )); + } + + if in_offsets_buf.len() > out_offsets_buf.len() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "output offsets buffer ({}) too small for FSST decoder (need at least {})", + out_offsets_buf.len(), + in_offsets_buf.len() + ), + )); + } + let symbol_num = (st_info & 255) as u8; + let mut pos = 8; + for i in 0..symbol_num as usize { + self.symbols[i] = fsst_unaligned_load_unchecked(symbol_table[pos..].as_ptr()); + pos += 8; + } + for i in 0..symbol_num as usize { + self.lens[i] = symbol_table[pos]; + pos += 1; + } + Ok(()) + } + + fn decompress( + &mut self, + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &mut Vec, + out_offsets_buf: &mut Vec, + ) -> io::Result<()> { + if !self.decoder_switch_on { + out_buf.resize(in_buf.len(), 0); + out_buf.copy_from_slice(in_buf); + out_offsets_buf.resize(in_offsets_buf.len(), T::from_usize(0).unwrap()); + out_offsets_buf.copy_from_slice(in_offsets_buf); + return Ok(()); + } + let mut out_pos = 0; + let mut out_offsets_len = 0; + decompress_bulk( + self, + in_buf, + in_offsets_buf, + out_buf, + out_offsets_buf, + &mut out_pos, + &mut out_offsets_len, + )?; + Ok(()) + } +} + +/// This is the public API for the FSST compression, when the in_buf is less than FSST_LEAST_INPUT_SIZE, we put the FSST_MAGIC header and then copy the input to the output +/// we check to make sure the out_buf's size is at least the same as the in_buf's size, otherwise Err is returned, this is actually +/// risky as in some randomly generated data, the output size can be larger than the input size. +/// the out_offsets_buf should be at least the same size as the in_offsets_buf, otherwise Err is returned +/// the symbol_table is used to store the symbol table created by `compression`, it's size should be FSST_SYMBOL_TABLE_SIZE +/// after compression, the first 64 bits of the output buffer is the fsst header: +/// from most significant bit to least significant bit: +/// FSST_MAGIC| encoder_switch | suffix_lim | terminator | n_symbols +/// | 32-bits | 8 bits | 8 bits | 8 bits | 8 bits +/// then followed by the compressed data +/// +pub fn compress( + symbol_table: &mut [u8], + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &mut Vec, + out_offsets_buf: &mut Vec, +) -> io::Result<()> { + FsstEncoder::new().compress( + in_buf, + in_offsets_buf, + out_buf, + out_offsets_buf, + symbol_table, + )?; + Ok(()) +} +// This is the public API for the FSST decompression, when the first 32 bits of in_buf is not the FSST_MAGIC, we know the input is not a +// valid FSST compressed data and return an error +// the following 32 bits after FSST_MAGIC contains information about FSST encoding, such as decoder_switch_on, suffix_lim, terminator, n_symbols +// when the decoder_switch_on is off in the in_buf header, `decompress` first make sure the out_buf is at least the same size as the in_buf, then simply copy the +// input data to the output +// when the decoder_switch_on is on, `decompress` first make sure the out_buf is at least 8 times the size of the in_buf, then start decoding the +// data using the symbol table. The 8x bound is required for correctness: a 1-byte code can expand to +// an 8-byte symbol, and the decode loop writes a full 8-byte word per code, so a smaller buffer can +// be written out of bounds. +// the out_offsets_buf should be at least the same size as the in_offsets_buf, otherwise an error is returned +// the symbol_table is the same symbol table created by `compression` +pub fn decompress( + symbol_table: &[u8], + in_buf: &[u8], + in_offsets_buf: &[T], + out_buf: &mut Vec, + out_offsets_buf: &mut Vec, +) -> io::Result<()> { + let mut decoder = FsstDecoder::new(); + decoder.init( + symbol_table, + in_buf, + in_offsets_buf, + out_buf, + out_offsets_buf, + )?; + decoder.decompress(in_buf, in_offsets_buf, out_buf, out_offsets_buf)?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use crate::fsst::*; + use arrow_array::StringArray; + + const TEST_PARAGRAPH: &str = "ACT I. Scene I. + Elsinore. A platform before the Castle. + + Enter two Sentinels-[first,] Francisco, [who paces up and down + at his post; then] Bernardo, [who approaches him]. + + Ber. Who's there.? + Fran. Nay, answer me. Stand and unfold yourself. + Ber. Long live the King! + Fran. Bernardo? + Ber. He. + Fran. You come most carefully upon your hour. + Ber. 'Tis now struck twelve. Get thee to bed, Francisco. + Fran. For this relief much thanks. 'Tis bitter cold, + And I am sick at heart. + Ber. Have you had quiet guard? + Fran. Not a mouse stirring. + Ber. Well, good night. + If you do meet Horatio and Marcellus, + The rivals of my watch, bid them make haste. + Enter Horatio and Marcellus. + + Fran. I think I hear them. Stand, ho! Who is there? + Hor. Friends to this ground. + Mar. And liegemen to the Dane. + Fran. Give you good night. + Mar. O, farewell, honest soldier. + Who hath reliev'd you? + Fran. Bernardo hath my place. + Give you good night. Exit. + Mar. Holla, Bernardo! + Ber. Say- + What, is Horatio there ? + Hor. A piece of him. + Ber. Welcome, Horatio. Welcome, good Marcellus. + Mar. What, has this thing appear'd again to-night? + Ber. I have seen nothing. + Mar. Horatio says 'tis but our fantasy, + And will not let belief take hold of him + Touching this dreaded sight, twice seen of us. + Therefore I have entreated him along, + With us to watch the minutes of this night, + That, if again this apparition come, + He may approve our eyes and speak to it. + Hor. Tush, tush, 'twill not appear. + Ber. Sit down awhile, + And let us once again assail your ears, + That are so fortified against our story, + What we two nights have seen. + Hor. Well, sit we down, + And let us hear Bernardo speak of this. + Ber. Last night of all, + When yond same star that's westward from the pole + Had made his course t' illume that part of heaven + Where now it burns, Marcellus and myself, + The bell then beating one- + + Enter Ghost. + + Mar. Peace! break thee off! Look where it comes again! + Ber. In the same figure, like the King that's dead. + Mar. Thou art a scholar; speak to it, Horatio. + Ber. Looks it not like the King? Mark it, Horatio. + Hor. Most like. It harrows me with fear and wonder. + Ber. It would be spoke to. + Mar. Question it, Horatio. + Hor. What art thou that usurp'st this time of night + Together with that fair and warlike form + In which the majesty of buried Denmark + Did sometimes march? By heaven I charge thee speak! + Mar. It is offended. + Ber. See, it stalks away! + Hor. Stay! Speak, speak! I charge thee speak! + Exit Ghost. + Mar. 'Tis gone and will not answer. + Ber. How now, Horatio? You tremble and look pale. + Is not this something more than fantasy? + What think you on't? + Hor. Before my God, I might not this believe + Without the sensible and true avouch + Of mine own eyes. + Mar. Is it not like the King? + Hor. As thou art to thyself. + Such was the very armour he had on + When he th' ambitious Norway combated. + So frown'd he once when, in an angry parle, + He smote the sledded Polacks on the ice. + 'Tis strange. + Mar. Thus twice before, and jump at this dead hour, + With martial stalk hath he gone by our watch. + Hor. In what particular thought to work I know not; + But, in the gross and scope of my opinion, + This bodes some strange eruption to our state. + Mar. Good now, sit down, and tell me he that knows, + Why this same strict and most observant watch + So nightly toils the subject of the land, + And why such daily cast of brazen cannon + And foreign mart for implements of war; + Why such impress of shipwrights, whose sore task + Does not divide the Sunday from the week. + What might be toward, that this sweaty haste + Doth make the night joint-labourer with the day? + Who is't that can inform me?"; + + const TEST_PARAGRAPH2: &str = "Towards the end of November, during a thaw, at nine o’clock one morning, a train on the Warsaw and Petersburg railway was approaching the latter city at full speed. +The morning was so damp and misty that it was only with great difficulty that the day succeeded in breaking; +and it was impossible to distinguish anything more than a few yards away from the carriage windows. +Some of the passengers by this particular train were returning from abroad; but the third-class carriages were the best filled, chiefly with insignificant persons of various occupations and degrees, +picked up at the different stations nearer town. +All of them seemed weary, and most of them had sleepy eyes and a shivering expression, while their complexions generally appeared to have taken on the colour of the fog outside. +When day dawned, two passengers in one of the third-class carriages found themselves opposite each other. Both were young fellows, both were rather poorly dressed, both had remarkable faces, +and both were evidently anxious to start a conversation. +If they had but known why, at this particular moment, they were both remarkable persons, they would undoubtedly have wondered at the strange chance which had set them down opposite to one another in a third-class carriage of the Warsaw Railway Company. +One of them was a young fellow of about twenty-seven, not tall, with black curling hair, and small, grey, fiery eyes. His nose was broad and flat, and he had high cheek bones; his thin lips were constantly compressed into an impudent, +ironical—it might almost be called a malicious—smile; +but his forehead was high and well formed, and atoned for a good deal of the ugliness of the lower part of his face. +A special feature of this physiognomy was its death-like pallor, which gave to the whole man an indescribably emaciated appearance in spite of his hard look, +and at the same time a sort of passionate and suffering expression which did not harmonize with his impudent, +sarcastic smile and keen, self-satisfied bearing. +He wore a large fur—or rather astrachan—overcoat, which had kept him warm all night, while his neighbour had been obliged to bear the full severity of a Russian November night entirely unprepared. +His wide sleeveless mantle with a large cape to it—the sort of cloak one sees upon travellers during the winter months in Switzerland or North Italy—was by no means adapted to the long cold journey through Russia, from Eydkuhnen to St. Petersburg. +The wearer of this cloak was a young fellow, also of about twenty-six or twenty-seven years of age, slightly above the middle height, very fair, with a thin, pointed and very light coloured beard; +his eyes were large and blue, and had an intent look about them, yet that heavy expression which some people affirm to be a peculiarity as well as evidence, of an epileptic subject. +His face was decidedly a pleasant one for all that; refined, but quite colourless, except for the circumstance that at this moment it was blue with cold. +He held a bundle made up of an old faded silk handkerchief that apparently contained all his travelling wardrobe, and wore thick shoes and gaiters, his whole appearance being very un-Russian. +His black-haired neighbour inspected these peculiarities, having nothing better to do, and at length remarked, with that rude enjoyment of the discomforts of others which the common classes so often show: +“Cold?” +“Very,” said his neighbour, readily, “and this is a thaw, too. Fancy if it had been a hard frost! I never thought it would be so cold in the old country. I’ve grown quite out of the way of it.” +“What, been abroad, I suppose?” +“Yes, straight from Switzerland.” +“Wheugh! my goodness!” The black-haired young fellow whistled, and then laughed. +The conversation proceeded. The readiness of the fair-haired young man in the cloak to answer all his opposite neighbour’s questions was surprising. +He seemed to have no suspicion of any impertinence or inappropriateness in the fact of such questions being put to him. +Replying to them, he made known to the inquirer that he certainly had been long absent from Russia, more than four years; that he had been sent abroad for his health; +that he had suffered from some strange nervous malady—a kind of epilepsy, with convulsive spasms. His interlocutor burst out laughing several times at his answers; and more than ever, when to the question, “whether he had been cured?” the patient replied: +“No, they did not cure me.” +“Hey! that’s it! You stumped up your money for nothing, and we believe in those fellows, here!” remarked the black-haired individual, sarcastically."; + + const TEST_PARAGRAPH3: &str = "When the widow hurried away to Pavlofsk, she went straight to Daria Alexeyevna’s house, and telling all she knew, threw her into a state of great alarm. +Both ladies decided to communicate at once with Lebedeff, who, as the friend and landlord of the prince, was also much agitated. +Vera Lebedeff told all she knew, and by Lebedeff’s advice it was decided that all three should go to Petersburg as quickly as possible, in order to avert “what might so easily happen.” +This is how it came about that at eleven o’clock next morning Rogojin’s flat was opened by the police in the presence of Lebedeff, the two ladies, and Rogojin’s own brother, who lived in the wing. +The evidence of the porter went further than anything else towards the success of Lebedeff in gaining the assistance of the police. +He declared that he had seen Rogojin return to the house last night, accompanied by a friend, and that both had gone upstairs very secretly and cautiously. +After this there was no hesitation about breaking open the door, since it could not be got open in any other way. +Rogojin suffered from brain fever for two months. When he recovered from the attack he was at once brought up on trial for murder. +He gave full, satisfactory, and direct evidence on every point; and the prince’s name was, thanks to this, not brought into the proceedings. +Rogojin was very quiet during the progress of the trial. He did not contradict his clever and eloquent counsel, who argued that the brain fever, +or inflammation of the brain, was the cause of the crime; clearly proving that this malady had existed long before the murder was perpetrated, and had been brought on by the sufferings of the accused. +But Rogojin added no words of his own in confirmation of this view, and as before, he recounted with marvellous exactness the details of his crime. +He was convicted, but with extenuating circumstances, and condemned to hard labour in Siberia for fifteen years. He heard his sentence grimly, silently, and thoughtfully. His colossal fortune, +with the exception of the comparatively small portion wasted in the first wanton period of his inheritance, went to his brother, to the great satisfaction of the latter. +The old lady, Rogojin’s mother, is still alive, and remembers her favourite son Parfen sometimes, but not clearly. God spared her the knowledge of this dreadful calamity which had overtaken her house. +Lebedeff, Keller, Gania, Ptitsin, and many other friends of ours continue to live as before. There is scarcely any change in them, so that there is no need to tell of their subsequent doings. +Hippolyte died in great agitation, and rather sooner than he expected, about a fortnight after Nastasia Philipovna’s death. Colia was much affected by these events, +and drew nearer to his mother in heart and sympathy. Nina Alexandrovna is anxious, because he is “thoughtful beyond his years,” but he will, we think, make a useful and active man. +The prince’s further fate was more or less decided by Colia, who selected, out of all the persons he had met during the last six or seven months, Evgenie Pavlovitch, as friend and confidant. +To him he made over all that he knew as to the events above recorded, and as to the present condition of the prince. He was not far wrong in his choice. +Evgenie Pavlovitch took the deepest interest in the fate of the unfortunate “idiot,” and, thanks to his influence, the prince found himself once more with Dr. Schneider, in Switzerland. +Evgenie Pavlovitch, who went abroad at this time, intending to live a long while on the continent, being, as he often said, quite superfluous in Russia, visits his sick friend at Schneider’s every few months. +But Dr. Schneider frowns ever more and more and shakes his head; he hints that the brain is fatally injured; he does not as yet declare that his patient is incurable, but he allows himself to express the gravest fears. +Evgenie takes this much to heart, and he has a heart, as is proved by the fact that he receives and even answers letters from Colia. But besides this, +another trait in his character has become apparent, and as it is a good trait we will make haste to reveal it. +After each visit to Schneider’s establishment, Evgenie Pavlovitch writes another letter, besides that to Colia, giving the most minute particulars concerning the invalid’s condition. +In these letters is to be detected, and in each one more than the last, a growing feeling of friendship and sympathy. +The individual who corresponds thus with Evgenie Pavlovitch, and who engages so much of his attention and respect, is Vera Lebedeff. +We have never been able to discover clearly how such relations sprang up. +Of course the root of them was in the events which we have already recorded, and which so filled Vera with grief on the prince’s account that she fell seriously ill. +But exactly how the acquaintance and friendship came about, we cannot say."; + + #[test_log::test(tokio::test)] + async fn test_symbol_new() { + let st = SymbolTable::new(); + assert!(st.n_symbols == 0); + for i in 0..=255_u8 { + assert!(st.symbols[i as usize] == Symbol::from_char(i, i as u16)); + } + let s = Symbol::from_char(1, 1); + assert!(s == st.symbols[1]); + for i in 0..FSST_HASH_TAB_SIZE { + assert!(st.hash_tab[i] == Symbol::new()); + } + } + + #[test_log::test(tokio::test)] + async fn test_fsst() { + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH.len(); + let test_input = TEST_PARAGRAPH.repeat(repeat_num); + helper(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH.len(); + let test_input = TEST_PARAGRAPH.repeat(repeat_num); + helper(&test_input); + + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH2.len(); + let test_input = TEST_PARAGRAPH.repeat(repeat_num); + helper(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH2.len(); + let test_input = TEST_PARAGRAPH2.repeat(repeat_num); + helper(&test_input); + + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH3.len(); + let test_input = TEST_PARAGRAPH3.repeat(repeat_num); // Also corrected `repea_num` to `repeat_num` + helper(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH3.len(); + let test_input = TEST_PARAGRAPH3.repeat(repeat_num); // Also corrected `repea_num` to `repeat_num` + helper(&test_input); + } + + fn helper(test_input: &str) { + let lines_vec = test_input.lines().collect::>(); + let string_array = StringArray::from(lines_vec); + let mut compress_output_buf: Vec = vec![0; string_array.value_data().len()]; + let mut compress_offset_buf: Vec = vec![0; string_array.value_offsets().len()]; + let mut symbol_table = [0; FSST_SYMBOL_TABLE_SIZE]; + compress( + symbol_table.as_mut(), + string_array.value_data(), + string_array.value_offsets(), + &mut compress_output_buf, + &mut compress_offset_buf, + ) + .unwrap(); + let mut decompress_output: Vec = vec![0; compress_output_buf.len() * 8]; + let mut decompress_offsets: Vec = vec![0; compress_offset_buf.len()]; + decompress( + &symbol_table, + &compress_output_buf, + &compress_offset_buf, + &mut decompress_output, + &mut decompress_offsets, + ) + .unwrap(); + for i in 1..decompress_offsets.len() { + let s = &decompress_output + [decompress_offsets[i - 1] as usize..decompress_offsets[i] as usize]; + let original = &string_array.value_data()[string_array.value_offsets().to_vec()[i - 1] + as usize + ..string_array.value_offsets().to_vec()[i] as usize]; + assert!( + s == original, + "s: {:?}\n\n, original: {:?}", + std::str::from_utf8(s), + std::str::from_utf8(original) + ); + } + } + + #[test_log::test(tokio::test)] + async fn test_fsst_64_bit_offsets() { + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH.len(); + let test_input = TEST_PARAGRAPH.repeat(repeat_num); + helper_64_bit(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH.len(); + let test_input = TEST_PARAGRAPH.repeat(repeat_num); + helper_64_bit(&test_input); + + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH2.len(); + let test_input = TEST_PARAGRAPH2.repeat(repeat_num); + helper_64_bit(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH2.len(); + let test_input = TEST_PARAGRAPH2.repeat(repeat_num); + helper_64_bit(&test_input); + + let test_input_size = 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH3.len(); + let test_input = TEST_PARAGRAPH3.repeat(repeat_num); + helper_64_bit(&test_input); + + let test_input_size = 2 * 1024 * 1024; + let repeat_num = test_input_size / TEST_PARAGRAPH3.len(); + let test_input = TEST_PARAGRAPH3.repeat(repeat_num); + helper_64_bit(&test_input); + } + + fn helper_64_bit(test_input: &str) { + use arrow_array::LargeStringArray; + let lines_vec = test_input.lines().collect::>(); + let string_array = LargeStringArray::from(lines_vec); + let mut compress_output_buf: Vec = vec![0; string_array.value_data().len()]; + let mut compress_offset_buf: Vec = vec![0; string_array.value_offsets().len()]; + let mut symbol_table = [0; FSST_SYMBOL_TABLE_SIZE]; + compress( + symbol_table.as_mut(), + string_array.value_data(), + string_array.value_offsets(), + &mut compress_output_buf, + &mut compress_offset_buf, + ) + .unwrap(); + let mut decompress_output: Vec = vec![0; compress_output_buf.len() * 8]; + let mut decompress_offsets: Vec = vec![0; compress_offset_buf.len()]; + decompress( + &symbol_table, + &compress_output_buf, + &compress_offset_buf, + &mut decompress_output, + &mut decompress_offsets, + ) + .unwrap(); + for i in 1..decompress_offsets.len() { + let s = &decompress_output + [decompress_offsets[i - 1] as usize..decompress_offsets[i] as usize]; + let original = &string_array.value_data()[string_array.value_offsets().to_vec()[i - 1] + as usize + ..string_array.value_offsets().to_vec()[i] as usize]; + assert!( + s == original, + "s: {:?}\n\n, original: {:?}", + std::str::from_utf8(s), + std::str::from_utf8(original) + ); + } + } + + // Build a genuinely FSST-compressed (decoder_switch_on) buffer to exercise the decode-side + // output-buffer size contract. Returns (symbol_table, compressed_bytes, compressed_offsets). + fn compress_paragraph() -> ([u8; FSST_SYMBOL_TABLE_SIZE], Vec, Vec) { + let test_input = TEST_PARAGRAPH.repeat((1024 * 1024) / TEST_PARAGRAPH.len()); + let lines_vec = test_input.lines().collect::>(); + let string_array = StringArray::from(lines_vec); + let mut compress_output_buf: Vec = vec![0; string_array.value_data().len()]; + let mut compress_offset_buf: Vec = vec![0; string_array.value_offsets().len()]; + let mut symbol_table = [0; FSST_SYMBOL_TABLE_SIZE]; + compress( + symbol_table.as_mut(), + string_array.value_data(), + string_array.value_offsets(), + &mut compress_output_buf, + &mut compress_offset_buf, + ) + .unwrap(); + (symbol_table, compress_output_buf, compress_offset_buf) + } + + // The decoder writes a full 8-byte word per code, so the output buffer must be at least 8x the + // compressed input. A buffer sized below 8x must be rejected rather than written out of bounds. + #[test_log::test(tokio::test)] + async fn test_decompress_rejects_undersized_output_buffer() { + let (symbol_table, compressed, compressed_offsets) = compress_paragraph(); + // Sanity check: this input actually engaged FSST compression (decoder_switch_on). + let st_info = u64::from_ne_bytes(symbol_table[..8].try_into().unwrap()); + assert!(st_info & (1 << 24) != 0, "expected decoder_switch_on input"); + + // One byte short of the 8x requirement must be rejected. + let mut too_small = vec![0u8; compressed.len() * 8 - 1]; + let mut out_offsets = vec![0i32; compressed_offsets.len()]; + let err = decompress( + &symbol_table, + &compressed, + &compressed_offsets, + &mut too_small, + &mut out_offsets, + ) + .unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput); + + // Exactly 8x is the tight bound and must succeed. + let mut exact = vec![0u8; compressed.len() * 8]; + let mut out_offsets = vec![0i32; compressed_offsets.len()]; + decompress( + &symbol_table, + &compressed, + &compressed_offsets, + &mut exact, + &mut out_offsets, + ) + .unwrap(); + } +} diff --git a/lance-artifact/rust/compression/fsst/src/lib.rs b/lance-artifact/rust/compression/fsst/src/lib.rs new file mode 100644 index 000000000..ab6a1e9c1 --- /dev/null +++ b/lance-artifact/rust/compression/fsst/src/lib.rs @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod fsst; diff --git a/lance-artifact/rust/examples/Cargo.toml b/lance-artifact/rust/examples/Cargo.toml new file mode 100644 index 000000000..80eff4571 --- /dev/null +++ b/lance-artifact/rust/examples/Cargo.toml @@ -0,0 +1,54 @@ +[package] +name = "lance-examples" +description = "Lance examples in Rust" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme.workspace = true +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[[example]] +name = "full_text_search" +path = "src/full_text_search.rs" + +[[example]] +name = "hnsw" +path = "src/hnsw.rs" + +[[example]] +name = "ivf_hnsw" +path = "src/ivf_hnsw.rs" + +[[example]] +name = "llm_dataset_creation" +path = "src/llm_dataset_creation.rs" + +[[example]] +name = "write_read_ds" +path = "src/write_read_ds.rs" + +[dependencies] +arrow = { workspace = true } +arrow-schema = { workspace = true } +arrow-select = { workspace = true } +clap = { workspace = true, features = ["derive"] } +itertools = { workspace = true } +futures = { workspace = true } +lance = { workspace = true, features = ["aws", "azure", "gcp", "oss", "huggingface", "tencent", "goosefs"] } +lance-index = { workspace = true } +lance-core = { workspace = true } +lance-linalg = { workspace = true } +lance-datagen = { workspace = true } +object_store = {workspace = true} +tempfile = { workspace = true } +tokio = { workspace = true } +all_asserts = "2.3.1" +env_logger = "0.11.7" +hf-hub = "0.4.2" +parquet = { version = "58.0.0", default-features = false, features = ["arrow", "async"] } +tokenizers = "0.15.2" +rand.workspace = true diff --git a/lance-artifact/rust/examples/src/full_text_search.rs b/lance-artifact/rust/examples/src/full_text_search.rs new file mode 100644 index 000000000..8269f590e --- /dev/null +++ b/lance-artifact/rust/examples/src/full_text_search.rs @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Benchmark of HNSW graph. +//! +//! +#![allow(clippy::print_stdout)] +use std::collections::HashSet; +use std::sync::Arc; + +use all_asserts::assert_gt; +use arrow::array::AsArray; +use arrow::array::{Array, LargeStringArray, RecordBatch, RecordBatchIterator, UInt64Array}; +use arrow::datatypes::UInt64Type; +use arrow_schema::{DataType, Field, Schema}; +use itertools::Itertools; +use lance::Dataset; +use lance::index::DatasetIndexExt; +use lance_datagen::{RowCount, array}; +use lance_index::scalar::inverted::flat_full_text_search; +use lance_index::scalar::{FullTextSearchQuery, InvertedIndexParams}; +use object_store::path::Path; + +#[tokio::main] +async fn main() { + env_logger::init(); + + const TOTAL: usize = 10_000; + const DOC_ID: &str = "__example_doc_id"; + + let tempdir = tempfile::tempdir().unwrap(); + let dataset_dir = Path::from_filesystem_path(tempdir.path()).unwrap(); + + let create_index = true; + if create_index { + let row_id_col = Arc::new(UInt64Array::from( + (0..TOTAL).map(|i| i as u64).collect_vec(), + )); + + // Generate random words using lance-datagen + let mut words_gen = array::random_sentence(1, 100, true); + let doc_col = words_gen + .generate_default(RowCount::from(TOTAL as u64)) + .unwrap(); + let batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![ + Field::new("doc", DataType::LargeUtf8, false), + Field::new(DOC_ID, DataType::UInt64, false), + ])), + vec![doc_col.clone(), row_id_col.clone()], + ) + .unwrap(); + + let batches = RecordBatchIterator::new([Ok(batch.clone())], batch.schema()); + let mut dataset = Dataset::write(batches, dataset_dir.as_ref(), None) + .await + .unwrap(); + let params = InvertedIndexParams::default(); + let start = std::time::Instant::now(); + dataset + .create_index( + &["doc"], + lance_index::IndexType::Inverted, + None, + ¶ms, + true, + ) + .await + .unwrap(); + println!("create_index: {:?}", start.elapsed()); + } + + let dataset = Dataset::open(dataset_dir.as_ref()).await.unwrap(); + // Use a sample word for query - fetch first doc and pick a word from it + let sample_batch = dataset + .scan() + .project(&["doc"]) + .unwrap() + .limit(Some(1), None) + .unwrap() + .try_into_batch() + .await + .unwrap(); + let sample_doc = sample_batch["doc"] + .as_any() + .downcast_ref::() + .unwrap() + .value(0); + let query_string = sample_doc.split_whitespace().next().unwrap(); + let query = FullTextSearchQuery::new(query_string.to_owned()).limit(Some(10)); + println!("query: {:?}", query); + let batch = dataset + .scan() + .full_text_search(query.clone()) + .unwrap() + .try_into_batch() + .await + .unwrap(); + let index_results = batch[DOC_ID] + .as_primitive::() + .iter() + .map(|v| v.unwrap()) + .collect::>(); + + let start = std::time::Instant::now(); + dataset + .scan() + .full_text_search(query.clone()) + .unwrap() + .try_into_batch() + .await + .unwrap(); + println!("full_text_search: {:?}", start.elapsed()); + + let batch = dataset + .scan() + .project(&["doc"]) + .unwrap() + .with_row_id() + .try_into_batch() + .await + .unwrap(); + let flat_results = flat_full_text_search(&[&batch], "doc", query_string, None) + .unwrap() + .into_iter() + .collect::>(); + assert_gt!(index_results.len(), 0); + assert_eq!(index_results, flat_results); +} diff --git a/lance-artifact/rust/examples/src/hnsw.rs b/lance-artifact/rust/examples/src/hnsw.rs new file mode 100644 index 000000000..52c15bd08 --- /dev/null +++ b/lance-artifact/rust/examples/src/hnsw.rs @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! HNSW is a graph based algorithm for approximate neighbor search in high-dimensional spaces. +//! In this example, we will demonstrate how to build HNSW vector indexing against a Lance dataset. +//! run with `cargo run -v --package lance-examples --example hnsw`` +// linked to `docs/examples/Rust/hnsw.rst` +#![allow(clippy::print_stdout)] +use std::collections::HashSet; +use std::sync::Arc; + +use arrow::array::{Array, FixedSizeListArray, types::Float32Type}; +use arrow::array::{AsArray, FixedSizeListBuilder, Float32Builder}; +use arrow::datatypes::{DataType, Field, Schema}; +use arrow::record_batch::RecordBatch; +use arrow::record_batch::RecordBatchIterator; +use arrow_select::concat::concat; +use futures::stream::StreamExt; +use lance::Dataset; +use lance_index::vector::v3::subindex::IvfSubIndex; +use lance_index::vector::{ + flat::storage::FlatFloatStorage, + hnsw::{ + HNSW, + builder::{HnswBuildParams, HnswQueryParams}, + }, +}; +use lance_linalg::distance::DistanceType; + +fn ground_truth(fsl: &FixedSizeListArray, query: &[f32], k: usize) -> HashSet { + let mut dists = vec![]; + for i in 0..fsl.len() { + let dist = lance_linalg::distance::l2_distance( + query, + fsl.value(i).as_primitive::().values(), + ); + dists.push((dist, i as u32)); + } + dists.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); + dists.truncate(k); + dists.into_iter().map(|(_, i)| i).collect() +} + +pub async fn create_test_vector_dataset(output: &str, num_rows: usize, dim: i32) { + let schema = Arc::new(Schema::new(vec![Field::new( + "vector", + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float32, true)), dim), + false, + )])); + + let mut batches = Vec::new(); + + // Create a few batches + for _ in 0..2 { + let v_builder = Float32Builder::new(); + let mut list_builder = FixedSizeListBuilder::new(v_builder, dim); + + for _ in 0..num_rows { + for _ in 0..dim { + list_builder.values().append_value(rand::random::()); + } + list_builder.append(true); + } + let array = Arc::new(list_builder.finish()); + let batch = RecordBatch::try_new(schema.clone(), vec![array]).unwrap(); + batches.push(batch); + } + let batch_reader = RecordBatchIterator::new(batches.into_iter().map(Ok), schema.clone()); + println!("Writing dataset to {}", output); + Dataset::write(batch_reader, output, None).await.unwrap(); +} + +#[tokio::main] +async fn main() { + let uri: Option = None; // None means generate test data + let column = "vector"; + let ef = 100; + let max_edges = 30; + let max_level = 7; + + // 1. Generate a synthetic test data of specified dimensions + let dataset = match uri.as_deref() { + None => { + println!("No uri is provided, generating test dataset..."); + let output = "test_vectors.lance"; + create_test_vector_dataset(output, 1000, 64).await; + Dataset::open(output).await.expect("Failed to open dataset") + } + Some(uri) => Dataset::open(uri).await.expect("Failed to open dataset"), + }; + + println!("Dataset schema: {:#?}", dataset.schema()); + let batches = dataset + .scan() + .project(&[column]) + .unwrap() + .try_into_stream() + .await + .unwrap() + .then(|batch| async move { batch.unwrap().column_by_name(column).unwrap().clone() }) + .collect::>() + .await; + let arrs = batches.iter().map(|b| b.as_ref()).collect::>(); + let fsl = concat(&arrs).unwrap().as_fixed_size_list().clone(); + println!("Loaded {:?} batches", fsl.len()); + + let vector_store = Arc::new(FlatFloatStorage::new(fsl.clone(), DistanceType::L2)); + + let q = fsl.value(0); + let k = 10; + let gt = ground_truth(&fsl, q.as_primitive::().values(), k); + + for ef_construction in [15, 30, 50] { + let now = std::time::Instant::now(); + // 2. Build a hierarchical graph structure for efficient vector search using Lance API + let hnsw = HNSW::index_vectors( + vector_store.as_ref(), + HnswBuildParams::default() + .max_level(max_level) + .num_edges(max_edges) + .ef_construction(ef_construction), + ) + .unwrap(); + let construct_time = now.elapsed().as_secs_f32(); + let now = std::time::Instant::now(); + // 3. Perform vector search with different parameters and compute the ground truth using L2 distance search + let params = HnswQueryParams { + ef, + lower_bound: None, + upper_bound: None, + dist_q_c: 0.0, + use_acorn: false, + }; + let results: HashSet = hnsw + .search_basic(q.clone(), k, ¶ms, None, vector_store.as_ref()) + .unwrap() + .iter() + .map(|node| node.id) + .collect(); + let search_time = now.elapsed().as_micros(); + println!( + "level={}, ef_construct={}, ef={} recall={}: construct={:.3}s search={:.3} us", + max_level, + ef_construction, + ef, + results.intersection(>).count() as f32 / k as f32, + construct_time, + search_time + ); + } +} diff --git a/lance-artifact/rust/examples/src/ivf_hnsw.rs b/lance-artifact/rust/examples/src/ivf_hnsw.rs new file mode 100644 index 000000000..c1898e106 --- /dev/null +++ b/lance-artifact/rust/examples/src/ivf_hnsw.rs @@ -0,0 +1,155 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Run recall benchmarks for HNSW. +//! +//! run with `cargo run --release --example hnsw` +#![allow(clippy::print_stdout)] +use arrow::array::AsArray; +use arrow::array::types::Float32Type; +use clap::Parser; +use futures::TryStreamExt; +use lance::Dataset; +use lance::dataset::ProjectionRequest; +use lance::index::DatasetIndexExt; +use lance::index::vector::VectorIndexParams; +use lance_index::IndexType; +use lance_index::vector::hnsw::builder::HnswBuildParams; +use lance_index::vector::ivf::IvfBuildParams; +use lance_index::vector::sq::builder::SQBuildParams; +use lance_linalg::distance::MetricType; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Dataset URI + uri: String, + + /// Vector column name + #[arg(short, long, value_name = "NAME", default_value = "vector")] + column: Option, + + #[arg(long, default_value = "100")] + ef: usize, + + /// Max number of edges of each node. + #[arg(long, default_value = "30")] + max_edges: usize, + + #[arg(long, default_value = "7")] + max_level: u16, + + #[arg(long, default_value = "1")] + nprobe: usize, + + #[arg(short, default_value = "10")] + k: usize, + + #[arg(long, default_value = "false")] + create_index: bool, + + #[arg(long, default_value = "cosine")] + metric_type: String, +} + +#[cfg(test)] +fn ground_truth(mat: &MatrixView, query: &[f32], k: usize) -> HashSet { + let mut dists = vec![]; + for i in 0..mat.num_rows() { + let dist = lance_linalg::distance::l2_distance(query, mat.row(i).unwrap()); + dists.push((dist, i as u32)); + } + dists.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); + dists.truncate(k); + dists.into_iter().map(|(_, i)| i).collect() +} + +#[tokio::main] +async fn main() { + env_logger::init(); + let args = Args::parse(); + + let mut dataset = Dataset::open(&args.uri) + .await + .expect("Failed to open dataset"); + println!("Dataset schema: {:#?}", dataset.schema()); + + let column = args.column.as_deref().unwrap_or("vector"); + let metric_type = MetricType::try_from(args.metric_type.as_str()).unwrap(); + + let mut ivf_params = IvfBuildParams::new(128); + ivf_params.sample_rate = 20480; + let hnsw_params = HnswBuildParams::default() + .ef_construction(100) + .num_edges(15); + let pq_params = SQBuildParams::default(); + let params = + VectorIndexParams::with_ivf_hnsw_sq_params(metric_type, ivf_params, hnsw_params, pq_params); + println!("{:?}", params); + + if args.create_index { + let now = std::time::Instant::now(); + dataset + .create_index(&[column], IndexType::Vector, None, ¶ms, true) + .await + .unwrap(); + println!("build={:.3}s", now.elapsed().as_secs_f32()); + } + + println!("Loaded {} records", dataset.count_rows(None).await.unwrap()); + + let take_projection = ProjectionRequest::from_columns([column], dataset.schema()); + + let q = dataset + .take(&[0], take_projection) + .await + .unwrap() + .column(0) + .as_fixed_size_list() + .values() + .as_primitive::() + .clone(); + + let columns: &[&str] = &[]; + let mut scan = dataset.scan(); + let plan = scan + .project(columns) + .unwrap() + .with_row_id() + .nearest(column, &q, args.k) + .unwrap() + .minimum_nprobes(args.nprobe); + println!("{:?}", plan.explain_plan(true).await.unwrap()); + + let now = std::time::Instant::now(); + plan.try_into_stream() + .await + .unwrap() + .try_collect::>() + .await + .unwrap(); + println!( + "level={}, nprobe={}, k={}, search={:?}", + args.max_level, + args.nprobe, + args.k, + now.elapsed(), + ); + + let now = std::time::Instant::now(); + for _ in 0..10 { + plan.try_into_stream() + .await + .unwrap() + .try_collect::>() + .await + .unwrap(); + } + println!( + "warm up: level={}, nprobe={}, k={}, search={:?}", + args.max_level, + args.nprobe, + args.k, + now.elapsed().div_f32(10.0), + ); +} diff --git a/lance-artifact/rust/examples/src/llm_dataset_creation.rs b/lance-artifact/rust/examples/src/llm_dataset_creation.rs new file mode 100644 index 000000000..7f981156b --- /dev/null +++ b/lance-artifact/rust/examples/src/llm_dataset_creation.rs @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! This example demonstrates how to: +//! +//! 1. Download and process a text dataset in parts from huggingface +//! 2. Tokenize the text data with a custom RecordBatchReader +//! 3. Save it as a Lance dataset using Lance API +//! +//! Run with `cargo run -v --package lance-examples --example llm_dataset_creation` +//! +//! +// linked to `docs/examples/Rust/llm_dataset_creation.rst` + +use arrow::array::{Array, Int64Builder, ListBuilder, UInt32Array}; +use arrow::datatypes::{DataType, Field, Schema}; +use arrow::record_batch::RecordBatch; +use arrow::record_batch::RecordBatchReader; +use futures::StreamExt; +use hf_hub::{Repo, RepoType, api::sync::Api}; +use lance::dataset::WriteParams; +use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder; +use rand::SeedableRng; +use rand::seq::SliceRandom; +use std::error::Error; +use std::fs::File; +use std::io::Write; +use std::sync::Arc; +use tempfile::NamedTempFile; +use tokenizers::Tokenizer; + +// Implement a custom stream batch reader +struct WikiTextBatchReader { + schema: Arc, + parquet_readers: Vec>>, + current_reader_idx: usize, + current_reader: Option>, + tokenizer: Tokenizer, + num_samples: u64, + cur_samples_cnt: u64, +} + +impl WikiTextBatchReader { + fn new( + parquet_readers: Vec>, + tokenizer: Tokenizer, + num_samples: Option, + ) -> Result> { + let schema = Arc::new(Schema::new(vec![Field::new( + "input_ids", + DataType::List(Arc::new(Field::new("item", DataType::Int64, true))), + false, + )])); + + Ok(Self { + schema, + parquet_readers: parquet_readers.into_iter().map(Some).collect(), + current_reader_idx: 0, + current_reader: None, + tokenizer, + num_samples: num_samples.unwrap_or(100_000), + cur_samples_cnt: 0, + }) + } + + fn process_batch( + &mut self, + input_batch: &RecordBatch, + ) -> Result { + let num_rows = input_batch.num_rows(); + let mut token_builder = ListBuilder::new(Int64Builder::with_capacity(num_rows * 1024)); // Pre-allocate space + let mut should_break = false; + + let column = input_batch.column_by_name("text").unwrap(); + let string_array = column + .as_any() + .downcast_ref::() + .unwrap(); + for i in 0..num_rows { + if self.cur_samples_cnt >= self.num_samples { + should_break = true; + break; + } + if !Array::is_null(string_array, i) { + let text = string_array.value(i); + // Split paragraph into lines + for line in text.split('\n') { + if let Ok(encoding) = self.tokenizer.encode(line, true) { + let tb_values = token_builder.values(); + for &id in encoding.get_ids() { + tb_values.append_value(id as i64); + } + token_builder.append(true); + self.cur_samples_cnt += 1; + if self.cur_samples_cnt.is_multiple_of(5000) { + println!("Processed {} rows", self.cur_samples_cnt); + } + if self.cur_samples_cnt >= self.num_samples { + should_break = true; + break; + } + } + } + } + } + + // Create array and shuffle it + let input_ids_array = token_builder.finish(); + + // Create shuffled array by randomly sampling indices + let mut rng = rand::rngs::StdRng::seed_from_u64(1337); + let len = input_ids_array.len(); + let mut indices: Vec = (0..len as u32).collect(); + indices.shuffle(&mut rng); + + // Take values in shuffled order + let indices_array = UInt32Array::from(indices); + let shuffled = arrow::compute::take(&input_ids_array, &indices_array, None)?; + + let batch = RecordBatch::try_new(self.schema.clone(), vec![Arc::new(shuffled)]); + if should_break { + println!("Stop at {} rows", self.cur_samples_cnt); + self.parquet_readers.clear(); + self.current_reader = None; + } + + batch + } +} + +impl RecordBatchReader for WikiTextBatchReader { + fn schema(&self) -> Arc { + self.schema.clone() + } +} + +impl Iterator for WikiTextBatchReader { + type Item = Result; + fn next(&mut self) -> Option { + loop { + // If we have a current reader, try to get next batch + if let Some(reader) = &mut self.current_reader + && let Some(batch_result) = reader.next() + { + return Some(batch_result.and_then(|batch| self.process_batch(&batch))); + } + + // If no current reader or current reader is exhausted, try to get next reader + if self.current_reader_idx < self.parquet_readers.len() + && let Some(builder) = self.parquet_readers[self.current_reader_idx].take() + { + match builder.build() { + Ok(reader) => { + self.current_reader = Some(Box::new(reader)); + self.current_reader_idx += 1; + continue; + } + Err(e) => { + return Some(Err(arrow::error::ArrowError::ExternalError(Box::new(e)))); + } + } + } + + // No more readers available + return None; + } + } +} + +fn main() -> Result<(), Box> { + let rt = tokio::runtime::Runtime::new()?; + rt.block_on(async { + // Load tokenizer + let tokenizer = load_tokenizer("gpt2")?; + + // Set up Hugging Face API + // Download from https://huggingface.co/datasets/Salesforce/wikitext/tree/main/wikitext-103-raw-v1 + let api = Api::new()?; + let repo = api.repo(Repo::with_revision( + "Salesforce/wikitext".into(), + RepoType::Dataset, + "main".into(), + )); + + // Define the parquet files we want to download + let train_files = vec![ + "wikitext-103-raw-v1/train-00000-of-00002.parquet", + "wikitext-103-raw-v1/train-00001-of-00002.parquet", + ]; + + let mut parquet_readers = Vec::new(); + for file in &train_files { + println!("Downloading file: {}", file); + let file_path = repo.get(file)?; + let data = std::fs::read(file_path)?; + + // Create a temporary file in the system temp directory and write the downloaded data to it + let mut temp_file = NamedTempFile::new()?; + temp_file.write_all(&data)?; + + // Create the parquet reader builder with a larger batch size + let builder = ParquetRecordBatchReaderBuilder::try_new(temp_file.into_file())? + .with_batch_size(8192); // Increase batch size for better performance + parquet_readers.push(builder); + } + + if parquet_readers.is_empty() { + println!("No parquet files found to process."); + return Ok(()); + } + + // Create batch reader + let num_samples: u64 = 500_000; + let batch_reader = WikiTextBatchReader::new(parquet_readers, tokenizer, Some(num_samples))?; + + // Save as Lance dataset + println!("Writing to Lance dataset..."); + let lance_dataset_path = "rust_wikitext_lance_dataset.lance"; + + let write_params = WriteParams::default(); + lance::Dataset::write(batch_reader, lance_dataset_path, Some(write_params)).await?; + + // Verify the dataset + let ds = lance::Dataset::open(lance_dataset_path).await?; + let scanner = ds.scan(); + let mut stream = scanner.try_into_stream().await?; + + let mut total_rows = 0; + while let Some(batch_result) = stream.next().await { + let batch = batch_result?; + total_rows += batch.num_rows(); + } + + println!( + "Lance dataset created successfully with {} rows", + total_rows + ); + println!("Dataset location: {}", lance_dataset_path); + + Ok(()) + }) +} + +fn load_tokenizer(model_name: &str) -> Result> { + let api = Api::new()?; + let repo = api.repo(Repo::with_revision( + model_name.into(), + RepoType::Model, + "main".into(), + )); + + let tokenizer_path = repo.get("tokenizer.json")?; + let tokenizer = Tokenizer::from_file(tokenizer_path)?; + + Ok(tokenizer) +} diff --git a/lance-artifact/rust/examples/src/write_read_ds.rs b/lance-artifact/rust/examples/src/write_read_ds.rs new file mode 100644 index 000000000..0b07aa00c --- /dev/null +++ b/lance-artifact/rust/examples/src/write_read_ds.rs @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors +#![allow(clippy::print_stdout)] + +use arrow::array::UInt32Array; +use arrow::datatypes::{DataType, Field, Schema}; +use arrow::record_batch::{RecordBatch, RecordBatchIterator}; +use futures::StreamExt; +use lance::Dataset; +use lance::dataset::{WriteMode, WriteParams}; +use lance::io::ObjectStore; +use lance_core::utils::tempfile::TempStrDir; +use std::sync::Arc; + +// Writes sample dataset to the given path +async fn write_dataset(data_path: &str) -> Result<(), Box> { + // Define new schema + let schema = Arc::new(Schema::new(vec![ + Field::new("key", DataType::UInt32, false), + Field::new("value", DataType::UInt32, false), + ])); + + // Create new record batches + let batch = RecordBatch::try_new( + schema.clone(), + vec![ + Arc::new(UInt32Array::from(vec![1, 2, 3, 4, 5, 6])), + Arc::new(UInt32Array::from(vec![6, 7, 8, 9, 10, 11])), + ], + )?; + + let batches = RecordBatchIterator::new([Ok(batch)], schema.clone()); + + // Define write parameters (e.g. overwrite dataset) + let write_params = WriteParams { + mode: WriteMode::Overwrite, + ..Default::default() + }; + + Dataset::write(batches, data_path, Some(write_params)).await?; + Ok(()) +} // End write dataset + +// Reads dataset from the given path and prints batch size, schema for all record batches. Also extracts and prints a slice from the first batch +async fn read_dataset(data_path: &str) -> Result<(), Box> { + let dataset = Dataset::open(data_path).await?; + let scanner = dataset.scan(); + + let mut batch_stream = scanner.try_into_stream().await?.map(|b| b.unwrap()); + + while let Some(batch) = batch_stream.next().await { + println!("Batch size: {}, {}", batch.num_rows(), batch.num_columns()); // print size of batch + println!("Schema: {:?}", batch.schema()); // print schema of recordbatch + + println!("Batch: {:?}", batch); // print the entire recordbatch (schema and data) + } + Ok(()) +} // End read dataset + +async fn clean_resources(data_path: &str) -> Result<(), Box> { + let (store, base) = ObjectStore::from_uri(data_path).await?; + store.remove_dir_all(base).await?; + println!("Cleaned up resources at: {}", data_path); + Ok(()) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let tempdir = TempStrDir::default(); + let data_path = tempdir.as_str(); + + let result = async { + write_dataset(data_path).await?; + read_dataset(data_path).await?; + Ok(()) + } + .await; + + if let Err(e) = clean_resources(data_path).await { + eprintln!("Failed to clean resources: {:?}", e); + } + + result +} diff --git a/lance-artifact/rust/img.png b/lance-artifact/rust/img.png new file mode 100644 index 0000000000000000000000000000000000000000..697ca7860cade73ca91793804806f8ae5bdea86b GIT binary patch literal 312479 zcmd43g;$i_*FJ6|kA#3ogTfFZ-3%p4r!>j{0#Z^AFm$LGG}0g`U4n#kh{Q-sH|PxG z5Cb?MUB5H<_<7&=v%Y`8k2Q<6xVi6hpV(*b>$>(n6LMcmne-p}f6kmaL#nExpnK-b zm69`OF21;Y30x_P4D&y8=Ghrl1$lihGu$k36_0`UB0j0)*BK&4%J_m1c}4kF$Ysiq z5cvqoyWg(rP`o&M^VyyIUm&sFnV+rS((%1`O7Ye^R3+(x{MDzAu3o+@enZIgPO^N5 z&t76#fAQv4>GJa0@{iQblMh?GeEG%T$6?~rPR-77nnnyn_5XZwZtkY-68+z6;FhUJ zU+VwsPKZ61hH>J5UnG4+yz+k<1UKZo7qB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v1H17*HcBHd)!fY)HUzO7 z{eA=p4-HK#NM`B*S(1XkPkr^@r~Z`<>EwdP2Cb|szkeoRe>$4AC*Pv(-dtCTrv&sR zz07w1KFXpcgl@d?xhK9XiEqaZRAjA+Z$b~*|3BVNZvz-i7@#+?uJ>^syOqRORo>2M zE}5y_yq&e)rt^DI?4O7E{(frkw>X0dK^{EBxIm9sh2X-Ug#$v%9Wfm+|5^0Ni&wxTGQJ9D}xOsR)qL z`?HYt|7#%u$ix_uc3J|Puo!~xh6-sW$-lvS2%ruF0H|iw2$a}1JE7;!yZYtd?}GjT s`t8jBKQ8z8F{4`k{|EnH%QF*tr@f3Tw>5BGynhq literal 0 HcmV?d00001 diff --git a/lance-artifact/rust/lance-arrow/Cargo.toml b/lance-artifact/rust/lance-arrow/Cargo.toml new file mode 100644 index 000000000..21513e638 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "lance-arrow" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme = "README.md" +description = "Arrow Extension for Lance" +keywords.workspace = true +categories.workspace = true + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +arrow-array = { workspace = true } +arrow-buffer = { workspace = true } +arrow-data = { workspace = true } +arrow-ipc = { workspace = true } +arrow-ord = { workspace = true } +arrow-schema = { workspace = true } +arrow-select = { workspace = true } +bytemuck = { workspace = true } +bytes = { workspace = true } +futures = { workspace = true } +half = { workspace = true } +jsonb ={ workspace = true } +num-traits = { workspace = true } +rand.workspace = true + +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = ["js"] } + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-arrow/README.md b/lance-artifact/rust/lance-arrow/README.md new file mode 100644 index 000000000..8c3bedac3 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/README.md @@ -0,0 +1,8 @@ +# lance-arrow + +`lance-arrow` is a internal sub-crate, containing [Apache-Arrow](https://github.com/apache/arrow-rs) +extensions used by [Lance](https://github.com/lancedb/lance). + +**Important Note**: This crate is **not intended for external usage**. + + diff --git a/lance-artifact/rust/lance-arrow/src/bfloat16.rs b/lance-artifact/rust/lance-arrow/src/bfloat16.rs new file mode 100644 index 000000000..74c7f259a --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/bfloat16.rs @@ -0,0 +1,404 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! bfloat16 support for Apache Arrow. + +use std::fmt::Formatter; +use std::slice; + +use arrow_array::{Array, FixedSizeBinaryArray, builder::BooleanBufferBuilder}; +use arrow_buffer::{Buffer, MutableBuffer}; +use arrow_data::ArrayData; +use arrow_schema::{ArrowError, DataType, Field as ArrowField}; +use half::bf16; + +use crate::{ARROW_EXT_NAME_KEY, FloatArray}; + +/// The name of the bfloat16 extension in Arrow metadata +pub const BFLOAT16_EXT_NAME: &str = "lance.bfloat16"; + +/// Check whether the given field is a bfloat16 field +/// +/// A field is a bfloat16 field if it has a data type of `FixedSizeBinary(2)` and the metadata +/// contains the bfloat16 extension name. +pub fn is_bfloat16_field(field: &ArrowField) -> bool { + field.data_type() == &DataType::FixedSizeBinary(2) + && field + .metadata() + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == BFLOAT16_EXT_NAME) + .unwrap_or_default() +} + +/// The bfloat16 data type +/// +/// This implements the [`ArrowFloatType`](crate::floats::ArrowFloatType) trait for bfloat16 values. +#[derive(Debug)] +pub struct BFloat16Type {} + +/// An array of bfloat16 values +/// +/// Note that bfloat16 is not the same thing as fp16 which is supported natively by arrow-rs. +#[derive(Clone)] +pub struct BFloat16Array { + inner: FixedSizeBinaryArray, +} + +impl std::fmt::Debug for BFloat16Array { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "BFloat16Array\n[\n")?; + from_arrow::print_long_array(&self.inner, f, |array, i, f| { + if array.is_null(i) { + write!(f, "null") + } else { + let binary_values = array.value(i); + let value = + bf16::from_bits(u16::from_le_bytes([binary_values[0], binary_values[1]])); + write!(f, "{:?}", value) + } + })?; + write!(f, "]") + } +} + +impl BFloat16Array { + pub fn from_iter_values(iter: impl IntoIterator) -> Self { + let values: Vec = iter.into_iter().collect(); + values.into() + } + + pub fn len(&self) -> usize { + self.inner.len() + } + + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + pub fn is_null(&self, i: usize) -> bool { + self.inner.is_null(i) + } + + pub fn null_count(&self) -> usize { + self.inner.null_count() + } + + pub fn iter(&self) -> BFloat16Iter<'_> { + BFloat16Iter { + array: self, + index: 0, + } + } + + pub fn value(&self, i: usize) -> bf16 { + assert!( + i < self.len(), + "Trying to access an element at index {} from a BFloat16Array of length {}", + i, + self.len() + ); + // Safety: + // `i < self.len() + unsafe { self.value_unchecked(i) } + } + + /// # Safety + /// Caller must ensure that `i < self.len()` + pub unsafe fn value_unchecked(&self, i: usize) -> bf16 { + let binary_value = self.inner.value_unchecked(i); + bf16::from_bits(u16::from_le_bytes([binary_value[0], binary_value[1]])) + } + + pub fn into_inner(self) -> FixedSizeBinaryArray { + self.inner + } +} + +impl FromIterator> for BFloat16Array { + fn from_iter>>(iter: I) -> Self { + let mut buffer = MutableBuffer::new(10); + // No null buffer builder :( + let mut nulls = BooleanBufferBuilder::new(10); + let mut len = 0; + + for maybe_value in iter { + if let Some(value) = maybe_value { + let bytes = value.to_le_bytes(); + buffer.extend(bytes); + } else { + buffer.extend([0u8, 0u8]); + } + nulls.append(maybe_value.is_some()); + len += 1; + } + + let null_buffer = nulls.finish(); + let num_valid = null_buffer.count_set_bits(); + let null_buffer = if num_valid == len { + None + } else { + Some(null_buffer.into_inner()) + }; + + let array_data = ArrayData::builder(DataType::FixedSizeBinary(2)) + .len(len) + .add_buffer(buffer.into()) + .null_bit_buffer(null_buffer); + // SAFETY: the value buffer contains exactly `2 * len` bytes (two bytes + // pushed per iteration of the loop above, including the zero-fill for + // null slots), which matches the `FixedSizeBinary(2)` storage layout. + // The null bit buffer, when present, has `len` bits appended above, so + // its length covers the array's logical range. + let array_data = unsafe { array_data.build_unchecked() }; + Self { + inner: FixedSizeBinaryArray::from(array_data), + } + } +} + +impl FromIterator for BFloat16Array { + fn from_iter>(iter: I) -> Self { + Self::from_iter_values(iter) + } +} + +impl From> for BFloat16Array { + fn from(data: Vec) -> Self { + let len = data.len(); + // Zero-copy: `bf16` is `#[repr(transparent)]` over `u16` and derives + // `bytemuck::Pod`, so `cast_vec` reinterprets the allocation in place — + // no per-element copy or heap alloc. The crate-root `compile_error!` + // pins `target_endian = "little"`, so the resulting bytes match the + // `FixedSizeBinary(2)` on-disk order Lance writes elsewhere. + let raw: Vec = bytemuck::cast_vec(data); + let array_data = ArrayData::builder(DataType::FixedSizeBinary(2)) + .len(len) + .add_buffer(Buffer::from_vec(raw)); + // SAFETY: the value buffer contains exactly `2 * len` bytes — one + // `u16` per element after the layout-compatible cast — matching the + // `FixedSizeBinary(2)` storage layout. No null buffer is attached, so + // every element is logically valid. + let array_data = unsafe { array_data.build_unchecked() }; + Self { + inner: FixedSizeBinaryArray::from(array_data), + } + } +} + +impl TryFrom for BFloat16Array { + type Error = ArrowError; + + fn try_from(value: FixedSizeBinaryArray) -> Result { + if value.value_length() == 2 { + Ok(Self { inner: value }) + } else { + Err(ArrowError::InvalidArgumentError( + "FixedSizeBinaryArray must have a value length of 2".to_string(), + )) + } + } +} + +impl PartialEq for BFloat16Array { + fn eq(&self, other: &Self) -> bool { + self.inner.eq(&other.inner) + } +} + +pub struct BFloat16Iter<'a> { + array: &'a BFloat16Array, + index: usize, +} + +impl<'a> Iterator for BFloat16Iter<'a> { + type Item = Option; + + fn next(&mut self) -> Option { + if self.index >= self.array.len() { + return None; + } + let i = self.index; + self.index += 1; + if self.array.is_null(i) { + Some(None) + } else { + Some(Some(self.array.value(i))) + } + } +} + +/// Methods that are lifted from arrow-rs temporarily until they are made public. +mod from_arrow { + use arrow_array::Array; + + /// Helper function for printing potentially long arrays. + pub(super) fn print_long_array( + array: &A, + f: &mut std::fmt::Formatter, + print_item: F, + ) -> std::fmt::Result + where + A: Array, + F: Fn(&A, usize, &mut std::fmt::Formatter) -> std::fmt::Result, + { + let head = std::cmp::min(10, array.len()); + + for i in 0..head { + if array.is_null(i) { + writeln!(f, " null,")?; + } else { + write!(f, " ")?; + print_item(array, i, f)?; + writeln!(f, ",")?; + } + } + if array.len() > 10 { + if array.len() > 20 { + writeln!(f, " ...{} elements...,", array.len() - 20)?; + } + + let tail = std::cmp::max(head, array.len() - 10); + + for i in tail..array.len() { + if array.is_null(i) { + writeln!(f, " null,")?; + } else { + write!(f, " ")?; + print_item(array, i, f)?; + writeln!(f, ",")?; + } + } + } + Ok(()) + } +} + +impl FloatArray for FixedSizeBinaryArray { + type FloatType = BFloat16Type; + + /// Returns the underlying `bf16` values as a borrowed slice. + /// + /// # Preconditions + /// + /// - `value_length()` must be 2 (the `FixedSizeBinary(2)` storage shape + /// used by [`BFloat16Array`]). Asserted at entry. + /// - The value buffer must be at least 2-byte aligned. Lance's in-tree + /// constructors always satisfy this: value buffers are built either via + /// `MutableBuffer` (aligned to arrow-buffer's `ALIGNMENT` constant, ≥32 + /// bytes) or via `Buffer::from_vec::` (aligned to `align_of::()` + /// == 2); both meet `bf16`'s 2-byte requirement. Externally-built + /// `FixedSizeBinaryArray`s arriving via FFI, IPC, or + /// `Buffer::from_custom_allocation` are not required by arrow-rs to be + /// aligned beyond a single byte; passing one to this method violates the + /// precondition. A `debug_assert` below catches such inputs in debug and + /// test builds. + /// + /// # Endianness + /// + /// `lance-arrow` is gated on `target_endian = "little"` at the crate root, + /// so this method always returns values in the same byte order Lance writes + /// (see [`BFloat16Array::value`] and the [`FromIterator`] impls). + fn as_slice(&self) -> &[bf16] { + assert_eq!( + self.value_length(), + 2, + "BFloat16 arrays must use FixedSizeBinary(2) storage" + ); + debug_assert_eq!( + (self.value_data().as_ptr() as usize) % std::mem::align_of::(), + 0, + "BFloat16 value buffer must be at least 2-byte aligned" + ); + // SAFETY: + // - The assert above pins `value_size == 2`, so `value_data().len() / 2` + // equals the array's logical element count. + // `FixedSizeBinaryArray::From` constructs its value buffer + // as `buffers[0].slice_with_length(offset * 2, len * 2)` (arrow-array + // `fixed_size_binary_array.rs`), so `value_data()` already returns + // the offset-adjusted slice. Do not replace `value_data()` with an + // accessor that returns the un-sliced backing buffer. + // - `bf16` is `#[repr(transparent)]` over `u16` (size 2, alignment 2); + // every `u16` bit pattern is a valid `bf16`, so any byte content + // yields a defined value — never UB. + // - Alignment is the caller's responsibility per the precondition + // documented above. The `debug_assert_eq!` immediately preceding this + // block catches violations in debug and test builds only — release + // builds rely on callers honoring the precondition. arrow-rs + // declares `FixedSizeBinary(n)`'s + // `BufferSpec::FixedWidth { alignment: align_of::() == 1 }` + // (arrow-data `data.rs`), so arrow-rs alone does not guarantee + // 2-byte alignment. Lance's in-tree construction paths build value + // buffers via `MutableBuffer` (arrow-buffer `ALIGNMENT` constant, + // ≥32 bytes) or `Buffer::from_vec::` (2-byte aligned), both of + // which satisfy `bf16`'s 2-byte requirement. + // - The returned slice borrows from `self`; the underlying ref-counted, + // immutable Arrow buffer cannot be mutated or freed for the slice's + // lifetime. + unsafe { + slice::from_raw_parts( + self.value_data().as_ptr() as *const bf16, + self.value_data().len() / 2, + ) + } + } + + fn from_values(values: Vec) -> Self { + BFloat16Array::from(values).into_inner() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basics() { + let values: Vec = vec![1.0, 2.0, 3.0]; + let values: Vec = values.iter().map(|v| bf16::from_f32(*v)).collect(); + + let array = BFloat16Array::from_iter_values(values.clone()); + let array2 = BFloat16Array::from(values.clone()); + assert_eq!(array, array2); + assert_eq!(array.len(), 3); + + // Pin the raw little-endian bytes emitted by `From>` (rewritten to + // reinterpret the Vec via `bytemuck::cast_vec`), so a layout/byte-order + // regression is caught directly rather than only through Debug formatting. + // bf16 is the high 16 bits of the f32: 1.0->0x3F80, 2.0->0x4000, 3.0->0x4040. + let inner = array2.clone().into_inner(); + let raw_bytes: Vec = (0..inner.len()) + .flat_map(|i| inner.value(i).to_vec()) + .collect(); + assert_eq!(raw_bytes, vec![0x80, 0x3F, 0x00, 0x40, 0x40, 0x40]); + + let expected_fmt = "BFloat16Array\n[\n 1.0,\n 2.0,\n 3.0,\n]"; + assert_eq!(expected_fmt, format!("{:?}", array)); + + for (expected, value) in values.iter().zip(array.iter()) { + assert_eq!(Some(*expected), value); + } + + for (expected, value) in values.as_slice().iter().zip(array2.iter()) { + assert_eq!(Some(*expected), value); + } + + let arrow_array = array.into_inner(); + assert_eq!(arrow_array.as_slice(), values.as_slice()); + } + + #[test] + fn test_nulls() { + let values: Vec> = + vec![Some(bf16::from_f32(1.0)), None, Some(bf16::from_f32(3.0))]; + let array = BFloat16Array::from_iter(values.clone()); + assert_eq!(array.len(), 3); + assert_eq!(array.null_count(), 1); + + let expected_fmt = "BFloat16Array\n[\n 1.0,\n null,\n 3.0,\n]"; + assert_eq!(expected_fmt, format!("{:?}", array)); + + for (expected, value) in values.iter().zip(array.iter()) { + assert_eq!(*expected, value); + } + } +} diff --git a/lance-artifact/rust/lance-arrow/src/deepcopy.rs b/lance-artifact/rust/lance-arrow/src/deepcopy.rs new file mode 100644 index 000000000..afeb85335 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/deepcopy.rs @@ -0,0 +1,238 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_array::{Array, RecordBatch, make_array}; +use arrow_buffer::{BooleanBuffer, Buffer, NullBuffer}; +use arrow_data::{ArrayData, ArrayDataBuilder, transform::MutableArrayData}; + +pub fn deep_copy_buffer(buffer: &Buffer) -> Buffer { + Buffer::from(buffer.as_slice()) +} + +pub fn deep_copy_nulls(nulls: Option<&NullBuffer>) -> Option { + let nulls = nulls?; + let bit_buffer = deep_copy_buffer(nulls.inner().inner()); + // SAFETY: `null_count` is taken from the source `NullBuffer`, which already + // upheld `NullBuffer::new_unchecked`'s invariant — the unset-bit count over + // the logical bit slice `[bit_offset, bit_offset + bit_len)`. `NullBuffer::slice` + // adjusts only `BooleanBuffer::bit_offset` / `bit_len` and never byte-advances + // the inner `Buffer`, so `deep_copy_buffer` (which copies the source `Buffer`'s + // `as_slice()` view from byte 0) reproduces the exact bit pattern at the same + // bit offsets; the unset-bit count is therefore preserved. `BooleanBuffer::new` + // panics (does not UB) if `bit_offset + bit_len > 8 * buffer.len()`, and the + // copy has the same length, so that check still passes. + Some(unsafe { + NullBuffer::new_unchecked( + BooleanBuffer::new(bit_buffer, nulls.offset(), nulls.len()), + nulls.null_count(), + ) + }) +} + +pub fn deep_copy_array_data(data: &ArrayData) -> ArrayData { + let data_type = data.data_type().clone(); + let len = data.len(); + let nulls = deep_copy_nulls(data.nulls()); + let offset = data.offset(); + let buffers = data + .buffers() + .iter() + .map(deep_copy_buffer) + .collect::>(); + let child_data = data + .child_data() + .iter() + .map(deep_copy_array_data) + .collect::>(); + // SAFETY: `build_unchecked` inherits `ArrayData::new_unchecked`'s contract — + // `(data_type, len, offset, nulls, buffers, child_data)` must form a valid + // Arrow array. This call reproduces `data` structurally: `data_type`, `len`, + // and `offset` are forwarded unchanged; each buffer is replaced by a byte- + // identical copy of its offset-applied `as_slice()` view (the output buffer + // is `MutableBuffer`-allocated, at least as aligned as the source); `nulls` + // is deep-copied with the same bit offset/length and unset-bit count (see + // `deep_copy_nulls`); `child_data` is recursively cloned with the same + // guarantee. Every value-level invariant the source upheld — UTF-8 validity, + // monotonic offsets, in-bounds dictionary indices, run-end monotonicity, + // struct child-length matching — therefore transfers to the copy. If the + // source `ArrayData` was itself constructed via `new_unchecked` with an + // invalid payload, this function faithfully reproduces that invalidity. + unsafe { + ArrayDataBuilder::new(data_type) + .len(len) + .nulls(nulls) + .offset(offset) + .buffers(buffers) + .child_data(child_data) + .build_unchecked() + } +} + +pub fn deep_copy_array(array: &dyn Array) -> Arc { + let data = array.to_data(); + let data = deep_copy_array_data(&data); + make_array(data) +} + +pub fn deep_copy_batch(batch: &RecordBatch) -> crate::Result { + let arrays = batch + .columns() + .iter() + .map(|array| deep_copy_array(array)) + .collect::>(); + RecordBatch::try_new(batch.schema(), arrays) +} + +/// Deep copy array data, extracting only the sliced portion using MutableArrayData +/// This is the most efficient and correct way to copy just the sliced data +pub fn deep_copy_array_data_sliced(data: &ArrayData) -> ArrayData { + // Use MutableArrayData to efficiently copy just the slice + let mut mutable = MutableArrayData::new(vec![data], false, data.len()); + + // Copy from offset to offset+len (the visible slice) + mutable.extend(0, data.offset(), data.offset() + data.len()); + + // Freeze into immutable ArrayData + mutable.freeze() +} + +/// Deep copy an array, extracting only the sliced portion using MutableArrayData +pub fn deep_copy_array_sliced(array: &dyn Array) -> Arc { + let data = array.to_data(); + let data = deep_copy_array_data_sliced(&data); + make_array(data) +} + +/// Deep copy a RecordBatch, extracting only the sliced portion using MutableArrayData +pub fn deep_copy_batch_sliced(batch: &RecordBatch) -> crate::Result { + let arrays = batch + .columns() + .iter() + .map(|array| deep_copy_array_sliced(array)) + .collect::>(); + RecordBatch::try_new(batch.schema(), arrays) +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{Array, Int32Array, RecordBatch, StringArray}; + use arrow_schema::{DataType, Field, Schema}; + + #[test] + fn test_deep_copy_sliced_array_with_nulls() { + let array = Arc::new(Int32Array::from(vec![ + Some(1), + None, + Some(3), + None, + Some(5), + ])); + let sliced_array = array.slice(1, 3); + let copied_array = super::deep_copy_array(&sliced_array); + assert_eq!(sliced_array.len(), copied_array.len()); + assert_eq!(sliced_array.nulls(), copied_array.nulls()); + } + + #[test] + fn test_deep_copy_array_data_sliced() { + let array = Int32Array::from((0..1000).collect::>()); + let sliced = array.slice(100, 10); + + let sliced_data = sliced.to_data(); + let copied_data = super::deep_copy_array_data_sliced(&sliced_data); + + assert_eq!(copied_data.len(), 10); + assert_eq!(copied_data.offset(), 0); + + // Verify data correctness + let copied_array = Int32Array::from(copied_data); + for i in 0..10 { + assert_eq!(copied_array.value(i), 100 + i as i32); + } + } + + #[test] + fn test_deep_copy_array_sliced() { + let array = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])); + let sliced = array.slice(1, 3); + + let copied = super::deep_copy_array_sliced(&sliced); + + assert_eq!(copied.len(), 3); + let copied_int = copied.as_any().downcast_ref::().unwrap(); + assert_eq!(copied_int.value(0), 2); + assert_eq!(copied_int.value(1), 3); + assert_eq!(copied_int.value(2), 4); + } + + #[test] + fn test_deep_copy_batch_sliced() { + let schema = Arc::new(Schema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new("name", DataType::Utf8, false), + ])); + + let id_array = Arc::new(Int32Array::from((0..100).collect::>())); + let name_array = Arc::new(StringArray::from( + (0..100) + .map(|i| format!("name_{}", i)) + .collect::>(), + )); + + let batch = RecordBatch::try_new( + schema, + vec![id_array as Arc, name_array as Arc], + ) + .unwrap(); + + let sliced = batch.slice(10, 5); + let copied = super::deep_copy_batch_sliced(&sliced).unwrap(); + + assert_eq!(copied.num_rows(), 5); + assert_eq!(copied.num_columns(), 2); + + // Verify data correctness + let id_col = copied + .column(0) + .as_any() + .downcast_ref::() + .unwrap(); + let name_col = copied + .column(1) + .as_any() + .downcast_ref::() + .unwrap(); + + for i in 0..5 { + assert_eq!(id_col.value(i), 10 + i as i32); + assert_eq!(name_col.value(i), format!("name_{}", 10 + i)); + } + } + + #[test] + fn test_deep_copy_array_sliced_with_nulls() { + let array = Arc::new(Int32Array::from(vec![ + Some(1), + None, + Some(3), + None, + Some(5), + ])); + let sliced = array.slice(1, 3); // [None, Some(3), None] + + let copied = super::deep_copy_array_sliced(&sliced); + + assert_eq!(copied.len(), 3); + assert_eq!(copied.null_count(), 2); // Two nulls in the slice + + let copied_int = copied.as_any().downcast_ref::().unwrap(); + assert!(!copied_int.is_valid(0)); // None + assert!(copied_int.is_valid(1)); // Some(3) + assert!(!copied_int.is_valid(2)); // None + assert_eq!(copied_int.value(1), 3); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/floats.rs b/lance-artifact/rust/lance-arrow/src/floats.rs new file mode 100644 index 000000000..ad5f37689 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/floats.rs @@ -0,0 +1,291 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Floats Array + +use std::fmt::{Debug, Display}; +use std::iter::Sum; +use std::sync::Arc; +use std::{ + fmt::Formatter, + ops::{AddAssign, DivAssign}, +}; + +use arrow_array::{ + Array, FixedSizeBinaryArray, Float16Array, Float32Array, Float64Array, + types::{Float16Type, Float32Type, Float64Type}, +}; +use arrow_schema::{DataType, Field}; +use half::{bf16, f16}; +use num_traits::{AsPrimitive, Bounded, Float, FromPrimitive}; + +use super::bfloat16::{BFloat16Array, BFloat16Type}; +use crate::Result; +use crate::bfloat16::is_bfloat16_field; + +/// Float data type. +/// +/// This helps differentiate between the different float types, +/// because bf16 is not officially supported [DataType] in arrow-rs. +#[derive(Debug)] +pub enum FloatType { + BFloat16, + Float16, + Float32, + Float64, +} + +impl std::fmt::Display for FloatType { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::BFloat16 => write!(f, "bfloat16"), + Self::Float16 => write!(f, "float16"), + Self::Float32 => write!(f, "float32"), + Self::Float64 => write!(f, "float64"), + } + } +} + +/// Try to convert a [DataType] to a [FloatType]. To support bfloat16, always +/// prefer using the `TryFrom<&Field>` implementation. +impl TryFrom<&DataType> for FloatType { + type Error = crate::ArrowError; + + fn try_from(value: &DataType) -> Result { + match *value { + DataType::Float16 => Ok(Self::Float16), + DataType::Float32 => Ok(Self::Float32), + DataType::Float64 => Ok(Self::Float64), + _ => Err(crate::ArrowError::InvalidArgumentError(format!( + "{:?} is not a floating type", + value + ))), + } + } +} + +impl TryFrom<&Field> for FloatType { + type Error = crate::ArrowError; + + fn try_from(field: &Field) -> Result { + match field.data_type() { + DataType::FixedSizeBinary(2) if is_bfloat16_field(field) => Ok(Self::BFloat16), + _ => Self::try_from(field.data_type()), + } + } +} + +/// Trait for float types used in Lance indexes +/// +/// This mimics the utilities provided by [`arrow_array::ArrowPrimitiveType`] +/// but applies to all float types (including bfloat16) +pub trait ArrowFloatType: Debug { + type Native: FromPrimitive + + FloatToArrayType + + AsPrimitive + + Debug + + Display; + + const FLOAT_TYPE: FloatType; + const MIN: Self::Native; + const MAX: Self::Native; + + /// Arrow Float Array Type. + type ArrayType: FloatArray; + + /// Returns empty array of this type. + fn empty_array() -> Self::ArrayType { + >::from_values(Vec::new()) + } +} + +/// Trait to be implemented by native types that have a corresponding [`ArrowFloatType`] +/// implementation. +/// +/// This helps define what operations are supported by native floats and also helps convert +/// from a native type back to the corresponding Arrow float type. +pub trait FloatToArrayType: + Float + + Bounded + + Sum + + AddAssign + + AsPrimitive + + AsPrimitive + + DivAssign + + Send + + Sync + + Copy +{ + /// The corresponding [`ArrowFloatType`] implementation for this native type + type ArrowType: ArrowFloatType; +} + +impl FloatToArrayType for bf16 { + type ArrowType = BFloat16Type; +} + +impl FloatToArrayType for f16 { + type ArrowType = Float16Type; +} + +impl FloatToArrayType for f32 { + type ArrowType = Float32Type; +} + +impl FloatToArrayType for f64 { + type ArrowType = Float64Type; +} + +impl ArrowFloatType for BFloat16Type { + type Native = bf16; + + const FLOAT_TYPE: FloatType = FloatType::BFloat16; + const MIN: Self::Native = bf16::MIN; + const MAX: Self::Native = bf16::MAX; + + type ArrayType = FixedSizeBinaryArray; +} + +impl ArrowFloatType for Float16Type { + type Native = f16; + + const FLOAT_TYPE: FloatType = FloatType::Float16; + const MIN: Self::Native = f16::MIN; + const MAX: Self::Native = f16::MAX; + + type ArrayType = Float16Array; +} + +impl ArrowFloatType for Float32Type { + type Native = f32; + + const FLOAT_TYPE: FloatType = FloatType::Float32; + const MIN: Self::Native = f32::MIN; + const MAX: Self::Native = f32::MAX; + + type ArrayType = Float32Array; +} + +impl ArrowFloatType for Float64Type { + type Native = f64; + + const FLOAT_TYPE: FloatType = FloatType::Float64; + const MIN: Self::Native = f64::MIN; + const MAX: Self::Native = f64::MAX; + + type ArrayType = Float64Array; +} + +/// [FloatArray] is a trait that is implemented by all float type arrays +/// +/// This is similar to [`arrow_array::PrimitiveArray`] but applies to all float types (including bfloat16) +/// and is implemented as a trait and not a struct +pub trait FloatArray: Array + Clone + 'static { + type FloatType: ArrowFloatType; + + /// Returns a reference to the underlying data as a slice. + /// + /// # Panics + /// + /// Implementations may panic if the array's storage shape does not match + /// the expected element layout. In particular, the `bf16` impl panics if + /// `value_length() != 2` (the `FixedSizeBinary(2)` shape required by + /// `BFloat16Array`). + /// + /// # Preconditions + /// + /// Implementations may impose additional invariants on the underlying + /// buffer. The `bf16` impl requires the value buffer to be at least + /// 2-byte aligned — satisfied automatically by every in-tree Lance + /// constructor, but external callers passing externally-built arrays + /// (FFI, IPC, `Buffer::from_custom_allocation`) must ensure alignment. + /// See the impl's docstring for details. + fn as_slice(&self) -> &[T::Native]; + + /// Construct an array from a vector of values. + fn from_values(values: Vec) -> Self; + + /// Construct an array from an iterator of values. + fn from_iter_values(values: impl IntoIterator) -> Self + where + Self: Sized, + { + Self::from_values(values.into_iter().collect()) + } +} + +impl FloatArray for Float16Array { + type FloatType = Float16Type; + + fn as_slice(&self) -> &[::Native] { + self.values() + } + + fn from_values(values: Vec<::Native>) -> Self { + Self::from(values) + } +} + +impl FloatArray for Float32Array { + type FloatType = Float32Type; + + fn as_slice(&self) -> &[::Native] { + self.values() + } + + fn from_values(values: Vec<::Native>) -> Self { + Self::from(values) + } +} + +impl FloatArray for Float64Array { + type FloatType = Float64Type; + + fn as_slice(&self) -> &[::Native] { + self.values() + } + + fn from_values(values: Vec<::Native>) -> Self { + Self::from(values) + } +} + +/// Convert a float32 array to another float array +/// +/// This is used during queries as query vectors are always provided as float32 arrays +/// and need to be converted to the appropriate float type for the index. +pub fn coerce_float_vector(input: &Float32Array, float_type: FloatType) -> Result> { + match float_type { + FloatType::BFloat16 => Ok(Arc::new( + BFloat16Array::from_iter_values(input.values().iter().map(|v| bf16::from_f32(*v))) + .into_inner(), + )), + FloatType::Float16 => Ok(Arc::new(Float16Array::from_iter_values( + input.values().iter().map(|v| f16::from_f32(*v)), + ))), + FloatType::Float32 => Ok(Arc::new(input.clone())), + FloatType::Float64 => Ok(Arc::new(Float64Array::from_iter_values( + input.values().iter().map(|v| *v as f64), + ))), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_coerce_float_vector_bfloat16() { + let input = Float32Array::from(vec![1.0f32, 2.0, 3.0]); + let array = coerce_float_vector(&input, FloatType::BFloat16).unwrap(); + + assert_eq!(array.data_type(), &DataType::FixedSizeBinary(2)); + + let fixed = array + .as_any() + .downcast_ref::() + .unwrap(); + let expected: Vec = input.values().iter().map(|v| bf16::from_f32(*v)).collect(); + assert_eq!(fixed.as_slice(), expected.as_slice()); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/ipc.rs b/lance-artifact/rust/lance-arrow/src/ipc.rs new file mode 100644 index 000000000..8b6e5cf41 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/ipc.rs @@ -0,0 +1,620 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Zero-copy Arrow IPC stream read/write utilities. +//! +//! Provides helpers for serializing and deserializing [`RecordBatch`]es as +//! self-delimiting Arrow IPC streams using synchronous [`Read`]/[`Write`] I/O. +//! +//! These are designed for embedding IPC streams inside larger binary formats +//! (e.g. a cache entry that contains multiple IPC sections). Each stream is +//! self-delimiting (schema + batches + EOS marker) and can be read back +//! independently. +//! +//! # Zero-copy reads +//! +//! [`read_ipc_stream`] and [`read_ipc_stream_single`] take `&Bytes` and use +//! [`Bytes::slice`] to produce each message buffer. Because `Bytes::slice` +//! increments a reference count rather than copying, the resulting +//! [`Buffer`]s — and the array data decoded from them by [`FileDecoder`] — +//! are all backed by the same allocation as the input. + +use std::io::{Read, Write}; +use std::sync::Arc; + +use arrow_array::RecordBatch; +use arrow_buffer::Buffer; +use arrow_ipc::convert::fb_to_schema; +use arrow_ipc::reader::FileDecoder; +use arrow_ipc::root_as_message; +use arrow_ipc::writer::StreamWriter; +use arrow_schema::ArrowError; +use bytes::Bytes; + +// --------------------------------------------------------------------------- +// Length-prefixed byte utilities +// --------------------------------------------------------------------------- + +/// Write `data` prefixed by its length as a little-endian `u64`. +/// +/// Paired with [`read_len_prefixed_bytes`]. +pub fn write_len_prefixed_bytes(writer: &mut dyn Write, data: &[u8]) -> Result<(), ArrowError> { + writer + .write_all(&(data.len() as u64).to_le_bytes()) + .map_err(|e| ArrowError::IoError(e.to_string(), e))?; + writer + .write_all(data) + .map_err(|e| ArrowError::IoError(e.to_string(), e)) +} + +/// Read a byte slice written by [`write_len_prefixed_bytes`]. +/// +/// Reads an 8-byte little-endian length then exactly that many bytes. +pub fn read_len_prefixed_bytes(reader: &mut dyn Read) -> Result, ArrowError> { + let mut len_buf = [0u8; 8]; + reader + .read_exact(&mut len_buf) + .map_err(|e| ArrowError::IoError(e.to_string(), e))?; + let len = u64::from_le_bytes(len_buf) as usize; + let mut buf = vec![0u8; len]; + reader + .read_exact(&mut buf) + .map_err(|e| ArrowError::IoError(e.to_string(), e))?; + Ok(buf) +} + +// --------------------------------------------------------------------------- +// IPC stream utilities +// --------------------------------------------------------------------------- + +// 4-byte continuation marker used by modern Arrow IPC streams. +const IPC_CONTINUATION: [u8; 4] = [0xff; 4]; + +/// Write `batch` as a single-batch Arrow IPC stream to `writer`. +pub fn write_ipc_stream(batch: &RecordBatch, writer: &mut dyn Write) -> Result<(), ArrowError> { + let mut sw = StreamWriter::try_new(&mut *writer, batch.schema_ref())?; + sw.write(batch)?; + sw.finish() +} + +/// Write all batches from `iter` as a single Arrow IPC stream to `writer`. +/// +/// `iter` must yield at least one batch; the schema is inferred from the first +/// batch. Returns `ArrowError::InvalidArgumentError` if the iterator is empty. +/// If you need to write an empty stream (schema only, no rows), construct a +/// `StreamWriter` directly. +pub fn write_ipc_stream_batches(iter: I, writer: &mut dyn Write) -> Result<(), ArrowError> +where + I: IntoIterator, +{ + let mut iter = iter.into_iter(); + let first = iter + .next() + .ok_or_else(|| ArrowError::InvalidArgumentError("no batches to serialize".into()))?; + let mut sw = StreamWriter::try_new(&mut *writer, first.schema_ref())?; + sw.write(&first)?; + for batch in iter { + sw.write(&batch)?; + } + sw.finish() +} + +/// Read one complete Arrow IPC stream message from `data` as a zero-copy [`Buffer`]. +/// +/// Parses the first message starting at byte 0 of `data`. Returns `None` on +/// EOS (size field == 0) or empty input. The returned [`Buffer`] is backed by +/// `data`'s allocation — no bytes are copied. +/// +/// The caller should advance its position by `buf.len()` after each call. +fn read_one_ipc_message(data: &Bytes) -> Result, ArrowError> { + let bytes = data.as_ref(); + + if bytes.is_empty() { + return Ok(None); + } + if bytes.len() < 4 { + return Err(ArrowError::IoError( + "IPC: truncated header".into(), + std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "truncated IPC header"), + )); + } + + let has_continuation = bytes[..4] == IPC_CONTINUATION; + let (size_bytes, prefix_len): ([u8; 4], usize) = if has_continuation { + if bytes.len() < 8 { + return Err(ArrowError::IoError( + "IPC: truncated header after continuation".into(), + std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "truncated after continuation", + ), + )); + } + (bytes[4..8].try_into().unwrap(), 8) + } else { + (bytes[..4].try_into().unwrap(), 4) + }; + + let meta_size = u32::from_le_bytes(size_bytes) as usize; + if meta_size == 0 { + return Ok(None); // EOS + } + + let meta_end = prefix_len + meta_size; + if bytes.len() < meta_end { + return Err(ArrowError::IoError( + "IPC: truncated metadata".into(), + std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "truncated IPC metadata"), + )); + } + + let msg = root_as_message(&bytes[prefix_len..meta_end]) + .map_err(|e| ArrowError::ParseError(format!("IPC message parse error: {e}")))?; + let body_len = msg.bodyLength() as usize; + + let total = meta_end + body_len; + if bytes.len() < total { + return Err(ArrowError::IoError( + "IPC: truncated body".into(), + std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "truncated IPC body"), + )); + } + + // Zero-copy: Bytes::slice shares the backing allocation; Buffer::from + // wraps it without copying. + Ok(Some(Buffer::from(data.slice(0..total)))) +} + +/// Read a length-prefixed byte slice at `offset` in `data`, advancing `offset`. +/// +/// Reads an 8-byte little-endian length, then slices exactly that many bytes +/// from `data`. The returned [`Bytes`] is zero-copy (shares `data`'s allocation). +pub fn read_len_prefixed_bytes_at(data: &Bytes, offset: &mut usize) -> Result { + let bytes = data.as_ref(); + let len_end = offset + .checked_add(8) + .filter(|&e| e <= bytes.len()) + .ok_or_else(|| { + ArrowError::IoError( + "length-prefixed bytes: truncated length field".into(), + std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "truncated length"), + ) + })?; + let len = u64::from_le_bytes(bytes[*offset..len_end].try_into().unwrap()) as usize; + *offset = len_end; + let data_end = offset + .checked_add(len) + .filter(|&e| e <= bytes.len()) + .ok_or_else(|| { + ArrowError::IoError( + "length-prefixed bytes: truncated data".into(), + std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "truncated data"), + ) + })?; + let result = data.slice(*offset..data_end); + *offset = data_end; + Ok(result) +} + +/// Read all [`RecordBatch`]es from one Arrow IPC stream starting at `offset`, +/// advancing `offset` past the stream (including the EOS marker). +/// +/// Zero-copy: array buffers borrow from `data`'s allocation. +pub fn read_ipc_stream_at( + data: &Bytes, + offset: &mut usize, +) -> Result, ArrowError> { + let batches = read_ipc_stream(&data.slice(*offset..))?; + + // Recompute how many bytes were consumed by re-parsing message sizes. + // We can't get this from read_ipc_stream directly, so we re-walk the + // message headers (metadata only, no body re-read) to sum up lengths. + let slice = &data.as_ref()[*offset..]; + let mut consumed = 0usize; + loop { + let rem = &slice[consumed..]; + if rem.is_empty() { + break; + } + let has_cont = rem.len() >= 4 && rem[..4] == IPC_CONTINUATION; + let (size_bytes, prefix_len): ([u8; 4], usize) = if has_cont { + if rem.len() < 8 { + break; + } + (rem[4..8].try_into().unwrap(), 8) + } else { + if rem.len() < 4 { + break; + } + (rem[..4].try_into().unwrap(), 4) + }; + let meta_size = u32::from_le_bytes(size_bytes) as usize; + if meta_size == 0 { + // EOS — consume it and stop. + consumed += prefix_len; + break; + } + let meta_end = prefix_len + meta_size; + if rem.len() < meta_end { + break; + } + let msg = root_as_message(&rem[prefix_len..meta_end]) + .map_err(|e| ArrowError::ParseError(format!("IPC message parse error: {e}")))?; + let body_len = msg.bodyLength() as usize; + consumed += meta_end + body_len; + } + *offset += consumed; + + Ok(batches) +} + +/// Read exactly one [`RecordBatch`] from one Arrow IPC stream starting at `offset`, +/// advancing `offset` past the stream (including the EOS marker). +/// +/// Zero-copy: array buffers borrow from `data`'s allocation. +pub fn read_ipc_stream_single_at( + data: &Bytes, + offset: &mut usize, +) -> Result { + let mut batches = read_ipc_stream_at(data, offset)?; + match batches.len() { + 1 => Ok(batches.remove(0)), + n => Err(ArrowError::ParseError(format!( + "expected exactly 1 IPC record batch, got {n}" + ))), + } +} + +/// Extract the prefix length and metadata size from a raw IPC message buffer. +/// +/// Modern IPC streams have an 8-byte prefix `[continuation: 4][size: 4]`. +/// Legacy streams have a 4-byte prefix `[size: 4]`. Returns `(prefix_len, meta_size)`. +fn parse_ipc_message_prefix(buf: &Buffer) -> Result<(usize, usize), ArrowError> { + let has_continuation = buf.len() >= 4 && buf[..4] == IPC_CONTINUATION; + if has_continuation { + if buf.len() < 8 { + return Err(ArrowError::ParseError( + "IPC message buffer too short".into(), + )); + } + let meta_size = u32::from_le_bytes(buf[4..8].try_into().unwrap()) as usize; + Ok((8, meta_size)) + } else { + if buf.len() < 4 { + return Err(ArrowError::ParseError( + "IPC message buffer too short".into(), + )); + } + let meta_size = u32::from_le_bytes(buf[..4].try_into().unwrap()) as usize; + Ok((4, meta_size)) + } +} + +/// Read all [`RecordBatch`]es from one Arrow IPC stream. +/// +/// Zero-copy: each batch's array data buffers are borrowed from the input +/// message buffer(s) and not copied during decoding. +/// +/// Uses [`FileDecoder`] directly (rather than `StreamDecoder`) to avoid a +/// known edge case where `StreamDecoder` does not produce a batch for messages +/// with a zero-length body when the message exactly fills the decode buffer. +pub fn read_ipc_stream(data: &Bytes) -> Result, ArrowError> { + let mut offset = 0usize; + + let schema_buf = read_one_ipc_message(&data.slice(offset..))?.ok_or_else(|| { + ArrowError::ParseError("IPC stream: expected schema message, got EOS".into()) + })?; + offset += schema_buf.len(); + + let (prefix_len, meta_size) = parse_ipc_message_prefix(&schema_buf)?; + let schema_msg = root_as_message(&schema_buf[prefix_len..prefix_len + meta_size]) + .map_err(|e| ArrowError::ParseError(format!("IPC schema parse error: {e}")))?; + let schema = Arc::new(fb_to_schema(schema_msg.header_as_schema().ok_or_else( + || ArrowError::ParseError("IPC stream: first message is not a schema".into()), + )?)); + let mut decoder = FileDecoder::new(schema, schema_msg.version()); + + let mut batches = Vec::new(); + + loop { + let Some(buf) = read_one_ipc_message(&data.slice(offset..))? else { + break; + }; + offset += buf.len(); + + let (prefix_len, meta_size) = parse_ipc_message_prefix(&buf)?; + let msg = root_as_message(&buf[prefix_len..prefix_len + meta_size]) + .map_err(|e| ArrowError::ParseError(format!("IPC message parse error: {e}")))?; + let body_len = msg.bodyLength() as usize; + + // Block offset = 0 since the buffer starts at the message boundary. + // metaDataLength = prefix_len + meta_size (prefix + flatbuf + padding). + let block = arrow_ipc::Block::new(0, (prefix_len + meta_size) as i32, body_len as i64); + + match msg.header_type() { + arrow_ipc::MessageHeader::RecordBatch => { + if let Some(batch) = decoder.read_record_batch(&block, &buf)? { + batches.push(batch); + } + } + arrow_ipc::MessageHeader::DictionaryBatch => { + decoder.read_dictionary(&block, &buf)?; + } + _ => break, + } + } + + Ok(batches) +} + +/// Read exactly one [`RecordBatch`] from one Arrow IPC stream. +pub fn read_ipc_stream_single(data: &Bytes) -> Result { + let mut batches = read_ipc_stream(data)?; + match batches.len() { + 1 => Ok(batches.remove(0)), + n => Err(ArrowError::ParseError(format!( + "expected exactly 1 IPC record batch, got {n}" + ))), + } +} + +// --------------------------------------------------------------------------- +// Aligned IPC sections +// --------------------------------------------------------------------------- + +/// Byte alignment that each IPC section's stream start is padded to. +/// +/// When several IPC streams are concatenated into one larger blob (e.g. a +/// cache entry), a section that starts at an arbitrary offset would leave its +/// array data misaligned. [`FileDecoder`] with `require_alignment = false` +/// then silently copies each buffer into a freshly aligned allocation on +/// every read, defeating zero-copy. Padding each section start to a 64-byte +/// boundary keeps the decoded buffers borrowed directly from the input. +pub const IPC_SECTION_ALIGNMENT: usize = 64; + +/// Number of zero-padding bytes needed to advance `pos` to the next +/// [`IPC_SECTION_ALIGNMENT`] boundary. +fn section_padding(pos: usize) -> usize { + (IPC_SECTION_ALIGNMENT - (pos % IPC_SECTION_ALIGNMENT)) % IPC_SECTION_ALIGNMENT +} + +/// A [`Write`] adapter that counts the bytes written through it. +struct CountingWriter<'a> { + inner: &'a mut dyn Write, + count: usize, +} + +impl Write for CountingWriter<'_> { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + let n = self.inner.write(buf)?; + self.count += n; + Ok(n) + } + + fn flush(&mut self) -> std::io::Result<()> { + self.inner.flush() + } +} + +/// Write zero padding so the next byte lands on an [`IPC_SECTION_ALIGNMENT`] +/// boundary, advancing `pos` past it. +fn write_section_padding(writer: &mut dyn Write, pos: &mut usize) -> Result<(), ArrowError> { + let pad = section_padding(*pos); + if pad > 0 { + const ZEROS: [u8; IPC_SECTION_ALIGNMENT] = [0u8; IPC_SECTION_ALIGNMENT]; + writer + .write_all(&ZEROS[..pad]) + .map_err(|e| ArrowError::IoError(e.to_string(), e))?; + *pos += pad; + } + Ok(()) +} + +/// Write `batch` as a 64-byte-aligned single-batch Arrow IPC section. +/// +/// `pos` is the absolute byte offset of `writer` within the enclosing blob. +/// Zero padding is written first so the IPC stream begins on an +/// [`IPC_SECTION_ALIGNMENT`] boundary, then the stream itself. `pos` is +/// advanced past both the padding and the stream so the caller can write +/// further aligned sections. +/// +/// Paired with [`read_ipc_section_at`]. For the decoded buffers to be borrowed +/// zero-copy, the blob must ultimately be read back from a buffer whose base +/// address is at least 64-byte aligned. +pub fn write_ipc_section( + writer: &mut dyn Write, + pos: &mut usize, + batch: &RecordBatch, +) -> Result<(), ArrowError> { + write_section_padding(writer, pos)?; + + let mut counting = CountingWriter { + inner: writer, + count: 0, + }; + write_ipc_stream(batch, &mut counting)?; + *pos += counting.count; + Ok(()) +} + +/// Read a single [`RecordBatch`] from an aligned IPC section at `offset`. +/// +/// Skips the alignment padding written by [`write_ipc_section`], then reads +/// the stream, advancing `offset` past the section (padding + stream + EOS). +/// +/// Zero-copy: array buffers borrow from `data`'s allocation when `data`'s base +/// address is at least 64-byte aligned (see [`write_ipc_section`]). +pub fn read_ipc_section_at(data: &Bytes, offset: &mut usize) -> Result { + *offset += section_padding(*offset); + read_ipc_stream_single_at(data, offset) +} + +/// Write `batches` as a single 64-byte-aligned multi-batch Arrow IPC section. +/// +/// Like [`write_ipc_section`] but emits every batch from `iter` into one IPC +/// stream (schema + N batches + EOS). `iter` must yield at least one batch. +/// Paired with [`read_ipc_section_batches_at`]. +pub fn write_ipc_section_batches( + writer: &mut dyn Write, + pos: &mut usize, + iter: I, +) -> Result<(), ArrowError> +where + I: IntoIterator, +{ + write_section_padding(writer, pos)?; + + let mut counting = CountingWriter { + inner: writer, + count: 0, + }; + write_ipc_stream_batches(iter, &mut counting)?; + *pos += counting.count; + Ok(()) +} + +/// Read all [`RecordBatch`]es from an aligned multi-batch IPC section at +/// `offset`, advancing `offset` past the section (padding + stream + EOS). +/// +/// Zero-copy: array buffers borrow from `data`'s allocation when `data`'s base +/// address is at least 64-byte aligned (see [`write_ipc_section_batches`]). +pub fn read_ipc_section_batches_at( + data: &Bytes, + offset: &mut usize, +) -> Result, ArrowError> { + *offset += section_padding(*offset); + read_ipc_stream_at(data, offset) +} + +#[cfg(test)] +mod tests { + use arrow_array::{ArrayRef, record_batch}; + + use super::*; + + #[test] + fn test_ipc_roundtrip() { + let batch1 = record_batch!( + ("int", Int32, [1, 2, 3]), + ("str", Utf8, ["foo", "bar", "baz"]) + ) + .unwrap(); + let batch2 = record_batch!(("int", Int32, [4, 5]), ("str", Utf8, ["qux", "quux"])).unwrap(); + let batches = vec![batch1.clone(), batch2.clone()]; + + let mut buf = Vec::new(); + write_ipc_stream_batches(batches, &mut buf).unwrap(); + + let data = Bytes::from(buf); + + let batches = read_ipc_stream(&data).unwrap(); + assert_eq!(batches.len(), 2); + assert_eq!(batches[0], batch1); + assert_eq!(batches[1], batch2); + + let data_base = data.as_ptr() as usize; + let data_end = data_base + data.len(); + let assert_col_zero_copy = |array: &ArrayRef| { + for buffer in array.to_data().buffers() { + let ptr = buffer.as_ptr() as usize; + assert!( + ptr >= data_base && ptr < data_end, + "buffer at {ptr:#x} is not backed by the input Bytes allocation \ + [{data_base:#x}..{data_end:#x})" + ); + } + }; + + for batch in &batches { + assert_eq!(batch.schema(), batch1.schema()); + assert_col_zero_copy(batch.column(0)); + assert_col_zero_copy(batch.column(1)); + } + } + + /// Allocate a [`Bytes`] whose base address is 64-byte aligned, modelling a + /// backend that reads cache entries into an aligned buffer. A plain + /// `Bytes::from(vec)` only guarantees the allocator's alignment for `u8`. + fn aligned_bytes(payload: &[u8]) -> Bytes { + let mut v = vec![0u8; payload.len() + IPC_SECTION_ALIGNMENT]; + let pad = section_padding(v.as_ptr() as usize); + v[pad..pad + payload.len()].copy_from_slice(payload); + Bytes::from(v).slice(pad..pad + payload.len()) + } + + #[test] + fn test_aligned_ipc_sections_are_zero_copy() { + // A LargeBinary column exercises the i64-offset buffer whose 8-byte + // alignment requirement triggers a realigning memcpy when misaligned. + let blocks = arrow_array::LargeBinaryArray::from_vec(vec![&b"hello"[..], b"world"]); + let section_a = RecordBatch::try_from_iter([("a", Arc::new(blocks) as ArrayRef)]).unwrap(); + let section_b = record_batch!(("b", Int64, [10i64, 20, 30, 40, 50])).unwrap(); + + let mut buf = Vec::new(); + // Arbitrary, deliberately non-64-aligned preamble so the first section + // must be padded rather than landing at offset 0 by luck. + buf.extend_from_slice(&[0xABu8; 7]); + let mut pos = buf.len(); + // The first section's stream begins after padding the 7-byte preamble + // up to the next 64-byte boundary. + assert_eq!(7 + section_padding(7), IPC_SECTION_ALIGNMENT); + write_ipc_section(&mut buf, &mut pos, §ion_a).unwrap(); + write_ipc_section(&mut buf, &mut pos, §ion_b).unwrap(); + + let data = aligned_bytes(&buf); + assert_eq!( + section_padding(data.as_ptr() as usize), + 0, + "base not aligned" + ); + + let mut offset = 7; + let read_a = read_ipc_section_at(&data, &mut offset).unwrap(); + let read_b = read_ipc_section_at(&data, &mut offset).unwrap(); + assert_eq!(read_a, section_a); + assert_eq!(read_b, section_b); + + let data_base = data.as_ptr() as usize; + let data_end = data_base + data.len(); + for batch in [&read_a, &read_b] { + for buffer in batch.column(0).to_data().buffers() { + let ptr = buffer.as_ptr() as usize; + assert!( + ptr >= data_base && ptr < data_end, + "section buffer at {ptr:#x} was realigned out of the input \ + [{data_base:#x}..{data_end:#x}) — misaligned section", + ); + } + } + } + + #[test] + fn test_aligned_multi_batch_section_roundtrip_zero_copy() { + // A multi-batch section (e.g. IVF SQ storage chunks) must round-trip + // every batch and decode the first batch's buffers zero-copy. + let b1 = record_batch!(("v", Int64, [1i64, 2, 3])).unwrap(); + let b2 = record_batch!(("v", Int64, [4i64, 5])).unwrap(); + let b3 = record_batch!(("v", Int64, [6i64])).unwrap(); + + let mut buf = vec![0xCDu8; 5]; + let mut pos = buf.len(); + write_ipc_section_batches(&mut buf, &mut pos, [b1.clone(), b2.clone(), b3.clone()]) + .unwrap(); + + let data = aligned_bytes(&buf); + let mut offset = 5; + let read = read_ipc_section_batches_at(&data, &mut offset).unwrap(); + assert_eq!(read, vec![b1, b2, b3]); + assert_eq!(offset, buf.len(), "offset should land at section end"); + + let data_base = data.as_ptr() as usize; + let data_end = data_base + data.len(); + for buffer in read[0].column(0).to_data().buffers() { + let ptr = buffer.as_ptr() as usize; + assert!( + ptr >= data_base && ptr < data_end, + "first batch buffer at {ptr:#x} was realigned out of the input", + ); + } + } +} diff --git a/lance-artifact/rust/lance-arrow/src/json.rs b/lance-artifact/rust/lance-arrow/src/json.rs new file mode 100644 index 000000000..96ede058a --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/json.rs @@ -0,0 +1,1445 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! JSON support for Apache Arrow. + +use std::convert::TryFrom; +use std::sync::Arc; + +use arrow_array::builder::{LargeBinaryBuilder, StringBuilder}; +use arrow_array::cast::AsArray; +use arrow_array::{ + Array, ArrayRef, FixedSizeListArray, LargeBinaryArray, LargeListArray, LargeStringArray, + ListArray, MapArray, RecordBatch, StringArray, StructArray, +}; +use arrow_schema::{ArrowError, DataType, Field as ArrowField, Fields, Schema}; + +use crate::ARROW_EXT_NAME_KEY; + +/// Arrow extension type name for JSON data (Lance internal) +pub const JSON_EXT_NAME: &str = "lance.json"; + +/// Arrow extension type name for JSON data (Arrow official) +pub const ARROW_JSON_EXT_NAME: &str = "arrow.json"; + +/// Check if a field is a JSON extension field (Lance internal JSONB storage) +pub fn is_json_field(field: &ArrowField) -> bool { + field.data_type() == &DataType::LargeBinary + && field + .metadata() + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == JSON_EXT_NAME) + .unwrap_or_default() +} + +/// Check if a field is an Arrow JSON extension field (PyArrow pa.json() type) +pub fn is_arrow_json_field(field: &ArrowField) -> bool { + // Arrow JSON extension type uses Utf8 or LargeUtf8 as storage type + (field.data_type() == &DataType::Utf8 || field.data_type() == &DataType::LargeUtf8) + && field + .metadata() + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == ARROW_JSON_EXT_NAME) + .unwrap_or_default() +} + +/// Check if a field or any of its descendants is a JSON field +pub fn has_json_fields(field: &ArrowField) -> bool { + if is_json_field(field) { + return true; + } + + match field.data_type() { + DataType::Struct(fields) => fields.iter().any(|f| has_json_fields(f)), + DataType::List(f) | DataType::LargeList(f) | DataType::FixedSizeList(f, _) => { + has_json_fields(f) + } + DataType::Map(f, _) => has_json_fields(f), + _ => false, + } +} + +/// Check if a field or any of its descendants is an Arrow JSON field +pub fn has_arrow_json_fields(field: &ArrowField) -> bool { + if is_arrow_json_field(field) { + return true; + } + + match field.data_type() { + DataType::Struct(fields) => fields.iter().any(|f| has_arrow_json_fields(f)), + DataType::List(f) | DataType::LargeList(f) | DataType::FixedSizeList(f, _) => { + has_arrow_json_fields(f) + } + DataType::Map(f, _) => has_arrow_json_fields(f), + _ => false, + } +} + +/// Create a JSON field with the appropriate extension metadata +pub fn json_field(name: &str, nullable: bool) -> ArrowField { + let mut field = ArrowField::new(name, DataType::LargeBinary, nullable); + let mut metadata = std::collections::HashMap::new(); + metadata.insert(ARROW_EXT_NAME_KEY.to_string(), JSON_EXT_NAME.to_string()); + field.set_metadata(metadata); + field +} + +/// A specialized array for JSON data stored as JSONB binary format +#[derive(Debug, Clone)] +pub struct JsonArray { + inner: LargeBinaryArray, +} + +impl JsonArray { + /// Create a new JsonArray from an iterator of JSON strings + pub fn try_from_iter(iter: I) -> Result + where + I: IntoIterator>, + S: AsRef, + { + let mut builder = LargeBinaryBuilder::new(); + + for json_str in iter { + match json_str { + Some(s) => { + let encoded = encode_json(s.as_ref()).map_err(|e| { + ArrowError::InvalidArgumentError(format!("Failed to encode JSON: {}", e)) + })?; + builder.append_value(&encoded); + } + None => builder.append_null(), + } + } + + Ok(Self { + inner: builder.finish(), + }) + } + + /// Get the underlying LargeBinaryArray + pub fn into_inner(self) -> LargeBinaryArray { + self.inner + } + + /// Get a reference to the underlying LargeBinaryArray + pub fn inner(&self) -> &LargeBinaryArray { + &self.inner + } + + /// Get the value at index i as decoded JSON string + pub fn value(&self, i: usize) -> Result { + if self.inner.is_null(i) { + return Err(ArrowError::InvalidArgumentError( + "Value is null".to_string(), + )); + } + + let jsonb_bytes = self.inner.value(i); + Ok(decode_json(jsonb_bytes)) + } + + /// Get the value at index i as raw JSONB bytes + pub fn value_bytes(&self, i: usize) -> &[u8] { + self.inner.value(i) + } + + /// Get JSONPath value from the JSON at index i + pub fn json_path(&self, i: usize, path: &str) -> Result, ArrowError> { + if self.inner.is_null(i) { + return Ok(None); + } + + let jsonb_bytes = self.inner.value(i); + get_json_path(jsonb_bytes, path).map_err(|e| { + ArrowError::InvalidArgumentError(format!("Failed to extract JSONPath: {}", e)) + }) + } + + /// Convert to Arrow string array (JSON as UTF-8) + pub fn to_arrow_json(&self) -> ArrayRef { + let mut builder = arrow_array::builder::StringBuilder::new(); + + for i in 0..self.inner.len() { + if self.inner.is_null(i) { + builder.append_null(); + } else { + let jsonb_bytes = self.inner.value(i); + let json_str = decode_json(jsonb_bytes); + builder.append_value(&json_str); + } + } + + // Return as UTF-8 string array (Arrow represents JSON as strings) + Arc::new(builder.finish()) + } + + pub fn len(&self) -> usize { + self.inner.len() + } + + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + pub fn is_null(&self, i: usize) -> bool { + self.inner.is_null(i) + } +} + +// TryFrom implementations for string arrays +impl TryFrom for JsonArray { + type Error = ArrowError; + + fn try_from(array: StringArray) -> Result { + Self::try_from(&array) + } +} + +impl TryFrom<&StringArray> for JsonArray { + type Error = ArrowError; + + fn try_from(array: &StringArray) -> Result { + let mut builder = LargeBinaryBuilder::with_capacity(array.len(), array.value_data().len()); + + for i in 0..array.len() { + if array.is_null(i) { + builder.append_null(); + } else { + let json_str = array.value(i); + let encoded = encode_json(json_str).map_err(|e| { + ArrowError::InvalidArgumentError(format!("Failed to encode JSON: {}", e)) + })?; + builder.append_value(&encoded); + } + } + + Ok(Self { + inner: builder.finish(), + }) + } +} + +impl TryFrom for JsonArray { + type Error = ArrowError; + + fn try_from(array: LargeStringArray) -> Result { + Self::try_from(&array) + } +} + +impl TryFrom<&LargeStringArray> for JsonArray { + type Error = ArrowError; + + fn try_from(array: &LargeStringArray) -> Result { + let mut builder = LargeBinaryBuilder::with_capacity(array.len(), array.value_data().len()); + + for i in 0..array.len() { + if array.is_null(i) { + builder.append_null(); + } else { + let json_str = array.value(i); + let encoded = encode_json(json_str).map_err(|e| { + ArrowError::InvalidArgumentError(format!("Failed to encode JSON: {}", e)) + })?; + builder.append_value(&encoded); + } + } + + Ok(Self { + inner: builder.finish(), + }) + } +} + +impl TryFrom for JsonArray { + type Error = ArrowError; + + fn try_from(array_ref: ArrayRef) -> Result { + match array_ref.data_type() { + DataType::Utf8 => { + // Downcast is guaranteed to succeed after matching on DataType::Utf8 + let string_array = array_ref + .as_any() + .downcast_ref::() + .expect("DataType::Utf8 array must be StringArray"); + Self::try_from(string_array) + } + DataType::LargeUtf8 => { + // Downcast is guaranteed to succeed after matching on DataType::LargeUtf8 + let large_string_array = array_ref + .as_any() + .downcast_ref::() + .expect("DataType::LargeUtf8 array must be LargeStringArray"); + Self::try_from(large_string_array) + } + dt => Err(ArrowError::InvalidArgumentError(format!( + "Unsupported array type for JSON: {:?}. Expected Utf8 or LargeUtf8", + dt + ))), + } + } +} + +/// Encode JSON string to JSONB format +pub fn encode_json(json_str: &str) -> Result, Box> { + let value = jsonb::parse_value(json_str.as_bytes())?; + Ok(value.to_vec()) +} + +/// Decode JSONB bytes to JSON string +pub fn decode_json(jsonb_bytes: &[u8]) -> String { + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + raw_jsonb.to_string() +} + +/// Extract JSONPath value from JSONB +fn get_json_path( + jsonb_bytes: &[u8], + path: &str, +) -> Result, Box> { + let json_path = jsonb::jsonpath::parse_json_path(path.as_bytes())?; + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + + let values = selector.select_values(&json_path)?; + if values.is_empty() { + Ok(None) + } else { + Ok(Some(values[0].to_string())) + } +} + +/// Convert an Arrow JSON field to Lance JSON field (with JSONB storage) +pub fn arrow_json_to_lance_json(field: &ArrowField) -> ArrowField { + if is_arrow_json_field(field) { + return field_with_extension(field, DataType::LargeBinary, JSON_EXT_NAME); + } + + let data_type = match field.data_type() { + DataType::Struct(fields) => { + let fields = fields + .iter() + .map(|field| Arc::new(arrow_json_to_lance_json(field))) + .collect::>(); + DataType::Struct(Fields::from(fields)) + } + DataType::List(item) => DataType::List(Arc::new(arrow_json_to_lance_json(item))), + DataType::LargeList(item) => DataType::LargeList(Arc::new(arrow_json_to_lance_json(item))), + DataType::FixedSizeList(item, size) => { + DataType::FixedSizeList(Arc::new(arrow_json_to_lance_json(item)), *size) + } + DataType::Map(entries, keys_sorted) => { + DataType::Map(Arc::new(arrow_json_to_lance_json(entries)), *keys_sorted) + } + _ => return field.clone(), + }; + + field_with_data_type(field, data_type) +} + +/// Convert a Lance JSON field to Arrow JSON field. +pub fn lance_json_to_arrow_json(field: &ArrowField) -> ArrowField { + if is_json_field(field) { + return field_with_extension(field, DataType::Utf8, ARROW_JSON_EXT_NAME); + } + + let data_type = match field.data_type() { + DataType::Struct(fields) => { + let fields = fields + .iter() + .map(|field| Arc::new(lance_json_to_arrow_json(field))) + .collect::>(); + DataType::Struct(Fields::from(fields)) + } + DataType::List(item) => DataType::List(Arc::new(lance_json_to_arrow_json(item))), + DataType::LargeList(item) => DataType::LargeList(Arc::new(lance_json_to_arrow_json(item))), + DataType::FixedSizeList(item, size) => { + DataType::FixedSizeList(Arc::new(lance_json_to_arrow_json(item)), *size) + } + DataType::Map(entries, keys_sorted) => { + DataType::Map(Arc::new(lance_json_to_arrow_json(entries)), *keys_sorted) + } + _ => return field.clone(), + }; + + field_with_data_type(field, data_type) +} + +fn field_with_data_type(field: &ArrowField, data_type: DataType) -> ArrowField { + ArrowField::new(field.name(), data_type, field.is_nullable()) + .with_metadata(field.metadata().clone()) +} + +fn field_with_extension( + field: &ArrowField, + data_type: DataType, + extension_name: &str, +) -> ArrowField { + let mut metadata = field.metadata().clone(); + metadata.insert(ARROW_EXT_NAME_KEY.to_string(), extension_name.to_string()); + ArrowField::new(field.name(), data_type, field.is_nullable()).with_metadata(metadata) +} + +fn convert_json_array( + field: &ArrowField, + array: &ArrayRef, + convert_leaf: &F, +) -> Result<(ArrowField, ArrayRef, bool), ArrowError> +where + F: Fn(&ArrowField, &ArrayRef) -> Result, ArrowError>, +{ + if let Some((field, array)) = convert_leaf(field, array)? { + return Ok((field, array, true)); + } + + match field.data_type() { + DataType::Struct(fields) => { + let struct_array = array.as_struct(); + let mut new_fields = Vec::with_capacity(fields.len()); + let mut new_columns = Vec::with_capacity(fields.len()); + let mut changed = false; + + for (field, column) in fields.iter().zip(struct_array.columns()) { + let (new_field, new_column, field_changed) = + convert_json_array(field, column, convert_leaf)?; + changed |= field_changed; + new_fields.push(Arc::new(new_field)); + new_columns.push(new_column); + } + + if changed { + let fields = Fields::from(new_fields); + let new_field = field_with_data_type(field, DataType::Struct(fields.clone())); + let new_array = + StructArray::new(fields, new_columns, struct_array.nulls().cloned()); + Ok((new_field, Arc::new(new_array) as ArrayRef, true)) + } else { + Ok((field.clone(), array.clone(), false)) + } + } + DataType::List(item) => { + let list_array: &ListArray = array.as_list(); + let (new_item, new_values, changed) = + convert_json_array(item, list_array.values(), convert_leaf)?; + if changed { + let new_field = + field_with_data_type(field, DataType::List(Arc::new(new_item.clone()))); + let new_array = ListArray::new( + Arc::new(new_item), + list_array.offsets().clone(), + new_values, + list_array.nulls().cloned(), + ); + Ok((new_field, Arc::new(new_array) as ArrayRef, true)) + } else { + Ok((field.clone(), array.clone(), false)) + } + } + DataType::LargeList(item) => { + let list_array: &LargeListArray = array.as_list(); + let (new_item, new_values, changed) = + convert_json_array(item, list_array.values(), convert_leaf)?; + if changed { + let new_field = + field_with_data_type(field, DataType::LargeList(Arc::new(new_item.clone()))); + let new_array = LargeListArray::new( + Arc::new(new_item), + list_array.offsets().clone(), + new_values, + list_array.nulls().cloned(), + ); + Ok((new_field, Arc::new(new_array) as ArrayRef, true)) + } else { + Ok((field.clone(), array.clone(), false)) + } + } + DataType::FixedSizeList(item, size) => { + let list_array: &FixedSizeListArray = array.as_fixed_size_list(); + let (new_item, new_values, changed) = + convert_json_array(item, list_array.values(), convert_leaf)?; + if changed { + let new_field = field_with_data_type( + field, + DataType::FixedSizeList(Arc::new(new_item.clone()), *size), + ); + let new_array = FixedSizeListArray::try_new_with_length( + Arc::new(new_item), + *size, + new_values, + list_array.nulls().cloned(), + list_array.len(), + )?; + Ok((new_field, Arc::new(new_array) as ArrayRef, true)) + } else { + Ok((field.clone(), array.clone(), false)) + } + } + DataType::Map(entries, keys_sorted) => { + let map_array = array + .as_any() + .downcast_ref::() + .expect("DataType::Map array must be MapArray"); + let entries_array = Arc::new(map_array.entries().clone()) as ArrayRef; + let (new_entries, new_entries_array, changed) = + convert_json_array(entries, &entries_array, convert_leaf)?; + if changed { + let entries_struct = new_entries_array + .as_any() + .downcast_ref::() + .expect("Map entries must be StructArray") + .clone(); + let new_field = field_with_data_type( + field, + DataType::Map(Arc::new(new_entries.clone()), *keys_sorted), + ); + let new_array = MapArray::new( + Arc::new(new_entries), + map_array.offsets().clone(), + entries_struct, + map_array.nulls().cloned(), + *keys_sorted, + ); + Ok((new_field, Arc::new(new_array) as ArrayRef, true)) + } else { + Ok((field.clone(), array.clone(), false)) + } + } + _ => Ok((field.clone(), array.clone(), false)), + } +} + +fn convert_arrow_json_array( + field: &ArrowField, + array: &ArrayRef, +) -> Result<(ArrowField, ArrayRef, bool), ArrowError> { + convert_json_array(field, array, &|field, array| { + if is_arrow_json_field(field) { + let json_array = JsonArray::try_from(array.clone())?; + Ok(Some(( + arrow_json_to_lance_json(field), + Arc::new(json_array.into_inner()) as ArrayRef, + ))) + } else { + Ok(None) + } + }) +} + +fn convert_lance_json_array( + field: &ArrowField, + array: &ArrayRef, +) -> Result<(ArrowField, ArrayRef, bool), ArrowError> { + convert_json_array(field, array, &|field, array| { + if is_json_field(field) { + let binary_array = array + .as_any() + .downcast_ref::() + .expect("Lance JSON field must be LargeBinaryArray"); + let mut builder = StringBuilder::new(); + + for i in 0..binary_array.len() { + if binary_array.is_null(i) { + builder.append_null(); + } else { + let jsonb_bytes = binary_array.value(i); + let json_str = decode_json(jsonb_bytes); + builder.append_value(&json_str); + } + } + + Ok(Some(( + lance_json_to_arrow_json(field), + Arc::new(builder.finish()) as ArrayRef, + ))) + } else { + Ok(None) + } + }) +} + +/// Convert a RecordBatch with Lance JSON columns (JSONB) back to Arrow JSON format (strings) +pub fn convert_lance_json_to_arrow( + batch: &arrow_array::RecordBatch, +) -> Result { + let schema = batch.schema(); + let mut needs_conversion = false; + let mut new_fields = Vec::with_capacity(schema.fields().len()); + let mut new_columns = Vec::with_capacity(batch.num_columns()); + + for (i, field) in schema.fields().iter().enumerate() { + let column = batch.column(i); + let (new_field, new_column, changed) = convert_lance_json_array(field, column)?; + + needs_conversion |= changed; + new_fields.push(new_field); + new_columns.push(new_column); + } + + if needs_conversion { + let new_schema = Arc::new(Schema::new_with_metadata( + new_fields, + schema.metadata().clone(), + )); + RecordBatch::try_new(new_schema, new_columns) + } else { + // No conversion needed, return original batch + Ok(batch.clone()) + } +} + +/// Convert a RecordBatch with Arrow JSON columns to Lance JSON format (JSONB) +pub fn convert_json_columns( + batch: &arrow_array::RecordBatch, +) -> Result { + let schema = batch.schema(); + let mut needs_conversion = false; + let mut new_fields = Vec::with_capacity(schema.fields().len()); + let mut new_columns = Vec::with_capacity(batch.num_columns()); + + for (i, field) in schema.fields().iter().enumerate() { + let column = batch.column(i); + let (new_field, new_column, changed) = convert_arrow_json_array(field, column)?; + + needs_conversion |= changed; + new_fields.push(new_field); + new_columns.push(new_column); + } + + if needs_conversion { + let new_schema = Arc::new(Schema::new_with_metadata( + new_fields, + schema.metadata().clone(), + )); + RecordBatch::try_new(new_schema, new_columns) + } else { + // No conversion needed, return original batch + Ok(batch.clone()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_json_field_creation() { + let field = json_field("data", true); + assert_eq!(field.name(), "data"); + assert_eq!(field.data_type(), &DataType::LargeBinary); + assert!(field.is_nullable()); + assert!(is_json_field(&field)); + } + + #[test] + fn test_json_array_from_strings() { + let json_strings = vec![ + Some(r#"{"name": "Alice", "age": 30}"#), + None, + Some(r#"{"name": "Bob", "age": 25}"#), + ]; + + let array = JsonArray::try_from_iter(json_strings).unwrap(); + assert_eq!(array.len(), 3); + assert!(!array.is_null(0)); + assert!(array.is_null(1)); + assert!(!array.is_null(2)); + + let decoded = array.value(0).unwrap(); + assert!(decoded.contains("Alice")); + } + + #[test] + fn test_json_array_from_string_array() { + let string_array = StringArray::from(vec![ + Some(r#"{"name": "Alice"}"#), + Some(r#"{"name": "Bob"}"#), + None, + ]); + + let json_array = JsonArray::try_from(string_array).unwrap(); + assert_eq!(json_array.len(), 3); + assert!(!json_array.is_null(0)); + assert!(!json_array.is_null(1)); + assert!(json_array.is_null(2)); + } + + #[test] + fn test_json_path_extraction() { + let json_array = JsonArray::try_from_iter(vec![ + Some(r#"{"user": {"name": "Alice", "age": 30}}"#), + Some(r#"{"user": {"name": "Bob"}}"#), + ]) + .unwrap(); + + let name = json_array.json_path(0, "$.user.name").unwrap(); + assert_eq!(name, Some("\"Alice\"".to_string())); + + let age = json_array.json_path(1, "$.user.age").unwrap(); + assert_eq!(age, None); + } + + #[test] + fn test_convert_json_columns() { + // Create a batch with Arrow JSON column + let json_strings = vec![Some(r#"{"name": "Alice"}"#), Some(r#"{"name": "Bob"}"#)]; + let json_arr = StringArray::from(json_strings); + + // Create field with arrow.json extension + let mut field = ArrowField::new("data", DataType::Utf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(json_arr) as ArrayRef]).unwrap(); + + // Convert the batch + let converted = convert_json_columns(&batch).unwrap(); + + // Check the converted schema + assert_eq!(converted.num_columns(), 1); + let converted_schema = converted.schema(); + let converted_field = converted_schema.field(0); + assert_eq!(converted_field.data_type(), &DataType::LargeBinary); + assert_eq!( + converted_field.metadata().get(ARROW_EXT_NAME_KEY), + Some(&JSON_EXT_NAME.to_string()) + ); + + // Check the data was converted + let converted_column = converted.column(0); + assert_eq!(converted_column.data_type(), &DataType::LargeBinary); + assert_eq!(converted_column.len(), 2); + + // Verify the data is valid JSONB + let binary_array = converted_column + .as_any() + .downcast_ref::() + .unwrap(); + for i in 0..binary_array.len() { + let jsonb_bytes = binary_array.value(i); + let decoded = decode_json(jsonb_bytes); + assert!(decoded.contains("name")); + } + } + + #[test] + fn test_convert_nested_json_columns() { + use arrow_buffer::{OffsetBuffer, ScalarBuffer}; + + let uri_field = Arc::new(ArrowField::new("uri", DataType::Utf8, false)); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + let extra_field = + Arc::new(ArrowField::new("extra", DataType::Utf8, true).with_metadata(metadata)); + let item_fields = Fields::from(vec![uri_field, extra_field]); + + let values = StructArray::new( + item_fields.clone(), + vec![ + Arc::new(StringArray::from(vec![Some("a.jpg"), Some("b.jpg")])) as ArrayRef, + Arc::new(StringArray::from(vec![ + Some(r#"{"codec":"h264"}"#), + None::<&str>, + ])) as ArrayRef, + ], + None, + ); + let item = Arc::new(ArrowField::new("item", DataType::Struct(item_fields), true)); + let media = ListArray::new( + item, + OffsetBuffer::new(ScalarBuffer::from(vec![0, 1, 2])), + Arc::new(values), + None, + ); + let schema = Arc::new(Schema::new(vec![ArrowField::new( + "media", + media.data_type().clone(), + true, + )])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(media) as ArrayRef]).unwrap(); + + assert!(has_arrow_json_fields(batch.schema().field(0))); + + let converted = convert_json_columns(&batch).unwrap(); + let converted_schema = converted.schema(); + let DataType::List(item) = converted_schema.field(0).data_type() else { + panic!("expected list field"); + }; + let DataType::Struct(fields) = item.data_type() else { + panic!("expected struct item"); + }; + assert!(is_json_field(&fields[1])); + + let list_array: &ListArray = converted.column(0).as_list(); + let values = list_array.values().as_struct(); + let extra = values + .column(1) + .as_any() + .downcast_ref::() + .unwrap(); + assert!(decode_json(extra.value(0)).contains("h264")); + assert!(extra.is_null(1)); + + let logical = convert_lance_json_to_arrow(&converted).unwrap(); + let logical_schema = logical.schema(); + let DataType::List(item) = logical_schema.field(0).data_type() else { + panic!("expected list field"); + }; + let DataType::Struct(fields) = item.data_type() else { + panic!("expected struct item"); + }; + assert!(is_arrow_json_field(&fields[1])); + + let list_array: &ListArray = logical.column(0).as_list(); + let values = list_array.values().as_struct(); + let extra = values + .column(1) + .as_any() + .downcast_ref::() + .unwrap(); + assert!(extra.value(0).contains("h264")); + assert!(extra.is_null(1)); + } + + #[test] + fn test_convert_fixed_size_list_zero_json_preserves_length() { + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + let item = Arc::new(ArrowField::new("item", DataType::Utf8, true).with_metadata(metadata)); + let values = Arc::new(StringArray::from(Vec::>::new())) as ArrayRef; + let lists = FixedSizeListArray::try_new_with_length(item, 0, values, None, 3).unwrap(); + let schema = Arc::new(Schema::new(vec![ArrowField::new( + "lists", + lists.data_type().clone(), + true, + )])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(lists) as ArrayRef]).unwrap(); + + let converted = convert_json_columns(&batch).unwrap(); + assert_eq!(converted.num_rows(), 3); + assert_eq!(converted.column(0).len(), 3); + + let converted_schema = converted.schema(); + let DataType::FixedSizeList(item, size) = converted_schema.field(0).data_type() else { + panic!("expected fixed size list field"); + }; + assert_eq!(*size, 0); + assert!(is_json_field(item)); + + let logical = convert_lance_json_to_arrow(&converted).unwrap(); + assert_eq!(logical.num_rows(), 3); + assert_eq!(logical.column(0).len(), 3); + + let logical_schema = logical.schema(); + let DataType::FixedSizeList(item, size) = logical_schema.field(0).data_type() else { + panic!("expected fixed size list field"); + }; + assert_eq!(*size, 0); + assert!(is_arrow_json_field(item)); + } + + #[test] + fn test_has_json_fields() { + // Test direct JSON field + let json_f = json_field("data", true); + assert!(has_json_fields(&json_f)); + + // Test non-JSON field + let non_json = ArrowField::new("data", DataType::Utf8, true); + assert!(!has_json_fields(&non_json)); + + // Test struct containing JSON field + let struct_field = ArrowField::new( + "struct", + DataType::Struct(vec![json_field("nested_json", true)].into()), + true, + ); + assert!(has_json_fields(&struct_field)); + + // Test struct without JSON field + let struct_no_json = ArrowField::new( + "struct", + DataType::Struct(vec![ArrowField::new("text", DataType::Utf8, true)].into()), + true, + ); + assert!(!has_json_fields(&struct_no_json)); + + // Test List containing JSON field + let list_field = ArrowField::new( + "list", + DataType::List(Arc::new(json_field("item", true))), + true, + ); + assert!(has_json_fields(&list_field)); + + // Test LargeList containing JSON field + let large_list_field = ArrowField::new( + "large_list", + DataType::LargeList(Arc::new(json_field("item", true))), + true, + ); + assert!(has_json_fields(&large_list_field)); + + // Test FixedSizeList containing JSON field + let fixed_list_field = ArrowField::new( + "fixed_list", + DataType::FixedSizeList(Arc::new(json_field("item", true)), 3), + true, + ); + assert!(has_json_fields(&fixed_list_field)); + + // Test Map containing JSON field + let map_field = ArrowField::new( + "map", + DataType::Map( + Arc::new(ArrowField::new( + "entries", + DataType::Struct( + vec![ + ArrowField::new("key", DataType::Utf8, false), + json_field("value", true), + ] + .into(), + ), + false, + )), + false, + ), + true, + ); + assert!(has_json_fields(&map_field)); + } + + #[test] + fn test_json_array_inner() { + let json_array = JsonArray::try_from_iter(vec![Some(r#"{"a": 1}"#)]).unwrap(); + let inner = json_array.inner(); + assert_eq!(inner.len(), 1); + } + + #[test] + fn test_json_array_value_null_error() { + let json_array = JsonArray::try_from_iter(vec![None::<&str>]).unwrap(); + let result = json_array.value(0); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("null")); + } + + #[test] + fn test_json_array_value_bytes() { + let json_array = JsonArray::try_from_iter(vec![Some(r#"{"a": 1}"#)]).unwrap(); + let bytes = json_array.value_bytes(0); + assert!(!bytes.is_empty()); + } + + #[test] + fn test_json_path_with_null() { + let json_array = + JsonArray::try_from_iter(vec![Some(r#"{"user": {"name": "Alice"}}"#), None::<&str>]) + .unwrap(); + + let result = json_array.json_path(1, "$.user.name").unwrap(); + assert_eq!(result, None); + } + + #[test] + fn test_to_arrow_json() { + let json_array = JsonArray::try_from_iter(vec![ + Some(r#"{"name": "Alice"}"#), + None::<&str>, + Some(r#"{"name": "Bob"}"#), + ]) + .unwrap(); + + let arrow_json = json_array.to_arrow_json(); + assert_eq!(arrow_json.len(), 3); + assert!(!arrow_json.is_null(0)); + assert!(arrow_json.is_null(1)); + assert!(!arrow_json.is_null(2)); + + let string_array = arrow_json.as_any().downcast_ref::().unwrap(); + assert!(string_array.value(0).contains("Alice")); + assert!(string_array.value(2).contains("Bob")); + } + + #[test] + fn test_json_array_trait_methods() { + let json_array = + JsonArray::try_from_iter(vec![Some(r#"{"a": 1}"#), Some(r#"{"b": 2}"#)]).unwrap(); + + // Wrapper methods + assert_eq!(json_array.len(), 2); + assert!(!json_array.is_empty()); + assert!(!json_array.is_null(0)); + + // Underlying Arrow array + assert_eq!(json_array.inner().data_type(), &DataType::LargeBinary); + assert_eq!(json_array.inner().len(), 2); + } + + #[test] + fn test_json_array_empty() { + let json_array = JsonArray::try_from_iter(Vec::>::new()).unwrap(); + assert!(json_array.is_empty()); + assert_eq!(json_array.len(), 0); + } + + #[test] + fn test_try_from_large_string_array() { + let large_string_array = LargeStringArray::from(vec![ + Some(r#"{"name": "Alice"}"#), + Some(r#"{"name": "Bob"}"#), + None, + ]); + + // Test TryFrom<&LargeStringArray> + let json_array = JsonArray::try_from(&large_string_array).unwrap(); + assert_eq!(json_array.len(), 3); + assert!(!json_array.is_null(0)); + assert!(!json_array.is_null(1)); + assert!(json_array.is_null(2)); + + // Test TryFrom (owned) + let large_string_array2 = LargeStringArray::from(vec![Some(r#"{"x": 1}"#)]); + let json_array2 = JsonArray::try_from(large_string_array2).unwrap(); + assert_eq!(json_array2.len(), 1); + } + + #[test] + fn test_try_from_array_ref() { + // Test with Utf8 + let string_array: ArrayRef = Arc::new(StringArray::from(vec![ + Some(r#"{"a": 1}"#), + Some(r#"{"b": 2}"#), + ])); + let json_array = JsonArray::try_from(string_array).unwrap(); + assert_eq!(json_array.len(), 2); + + // Test with LargeUtf8 + let large_string_array: ArrayRef = Arc::new(LargeStringArray::from(vec![ + Some(r#"{"c": 3}"#), + Some(r#"{"d": 4}"#), + ])); + let json_array2 = JsonArray::try_from(large_string_array).unwrap(); + assert_eq!(json_array2.len(), 2); + + // Test with unsupported type + let int_array: ArrayRef = Arc::new(arrow_array::Int32Array::from(vec![1, 2, 3])); + let result = JsonArray::try_from(int_array); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("Unsupported")); + } + + #[test] + fn test_arrow_json_to_lance_json_non_json_field() { + // Test that non-JSON fields are returned unchanged + let field = ArrowField::new("text", DataType::Utf8, true); + let converted = arrow_json_to_lance_json(&field); + assert_eq!(converted.data_type(), &DataType::Utf8); + assert_eq!(converted.name(), "text"); + } + + #[test] + fn test_convert_lance_json_to_arrow() { + // Create a batch with Lance JSON column (JSONB) + let json_array = JsonArray::try_from_iter(vec![ + Some(r#"{"name": "Alice"}"#), + None::<&str>, + Some(r#"{"name": "Bob"}"#), + ]) + .unwrap(); + + let lance_json_field = json_field("data", true); + let schema = Arc::new(Schema::new(vec![lance_json_field])); + let batch = + RecordBatch::try_new(schema, vec![Arc::new(json_array.into_inner()) as ArrayRef]) + .unwrap(); + + // Convert back to Arrow JSON + let converted = convert_lance_json_to_arrow(&batch).unwrap(); + + // Check schema + let converted_schema = converted.schema(); + let converted_field = converted_schema.field(0); + assert_eq!(converted_field.data_type(), &DataType::Utf8); + assert_eq!( + converted_field.metadata().get(ARROW_EXT_NAME_KEY), + Some(&ARROW_JSON_EXT_NAME.to_string()) + ); + + // Check data + let string_array = converted + .column(0) + .as_any() + .downcast_ref::() + .unwrap(); + assert!(!string_array.is_null(0)); + assert!(string_array.is_null(1)); + assert!(!string_array.is_null(2)); + assert!(string_array.value(0).contains("Alice")); + assert!(string_array.value(2).contains("Bob")); + } + + #[test] + fn test_convert_lance_json_to_arrow_empty_batch() { + // Create an empty batch with Lance JSON column + let lance_json_field = json_field("data", true); + let schema = Arc::new(Schema::new(vec![lance_json_field])); + let empty_binary = LargeBinaryBuilder::new().finish(); + let batch = RecordBatch::try_new(schema, vec![Arc::new(empty_binary) as ArrayRef]).unwrap(); + + // Convert back to Arrow JSON + let converted = convert_lance_json_to_arrow(&batch).unwrap(); + assert_eq!(converted.num_rows(), 0); + assert_eq!(converted.schema().field(0).data_type(), &DataType::Utf8); + } + + #[test] + fn test_convert_lance_json_to_arrow_no_json_columns() { + // Create a batch without JSON columns + let field = ArrowField::new("text", DataType::Utf8, true); + let schema = Arc::new(Schema::new(vec![field])); + let string_array = StringArray::from(vec![Some("hello"), Some("world")]); + let batch = RecordBatch::try_new(schema, vec![Arc::new(string_array) as ArrayRef]).unwrap(); + + // Convert - should return the same batch + let converted = convert_lance_json_to_arrow(&batch).unwrap(); + assert_eq!(converted.num_columns(), 1); + assert_eq!(converted.schema().field(0).data_type(), &DataType::Utf8); + } + + #[test] + fn test_convert_json_columns_empty_batch() { + // Create an empty batch with Arrow JSON column + let mut field = ArrowField::new("data", DataType::Utf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let empty_strings = arrow_array::builder::StringBuilder::new().finish(); + let batch = + RecordBatch::try_new(schema, vec![Arc::new(empty_strings) as ArrayRef]).unwrap(); + + let converted = convert_json_columns(&batch).unwrap(); + assert_eq!(converted.num_rows(), 0); + assert_eq!( + converted.schema().field(0).data_type(), + &DataType::LargeBinary + ); + } + + #[test] + fn test_convert_json_columns_large_string() { + // Create a batch with Arrow JSON column using LargeUtf8 + let json_strings = LargeStringArray::from(vec![ + Some(r#"{"name": "Alice"}"#), + Some(r#"{"name": "Bob"}"#), + ]); + + let mut field = ArrowField::new("data", DataType::LargeUtf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(json_strings) as ArrayRef]).unwrap(); + + let converted = convert_json_columns(&batch).unwrap(); + assert_eq!(converted.num_columns(), 1); + assert_eq!( + converted.schema().field(0).data_type(), + &DataType::LargeBinary + ); + assert_eq!(converted.num_rows(), 2); + } + + #[test] + fn test_convert_json_columns_no_json_columns() { + // Create a batch without JSON columns + let field = ArrowField::new("text", DataType::Utf8, true); + let schema = Arc::new(Schema::new(vec![field])); + let string_array = StringArray::from(vec![Some("hello"), Some("world")]); + let batch = RecordBatch::try_new(schema, vec![Arc::new(string_array) as ArrayRef]).unwrap(); + + // Convert - should return the same batch + let converted = convert_json_columns(&batch).unwrap(); + assert_eq!(converted.num_columns(), 1); + assert_eq!(converted.schema().field(0).data_type(), &DataType::Utf8); + } + + #[test] + fn test_convert_json_columns_mixed_columns() { + // Create a batch with both JSON and non-JSON columns + let json_strings = StringArray::from(vec![ + Some(r#"{"name": "Alice"}"#), + Some(r#"{"name": "Bob"}"#), + ]); + let text_strings = StringArray::from(vec![Some("hello"), Some("world")]); + + let mut json_field = ArrowField::new("json_data", DataType::Utf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + json_field.set_metadata(metadata); + + let text_field = ArrowField::new("text_data", DataType::Utf8, true); + + let schema = Arc::new(Schema::new(vec![json_field, text_field])); + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(json_strings) as ArrayRef, + Arc::new(text_strings) as ArrayRef, + ], + ) + .unwrap(); + + let converted = convert_json_columns(&batch).unwrap(); + assert_eq!(converted.num_columns(), 2); + assert_eq!( + converted.schema().field(0).data_type(), + &DataType::LargeBinary + ); + assert_eq!(converted.schema().field(1).data_type(), &DataType::Utf8); + } + + #[test] + fn test_is_arrow_json_field_large_utf8() { + // Test with LargeUtf8 storage type + let mut field = ArrowField::new("data", DataType::LargeUtf8, true); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + assert!(is_arrow_json_field(&field)); + } + + #[test] + fn test_encode_json_invalid() { + // Test encoding invalid JSON + let result = encode_json("not valid json {"); + assert!(result.is_err()); + } + + #[test] + fn test_json_array_from_invalid_json() { + // Test creating JsonArray from invalid JSON strings + let result = JsonArray::try_from_iter(vec![Some("invalid json {")]); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("Failed to encode")); + } + + #[test] + fn test_try_from_string_array_invalid_json() { + let string_array = StringArray::from(vec![Some("invalid json {")]); + let result = JsonArray::try_from(string_array); + assert!(result.is_err()); + } + + #[test] + fn test_try_from_large_string_array_invalid_json() { + let large_string_array = LargeStringArray::from(vec![Some("invalid json {")]); + let result = JsonArray::try_from(large_string_array); + assert!(result.is_err()); + } + + #[test] + fn test_convert_lance_json_to_arrow_mixed_columns() { + // Create a batch with both JSON and non-JSON columns + let json_array = JsonArray::try_from_iter(vec![ + Some(r#"{"name": "Alice"}"#), + Some(r#"{"name": "Bob"}"#), + ]) + .unwrap(); + let text_strings = StringArray::from(vec![Some("hello"), Some("world")]); + + let json_f = json_field("json_data", true); + let text_field = ArrowField::new("text_data", DataType::Utf8, true); + + let schema = Arc::new(Schema::new(vec![json_f, text_field])); + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(json_array.into_inner()) as ArrayRef, + Arc::new(text_strings) as ArrayRef, + ], + ) + .unwrap(); + + let converted = convert_lance_json_to_arrow(&batch).unwrap(); + assert_eq!(converted.num_columns(), 2); + assert_eq!(converted.schema().field(0).data_type(), &DataType::Utf8); + assert_eq!(converted.schema().field(1).data_type(), &DataType::Utf8); + } + + #[test] + fn test_json_path_invalid_path() { + let json_array = JsonArray::try_from_iter(vec![Some(r#"{"a": 1}"#)]).unwrap(); + // Invalid JSONPath syntax should return error + let result = json_array.json_path(0, "invalid path without $"); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("Failed to extract JSONPath") + ); + } + + #[test] + fn test_convert_json_columns_invalid_storage_type() { + // Create a batch with Arrow JSON field but wrong storage type (Int32 instead of Utf8) + let int_array = arrow_array::Int32Array::from(vec![1, 2, 3]); + + let mut field = ArrowField::new("data", DataType::Int32, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(int_array) as ArrayRef]).unwrap(); + + // This should succeed since Int32 doesn't match is_arrow_json_field check + // (is_arrow_json_field requires Utf8 or LargeUtf8) + let result = convert_json_columns(&batch); + assert!(result.is_ok()); + } + + #[test] + fn test_is_json_field_wrong_extension() { + // LargeBinary field without the correct extension metadata + let field = ArrowField::new("data", DataType::LargeBinary, true); + assert!(!is_json_field(&field)); + + // LargeBinary field with wrong extension name + let mut field2 = ArrowField::new("data", DataType::LargeBinary, true); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + "other.extension".to_string(), + ); + field2.set_metadata(metadata); + assert!(!is_json_field(&field2)); + } + + #[test] + fn test_is_arrow_json_field_wrong_extension() { + // Utf8 field without extension metadata + let field = ArrowField::new("data", DataType::Utf8, true); + assert!(!is_arrow_json_field(&field)); + + // Utf8 field with wrong extension name + let mut field2 = ArrowField::new("data", DataType::Utf8, true); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + "other.extension".to_string(), + ); + field2.set_metadata(metadata); + assert!(!is_arrow_json_field(&field2)); + + // Wrong type entirely + let field3 = ArrowField::new("data", DataType::Int32, true); + assert!(!is_arrow_json_field(&field3)); + } + + #[test] + fn test_convert_json_columns_invalid_json_utf8() { + // Test error propagation when converting invalid JSON (Utf8) + let invalid_json = StringArray::from(vec![Some("invalid json {")]); + + let mut field = ArrowField::new("data", DataType::Utf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(invalid_json) as ArrayRef]).unwrap(); + + let result = convert_json_columns(&batch); + assert!(result.is_err()); + } + + #[test] + fn test_convert_json_columns_invalid_json_large_utf8() { + // Test error propagation when converting invalid JSON (LargeUtf8) + let invalid_json = LargeStringArray::from(vec![Some("invalid json {")]); + + let mut field = ArrowField::new("data", DataType::LargeUtf8, false); + let mut metadata = std::collections::HashMap::new(); + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + ARROW_JSON_EXT_NAME.to_string(), + ); + field.set_metadata(metadata); + + let schema = Arc::new(Schema::new(vec![field])); + let batch = RecordBatch::try_new(schema, vec![Arc::new(invalid_json) as ArrayRef]).unwrap(); + + let result = convert_json_columns(&batch); + assert!(result.is_err()); + } + + #[test] + fn test_json_path_on_corrupted_jsonb() { + // Create corrupted JSONB bytes directly + let corrupted_bytes: &[u8] = &[0xFF, 0xFE, 0x00, 0x01, 0x02]; + let corrupted_binary = LargeBinaryArray::from(vec![Some(corrupted_bytes)]); + + // Wrap in JsonArray + let corrupted_json = JsonArray { + inner: corrupted_binary, + }; + + // Try to use json_path on corrupted data - the selector might fail or return unexpected results + // This exercises the code path but may not produce an error depending on jsonb library behavior + let _result = corrupted_json.json_path(0, "$.a"); + // We don't assert on the result as the behavior depends on the jsonb library + } + + #[test] + fn test_decode_json_on_various_inputs() { + // Test decode_json with various inputs + let valid_jsonb = encode_json(r#"{"key": "value"}"#).unwrap(); + let decoded = decode_json(&valid_jsonb); + assert!(decoded.contains("key")); + + // Empty bytes - jsonb library handles this gracefully + let decoded_empty = decode_json(&[]); + // Just verify it doesn't panic + let _ = decoded_empty; + + // Random bytes - jsonb library handles this gracefully + let decoded_random = decode_json(&[0xFF, 0xFE, 0x00]); + // Just verify it doesn't panic + let _ = decoded_random; + } +} diff --git a/lance-artifact/rust/lance-arrow/src/lib.rs b/lance-artifact/rust/lance-arrow/src/lib.rs new file mode 100644 index 000000000..942fdf7d9 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/lib.rs @@ -0,0 +1,2764 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Extend Arrow Functionality +//! +//! To improve Arrow-RS ergonomic + +#![warn(clippy::undocumented_unsafe_blocks)] + +// lance-arrow reinterprets value bytes as native numeric types in +// `FloatArray::as_slice` for `bf16` (rust/lance-arrow/src/bfloat16.rs), which +// requires the host byte order to match the on-disk byte order Lance writes. +// Lance writes little-endian; building on a big-endian target would silently +// produce wrong numeric values. +#[cfg(not(target_endian = "little"))] +compile_error!("lance-arrow only supports little-endian targets"); + +use std::sync::Arc; +use std::{collections::HashMap, ptr::NonNull}; + +use arrow_array::{ + Array, ArrayRef, ArrowNumericType, FixedSizeBinaryArray, FixedSizeListArray, GenericListArray, + LargeListArray, ListArray, OffsetSizeTrait, PrimitiveArray, RecordBatch, StructArray, + UInt8Array, UInt32Array, cast::AsArray, +}; +use arrow_array::{ + Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Int64Array, new_null_array, +}; +use arrow_buffer::MutableBuffer; +use arrow_data::ArrayDataBuilder; +use arrow_schema::{ArrowError, DataType, Field, Fields, IntervalUnit, Schema, SortOptions}; +use arrow_select::{interleave::interleave, take::take}; +use rand::prelude::*; + +pub mod deepcopy; +pub mod schema; +pub use schema::*; +pub mod bfloat16; +pub mod floats; +use crate::list::ListArrayExt; +pub use floats::*; + +pub mod ipc; +pub mod json; +pub mod list; +pub mod memory; +pub mod scalar; +pub mod stream; +pub mod r#struct; + +/// Arrow extension metadata key for extension name +pub const ARROW_EXT_NAME_KEY: &str = "ARROW:extension:name"; + +/// Arrow extension metadata key for extension metadata +pub const ARROW_EXT_META_KEY: &str = "ARROW:extension:metadata"; + +/// Key used by lance to mark a field as a blob +/// TODO: Use Arrow extension mechanism instead? +pub const BLOB_META_KEY: &str = "lance-encoding:blob"; +/// Arrow extension type name for Lance blob v2 columns +pub const BLOB_V2_EXT_NAME: &str = "lance.blob.v2"; +/// Metadata key for overriding the dedicated blob size threshold (in bytes) +pub const BLOB_DEDICATED_SIZE_THRESHOLD_META_KEY: &str = + "lance-encoding:blob-dedicated-size-threshold"; +/// Metadata key for overriding the inline blob size threshold (in bytes) +pub const BLOB_INLINE_SIZE_THRESHOLD_META_KEY: &str = "lance-encoding:blob-inline-size-threshold"; +/// Metadata key for overriding the maximum size (in bytes) of a packed blob sidecar file +pub const BLOB_PACK_FILE_SIZE_THRESHOLD_META_KEY: &str = + "lance-encoding:blob-pack-file-size-threshold"; + +type Result = std::result::Result; + +pub trait DataTypeExt { + /// Returns true if the data type is binary-like, such as Utf8, Binary, or the large and/or view variants. + fn is_binary_like(&self) -> bool; + + /// Returns true if the data type is a struct. + fn is_struct(&self) -> bool; + + /// Check whether the given Arrow DataType is fixed stride. + /// + /// A fixed stride type has the same byte width for all array elements + /// This includes all PrimitiveType's Boolean, FixedSizeList, FixedSizeBinary, and Decimals + fn is_fixed_stride(&self) -> bool; + + /// Returns true if the [DataType] is a dictionary type. + fn is_dictionary(&self) -> bool; + + /// Returns the byte width of the data type + /// Panics if the data type is not fixed stride. + fn byte_width(&self) -> usize; + + /// Returns the byte width of the data type, if it is fixed stride. + /// Returns None if the data type is not fixed stride. + fn byte_width_opt(&self) -> Option; +} + +impl DataTypeExt for DataType { + fn is_binary_like(&self) -> bool { + use DataType::*; + matches!( + self, + Utf8 | Binary | LargeUtf8 | LargeBinary | Utf8View | BinaryView + ) + } + + fn is_struct(&self) -> bool { + matches!(self, Self::Struct(_)) + } + + fn is_fixed_stride(&self) -> bool { + use DataType::*; + matches!( + self, + Boolean + | UInt8 + | UInt16 + | UInt32 + | UInt64 + | Int8 + | Int16 + | Int32 + | Int64 + | Float16 + | Float32 + | Float64 + | Decimal128(_, _) + | Decimal256(_, _) + | FixedSizeList(_, _) + | FixedSizeBinary(_) + | Duration(_) + | Timestamp(_, _) + | Date32 + | Date64 + | Time32(_) + | Time64(_) + ) + } + + fn is_dictionary(&self) -> bool { + matches!(self, Self::Dictionary(_, _)) + } + + fn byte_width_opt(&self) -> Option { + match self { + Self::Int8 => Some(1), + Self::Int16 => Some(2), + Self::Int32 => Some(4), + Self::Int64 => Some(8), + Self::UInt8 => Some(1), + Self::UInt16 => Some(2), + Self::UInt32 => Some(4), + Self::UInt64 => Some(8), + Self::Float16 => Some(2), + Self::Float32 => Some(4), + Self::Float64 => Some(8), + Self::Date32 => Some(4), + Self::Date64 => Some(8), + Self::Time32(_) => Some(4), + Self::Time64(_) => Some(8), + Self::Timestamp(_, _) => Some(8), + Self::Duration(_) => Some(8), + Self::Decimal128(_, _) => Some(16), + Self::Decimal256(_, _) => Some(32), + Self::Interval(unit) => match unit { + IntervalUnit::YearMonth => Some(4), + IntervalUnit::DayTime => Some(8), + IntervalUnit::MonthDayNano => Some(16), + }, + Self::FixedSizeBinary(s) => Some(*s as usize), + Self::FixedSizeList(dt, s) => dt + .data_type() + .byte_width_opt() + .map(|width| width * *s as usize), + _ => None, + } + } + + fn byte_width(&self) -> usize { + self.byte_width_opt() + .unwrap_or_else(|| panic!("Expecting fixed stride data type, found {:?}", self)) + } +} + +/// Create an [`GenericListArray`] from values and offsets. +/// +/// ``` +/// use arrow_array::{Int32Array, Int64Array, ListArray}; +/// use arrow_array::types::Int64Type; +/// use lance_arrow::try_new_generic_list_array; +/// +/// let offsets = Int32Array::from_iter([0, 2, 7, 10]); +/// let int_values = Int64Array::from_iter(0..10); +/// let list_arr = try_new_generic_list_array(int_values, &offsets).unwrap(); +/// assert_eq!(list_arr, +/// ListArray::from_iter_primitive::(vec![ +/// Some(vec![Some(0), Some(1)]), +/// Some(vec![Some(2), Some(3), Some(4), Some(5), Some(6)]), +/// Some(vec![Some(7), Some(8), Some(9)]), +/// ])) +/// ``` +pub fn try_new_generic_list_array( + values: T, + offsets: &PrimitiveArray, +) -> Result> +where + Offset::Native: OffsetSizeTrait, +{ + let data_type = if Offset::Native::IS_LARGE { + DataType::LargeList(Arc::new(Field::new( + "item", + values.data_type().clone(), + true, + ))) + } else { + DataType::List(Arc::new(Field::new( + "item", + values.data_type().clone(), + true, + ))) + }; + let data = ArrayDataBuilder::new(data_type) + .len(offsets.len() - 1) + .add_buffer(offsets.into_data().buffers()[0].clone()) + .add_child_data(values.into_data()) + .build()?; + + Ok(GenericListArray::from(data)) +} + +pub fn fixed_size_list_type(list_width: i32, inner_type: DataType) -> DataType { + DataType::FixedSizeList(Arc::new(Field::new("item", inner_type, true)), list_width) +} + +pub trait FixedSizeListArrayExt { + /// Create an [`FixedSizeListArray`] from values and list size. + /// + /// ``` + /// use arrow_array::{Int64Array, FixedSizeListArray}; + /// use arrow_array::types::Int64Type; + /// use lance_arrow::FixedSizeListArrayExt; + /// + /// let int_values = Int64Array::from_iter(0..10); + /// let fixed_size_list_arr = FixedSizeListArray::try_new_from_values(int_values, 2).unwrap(); + /// assert_eq!(fixed_size_list_arr, + /// FixedSizeListArray::from_iter_primitive::(vec![ + /// Some(vec![Some(0), Some(1)]), + /// Some(vec![Some(2), Some(3)]), + /// Some(vec![Some(4), Some(5)]), + /// Some(vec![Some(6), Some(7)]), + /// Some(vec![Some(8), Some(9)]) + /// ], 2)) + /// ``` + fn try_new_from_values( + values: T, + list_size: i32, + ) -> Result; + + /// Sample `n` rows from the [FixedSizeListArray] + /// + /// ``` + /// use arrow_array::{Int64Array, FixedSizeListArray, Array}; + /// use lance_arrow::FixedSizeListArrayExt; + /// + /// let int_values = Int64Array::from_iter(0..256); + /// let fixed_size_list_arr = FixedSizeListArray::try_new_from_values(int_values, 16).unwrap(); + /// let sampled = fixed_size_list_arr.sample(10).unwrap(); + /// assert_eq!(sampled.len(), 10); + /// assert_eq!(sampled.value_length(), 16); + /// assert_eq!(sampled.values().len(), 160); + /// ``` + fn sample(&self, n: usize) -> Result; + + /// Ensure the [FixedSizeListArray] of Float16, Float32, Float64, + /// Int8, Int16, Int32, Int64, UInt8, UInt32 type to its closest floating point type. + fn convert_to_floating_point(&self) -> Result; +} + +impl FixedSizeListArrayExt for FixedSizeListArray { + fn try_new_from_values(values: T, list_size: i32) -> Result { + let field = Arc::new(Field::new("item", values.data_type().clone(), true)); + let values = Arc::new(values); + + Self::try_new(field, list_size, values, None) + } + + fn sample(&self, n: usize) -> Result { + if n >= self.len() { + return Ok(self.clone()); + } + let mut rng = SmallRng::from_os_rng(); + let chosen = (0..self.len() as u32).choose_multiple(&mut rng, n); + take(self, &UInt32Array::from(chosen), None).map(|arr| arr.as_fixed_size_list().clone()) + } + + fn convert_to_floating_point(&self) -> Result { + match self.data_type() { + DataType::FixedSizeList(field, size) => match field.data_type() { + DataType::Float16 | DataType::Float32 | DataType::Float64 => Ok(self.clone()), + DataType::Int8 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float32, + field.is_nullable(), + )), + *size, + Arc::new(Float32Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to Int8Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f32)), + )), + self.nulls().cloned(), + )), + DataType::Int16 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float32, + field.is_nullable(), + )), + *size, + Arc::new(Float32Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to Int16Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f32)), + )), + self.nulls().cloned(), + )), + DataType::Int32 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float32, + field.is_nullable(), + )), + *size, + Arc::new(Float32Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to Int32Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f32)), + )), + self.nulls().cloned(), + )), + DataType::Int64 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float64, + field.is_nullable(), + )), + *size, + Arc::new(Float64Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to Int64Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f64)), + )), + self.nulls().cloned(), + )), + DataType::UInt8 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float64, + field.is_nullable(), + )), + *size, + Arc::new(Float64Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to UInt8Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f64)), + )), + self.nulls().cloned(), + )), + DataType::UInt32 => Ok(Self::new( + Arc::new(arrow_schema::Field::new( + field.name(), + DataType::Float64, + field.is_nullable(), + )), + *size, + Arc::new(Float64Array::from_iter( + self.values() + .as_any() + .downcast_ref::() + .ok_or(ArrowError::ParseError( + "Fail to cast primitive array to UInt32Type".to_string(), + ))? + .into_iter() + .map(|x| x.map(|y| y as f64)), + )), + self.nulls().cloned(), + )), + data_type => Err(ArrowError::ParseError(format!( + "Expect either floating type or integer got {:?}", + data_type + ))), + }, + data_type => Err(ArrowError::ParseError(format!( + "Expect either FixedSizeList got {:?}", + data_type + ))), + } + } +} + +/// Force downcast of an [`Array`], such as an [`ArrayRef`], to +/// [`FixedSizeListArray`], panic'ing on failure. +pub fn as_fixed_size_list_array(arr: &dyn Array) -> &FixedSizeListArray { + arr.as_any().downcast_ref::().unwrap() +} + +pub trait FixedSizeBinaryArrayExt { + /// Create an [`FixedSizeBinaryArray`] from values and stride. + /// + /// ``` + /// use arrow_array::{UInt8Array, FixedSizeBinaryArray}; + /// use arrow_array::types::UInt8Type; + /// use lance_arrow::FixedSizeBinaryArrayExt; + /// + /// let int_values = UInt8Array::from_iter(0..10); + /// let fixed_size_list_arr = FixedSizeBinaryArray::try_new_from_values(&int_values, 2).unwrap(); + /// assert_eq!(fixed_size_list_arr, + /// FixedSizeBinaryArray::from(vec![ + /// Some(vec![0, 1].as_slice()), + /// Some(vec![2, 3].as_slice()), + /// Some(vec![4, 5].as_slice()), + /// Some(vec![6, 7].as_slice()), + /// Some(vec![8, 9].as_slice()) + /// ])) + /// ``` + fn try_new_from_values(values: &UInt8Array, stride: i32) -> Result; +} + +impl FixedSizeBinaryArrayExt for FixedSizeBinaryArray { + fn try_new_from_values(values: &UInt8Array, stride: i32) -> Result { + let data_type = DataType::FixedSizeBinary(stride); + let data = ArrayDataBuilder::new(data_type) + .len(values.len() / stride as usize) + .add_buffer(values.into_data().buffers()[0].clone()) + .build()?; + Ok(Self::from(data)) + } +} + +pub fn as_fixed_size_binary_array(arr: &dyn Array) -> &FixedSizeBinaryArray { + arr.as_any().downcast_ref::().unwrap() +} + +pub fn iter_str_array(arr: &dyn Array) -> Box> + Send + '_> { + match arr.data_type() { + DataType::Utf8 => Box::new(arr.as_string::().iter()), + DataType::LargeUtf8 => Box::new(arr.as_string::().iter()), + _ => panic!("Expecting Utf8 or LargeUtf8, found {:?}", arr.data_type()), + } +} + +/// Extends Arrow's [RecordBatch]. +pub trait RecordBatchExt { + /// Append a new column to this [`RecordBatch`] and returns a new RecordBatch. + /// + /// ``` + /// use std::sync::Arc; + /// use arrow_array::{RecordBatch, Int32Array, StringArray}; + /// use arrow_schema::{Schema, Field, DataType}; + /// use lance_arrow::*; + /// + /// let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)])); + /// let int_arr = Arc::new(Int32Array::from(vec![1, 2, 3, 4])); + /// let record_batch = RecordBatch::try_new(schema, vec![int_arr.clone()]).unwrap(); + /// + /// let new_field = Field::new("s", DataType::Utf8, true); + /// let str_arr = Arc::new(StringArray::from(vec!["a", "b", "c", "d"])); + /// let new_record_batch = record_batch.try_with_column(new_field, str_arr.clone()).unwrap(); + /// + /// assert_eq!( + /// new_record_batch, + /// RecordBatch::try_new( + /// Arc::new(Schema::new( + /// vec![ + /// Field::new("a", DataType::Int32, true), + /// Field::new("s", DataType::Utf8, true) + /// ]) + /// ), + /// vec![int_arr, str_arr], + /// ).unwrap() + /// ) + /// ``` + fn try_with_column(&self, field: Field, arr: ArrayRef) -> Result; + + /// Created a new RecordBatch with column at index. + fn try_with_column_at(&self, index: usize, field: Field, arr: ArrayRef) -> Result; + + /// Creates a new [`RecordBatch`] from the provided [`StructArray`]. + /// + /// The fields on the [`StructArray`] need to match this [`RecordBatch`] schema + fn try_new_from_struct_array(&self, arr: StructArray) -> Result; + + /// Merge with another [`RecordBatch`] and returns a new one. + /// + /// Fields are merged based on name. First we iterate the left columns. If a matching + /// name is found in the right then we merge the two columns. If there is no match then + /// we add the left column to the output. + /// + /// To merge two columns we consider the type. If both arrays are struct arrays we recurse. + /// Otherwise we use the left array. + /// + /// Afterwards we add all non-matching right columns to the output. + /// + /// Note: This method likely does not handle nested fields correctly and you may want to consider + /// using [`Self::merge_with_schema`] instead. + /// ``` + /// use std::sync::Arc; + /// use arrow_array::*; + /// use arrow_schema::{Schema, Field, DataType}; + /// use lance_arrow::*; + /// + /// let left_schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)])); + /// let int_arr = Arc::new(Int32Array::from(vec![1, 2, 3, 4])); + /// let left = RecordBatch::try_new(left_schema, vec![int_arr.clone()]).unwrap(); + /// + /// let right_schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + /// let str_arr = Arc::new(StringArray::from(vec!["a", "b", "c", "d"])); + /// let right = RecordBatch::try_new(right_schema, vec![str_arr.clone()]).unwrap(); + /// + /// let new_record_batch = left.merge(&right).unwrap(); + /// + /// assert_eq!( + /// new_record_batch, + /// RecordBatch::try_new( + /// Arc::new(Schema::new( + /// vec![ + /// Field::new("a", DataType::Int32, true), + /// Field::new("s", DataType::Utf8, true) + /// ]) + /// ), + /// vec![int_arr, str_arr], + /// ).unwrap() + /// ) + /// ``` + /// + /// TODO: add merge nested fields support. + fn merge(&self, other: &RecordBatch) -> Result; + + /// Create a batch by merging columns between two batches with a given schema. + /// + /// A reference schema is used to determine the proper ordering of nested fields. + /// + /// For each field in the reference schema we look for corresponding fields in + /// the left and right batches. If a field is found in both batches we recursively merge + /// it. + /// + /// If a field is only in the left or right batch we take it as it is. + fn merge_with_schema(&self, other: &RecordBatch, schema: &Schema) -> Result; + + /// Drop one column specified with the name and return the new [`RecordBatch`]. + /// + /// If the named column does not exist, it returns a copy of this [`RecordBatch`]. + fn drop_column(&self, name: &str) -> Result; + + /// Replace a column (specified by name) and return the new [`RecordBatch`]. + fn replace_column_by_name(&self, name: &str, column: Arc) -> Result; + + /// Replace a column schema (specified by name) and return the new [`RecordBatch`]. + fn replace_column_schema_by_name( + &self, + name: &str, + new_data_type: DataType, + column: Arc, + ) -> Result; + + /// Rename a column at a given index. + fn rename_column(&self, index: usize, new_name: &str) -> Result; + + /// Get (potentially nested) column by qualified name. + fn column_by_qualified_name(&self, name: &str) -> Option<&ArrayRef>; + + /// Project the schema over the [RecordBatch]. + fn project_by_schema(&self, schema: &Schema) -> Result; + + /// metadata of the schema. + fn metadata(&self) -> &HashMap; + + /// Add metadata to the schema. + fn add_metadata(&self, key: String, value: String) -> Result { + let mut metadata = self.metadata().clone(); + metadata.insert(key, value); + self.with_metadata(metadata) + } + + /// Replace the schema metadata with the provided one. + fn with_metadata(&self, metadata: HashMap) -> Result; + + /// Take selected rows from the [RecordBatch]. + fn take(&self, indices: &UInt32Array) -> Result; + + /// Create a new RecordBatch with compacted memory after slicing. + fn shrink_to_fit(&self) -> Result; + + /// Helper method to sort the RecordBatch by a column + fn sort_by_column(&self, column: usize, options: Option) -> Result; +} + +impl RecordBatchExt for RecordBatch { + fn try_with_column(&self, field: Field, arr: ArrayRef) -> Result { + let new_schema = Arc::new(self.schema().as_ref().try_with_column(field)?); + let mut new_columns = self.columns().to_vec(); + new_columns.push(arr); + Self::try_new(new_schema, new_columns) + } + + fn try_with_column_at(&self, index: usize, field: Field, arr: ArrayRef) -> Result { + let new_schema = Arc::new(self.schema().as_ref().try_with_column_at(index, field)?); + let mut new_columns = self.columns().to_vec(); + new_columns.insert(index, arr); + Self::try_new(new_schema, new_columns) + } + + fn try_new_from_struct_array(&self, arr: StructArray) -> Result { + let schema = Arc::new(Schema::new_with_metadata( + arr.fields().to_vec(), + self.schema().metadata.clone(), + )); + let batch = Self::from(arr); + batch.with_schema(schema) + } + + fn merge(&self, other: &Self) -> Result { + if self.num_rows() != other.num_rows() { + return Err(ArrowError::InvalidArgumentError(format!( + "Attempt to merge two RecordBatch with different sizes: {} != {}", + self.num_rows(), + other.num_rows() + ))); + } + let left_struct_array: StructArray = self.clone().into(); + let right_struct_array: StructArray = other.clone().into(); + self.try_new_from_struct_array(merge(&left_struct_array, &right_struct_array)) + } + + fn merge_with_schema(&self, other: &RecordBatch, schema: &Schema) -> Result { + if self.num_rows() != other.num_rows() { + return Err(ArrowError::InvalidArgumentError(format!( + "Attempt to merge two RecordBatch with different sizes: {} != {}", + self.num_rows(), + other.num_rows() + ))); + } + let left_struct_array: StructArray = self.clone().into(); + let right_struct_array: StructArray = other.clone().into(); + self.try_new_from_struct_array(merge_with_schema( + &left_struct_array, + &right_struct_array, + schema.fields(), + )) + } + + fn drop_column(&self, name: &str) -> Result { + let mut fields = vec![]; + let mut columns = vec![]; + for i in 0..self.schema().fields.len() { + if self.schema().field(i).name() != name { + fields.push(self.schema().field(i).clone()); + columns.push(self.column(i).clone()); + } + } + Self::try_new( + Arc::new(Schema::new_with_metadata( + fields, + self.schema().metadata().clone(), + )), + columns, + ) + } + + fn rename_column(&self, index: usize, new_name: &str) -> Result { + let mut fields = self.schema().fields().to_vec(); + if index >= fields.len() { + return Err(ArrowError::InvalidArgumentError(format!( + "Index out of bounds: {}", + index + ))); + } + fields[index] = Arc::new(Field::new( + new_name, + fields[index].data_type().clone(), + fields[index].is_nullable(), + )); + Self::try_new( + Arc::new(Schema::new_with_metadata( + fields, + self.schema().metadata().clone(), + )), + self.columns().to_vec(), + ) + } + + fn replace_column_by_name(&self, name: &str, column: Arc) -> Result { + let mut columns = self.columns().to_vec(); + let field_i = self + .schema() + .fields() + .iter() + .position(|f| f.name() == name) + .ok_or_else(|| ArrowError::SchemaError(format!("Field {} does not exist", name)))?; + columns[field_i] = column; + Self::try_new(self.schema(), columns) + } + + fn replace_column_schema_by_name( + &self, + name: &str, + new_data_type: DataType, + column: Arc, + ) -> Result { + let fields = self + .schema() + .fields() + .iter() + .map(|x| { + if x.name() != name { + x.clone() + } else { + let new_field = Field::new(name, new_data_type.clone(), x.is_nullable()); + Arc::new(new_field) + } + }) + .collect::>(); + let schema = Schema::new_with_metadata(fields, self.schema().metadata.clone()); + let mut columns = self.columns().to_vec(); + let field_i = self + .schema() + .fields() + .iter() + .position(|f| f.name() == name) + .ok_or_else(|| ArrowError::SchemaError(format!("Field {} does not exist", name)))?; + columns[field_i] = column; + Self::try_new(Arc::new(schema), columns) + } + + fn column_by_qualified_name(&self, name: &str) -> Option<&ArrayRef> { + let split = name.split('.').collect::>(); + if split.is_empty() { + return None; + } + + self.column_by_name(split[0]) + .and_then(|arr| get_sub_array(arr, &split[1..])) + } + + fn project_by_schema(&self, schema: &Schema) -> Result { + let struct_array: StructArray = self.clone().into(); + self.try_new_from_struct_array(project(&struct_array, schema.fields())?) + } + + fn metadata(&self) -> &HashMap { + self.schema_ref().metadata() + } + + fn with_metadata(&self, metadata: HashMap) -> Result { + let mut schema = self.schema_ref().as_ref().clone(); + schema.metadata = metadata; + Self::try_new(schema.into(), self.columns().into()) + } + + fn take(&self, indices: &UInt32Array) -> Result { + let struct_array: StructArray = self.clone().into(); + let taken = take(&struct_array, indices, None)?; + self.try_new_from_struct_array(taken.as_struct().clone()) + } + + fn shrink_to_fit(&self) -> Result { + // Deep copy the sliced record batch, instead of whole batch + crate::deepcopy::deep_copy_batch_sliced(self) + } + + fn sort_by_column(&self, column: usize, options: Option) -> Result { + if column >= self.num_columns() { + return Err(ArrowError::InvalidArgumentError(format!( + "Column index out of bounds: {}", + column + ))); + } + let column = self.column(column); + let sorted = arrow_ord::sort::sort_to_indices(column, options, None)?; + self.take(&sorted) + } +} + +/// Recursively projects an array to match the target field's structure. +/// This handles reordering fields inside nested List types. +fn project_array(array: &ArrayRef, target_field: &Field) -> Result { + match target_field.data_type() { + DataType::Struct(subfields) => { + let struct_arr = array.as_struct(); + let projected = project(struct_arr, subfields)?; + Ok(Arc::new(projected)) + } + DataType::List(inner_field) => { + let list_arr: &ListArray = array.as_list(); + let projected_values = project_array(list_arr.values(), inner_field.as_ref())?; + Ok(Arc::new(ListArray::new( + inner_field.clone(), + list_arr.offsets().clone(), + projected_values, + list_arr.nulls().cloned(), + ))) + } + DataType::LargeList(inner_field) => { + let list_arr: &LargeListArray = array.as_list(); + let projected_values = project_array(list_arr.values(), inner_field.as_ref())?; + Ok(Arc::new(LargeListArray::new( + inner_field.clone(), + list_arr.offsets().clone(), + projected_values, + list_arr.nulls().cloned(), + ))) + } + DataType::FixedSizeList(inner_field, size) => { + let list_arr = array.as_fixed_size_list(); + let projected_values = project_array(list_arr.values(), inner_field.as_ref())?; + Ok(Arc::new(FixedSizeListArray::new( + inner_field.clone(), + *size, + projected_values, + list_arr.nulls().cloned(), + ))) + } + _ => Ok(array.clone()), + } +} + +fn project(struct_array: &StructArray, fields: &Fields) -> Result { + if fields.is_empty() { + return Ok(StructArray::new_empty_fields( + struct_array.len(), + struct_array.nulls().cloned(), + )); + } + let mut columns: Vec = vec![]; + for field in fields.iter() { + if let Some(col) = struct_array.column_by_name(field.name()) { + let projected = project_array(col, field.as_ref())?; + columns.push(projected); + } else { + return Err(ArrowError::SchemaError(format!( + "field {} does not exist in the RecordBatch", + field.name() + ))); + } + } + // Preserve the struct's validity when projecting + StructArray::try_new(fields.clone(), columns, struct_array.nulls().cloned()) +} + +fn lists_have_same_offsets_helper(left: &dyn Array, right: &dyn Array) -> bool { + let left_list: &GenericListArray = left.as_list(); + let right_list: &GenericListArray = right.as_list(); + left_list.offsets().inner() == right_list.offsets().inner() +} + +fn merge_list_structs_helper( + left: &dyn Array, + right: &dyn Array, + items_field_name: impl Into, + items_nullable: bool, +) -> Arc { + let left_list: &GenericListArray = left.as_list(); + let right_list: &GenericListArray = right.as_list(); + let left_struct = left_list.values(); + let right_struct = right_list.values(); + let left_struct_arr = left_struct.as_struct(); + let right_struct_arr = right_struct.as_struct(); + let merged_items = Arc::new(merge(left_struct_arr, right_struct_arr)); + let items_field = Arc::new(Field::new( + items_field_name, + merged_items.data_type().clone(), + items_nullable, + )); + Arc::new(GenericListArray::::new( + items_field, + left_list.offsets().clone(), + merged_items, + left_list.nulls().cloned(), + )) +} + +fn merge_list_struct_null_helper( + left: &dyn Array, + right: &dyn Array, + not_null: &dyn Array, + items_field_name: impl Into, +) -> Arc { + let left_list: &GenericListArray = left.as_list::(); + let not_null_list = not_null.as_list::(); + let right_list = right.as_list::(); + + let left_struct = left_list.values().as_struct(); + let not_null_struct: &StructArray = not_null_list.values().as_struct(); + let right_struct = right_list.values().as_struct(); + + let values_len = not_null_list.values().len(); + let mut merged_fields = + Vec::with_capacity(not_null_struct.num_columns() + right_struct.num_columns()); + let mut merged_columns = + Vec::with_capacity(not_null_struct.num_columns() + right_struct.num_columns()); + + for (_, field) in left_struct.columns().iter().zip(left_struct.fields()) { + merged_fields.push(field.clone()); + if let Some(val) = not_null_struct.column_by_name(field.name()) { + merged_columns.push(val.clone()); + } else { + merged_columns.push(new_null_array(field.data_type(), values_len)) + } + } + for (_, field) in right_struct + .columns() + .iter() + .zip(right_struct.fields()) + .filter(|(_, field)| left_struct.column_by_name(field.name()).is_none()) + { + merged_fields.push(field.clone()); + if let Some(val) = not_null_struct.column_by_name(field.name()) { + merged_columns.push(val.clone()); + } else { + merged_columns.push(new_null_array(field.data_type(), values_len)); + } + } + + let merged_struct = Arc::new(StructArray::new( + Fields::from(merged_fields), + merged_columns, + not_null_struct.nulls().cloned(), + )); + let items_field = Arc::new(Field::new( + items_field_name, + merged_struct.data_type().clone(), + true, + )); + Arc::new(GenericListArray::::new( + items_field, + not_null_list.offsets().clone(), + merged_struct, + not_null_list.nulls().cloned(), + )) +} + +fn merge_list_struct_null( + left: &dyn Array, + right: &dyn Array, + not_null: &dyn Array, +) -> Arc { + match left.data_type() { + DataType::List(left_field) => { + merge_list_struct_null_helper::(left, right, not_null, left_field.name()) + } + DataType::LargeList(left_field) => { + merge_list_struct_null_helper::(left, right, not_null, left_field.name()) + } + _ => unreachable!(), + } +} + +fn merge_list_struct(left: &dyn Array, right: &dyn Array) -> Arc { + // Merging fields into a list> is tricky and can only succeed + // in two ways. First, if both lists have the same offsets. Second, if + // one of the lists is all-null + if left.null_count() == left.len() { + return merge_list_struct_null(left, right, right); + } else if right.null_count() == right.len() { + return merge_list_struct_null(left, right, left); + } + match (left.data_type(), right.data_type()) { + (DataType::List(left_field), DataType::List(_)) => { + if !lists_have_same_offsets_helper::(left, right) { + panic!("Attempt to merge list struct arrays which do not have same offsets"); + } + merge_list_structs_helper::( + left, + right, + left_field.name(), + left_field.is_nullable(), + ) + } + (DataType::LargeList(left_field), DataType::LargeList(_)) => { + if !lists_have_same_offsets_helper::(left, right) { + panic!("Attempt to merge list struct arrays which do not have same offsets"); + } + merge_list_structs_helper::( + left, + right, + left_field.name(), + left_field.is_nullable(), + ) + } + _ => unreachable!(), + } +} + +/// Helper function to merge validity buffers from two struct arrays. +/// +/// A row is valid if it is valid in either input. +/// An absent validity buffer means all rows are valid, an all-null buffer acts as the identity for this merge. +fn merge_struct_validity( + left_validity: Option<&arrow_buffer::NullBuffer>, + right_validity: Option<&arrow_buffer::NullBuffer>, +) -> Option { + match (left_validity, right_validity) { + // Fast paths: no computation needed + (None, _) | (_, None) => None, + (Some(left), Some(right)) => { + if left.null_count() == 0 || right.null_count() == 0 { + return None; + } + if left.null_count() == left.len() { + return Some(right.clone()); + } + if right.null_count() == right.len() { + return Some(left.clone()); + } + + let left_buffer = left.inner(); + let right_buffer = right.inner(); + + // Perform bitwise OR directly on BooleanBuffers + // This preserves the correct semantics: 1 = valid, 0 = null + let merged_buffer = left_buffer | right_buffer; + + Some(arrow_buffer::NullBuffer::from(merged_buffer)) + } + } +} + +fn merge_list_child_values( + child_field: &Field, + left_values: ArrayRef, + right_values: ArrayRef, +) -> ArrayRef { + match child_field.data_type() { + DataType::Struct(child_fields) => Arc::new(merge_with_schema( + left_values.as_struct(), + right_values.as_struct(), + child_fields, + )) as ArrayRef, + DataType::List(grandchild) => { + let left_list = left_values + .as_any() + .downcast_ref::() + .expect("left list values should be ListArray"); + let right_list = right_values + .as_any() + .downcast_ref::() + .expect("right list values should be ListArray"); + let merged_values = merge_list_child_values( + grandchild.as_ref(), + left_list.values().clone(), + right_list.values().clone(), + ); + let merged_validity = merge_struct_validity(left_list.nulls(), right_list.nulls()); + Arc::new(ListArray::new( + grandchild.clone(), + left_list.offsets().clone(), + merged_values, + merged_validity, + )) as ArrayRef + } + DataType::LargeList(grandchild) => { + let left_list = left_values + .as_any() + .downcast_ref::() + .expect("left list values should be LargeListArray"); + let right_list = right_values + .as_any() + .downcast_ref::() + .expect("right list values should be LargeListArray"); + let merged_values = merge_list_child_values( + grandchild.as_ref(), + left_list.values().clone(), + right_list.values().clone(), + ); + let merged_validity = merge_struct_validity(left_list.nulls(), right_list.nulls()); + Arc::new(LargeListArray::new( + grandchild.clone(), + left_list.offsets().clone(), + merged_values, + merged_validity, + )) as ArrayRef + } + DataType::FixedSizeList(grandchild, list_size) => { + let left_list = left_values + .as_any() + .downcast_ref::() + .expect("left list values should be FixedSizeListArray"); + let right_list = right_values + .as_any() + .downcast_ref::() + .expect("right list values should be FixedSizeListArray"); + let merged_values = merge_list_child_values( + grandchild.as_ref(), + left_list.values().clone(), + right_list.values().clone(), + ); + let merged_validity = merge_struct_validity(left_list.nulls(), right_list.nulls()); + Arc::new(FixedSizeListArray::new( + grandchild.clone(), + *list_size, + merged_values, + merged_validity, + )) as ArrayRef + } + _ => left_values.clone(), + } +} + +// Helper function to adjust child array validity based on parent struct validity +// When parent struct is null, propagates null to child array +// Optimized with fast paths and SIMD operations +fn adjust_child_validity( + child: &ArrayRef, + parent_validity: Option<&arrow_buffer::NullBuffer>, +) -> ArrayRef { + // Fast path: no parent validity means no adjustment needed + let parent_validity = match parent_validity { + None => return child.clone(), + Some(p) if p.null_count() == 0 => return child.clone(), // No nulls to propagate + Some(p) => p, + }; + + // Fast path: DataType::Null arrays are always entirely null by definition and cannot + // carry an explicit null bitmap (Arrow rejects it). No adjustment is needed. + if child.data_type() == &DataType::Null { + return child.clone(); + } + + let child_validity = child.nulls(); + + // Compute the new validity: child_validity AND parent_validity + let new_validity = match child_validity { + None => { + // Fast path: child has no existing validity, just use parent's + parent_validity.clone() + } + Some(child_nulls) => { + let child_buffer = child_nulls.inner(); + let parent_buffer = parent_validity.inner(); + + // Perform bitwise AND directly on BooleanBuffers + // This preserves the correct semantics: 1 = valid, 0 = null + let merged_buffer = child_buffer & parent_buffer; + + arrow_buffer::NullBuffer::from(merged_buffer) + } + }; + + // Create new array with adjusted validity + arrow_array::make_array( + arrow_data::ArrayData::try_new( + child.data_type().clone(), + child.len(), + Some(new_validity.into_inner().into_inner()), + child.offset(), + child.to_data().buffers().to_vec(), + child.to_data().child_data().to_vec(), + ) + .unwrap(), + ) +} + +fn merge(left_struct_array: &StructArray, right_struct_array: &StructArray) -> StructArray { + let mut fields: Vec = vec![]; + let mut columns: Vec = vec![]; + let right_fields = right_struct_array.fields(); + let right_columns = right_struct_array.columns(); + + // Get the validity buffers from both structs + let left_validity = left_struct_array.nulls(); + let right_validity = right_struct_array.nulls(); + + // Compute merged validity + let merged_validity = merge_struct_validity(left_validity, right_validity); + + // iterate through the fields on the left hand side + for (left_field, left_column) in left_struct_array + .fields() + .iter() + .zip(left_struct_array.columns().iter()) + { + match right_fields + .iter() + .position(|f| f.name() == left_field.name()) + { + // if the field exists on the right hand side, merge them recursively if appropriate + Some(right_index) => { + let right_field = right_fields.get(right_index).unwrap(); + let right_column = right_columns.get(right_index).unwrap(); + // if both fields are struct, merge them recursively + match (left_field.data_type(), right_field.data_type()) { + (DataType::Struct(_), DataType::Struct(_)) => { + let left_sub_array = left_column.as_struct(); + let right_sub_array = right_column.as_struct(); + let merged_sub_array = merge(left_sub_array, right_sub_array); + fields.push(Field::new( + left_field.name(), + merged_sub_array.data_type().clone(), + left_field.is_nullable(), + )); + columns.push(Arc::new(merged_sub_array) as ArrayRef); + } + (DataType::List(left_list), DataType::List(right_list)) + if left_list.data_type().is_struct() + && right_list.data_type().is_struct() => + { + // If there is nothing to merge just use the left field + if left_list.data_type() == right_list.data_type() { + fields.push(left_field.as_ref().clone()); + columns.push(left_column.clone()); + } + // If we have two List and they have different sets of fields then + // we can merge them if the offsets arrays are the same. Otherwise, we + // have to consider it an error. + let merged_sub_array = merge_list_struct(&left_column, &right_column); + + fields.push(Field::new( + left_field.name(), + merged_sub_array.data_type().clone(), + left_field.is_nullable(), + )); + columns.push(merged_sub_array); + } + // otherwise, just use the field on the left hand side + _ => { + // TODO handle list-of-struct and other types + fields.push(left_field.as_ref().clone()); + // Adjust the column validity: if left struct was null, propagate to child + let adjusted_column = adjust_child_validity(left_column, left_validity); + columns.push(adjusted_column); + } + } + } + None => { + fields.push(left_field.as_ref().clone()); + // Adjust the column validity: if left struct was null, propagate to child + let adjusted_column = adjust_child_validity(left_column, left_validity); + columns.push(adjusted_column); + } + } + } + + // now iterate through the fields on the right hand side + right_fields + .iter() + .zip(right_columns.iter()) + .for_each(|(field, column)| { + // add new columns on the right + if !left_struct_array + .fields() + .iter() + .any(|f| f.name() == field.name()) + { + fields.push(field.as_ref().clone()); + // This field doesn't exist on the left + // We use the right's column but need to adjust for struct validity + let adjusted_column = adjust_child_validity(column, right_validity); + columns.push(adjusted_column); + } + }); + + StructArray::try_new(Fields::from(fields), columns, merged_validity).unwrap() +} + +fn merge_with_schema( + left_struct_array: &StructArray, + right_struct_array: &StructArray, + fields: &Fields, +) -> StructArray { + // Helper function that returns true if both types are struct or both are non-struct + fn same_type_kind(left: &DataType, right: &DataType) -> bool { + match (left, right) { + (DataType::Struct(_), DataType::Struct(_)) => true, + (DataType::Struct(_), _) => false, + (_, DataType::Struct(_)) => false, + _ => true, + } + } + + let mut output_fields: Vec = Vec::with_capacity(fields.len()); + let mut columns: Vec = Vec::with_capacity(fields.len()); + + let left_fields = left_struct_array.fields(); + let left_columns = left_struct_array.columns(); + let right_fields = right_struct_array.fields(); + let right_columns = right_struct_array.columns(); + + // Get the validity buffers from both structs + let left_validity = left_struct_array.nulls(); + let right_validity = right_struct_array.nulls(); + + // Compute merged validity + let merged_validity = merge_struct_validity(left_validity, right_validity); + + for field in fields { + let left_match_idx = left_fields.iter().position(|f| { + f.name() == field.name() && same_type_kind(f.data_type(), field.data_type()) + }); + let right_match_idx = right_fields.iter().position(|f| { + f.name() == field.name() && same_type_kind(f.data_type(), field.data_type()) + }); + + match (left_match_idx, right_match_idx) { + (None, Some(right_idx)) => { + output_fields.push(right_fields[right_idx].as_ref().clone()); + // Adjust validity if the right struct was null + let adjusted_column = + adjust_child_validity(&right_columns[right_idx], right_validity); + columns.push(adjusted_column); + } + (Some(left_idx), None) => { + output_fields.push(left_fields[left_idx].as_ref().clone()); + // Adjust validity if the left struct was null + let adjusted_column = adjust_child_validity(&left_columns[left_idx], left_validity); + columns.push(adjusted_column); + } + (Some(left_idx), Some(right_idx)) => { + match field.data_type() { + DataType::Struct(child_fields) => { + let left_sub_array = left_columns[left_idx].as_struct(); + let right_sub_array = right_columns[right_idx].as_struct(); + let merged_sub_array = + merge_with_schema(left_sub_array, right_sub_array, child_fields); + output_fields.push(Field::new( + field.name(), + merged_sub_array.data_type().clone(), + field.is_nullable(), + )); + columns.push(Arc::new(merged_sub_array) as ArrayRef); + } + DataType::List(child_field) => { + let left_list = left_columns[left_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let right_list = right_columns[right_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let merged_values = merge_list_child_values( + child_field.as_ref(), + left_list.trimmed_values(), + right_list.trimmed_values(), + ); + let merged_validity = + merge_struct_validity(left_list.nulls(), right_list.nulls()); + // `trimmed_values` starts at the first used value, so offsets + // must be shifted to match or `ListArray::new` panics when the + // input list was sliced (e.g. from a filtered batch). + let merged_list = ListArray::new( + child_field.clone(), + left_list.trimmed_offsets(), + merged_values, + merged_validity, + ); + output_fields.push(field.as_ref().clone()); + columns.push(Arc::new(merged_list) as ArrayRef); + } + DataType::LargeList(child_field) => { + let left_list = left_columns[left_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let right_list = right_columns[right_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let merged_values = merge_list_child_values( + child_field.as_ref(), + left_list.trimmed_values(), + right_list.trimmed_values(), + ); + let merged_validity = + merge_struct_validity(left_list.nulls(), right_list.nulls()); + let merged_list = LargeListArray::new( + child_field.clone(), + left_list.trimmed_offsets(), + merged_values, + merged_validity, + ); + output_fields.push(field.as_ref().clone()); + columns.push(Arc::new(merged_list) as ArrayRef); + } + DataType::FixedSizeList(child_field, list_size) => { + let left_list = left_columns[left_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let right_list = right_columns[right_idx] + .as_any() + .downcast_ref::() + .unwrap(); + let merged_values = merge_list_child_values( + child_field.as_ref(), + left_list.values().clone(), + right_list.values().clone(), + ); + let merged_validity = + merge_struct_validity(left_list.nulls(), right_list.nulls()); + let merged_list = FixedSizeListArray::new( + child_field.clone(), + *list_size, + merged_values, + merged_validity, + ); + output_fields.push(field.as_ref().clone()); + columns.push(Arc::new(merged_list) as ArrayRef); + } + _ => { + output_fields.push(left_fields[left_idx].as_ref().clone()); + // For fields that exist in both, use left but adjust validity + let adjusted_column = + adjust_child_validity(&left_columns[left_idx], left_validity); + columns.push(adjusted_column); + } + } + } + (None, None) => { + // The field will not be included in the output + } + } + } + + StructArray::try_new(Fields::from(output_fields), columns, merged_validity).unwrap() +} + +fn get_sub_array<'a>(array: &'a ArrayRef, components: &[&str]) -> Option<&'a ArrayRef> { + if components.is_empty() { + return Some(array); + } + if !matches!(array.data_type(), DataType::Struct(_)) { + return None; + } + let struct_arr = array.as_struct(); + struct_arr + .column_by_name(components[0]) + .and_then(|arr| get_sub_array(arr, &components[1..])) +} + +/// Interleave multiple RecordBatches into a single RecordBatch. +/// +/// Behaves like [`arrow_select::interleave::interleave`], but for RecordBatches. +pub fn interleave_batches( + batches: &[RecordBatch], + indices: &[(usize, usize)], +) -> Result { + let first_batch = batches.first().ok_or_else(|| { + ArrowError::InvalidArgumentError("Cannot interleave zero RecordBatches".to_string()) + })?; + let schema = first_batch.schema(); + let num_columns = first_batch.num_columns(); + let mut columns = Vec::with_capacity(num_columns); + let mut chunks = Vec::with_capacity(batches.len()); + + for i in 0..num_columns { + for batch in batches { + chunks.push(batch.column(i).as_ref()); + } + let new_column = interleave(&chunks, indices)?; + columns.push(new_column); + chunks.clear(); + } + + RecordBatch::try_new(schema, columns) +} + +pub trait BufferExt { + /// Create an `arrow_buffer::Buffer`` from a `bytes::Bytes` object + /// + /// The alignment must be specified (as `bytes_per_value`) since we want to make + /// sure we can safely reinterpret the buffer. + /// + /// If the buffer is properly aligned this will be zero-copy. If not, a copy + /// will be made and an owned buffer returned. + /// + /// If `bytes_per_value` is not a power of two, then we assume the buffer is + /// never going to be reinterpreted into another type and we can safely + /// ignore the alignment. + /// + /// Yes, the method name is odd. It's because there is already a `from_bytes` + /// which converts from `arrow_buffer::bytes::Bytes` (not `bytes::Bytes`) + fn from_bytes_bytes(bytes: bytes::Bytes, bytes_per_value: u64) -> Self; + + /// Allocates a new properly aligned arrow buffer and copies `bytes` into it + /// + /// `size_bytes` can be larger than `bytes` and, if so, the trailing bytes will + /// be zeroed out. + /// + /// # Panics + /// + /// Panics if `size_bytes` is less than `bytes.len()` + fn copy_bytes_bytes(bytes: bytes::Bytes, size_bytes: usize) -> Self; +} + +fn is_pwr_two(n: u64) -> bool { + n & (n - 1) == 0 +} + +impl BufferExt for arrow_buffer::Buffer { + fn from_bytes_bytes(bytes: bytes::Bytes, bytes_per_value: u64) -> Self { + if is_pwr_two(bytes_per_value) && bytes.as_ptr().align_offset(bytes_per_value as usize) != 0 + { + // The original buffer is not aligned, cannot zero-copy + let size_bytes = bytes.len(); + Self::copy_bytes_bytes(bytes, size_bytes) + } else { + // The original buffer is aligned, can zero-copy + // SAFETY: the alignment is correct we can make this conversion + unsafe { + Self::from_custom_allocation( + NonNull::new(bytes.as_ptr() as _).expect("should be a valid pointer"), + bytes.len(), + Arc::new(bytes), + ) + } + } + } + + fn copy_bytes_bytes(bytes: bytes::Bytes, size_bytes: usize) -> Self { + assert!(size_bytes >= bytes.len()); + let mut buf = MutableBuffer::with_capacity(size_bytes); + let to_fill = size_bytes - bytes.len(); + buf.extend(bytes); + buf.extend(std::iter::repeat_n(0_u8, to_fill)); + + // FIX for issue #4512: Shrink buffer to actual size before converting to immutable + // This reduces memory overhead from capacity over-allocation + buf.shrink_to_fit(); + + Self::from(buf) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow_array::{Float32Array, Int32Array, NullArray, StructArray}; + use arrow_array::{ListArray, StringArray, new_empty_array, new_null_array}; + use arrow_buffer::OffsetBuffer; + + #[test] + fn test_convert_to_floating_point_preserves_inner_nulls() { + // A FixedSizeList with a null inner element must convert to a + // FixedSizeList with the null kept in place. Dropping it would + // shorten the values array and shift every later element (and, when the + // remaining count is not a multiple of the list size, panic). + let values = Int8Array::from(vec![Some(1), None, Some(3), Some(4)]); + let fsl = FixedSizeListArray::new( + Arc::new(Field::new("item", DataType::Int8, true)), + 2, + Arc::new(values), + None, + ); + + let converted = fsl.convert_to_floating_point().unwrap(); + + assert_eq!(converted.len(), 2); + let conv_values = converted + .values() + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(conv_values.len(), 4); + assert_eq!(conv_values.value(0), 1.0); + assert!(conv_values.is_null(1)); + assert_eq!(conv_values.value(2), 3.0); + assert_eq!(conv_values.value(3), 4.0); + } + + #[test] + fn test_convert_to_floating_point_preserves_inner_nulls_f64_arm() { + // The Float64-producing arms (Int64/UInt8/UInt32) share the same fix as the + // Float32 arms; cover one representative (UInt8 -> Float64) so both branch + // families are exercised. + let values = UInt8Array::from(vec![Some(10u8), None, Some(30), Some(40)]); + let fsl = FixedSizeListArray::new( + Arc::new(Field::new("item", DataType::UInt8, true)), + 2, + Arc::new(values), + None, + ); + + let converted = fsl.convert_to_floating_point().unwrap(); + + assert_eq!(converted.len(), 2); + let conv_values = converted + .values() + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(conv_values.len(), 4); + assert_eq!(conv_values.value(0), 10.0); + assert!(conv_values.is_null(1)); + assert_eq!(conv_values.value(2), 30.0); + assert_eq!(conv_values.value(3), 40.0); + } + + #[test] + fn test_merge_recursive() { + let a_array = Int32Array::from(vec![Some(1), Some(2), Some(3)]); + let e_array = Int32Array::from(vec![Some(4), Some(5), Some(6)]); + let c_array = Int32Array::from(vec![Some(7), Some(8), Some(9)]); + let d_array = StringArray::from(vec![Some("a"), Some("b"), Some("c")]); + + let left_schema = Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new( + "b", + DataType::Struct(vec![Field::new("c", DataType::Int32, true)].into()), + true, + ), + ]); + let left_batch = RecordBatch::try_new( + Arc::new(left_schema), + vec![ + Arc::new(a_array.clone()), + Arc::new(StructArray::from(vec![( + Arc::new(Field::new("c", DataType::Int32, true)), + Arc::new(c_array.clone()) as ArrayRef, + )])), + ], + ) + .unwrap(); + + let right_schema = Schema::new(vec![ + Field::new("e", DataType::Int32, true), + Field::new( + "b", + DataType::Struct(vec![Field::new("d", DataType::Utf8, true)].into()), + true, + ), + ]); + let right_batch = RecordBatch::try_new( + Arc::new(right_schema), + vec![ + Arc::new(e_array.clone()), + Arc::new(StructArray::from(vec![( + Arc::new(Field::new("d", DataType::Utf8, true)), + Arc::new(d_array.clone()) as ArrayRef, + )])) as ArrayRef, + ], + ) + .unwrap(); + + let merged_schema = Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new( + "b", + DataType::Struct( + vec![ + Field::new("c", DataType::Int32, true), + Field::new("d", DataType::Utf8, true), + ] + .into(), + ), + true, + ), + Field::new("e", DataType::Int32, true), + ]); + let merged_batch = RecordBatch::try_new( + Arc::new(merged_schema), + vec![ + Arc::new(a_array) as ArrayRef, + Arc::new(StructArray::from(vec![ + ( + Arc::new(Field::new("c", DataType::Int32, true)), + Arc::new(c_array) as ArrayRef, + ), + ( + Arc::new(Field::new("d", DataType::Utf8, true)), + Arc::new(d_array) as ArrayRef, + ), + ])) as ArrayRef, + Arc::new(e_array) as ArrayRef, + ], + ) + .unwrap(); + + let result = left_batch.merge(&right_batch).unwrap(); + assert_eq!(result, merged_batch); + } + + #[test] + fn test_merge_with_schema() { + fn test_batch(names: &[&str], types: &[DataType]) -> (Schema, RecordBatch) { + let fields: Fields = names + .iter() + .zip(types) + .map(|(name, ty)| Field::new(name.to_string(), ty.clone(), false)) + .collect(); + let schema = Schema::new(vec![Field::new( + "struct", + DataType::Struct(fields.clone()), + false, + )]); + let children = types.iter().map(new_empty_array).collect::>(); + let batch = RecordBatch::try_new( + Arc::new(schema.clone()), + vec![Arc::new(StructArray::new(fields, children, None)) as ArrayRef], + ); + (schema, batch.unwrap()) + } + + let (_, left_batch) = test_batch(&["a", "b"], &[DataType::Int32, DataType::Int64]); + let (_, right_batch) = test_batch(&["c", "b"], &[DataType::Int32, DataType::Int64]); + let (output_schema, _) = test_batch( + &["b", "a", "c"], + &[DataType::Int64, DataType::Int32, DataType::Int32], + ); + + // If we use merge_with_schema the schema is respected + let merged = left_batch + .merge_with_schema(&right_batch, &output_schema) + .unwrap(); + assert_eq!(merged.schema().as_ref(), &output_schema); + + // If we use merge we get first-come first-serve based on the left batch + let (naive_schema, _) = test_batch( + &["a", "b", "c"], + &[DataType::Int32, DataType::Int64, DataType::Int32], + ); + let merged = left_batch.merge(&right_batch).unwrap(); + assert_eq!(merged.schema().as_ref(), &naive_schema); + } + + #[test] + fn test_merge_list_struct() { + let x_field = Arc::new(Field::new("x", DataType::Int32, true)); + let y_field = Arc::new(Field::new("y", DataType::Int32, true)); + let x_struct_field = Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![x_field.clone()])), + true, + )); + let y_struct_field = Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![y_field.clone()])), + true, + )); + let both_struct_field = Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![x_field.clone(), y_field.clone()])), + true, + )); + let left_schema = Schema::new(vec![Field::new( + "list_struct", + DataType::List(x_struct_field.clone()), + true, + )]); + let right_schema = Schema::new(vec![Field::new( + "list_struct", + DataType::List(y_struct_field.clone()), + true, + )]); + let both_schema = Schema::new(vec![Field::new( + "list_struct", + DataType::List(both_struct_field.clone()), + true, + )]); + + let x = Arc::new(Int32Array::from(vec![1])); + let y = Arc::new(Int32Array::from(vec![2])); + let x_struct = Arc::new(StructArray::new( + Fields::from(vec![x_field.clone()]), + vec![x.clone()], + None, + )); + let y_struct = Arc::new(StructArray::new( + Fields::from(vec![y_field.clone()]), + vec![y.clone()], + None, + )); + let both_struct = Arc::new(StructArray::new( + Fields::from(vec![x_field.clone(), y_field.clone()]), + vec![x.clone(), y], + None, + )); + let both_null_struct = Arc::new(StructArray::new( + Fields::from(vec![x_field, y_field]), + vec![x, Arc::new(new_null_array(&DataType::Int32, 1))], + None, + )); + let offsets = OffsetBuffer::from_lengths([1]); + let x_s_list = ListArray::new(x_struct_field, offsets.clone(), x_struct, None); + let y_s_list = ListArray::new(y_struct_field, offsets.clone(), y_struct, None); + let both_list = ListArray::new( + both_struct_field.clone(), + offsets.clone(), + both_struct, + None, + ); + let both_null_list = ListArray::new(both_struct_field, offsets, both_null_struct, None); + let x_batch = + RecordBatch::try_new(Arc::new(left_schema), vec![Arc::new(x_s_list)]).unwrap(); + let y_batch = RecordBatch::try_new( + Arc::new(right_schema.clone()), + vec![Arc::new(y_s_list.clone())], + ) + .unwrap(); + let merged = x_batch.merge(&y_batch).unwrap(); + let expected = + RecordBatch::try_new(Arc::new(both_schema.clone()), vec![Arc::new(both_list)]).unwrap(); + assert_eq!(merged, expected); + + let y_null_list = new_null_array(y_s_list.data_type(), 1); + let y_null_batch = + RecordBatch::try_new(Arc::new(right_schema), vec![Arc::new(y_null_list.clone())]) + .unwrap(); + let expected = + RecordBatch::try_new(Arc::new(both_schema), vec![Arc::new(both_null_list)]).unwrap(); + let merged = x_batch.merge(&y_null_batch).unwrap(); + assert_eq!(merged, expected); + } + + #[test] + fn test_byte_width_opt() { + assert_eq!(DataType::Int32.byte_width_opt(), Some(4)); + assert_eq!(DataType::Int64.byte_width_opt(), Some(8)); + assert_eq!(DataType::Float32.byte_width_opt(), Some(4)); + assert_eq!(DataType::Float64.byte_width_opt(), Some(8)); + assert_eq!(DataType::Utf8.byte_width_opt(), None); + assert_eq!(DataType::Binary.byte_width_opt(), None); + assert_eq!( + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))).byte_width_opt(), + None + ); + assert_eq!( + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Int32, true)), 3) + .byte_width_opt(), + Some(12) + ); + assert_eq!( + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Int32, true)), 4) + .byte_width_opt(), + Some(16) + ); + assert_eq!( + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Utf8, true)), 5) + .byte_width_opt(), + None + ); + } + + #[test] + fn test_take_record_batch() { + let schema = Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Utf8, true), + ])); + let batch = RecordBatch::try_new( + schema.clone(), + vec![ + Arc::new(Int32Array::from_iter_values(0..20)), + Arc::new(StringArray::from_iter_values( + (0..20).map(|i| format!("str-{}", i)), + )), + ], + ) + .unwrap(); + let taken = batch.take(&(vec![1_u32, 5_u32, 10_u32].into())).unwrap(); + assert_eq!( + taken, + RecordBatch::try_new( + schema, + vec![ + Arc::new(Int32Array::from(vec![1, 5, 10])), + Arc::new(StringArray::from(vec!["str-1", "str-5", "str-10"])), + ], + ) + .unwrap() + ) + } + + #[test] + fn test_schema_project_by_schema() { + let metadata = [("key".to_string(), "value".to_string())]; + let schema = Arc::new( + Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Utf8, true), + ]) + .with_metadata(metadata.clone().into()), + ); + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(Int32Array::from_iter_values(0..20)), + Arc::new(StringArray::from_iter_values( + (0..20).map(|i| format!("str-{}", i)), + )), + ], + ) + .unwrap(); + + // Empty schema + let empty_schema = Schema::empty(); + let empty_projected = batch.project_by_schema(&empty_schema).unwrap(); + let expected_schema = empty_schema.with_metadata(metadata.clone().into()); + assert_eq!( + empty_projected, + RecordBatch::from(StructArray::new_empty_fields(batch.num_rows(), None)) + .with_schema(Arc::new(expected_schema)) + .unwrap() + ); + + // Re-ordered schema + let reordered_schema = Schema::new(vec![ + Field::new("b", DataType::Utf8, true), + Field::new("a", DataType::Int32, true), + ]); + let reordered_projected = batch.project_by_schema(&reordered_schema).unwrap(); + let expected_schema = Arc::new(reordered_schema.with_metadata(metadata.clone().into())); + assert_eq!( + reordered_projected, + RecordBatch::try_new( + expected_schema, + vec![ + Arc::new(StringArray::from_iter_values( + (0..20).map(|i| format!("str-{}", i)), + )), + Arc::new(Int32Array::from_iter_values(0..20)), + ], + ) + .unwrap() + ); + + // Sub schema + let sub_schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]); + let sub_projected = batch.project_by_schema(&sub_schema).unwrap(); + let expected_schema = Arc::new(sub_schema.with_metadata(metadata.into())); + assert_eq!( + sub_projected, + RecordBatch::try_new( + expected_schema, + vec![Arc::new(Int32Array::from_iter_values(0..20))], + ) + .unwrap() + ); + } + + #[test] + fn test_project_preserves_struct_validity() { + // Test that projecting a struct array preserves its validity (fix for issue #4385) + let fields = Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new("value", DataType::Float32, true), + ]); + + // Create a struct array with validity + let id_array = Int32Array::from(vec![1, 2, 3]); + let value_array = Float32Array::from(vec![Some(1.0), Some(2.0), Some(3.0)]); + let struct_array = StructArray::new( + fields.clone(), + vec![ + Arc::new(id_array) as ArrayRef, + Arc::new(value_array) as ArrayRef, + ], + Some(vec![true, false, true].into()), // Second struct is null + ); + + // Project the struct array + let projected = project(&struct_array, &fields).unwrap(); + + // Verify the validity is preserved + assert_eq!(projected.null_count(), 1); + assert!(!projected.is_null(0)); + assert!(projected.is_null(1)); + assert!(!projected.is_null(2)); + } + + #[test] + fn test_merge_struct_with_different_validity() { + // Test case from Weston's review comment + // File 1 has height field with some nulls + let height_array = Int32Array::from(vec![Some(500), None, Some(600), None]); + let left_fields = Fields::from(vec![Field::new("height", DataType::Int32, true)]); + let left_struct = StructArray::new( + left_fields, + vec![Arc::new(height_array) as ArrayRef], + Some(vec![true, false, true, false].into()), // Rows 2 and 4 are null structs + ); + + // File 2 has width field with some nulls + let width_array = Int32Array::from(vec![Some(300), Some(200), None, None]); + let right_fields = Fields::from(vec![Field::new("width", DataType::Int32, true)]); + let right_struct = StructArray::new( + right_fields, + vec![Arc::new(width_array) as ArrayRef], + Some(vec![true, true, false, false].into()), // Rows 3 and 4 are null structs + ); + + // Merge the two structs + let merged = merge(&left_struct, &right_struct); + + // Expected: + // Row 1: both non-null -> {width: 300, height: 500} + // Row 2: left null, right non-null -> {width: 200, height: null} + // Row 3: left non-null, right null -> {width: null, height: 600} + // Row 4: both null -> null struct + + assert_eq!(merged.null_count(), 1); // Only row 4 is null + assert!(!merged.is_null(0)); + assert!(!merged.is_null(1)); + assert!(!merged.is_null(2)); + assert!(merged.is_null(3)); + + // Check field values + let height_col = merged.column_by_name("height").unwrap(); + let height_values = height_col.as_any().downcast_ref::().unwrap(); + assert_eq!(height_values.value(0), 500); + assert!(height_values.is_null(1)); // height is null when left struct was null + assert_eq!(height_values.value(2), 600); + + let width_col = merged.column_by_name("width").unwrap(); + let width_values = width_col.as_any().downcast_ref::().unwrap(); + assert_eq!(width_values.value(0), 300); + assert_eq!(width_values.value(1), 200); + assert!(width_values.is_null(2)); // width is null when right struct was null + + // An all-null validity buffer is data, not a placeholder meaning "this side has no + // validity": merging two of them keeps the rows null. + let all_null_left = StructArray::new( + Fields::from(vec![Field::new("height", DataType::Int32, true)]), + vec![Arc::new(Int32Array::from(vec![None, None])) as ArrayRef], + Some(vec![false, false].into()), + ); + let all_null_right = StructArray::new( + Fields::from(vec![Field::new("width", DataType::Int32, true)]), + vec![Arc::new(Int32Array::from(vec![None, None])) as ArrayRef], + Some(vec![false, false].into()), + ); + + let merged = merge(&all_null_left, &all_null_right); + assert_eq!(merged.null_count(), 2); + + // An all-null side is the identity of the merge, so the other side decides each row. + let partial_left = StructArray::new( + Fields::from(vec![Field::new("height", DataType::Int32, true)]), + vec![Arc::new(Int32Array::from(vec![Some(1), None])) as ArrayRef], + Some(vec![true, false].into()), + ); + let merged = merge(&partial_left, &all_null_right); + assert!(!merged.is_null(0)); + assert!(merged.is_null(1)); + + // A missing validity buffer means all rows are valid, which absorbs an all-null side. + let all_valid_left = StructArray::new( + Fields::from(vec![Field::new("height", DataType::Int32, true)]), + vec![Arc::new(Int32Array::from(vec![1, 2])) as ArrayRef], + None, + ); + let merged = merge(&all_valid_left, &all_null_right); + assert_eq!(merged.null_count(), 0); + + // An explicit all-valid buffer has the same semantics as a missing buffer. + let all_valid: arrow_buffer::NullBuffer = vec![true, true].into(); + let partial: arrow_buffer::NullBuffer = vec![true, false].into(); + assert!(merge_struct_validity(Some(&all_valid), Some(&partial)).is_none()); + assert!(merge_struct_validity(Some(&partial), Some(&all_valid)).is_none()); + } + + #[test] + fn test_merge_null_typed_column_with_parent_validity() { + // Reproduces ENT-990: panic in adjust_child_validity when a Null-typed column + // exists on one side and the parent struct has null rows. + // Arrow's Null type has no null bitmap, so passing one to ArrayData::try_new panics. + let left_struct = StructArray::new( + Fields::from(vec![Field::new("a", DataType::Int32, true)]), + vec![Arc::new(Int32Array::from(vec![Some(1), None])) as ArrayRef], + Some(vec![true, false].into()), + ); + let right_struct = StructArray::new( + Fields::from(vec![Field::new("b", DataType::Null, true)]), + vec![Arc::new(NullArray::new(2)) as ArrayRef], + Some(vec![true, false].into()), + ); + + // Previously panicked: "Arrays of type Null cannot contain a null bitmask" + let merged = merge(&left_struct, &right_struct); + assert_eq!(merged.len(), 2); + let b_col = merged.column_by_name("b").unwrap(); + // DataType::Null implies all-null by definition; no null bitmap is stored. + assert_eq!(b_col.data_type(), &DataType::Null); + assert_eq!(b_col.len(), 2); + } + + #[test] + fn test_merge_with_schema_with_nullable_struct_list_schema_mismatch() { + // left_list setup + let left_company_id = Arc::new(Int32Array::from(vec![None, None])); + let left_count = Arc::new(Int32Array::from(vec![None, None])); + let left_struct = Arc::new(StructArray::new( + Fields::from(vec![ + Field::new("company_id", DataType::Int32, true), + Field::new("count", DataType::Int32, true), + ]), + vec![left_company_id, left_count], + None, + )); + let left_list = Arc::new(ListArray::new( + Arc::new(Field::new( + "item", + DataType::Struct(left_struct.fields().clone()), + true, + )), + OffsetBuffer::from_lengths([2]), + left_struct, + None, + )); + + // Right List Setup + let right_company_name = Arc::new(StringArray::from(vec!["Google", "Microsoft"])); + let right_struct = Arc::new(StructArray::new( + Fields::from(vec![Field::new("company_name", DataType::Utf8, true)]), + vec![right_company_name], + None, + )); + let right_list = Arc::new(ListArray::new( + Arc::new(Field::new( + "item", + DataType::Struct(right_struct.fields().clone()), + true, + )), + OffsetBuffer::from_lengths([2]), + right_struct, + None, + )); + + let target_fields = Fields::from(vec![Field::new( + "companies", + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("company_id", DataType::Int32, true), + Field::new("company_name", DataType::Utf8, true), + Field::new("count", DataType::Int32, true), + ])), + true, + ))), + true, + )]); + + let left_batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new( + "companies", + left_list.data_type().clone(), + true, + )])), + vec![left_list as ArrayRef], + ) + .unwrap(); + + let right_batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new( + "companies", + right_list.data_type().clone(), + true, + )])), + vec![right_list as ArrayRef], + ) + .unwrap(); + + let merged = left_batch + .merge_with_schema(&right_batch, &Schema::new(target_fields.to_vec())) + .unwrap(); + + // Verify the merged structure + let merged_list = merged + .column_by_name("companies") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + let merged_struct = merged_list.values().as_struct(); + + // Should have all 3 fields + assert_eq!(merged_struct.num_columns(), 3); + assert!(merged_struct.column_by_name("company_id").is_some()); + assert!(merged_struct.column_by_name("company_name").is_some()); + assert!(merged_struct.column_by_name("count").is_some()); + + // Verify values + let company_id = merged_struct + .column_by_name("company_id") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + assert!(company_id.is_null(0)); + assert!(company_id.is_null(1)); + + let company_name = merged_struct + .column_by_name("company_name") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(company_name.value(0), "Google"); + assert_eq!(company_name.value(1), "Microsoft"); + + let count = merged_struct + .column_by_name("count") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + assert!(count.is_null(0)); + assert!(count.is_null(1)); + } + + #[test] + fn test_merge_struct_lists() { + test_merge_struct_lists_generic::(); + } + + #[test] + fn test_merge_struct_large_lists() { + test_merge_struct_lists_generic::(); + } + + fn test_merge_struct_lists_generic() { + // left_list setup + let left_company_id = Arc::new(Int32Array::from(vec![ + Some(1), + Some(2), + Some(3), + Some(4), + Some(5), + Some(6), + Some(7), + Some(8), + Some(9), + Some(10), + Some(11), + Some(12), + Some(13), + Some(14), + Some(15), + Some(16), + Some(17), + Some(18), + Some(19), + Some(20), + ])); + let left_count = Arc::new(Int32Array::from(vec![ + Some(10), + Some(20), + Some(30), + Some(40), + Some(50), + Some(60), + Some(70), + Some(80), + Some(90), + Some(100), + Some(110), + Some(120), + Some(130), + Some(140), + Some(150), + Some(160), + Some(170), + Some(180), + Some(190), + Some(200), + ])); + let left_struct = Arc::new(StructArray::new( + Fields::from(vec![ + Field::new("company_id", DataType::Int32, true), + Field::new("count", DataType::Int32, true), + ]), + vec![left_company_id, left_count], + None, + )); + + let left_list = Arc::new(GenericListArray::::new( + Arc::new(Field::new( + "item", + DataType::Struct(left_struct.fields().clone()), + true, + )), + OffsetBuffer::from_lengths([3, 1]), + left_struct.clone(), + None, + )); + + let left_list_struct = Arc::new(StructArray::new( + Fields::from(vec![Field::new( + "companies", + if O::IS_LARGE { + DataType::LargeList(Arc::new(Field::new( + "item", + DataType::Struct(left_struct.fields().clone()), + true, + ))) + } else { + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(left_struct.fields().clone()), + true, + ))) + }, + true, + )]), + vec![left_list as ArrayRef], + None, + )); + + // right_list setup + let right_company_name = Arc::new(StringArray::from(vec![ + "Google", + "Microsoft", + "Apple", + "Facebook", + ])); + let right_struct = Arc::new(StructArray::new( + Fields::from(vec![Field::new("company_name", DataType::Utf8, true)]), + vec![right_company_name], + None, + )); + let right_list = Arc::new(GenericListArray::::new( + Arc::new(Field::new( + "item", + DataType::Struct(right_struct.fields().clone()), + true, + )), + OffsetBuffer::from_lengths([3, 1]), + right_struct.clone(), + None, + )); + + let right_list_struct = Arc::new(StructArray::new( + Fields::from(vec![Field::new( + "companies", + if O::IS_LARGE { + DataType::LargeList(Arc::new(Field::new( + "item", + DataType::Struct(right_struct.fields().clone()), + true, + ))) + } else { + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(right_struct.fields().clone()), + true, + ))) + }, + true, + )]), + vec![right_list as ArrayRef], + None, + )); + + // prepare schema + let target_fields = Fields::from(vec![Field::new( + "companies", + if O::IS_LARGE { + DataType::LargeList(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("company_id", DataType::Int32, true), + Field::new("company_name", DataType::Utf8, true), + Field::new("count", DataType::Int32, true), + ])), + true, + ))) + } else { + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("company_id", DataType::Int32, true), + Field::new("company_name", DataType::Utf8, true), + Field::new("count", DataType::Int32, true), + ])), + true, + ))) + }, + true, + )]); + + // merge left_list and right_list + let merged_array = merge_with_schema(&left_list_struct, &right_list_struct, &target_fields); + assert_eq!(merged_array.len(), 2); + } + + #[test] + fn test_merge_with_schema_sliced_list_struct() { + test_merge_with_schema_sliced_list_struct_generic::(); + } + + #[test] + fn test_merge_with_schema_sliced_large_list_struct() { + test_merge_with_schema_sliced_list_struct_generic::(); + } + + // Regression for #6580: merge_with_schema panicked when the left list was a + // sliced view whose offsets did not start at zero (common after a filtered + // scan). Cloning those offsets alongside `trimmed_values` produced offsets + // larger than the trimmed child, panicking in `(Large)ListArray::new`. + fn test_merge_with_schema_sliced_list_struct_generic() { + let make_list_dtype = |item_field: Arc| { + if O::IS_LARGE { + DataType::LargeList(item_field) + } else { + DataType::List(item_field) + } + }; + + // Build a List with two rows of 5 items each, then slice away + // the first row so the remaining list's offsets start at 5, not 0. + let struct_fields_a = Fields::from(vec![Field::new("a", DataType::Int32, true)]); + let left_values = Arc::new(StructArray::new( + struct_fields_a.clone(), + vec![Arc::new(Int32Array::from_iter_values(0..10)) as ArrayRef], + None, + )); + let full_list = GenericListArray::::new( + Arc::new(Field::new("item", DataType::Struct(struct_fields_a), true)), + OffsetBuffer::::from_lengths([5, 5]), + left_values, + None, + ); + let sliced_left = full_list.slice(1, 1); + assert_eq!(sliced_left.offsets()[0].as_usize(), 5); + assert_eq!(sliced_left.offsets()[1].as_usize(), 10); + + let struct_fields_b = Fields::from(vec![Field::new("b", DataType::Int32, true)]); + let right_values = Arc::new(StructArray::new( + struct_fields_b.clone(), + vec![Arc::new(Int32Array::from_iter_values(100..105)) as ArrayRef], + None, + )); + let right_list = GenericListArray::::new( + Arc::new(Field::new("item", DataType::Struct(struct_fields_b), true)), + OffsetBuffer::::from_lengths([5]), + right_values, + None, + ); + + let target_item_field = Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Int32, true), + ])), + true, + )); + let target_fields = Fields::from(vec![Field::new( + "items", + make_list_dtype(target_item_field), + true, + )]); + + let left_batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new( + "items", + sliced_left.data_type().clone(), + true, + )])), + vec![Arc::new(sliced_left) as ArrayRef], + ) + .unwrap(); + let right_batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new( + "items", + right_list.data_type().clone(), + true, + )])), + vec![Arc::new(right_list) as ArrayRef], + ) + .unwrap(); + + let merged = left_batch + .merge_with_schema(&right_batch, &Schema::new(target_fields.to_vec())) + .unwrap(); + + let merged_list = merged + .column_by_name("items") + .unwrap() + .as_any() + .downcast_ref::>() + .unwrap(); + assert_eq!(merged_list.len(), 1); + assert_eq!(merged_list.value_length(0).as_usize(), 5); + let merged_struct = merged_list.values().as_struct(); + assert_eq!(merged_struct.num_columns(), 2); + let a = merged_struct + .column_by_name("a") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + // After shifting offsets to zero, values 5..10 should be first. + let a_vals: Vec = a.iter().map(|v| v.unwrap()).collect(); + assert_eq!(a_vals, vec![5, 6, 7, 8, 9]); + } + + #[test] + fn test_project_by_schema_list_struct_reorder() { + // Test that project_by_schema correctly reorders fields inside List + // This is a regression test for issue #5702 + + // Source schema with inner struct fields in order: c, b, a + let source_inner_struct = DataType::Struct(Fields::from(vec![ + Field::new("c", DataType::Utf8, true), + Field::new("b", DataType::Utf8, true), + Field::new("a", DataType::Utf8, true), + ])); + let source_schema = Arc::new(Schema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "data", + DataType::List(Arc::new(Field::new( + "item", + source_inner_struct.clone(), + true, + ))), + true, + ), + ])); + + // Create source data with c, b, a order + let c_array = StringArray::from(vec!["c1", "c2"]); + let b_array = StringArray::from(vec!["b1", "b2"]); + let a_array = StringArray::from(vec!["a1", "a2"]); + let inner_struct = StructArray::from(vec![ + ( + Arc::new(Field::new("c", DataType::Utf8, true)), + Arc::new(c_array) as ArrayRef, + ), + ( + Arc::new(Field::new("b", DataType::Utf8, true)), + Arc::new(b_array) as ArrayRef, + ), + ( + Arc::new(Field::new("a", DataType::Utf8, true)), + Arc::new(a_array) as ArrayRef, + ), + ]); + + let list_array = ListArray::new( + Arc::new(Field::new("item", source_inner_struct, true)), + OffsetBuffer::from_lengths([1, 1]), + Arc::new(inner_struct), + None, + ); + + let batch = RecordBatch::try_new( + source_schema, + vec![Arc::new(Int32Array::from(vec![1, 2])), Arc::new(list_array)], + ) + .unwrap(); + + // Target schema with inner struct fields in order: a, b, c + let target_inner_struct = DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::Utf8, true), + Field::new("b", DataType::Utf8, true), + Field::new("c", DataType::Utf8, true), + ])); + let target_schema = Schema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "data", + DataType::List(Arc::new(Field::new("item", target_inner_struct, true))), + true, + ), + ]); + + // Project should reorder the inner struct fields + let projected = batch.project_by_schema(&target_schema).unwrap(); + + // Verify the schema is correct + assert_eq!(projected.schema().as_ref(), &target_schema); + + // Verify the data is correct by checking inner struct field order + let projected_list = projected.column(1).as_list::(); + let projected_struct = projected_list.values().as_struct(); + + // Fields should now be in order: a, b, c + assert_eq!( + projected_struct.column_by_name("a").unwrap().as_ref(), + &StringArray::from(vec!["a1", "a2"]) as &dyn Array + ); + assert_eq!( + projected_struct.column_by_name("b").unwrap().as_ref(), + &StringArray::from(vec!["b1", "b2"]) as &dyn Array + ); + assert_eq!( + projected_struct.column_by_name("c").unwrap().as_ref(), + &StringArray::from(vec!["c1", "c2"]) as &dyn Array + ); + + // Also verify positional access matches expected order (a=0, b=1, c=2) + assert_eq!( + projected_struct.column(0).as_ref(), + &StringArray::from(vec!["a1", "a2"]) as &dyn Array + ); + assert_eq!( + projected_struct.column(1).as_ref(), + &StringArray::from(vec!["b1", "b2"]) as &dyn Array + ); + assert_eq!( + projected_struct.column(2).as_ref(), + &StringArray::from(vec!["c1", "c2"]) as &dyn Array + ); + } + + #[test] + fn test_project_by_schema_nested_list_struct() { + // Test deeply nested List>> projection + let inner_struct = DataType::Struct(Fields::from(vec![ + Field::new("y", DataType::Int32, true), + Field::new("x", DataType::Int32, true), + ])); + let source_schema = Arc::new(Schema::new(vec![Field::new( + "outer", + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("b", DataType::Utf8, true), + Field::new( + "inner_list", + DataType::List(Arc::new(Field::new("item", inner_struct.clone(), true))), + true, + ), + Field::new("a", DataType::Utf8, true), + ])), + true, + ))), + true, + )])); + + // Create deeply nested data + let y_array = Int32Array::from(vec![1, 2]); + let x_array = Int32Array::from(vec![3, 4]); + let innermost_struct = StructArray::from(vec![ + ( + Arc::new(Field::new("y", DataType::Int32, true)), + Arc::new(y_array) as ArrayRef, + ), + ( + Arc::new(Field::new("x", DataType::Int32, true)), + Arc::new(x_array) as ArrayRef, + ), + ]); + let inner_list = ListArray::new( + Arc::new(Field::new("item", inner_struct.clone(), true)), + OffsetBuffer::from_lengths([2]), + Arc::new(innermost_struct), + None, + ); + + let b_array = StringArray::from(vec!["b1"]); + let a_array = StringArray::from(vec!["a1"]); + let middle_struct = StructArray::from(vec![ + ( + Arc::new(Field::new("b", DataType::Utf8, true)), + Arc::new(b_array) as ArrayRef, + ), + ( + Arc::new(Field::new( + "inner_list", + DataType::List(Arc::new(Field::new("item", inner_struct, true))), + true, + )), + Arc::new(inner_list) as ArrayRef, + ), + ( + Arc::new(Field::new("a", DataType::Utf8, true)), + Arc::new(a_array) as ArrayRef, + ), + ]); + + let outer_list = ListArray::new( + Arc::new(Field::new("item", middle_struct.data_type().clone(), true)), + OffsetBuffer::from_lengths([1]), + Arc::new(middle_struct), + None, + ); + + let batch = + RecordBatch::try_new(source_schema, vec![Arc::new(outer_list) as ArrayRef]).unwrap(); + + // Target schema with reordered fields at all levels + let target_inner_struct = DataType::Struct(Fields::from(vec![ + Field::new("x", DataType::Int32, true), // x before y now + Field::new("y", DataType::Int32, true), + ])); + let target_schema = Schema::new(vec![Field::new( + "outer", + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::Utf8, true), // a before b now + Field::new( + "inner_list", + DataType::List(Arc::new(Field::new("item", target_inner_struct, true))), + true, + ), + Field::new("b", DataType::Utf8, true), + ])), + true, + ))), + true, + )]); + + let projected = batch.project_by_schema(&target_schema).unwrap(); + + // Verify schema + assert_eq!(projected.schema().as_ref(), &target_schema); + + // Verify deeply nested data is reordered correctly + let outer_list = projected.column(0).as_list::(); + let middle_struct = outer_list.values().as_struct(); + + // Middle struct should have a first, then inner_list, then b + assert_eq!( + middle_struct.column(0).as_ref(), + &StringArray::from(vec!["a1"]) as &dyn Array + ); + assert_eq!( + middle_struct.column(2).as_ref(), + &StringArray::from(vec!["b1"]) as &dyn Array + ); + + // Inner list's struct should have x first, then y + let inner_list = middle_struct.column(1).as_list::(); + let innermost_struct = inner_list.values().as_struct(); + assert_eq!( + innermost_struct.column(0).as_ref(), + &Int32Array::from(vec![3, 4]) as &dyn Array + ); + assert_eq!( + innermost_struct.column(1).as_ref(), + &Int32Array::from(vec![1, 2]) as &dyn Array + ); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/list.rs b/lance-artifact/rust/lance-arrow/src/list.rs new file mode 100644 index 000000000..06b0fc592 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/list.rs @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_array::{Array, BooleanArray, GenericListArray, OffsetSizeTrait}; +use arrow_buffer::{BooleanBufferBuilder, OffsetBuffer, ScalarBuffer}; +use arrow_schema::Field; + +pub trait ListArrayExt { + /// Filters out masked null items from the list array + /// + /// It is legal for a list array to have a null entry with a non-zero length. The + /// values inside the entry are "garbage" and should be ignored. This function + /// filters the values array to remove the garbage values. + /// + /// The output list will always have zero-length nulls. + fn filter_garbage_nulls(&self) -> Self; + /// Returns a copy of the list's values array that has been sliced to size + /// + /// It is legal for a list array's offsets to not start with zero. It's also legal + /// for a list array's offsets to not extend to the entire values array. This function + /// behaves similarly to `values()` except it slices the array so that it starts at + /// the first list offset and ends at the last list offset. + fn trimmed_values(&self) -> Arc; + /// The offset type of the underlying list array. + type Offset: OffsetSizeTrait; + /// Returns offsets shifted so the first offset is zero, matching + /// [`Self::trimmed_values`]. + /// + /// Sliced list arrays (e.g. a filtered batch) keep offsets that reference the + /// original values buffer, so combining them with trimmed values produces + /// offsets that exceed the values length. Use this together with + /// `trimmed_values` when constructing a new list array. + fn trimmed_offsets(&self) -> OffsetBuffer; +} + +impl ListArrayExt for GenericListArray { + fn filter_garbage_nulls(&self) -> Self { + if self.is_empty() { + return self.clone(); + } + let Some(validity) = self.nulls().cloned() else { + return self.clone(); + }; + + let mut should_keep = BooleanBufferBuilder::new(self.values().len()); + + // Handle case where offsets do not start at 0 + let preamble_len = self.offsets().first().unwrap().to_usize().unwrap(); + should_keep.append_n(preamble_len, false); + + let mut new_offsets: Vec = Vec::with_capacity(self.len() + 1); + new_offsets.push(OffsetSize::zero()); + let mut cur_len = OffsetSize::zero(); + for (offset, is_valid) in self.offsets().windows(2).zip(validity.iter()) { + let len = offset[1] - offset[0]; + if is_valid { + cur_len += len; + should_keep.append_n(len.to_usize().unwrap(), true); + new_offsets.push(cur_len); + } else { + should_keep.append_n(len.to_usize().unwrap(), false); + new_offsets.push(cur_len); + } + } + + // Offsets may not reference entire values buffer + let trailer = self.values().len() - should_keep.len(); + should_keep.append_n(trailer, false); + + let should_keep = should_keep.finish(); + let should_keep = BooleanArray::new(should_keep, None); + let new_values = arrow_select::filter::filter(self.values(), &should_keep).unwrap(); + let new_offsets = ScalarBuffer::from(new_offsets); + let new_offsets = OffsetBuffer::new(new_offsets); + + Self::new( + Arc::new(Field::new( + "item", + self.value_type(), + self.values().is_nullable(), + )), + new_offsets, + new_values, + Some(validity), + ) + } + + fn trimmed_values(&self) -> Arc { + let first_value = self + .offsets() + .first() + .map(|v| v.to_usize().unwrap()) + .unwrap_or(0); + let last_value = self + .offsets() + .last() + .map(|v| v.to_usize().unwrap()) + .unwrap_or(0); + self.values().slice(first_value, last_value - first_value) + } + + type Offset = OffsetSize; + + fn trimmed_offsets(&self) -> OffsetBuffer { + let offsets = self.offsets(); + let Some(&first) = offsets.first() else { + return offsets.clone(); + }; + if first == OffsetSize::zero() { + return offsets.clone(); + } + let shifted: Vec = offsets.iter().map(|&o| o - first).collect(); + OffsetBuffer::new(ScalarBuffer::from(shifted)) + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{ListArray, UInt64Array}; + use arrow_buffer::{BooleanBuffer, NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field}; + + use super::ListArrayExt; + + #[test] + fn test_filter_garbage_nulls() { + let items = UInt64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + let offsets = ScalarBuffer::::from(vec![2, 5, 8, 9]); + let offsets = OffsetBuffer::new(offsets); + let list_validity = NullBuffer::new(BooleanBuffer::from(vec![true, false, true])); + let list_arr = ListArray::new( + Arc::new(Field::new("item", DataType::UInt64, true)), + offsets, + Arc::new(items), + Some(list_validity.clone()), + ); + + let filtered = list_arr.filter_garbage_nulls(); + + let expected_items = UInt64Array::from(vec![2, 3, 4, 8]); + let offsets = ScalarBuffer::::from(vec![0, 3, 3, 4]); + let expected = ListArray::new( + Arc::new(Field::new("item", DataType::UInt64, false)), + OffsetBuffer::new(offsets), + Arc::new(expected_items), + Some(list_validity), + ); + + assert_eq!(filtered, expected); + } + + #[test] + fn test_trim_values() { + let items = UInt64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + let offsets = ScalarBuffer::::from(vec![2, 5, 6, 8, 9]); + let offsets = OffsetBuffer::new(offsets); + let list_arr = ListArray::new( + Arc::new(Field::new("item", DataType::UInt64, true)), + offsets, + Arc::new(items), + None, + ); + let list_arr = list_arr.slice(1, 2); + + let trimmed = list_arr.trimmed_values(); + + let expected_items = UInt64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + let expected_items = expected_items.slice(5, 3); + + assert_eq!(trimmed.as_ref(), &expected_items); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/memory.rs b/lance-artifact/rust/lance-arrow/src/memory.rs new file mode 100644 index 000000000..6b8db9da7 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/memory.rs @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::collections::HashSet; + +use arrow_array::{Array, RecordBatch}; +use arrow_data::ArrayData; + +/// Counts memory used by buffers of Arrow arrays and RecordBatches. +/// +/// This is meant to capture how much memory is being used by the Arrow data +/// structures as they are. It does not represent the memory used if the data +/// were to be serialized and then deserialized. In particular: +/// +/// * This does not double count memory used by buffers shared by multiple +/// arrays or batches. Round-tripped data may use more memory because of this. +/// * This counts the **total** size of the buffers, even if the array is a slice. +/// Round-tripped data may use less memory because of this. +#[derive(Default)] +pub struct MemoryAccumulator { + seen: HashSet, + total: usize, +} + +impl MemoryAccumulator { + pub fn record_array(&mut self, array: &dyn Array) { + let data = array.to_data(); + self.record_array_data(&data); + } + + fn record_array_data(&mut self, data: &ArrayData) { + for buffer in data.buffers() { + let ptr = buffer.as_ptr(); + if self.seen.insert(ptr as usize) { + self.total += buffer.capacity(); + } + } + + if let Some(nulls) = data.nulls() { + let null_buf = nulls.inner().inner(); + let ptr = null_buf.as_ptr(); + if self.seen.insert(ptr as usize) { + self.total += null_buf.capacity(); + } + } + + for child in data.child_data() { + self.record_array_data(child); + } + } + + pub fn record_batch(&mut self, batch: &RecordBatch) { + for array in batch.columns() { + self.record_array(array); + } + } + + pub fn total(&self) -> usize { + self.total + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::Int32Array; + use arrow_schema::{DataType, Field, Schema}; + + use super::*; + + #[test] + fn test_memory_accumulator() { + let batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], + ) + .unwrap(); + let slice = batch.slice(1, 2); + + let mut acc = MemoryAccumulator::default(); + + // Should record whole buffer, not just slice + acc.record_batch(&slice); + assert_eq!(acc.total(), 3 * std::mem::size_of::()); + + // Should not double count + acc.record_batch(&slice); + assert_eq!(acc.total(), 3 * std::mem::size_of::()); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/scalar.rs b/lance-artifact/rust/lance-arrow/src/scalar.rs new file mode 100644 index 000000000..e9fd2516f --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/scalar.rs @@ -0,0 +1,282 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_array::{ArrayRef, make_array}; +use arrow_buffer::Buffer; +use arrow_data::{ArrayDataBuilder, transform::MutableArrayData}; +use arrow_schema::{ArrowError, DataType}; + +use crate::DataTypeExt; + +type Result = std::result::Result; + +pub const INLINE_VALUE_MAX_BYTES: usize = 32; + +pub fn extract_scalar_value(array: &ArrayRef, idx: usize) -> Result { + if idx >= array.len() { + return Err(ArrowError::InvalidArgumentError( + "Scalar index out of bounds".to_string(), + )); + } + + let data = array.to_data(); + let mut mutable = MutableArrayData::new(vec![&data], /*use_nulls=*/ true, 1); + mutable.extend(0, idx, idx + 1); + Ok(make_array(mutable.freeze())) +} + +fn read_u32(buf: &[u8], offset: &mut usize) -> Result { + if *offset + 4 > buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar value buffer: unexpected EOF".to_string(), + )); + } + let bytes = [ + buf[*offset], + buf[*offset + 1], + buf[*offset + 2], + buf[*offset + 3], + ]; + *offset += 4; + Ok(u32::from_le_bytes(bytes)) +} + +fn read_bytes<'a>(buf: &'a [u8], offset: &mut usize, len: usize) -> Result<&'a [u8]> { + if *offset + len > buf.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar value buffer: unexpected EOF".to_string(), + )); + } + let slice = &buf[*offset..*offset + len]; + *offset += len; + Ok(slice) +} + +fn write_u32(out: &mut Vec, v: u32) { + out.extend_from_slice(&v.to_le_bytes()); +} + +fn write_bytes(out: &mut Vec, bytes: &[u8]) { + out.extend_from_slice(bytes); +} + +pub fn encode_scalar_value_buffer(scalar: &ArrayRef) -> Result> { + if scalar.len() != 1 || scalar.null_count() != 0 { + return Err(ArrowError::InvalidArgumentError( + "Scalar value buffer must be a single non-null value".to_string(), + )); + } + let data = scalar.to_data(); + if data.offset() != 0 { + return Err(ArrowError::InvalidArgumentError( + "Scalar value buffer must have offset=0".to_string(), + )); + } + if !data.child_data().is_empty() { + return Err(ArrowError::InvalidArgumentError( + "Scalar value buffer does not support nested types".to_string(), + )); + } + + // Minimal format (RFC): store the Arrow value buffers for a length-1 array. + // Null bitmap and child data are intentionally not supported here. + // + // | u32 num_buffers | + // | u32 buffer_0_len | ... | u32 buffer_{n-1}_len | + // | buffer_0 bytes | ... | buffer_{n-1} bytes | + let mut out = Vec::with_capacity(128); + let buffers = data.buffers(); + write_u32(&mut out, buffers.len() as u32); + for b in buffers { + write_u32(&mut out, b.len() as u32); + } + for b in buffers { + write_bytes(&mut out, b.as_slice()); + } + Ok(out) +} + +pub fn decode_scalar_from_value_buffer( + data_type: &DataType, + value_buffer: &[u8], +) -> Result { + if matches!( + data_type, + DataType::Struct(_) | DataType::FixedSizeList(_, _) + ) { + return Err(ArrowError::InvalidArgumentError(format!( + "Scalar value buffer does not support nested data type {:?}", + data_type + ))); + } + + let mut offset = 0; + let num_buffers = read_u32(value_buffer, &mut offset)? as usize; + let buffer_lens = (0..num_buffers) + .map(|_| read_u32(value_buffer, &mut offset).map(|l| l as usize)) + .collect::>>()?; + + let mut buffers = Vec::with_capacity(num_buffers); + for len in buffer_lens { + let bytes = read_bytes(value_buffer, &mut offset, len)?; + buffers.push(Buffer::from_vec(bytes.to_vec())); + } + + if offset != value_buffer.len() { + return Err(ArrowError::InvalidArgumentError( + "Invalid scalar value buffer: trailing bytes".to_string(), + )); + } + + let mut builder = ArrayDataBuilder::new(data_type.clone()) + .len(1) + .null_count(0); + for b in buffers { + builder = builder.add_buffer(b); + } + Ok(make_array(builder.build()?)) +} + +pub fn decode_scalar_from_inline_value( + data_type: &DataType, + inline_value: &[u8], +) -> Result { + // I expect our input to be safe here, but I added some debug_assert_eq statements just in case. + // If they are triggered, we may need to change them to return actual errors. + // + // Boolean values are bit-packed in Arrow and therefore are not "fixed-stride" in bytes. + // As a result, `byte_width_opt()` returns `None` for `DataType::Boolean`, even though a + // length-1 scalar can be represented inline using a single byte (matching `try_inline_value`). + if matches!(data_type, DataType::Boolean) { + debug_assert_eq!( + inline_value.len(), + 1, + "Invalid boolean inline scalar length (expected 1 byte, got {})", + inline_value.len() + ); + } else if let Some(byte_width) = data_type.byte_width_opt() { + debug_assert_eq!( + inline_value.len(), + byte_width, + "Inline constant length mismatch for {:?}: expected {} bytes but got {}", + data_type, + byte_width, + inline_value.len() + ); + } + + let data = ArrayDataBuilder::new(data_type.clone()) + .len(1) + .null_count(0) + .add_buffer(Buffer::from_vec(inline_value.to_vec())) + .build()?; + Ok(make_array(data)) +} + +pub fn try_inline_value(scalar: &ArrayRef) -> Option> { + if scalar.null_count() != 0 || scalar.len() != 1 { + return None; + } + let data = scalar.to_data(); + if !data.child_data().is_empty() { + return None; + } + if data.buffers().len() != 1 { + return None; + } + let bytes = data.buffers()[0].as_slice(); + if bytes.len() > INLINE_VALUE_MAX_BYTES { + return None; + } + Some(bytes.to_vec()) +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{BooleanArray, FixedSizeBinaryArray, Int32Array, StringArray, cast::AsArray}; + + use super::*; + + #[test] + fn test_extract_scalar_value() { + let array: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), None, Some(3)])); + let scalar = extract_scalar_value(&array, 2).unwrap(); + assert_eq!(scalar.len(), 1); + assert_eq!( + scalar + .as_primitive::() + .value(0), + 3 + ); + } + + #[test] + fn test_scalar_value_buffer_utf8_round_trip() { + let scalar: ArrayRef = Arc::new(StringArray::from(vec!["hello"])); + let buf = encode_scalar_value_buffer(&scalar).unwrap(); + let decoded = decode_scalar_from_value_buffer(&DataType::Utf8, &buf).unwrap(); + assert_eq!(decoded.len(), 1); + assert_eq!(decoded.null_count(), 0); + assert_eq!(decoded.as_string::().value(0), "hello"); + } + + #[test] + fn test_scalar_value_buffer_fixed_size_binary_round_trip() { + let val = vec![0xABu8; 33]; + let scalar: ArrayRef = Arc::new( + FixedSizeBinaryArray::try_from_sparse_iter_with_size( + std::iter::once(Some(val.as_slice())), + 33, + ) + .unwrap(), + ); + let buf = encode_scalar_value_buffer(&scalar).unwrap(); + let decoded = + decode_scalar_from_value_buffer(&DataType::FixedSizeBinary(33), &buf).unwrap(); + assert_eq!(decoded.len(), 1); + assert_eq!(decoded.as_fixed_size_binary().value(0), val.as_slice()); + } + + #[test] + fn test_inline_value_boolean_round_trip() { + let scalar: ArrayRef = Arc::new(BooleanArray::from_iter([Some(true)])); + let inline = try_inline_value(&scalar).unwrap(); + let decoded = decode_scalar_from_inline_value(&DataType::Boolean, &inline).unwrap(); + assert_eq!(decoded.len(), 1); + assert_eq!(decoded.null_count(), 0); + assert!(decoded.as_boolean().value(0)); + } + + #[test] + fn test_scalar_value_buffer_rejects_nested_type() { + let field = Arc::new(arrow_schema::Field::new("item", DataType::Int32, false)); + let list: ArrayRef = Arc::new(arrow_array::FixedSizeListArray::new( + field, + 2, + Arc::new(Int32Array::from(vec![1, 2])), + None, + )); + let scalar = list.slice(0, 1); + assert!(encode_scalar_value_buffer(&scalar).is_err()); + } + + #[test] + fn test_decode_scalar_from_value_buffer_rejects_nested_type() { + let buf = Vec::::new(); + let res = + decode_scalar_from_value_buffer(&DataType::Struct(arrow_schema::Fields::empty()), &buf); + assert!(res.is_err()); + } + + #[test] + fn test_decode_scalar_from_value_buffer_trailing_bytes() { + // num_buffers = 0, plus an extra byte + let mut bytes = Vec::new(); + bytes.extend_from_slice(&0u32.to_le_bytes()); + bytes.push(1); + let res = decode_scalar_from_value_buffer(&DataType::Int32, &bytes); + assert!(res.is_err()); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/schema.rs b/lance-artifact/rust/lance-arrow/src/schema.rs new file mode 100644 index 000000000..8ce9442b4 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/schema.rs @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Extension to arrow schema + +use arrow_schema::{ArrowError, DataType, Field, FieldRef, Schema}; + +use crate::{ARROW_EXT_NAME_KEY, BLOB_META_KEY, BLOB_V2_EXT_NAME}; + +pub enum Indentation { + OneLine, + MultiLine(u8), +} + +impl Indentation { + fn value(&self) -> String { + match self { + Self::OneLine => "".to_string(), + Self::MultiLine(spaces) => " ".repeat(*spaces as usize), + } + } + + fn deepen(&self) -> Self { + match self { + Self::OneLine => Self::OneLine, + Self::MultiLine(spaces) => Self::MultiLine(spaces + 2), + } + } +} + +/// Extends the functionality of [arrow_schema::Field]. +pub trait FieldExt { + /// Create a compact string representation of the field + /// + /// This is intended for display purposes and not for serialization + fn to_compact_string(&self, indent: Indentation) -> String; + + /// Check if the field is marked as a packed struct + fn is_packed_struct(&self) -> bool; + + /// Check if the field is marked as a blob + fn is_blob(&self) -> bool; + + /// Check if the field is marked as a blob + fn is_blob_v2(&self) -> bool; +} + +impl FieldExt for Field { + fn to_compact_string(&self, indent: Indentation) -> String { + let mut result = format!("{}: ", self.name().clone()); + match self.data_type() { + DataType::Struct(fields) => { + result += "{"; + result += &indent.value(); + for (field_idx, field) in fields.iter().enumerate() { + result += field.to_compact_string(indent.deepen()).as_str(); + if field_idx < fields.len() - 1 { + result += ","; + } + result += indent.value().as_str(); + } + result += "}"; + } + DataType::List(field) + | DataType::LargeList(field) + | DataType::ListView(field) + | DataType::LargeListView(field) => { + result += "["; + result += field.to_compact_string(indent.deepen()).as_str(); + result += "]"; + } + DataType::FixedSizeList(child, dimension) => { + result += &format!( + "[{}; {}]", + child.to_compact_string(indent.deepen()), + dimension + ); + } + DataType::Dictionary(key_type, value_type) => { + result += &value_type.to_string(); + result += "@"; + result += &key_type.to_string(); + } + _ => { + result += &self.data_type().to_string(); + } + } + if self.is_nullable() { + result += "?"; + } + result + } + + // Check if field has metadata `packed` set to true, this check is case insensitive. + fn is_packed_struct(&self) -> bool { + let field_metadata = self.metadata(); + const PACKED_KEYS: [&str; 2] = ["packed", "lance-encoding:packed"]; + PACKED_KEYS.iter().any(|key| { + field_metadata + .get(*key) + .map(|value| value.eq_ignore_ascii_case("true")) + .unwrap_or(false) + }) + } + + fn is_blob(&self) -> bool { + let field_metadata = self.metadata(); + field_metadata.get(BLOB_META_KEY).is_some() + || field_metadata + .get(ARROW_EXT_NAME_KEY) + .map(|value| value == BLOB_V2_EXT_NAME) + .unwrap_or(false) + } + + fn is_blob_v2(&self) -> bool { + let field_metadata = self.metadata(); + field_metadata + .get(ARROW_EXT_NAME_KEY) + .map(|value| value == BLOB_V2_EXT_NAME) + .unwrap_or(false) + } +} + +/// Extends the functionality of [arrow_schema::Schema]. +pub trait SchemaExt { + /// Create a new [`Schema`] with one extra field. + fn try_with_column(&self, field: Field) -> std::result::Result; + + fn try_with_column_at( + &self, + index: usize, + field: Field, + ) -> std::result::Result; + + fn field_names(&self) -> Vec<&String>; + + fn without_column(&self, column_name: &str) -> Schema; + + /// Create a compact string representation of the schema + /// + /// This is intended for display purposes and not for serialization + fn to_compact_string(&self, indent: Indentation) -> String; +} + +impl SchemaExt for Schema { + fn try_with_column(&self, field: Field) -> std::result::Result { + if self.column_with_name(field.name()).is_some() { + return Err(ArrowError::SchemaError(format!( + "Can not append column {} on schema: {:?}", + field.name(), + self + ))); + }; + let mut fields: Vec = self.fields().iter().cloned().collect(); + fields.push(FieldRef::new(field)); + Ok(Self::new_with_metadata(fields, self.metadata.clone())) + } + + fn try_with_column_at( + &self, + index: usize, + field: Field, + ) -> std::result::Result { + if self.column_with_name(field.name()).is_some() { + return Err(ArrowError::SchemaError(format!( + "Failed to modify schema: Inserting column {} would create a duplicate column in schema: {:?}", + field.name(), + self + ))); + }; + let mut fields: Vec = self.fields().iter().cloned().collect(); + fields.insert(index, FieldRef::new(field)); + Ok(Self::new_with_metadata(fields, self.metadata.clone())) + } + + /// Project the schema to remove the given column. + /// + /// This only works on top-level fields right now. If a field does not exist, + /// the schema will be returned as is. + fn without_column(&self, column_name: &str) -> Schema { + let fields: Vec = self + .fields() + .iter() + .filter(|f| f.name() != column_name) + .cloned() + .collect(); + Self::new_with_metadata(fields, self.metadata.clone()) + } + + fn field_names(&self) -> Vec<&String> { + self.fields().iter().map(|f| f.name()).collect() + } + + fn to_compact_string(&self, indent: Indentation) -> String { + let mut result = "{".to_string(); + result += &indent.value(); + for (field_idx, field) in self.fields.iter().enumerate() { + result += field.to_compact_string(indent.deepen()).as_str(); + if field_idx < self.fields.len() - 1 { + result += ","; + } + result += indent.value().as_str(); + } + result += "}"; + result + } +} diff --git a/lance-artifact/rust/lance-arrow/src/stream.rs b/lance-artifact/rust/lance-arrow/src/stream.rs new file mode 100644 index 000000000..7ba1ed5f1 --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/stream.rs @@ -0,0 +1,504 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for working with streams of [`RecordBatch`]. + +use arrow_array::RecordBatch; +use arrow_schema::{ArrowError, SchemaRef}; +use futures::stream::{self, Stream, StreamExt}; +use std::pin::Pin; + +use crate::deepcopy::deep_copy_batch_sliced; + +/// Rechunks a stream of [`RecordBatch`] so that each output batch has +/// approximately `target_bytes` of array data. +/// +/// Small input batches are accumulated (by concatenation) until at least +/// `min_bytes` of data has been collected. If the resulting batch exceeds +/// `max_bytes`, it is sliced into roughly equal pieces of ~`max_bytes` +/// (assuming uniform row sizes). +pub fn rechunk_stream_by_size( + input: S, + input_schema: SchemaRef, + min_bytes: usize, + max_bytes: usize, +) -> impl Stream> +where + S: Stream>, + E: From, +{ + rechunk_stream_by_size_inner(input, input_schema, min_bytes, max_bytes, false) +} + +/// Like [`rechunk_stream_by_size`] but deep-copies slices so that +/// `get_array_memory_size` reflects the true size of each output batch. +/// +/// After a normal `RecordBatch::slice`, the backing buffers are shared with +/// the original batch, so `get_array_memory_size` still reports the full +/// parent size. This variant deep-copies every slice produced during the +/// splitting phase, which allows the stream to detect and re-split slices +/// that still exceed `max_bytes` (e.g. because a single row is much larger +/// than average). +/// +/// The deep copy is a last resort and potentially expensive for large +/// batches. However, it is only performed when a batch actually needs to be +/// sliced — batches that are already within the target range pass through at +/// zero cost. Use this only when the hard cap on `max_bytes` is a +/// correctness requirement, not merely a performance hint. +pub fn rechunk_stream_by_size_deep_copy( + input: S, + input_schema: SchemaRef, + min_bytes: usize, + max_bytes: usize, +) -> impl Stream> +where + S: Stream>, + E: From, +{ + rechunk_stream_by_size_inner(input, input_schema, min_bytes, max_bytes, true) +} + +fn rechunk_stream_by_size_inner( + input: S, + input_schema: SchemaRef, + min_bytes: usize, + max_bytes: usize, + deep_copy: bool, +) -> impl Stream> +where + S: Stream>, + E: From, +{ + stream::try_unfold( + RechunkState { + input: Box::pin(input), + accumulated: Vec::new(), + acc_bytes: 0, + done: false, + input_schema, + min_bytes, + max_bytes, + deep_copy, + }, + |mut state| async move { + if state.done && state.accumulated.is_empty() { + return Ok(None); + } + + // Pull batches until we reach the byte target or exhaust input. + // Always pull at least one batch so that min_bytes=0 works. + while !state.done && (state.accumulated.is_empty() || state.acc_bytes < state.min_bytes) + { + match state.input.next().await { + Some(Ok(batch)) => { + state.acc_bytes += batch.get_array_memory_size(); + state.accumulated.push(batch); + } + Some(Err(e)) => return Err(e), + None => { + state.done = true; + } + } + } + + if state.accumulated.is_empty() { + return Ok(None); + } + + // Fast path: if the first accumulated batch already meets the + // byte threshold, deliver it directly instead of concatenating + // everything together (which would just get sliced back apart). + if state.accumulated.len() > 1 + && state.accumulated[0].get_array_memory_size() >= state.min_bytes + { + let b = state.accumulated.remove(0); + state.acc_bytes -= b.get_array_memory_size(); + return Ok(Some((b, state))); + } + + let batch = if state.accumulated.len() == 1 { + state.accumulated.pop().unwrap() + } else { + let b = + arrow_select::concat::concat_batches(&state.input_schema, &state.accumulated) + .map_err(E::from)?; + state.accumulated.clear(); + b + }; + state.acc_bytes = 0; + + // Slice the batch into ~max_bytes pieces assuming uniform row sizes. + let mut slices = + slice_batch(batch, state.max_bytes, state.deep_copy).map_err(E::from)?; + + if slices.len() == 1 { + Ok(Some((slices.pop().unwrap(), state))) + } else { + let first = slices.remove(0); + + // Stash leftover slices for subsequent iterations. + for a in &slices { + state.acc_bytes += a.get_array_memory_size(); + } + state.accumulated = slices; + + Ok(Some((first, state))) + } + }, + ) +} + +/// Slice a batch into pieces of at most `max_bytes`. +/// +/// When `deep_copy` is false, slices share buffers with the original batch +/// and `get_array_memory_size` will still report the parent buffer size. +/// This is fine when the caller only needs approximate sizing. +/// +/// When `deep_copy` is true, each slice is deep-copied so that +/// `get_array_memory_size` reflects the true size. If a deep-copied slice +/// still exceeds `max_bytes` (due to non-uniform row sizes), it is +/// recursively split until every piece is within budget or contains only a +/// single row. +fn slice_batch( + batch: RecordBatch, + max_bytes: usize, + deep_copy: bool, +) -> Result, ArrowError> { + let batch_bytes = batch.get_array_memory_size(); + let num_rows = batch.num_rows(); + + if batch_bytes <= max_bytes || num_rows <= 1 { + return Ok(vec![batch]); + } + + let rows_per_chunk = (max_bytes as u64 * num_rows as u64 / batch_bytes as u64).max(1) as usize; + + let mut result = Vec::new(); + let mut offset = 0; + while offset < num_rows { + let len = rows_per_chunk.min(num_rows - offset); + let slice = batch.slice(offset, len); + if deep_copy { + let copied = deep_copy_batch_sliced(&slice)?; + // Recurse: the deep-copied slice has accurate sizes, so if it + // still exceeds max_bytes we can split further. + result.extend(slice_batch(copied, max_bytes, true)?); + } else { + result.push(slice); + } + offset += len; + } + + Ok(result) +} + +/// Internal state for [`rechunk_stream`]. +/// +/// Kept as a named struct so the `try_unfold` closure stays readable. +struct RechunkState { + input: Pin>, + accumulated: Vec, + acc_bytes: usize, + done: bool, + input_schema: SchemaRef, + min_bytes: usize, + max_bytes: usize, + deep_copy: bool, +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::sync::Arc; + + use arrow_array::Int32Array; + use arrow_schema::{DataType, Field, Schema}; + use futures::executor::block_on; + + fn make_batch(num_rows: usize) -> RecordBatch { + let schema = test_schema(); + let values: Vec = (0..num_rows as i32).collect(); + RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from(values))]).unwrap() + } + + fn test_schema() -> SchemaRef { + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])) + } + + fn collect_rechunked( + batches: Vec, + min_bytes: usize, + max_bytes: usize, + ) -> Vec { + let input = stream::iter(batches.into_iter().map(Ok::<_, ArrowError>)); + let rechunked = rechunk_stream_by_size(input, test_schema(), min_bytes, max_bytes); + block_on(rechunked.collect::>()) + .into_iter() + .map(|r| r.unwrap()) + .collect() + } + + fn total_rows(batches: &[RecordBatch]) -> usize { + batches.iter().map(|b| b.num_rows()).sum() + } + + #[test] + fn test_empty_stream() { + let result = collect_rechunked(vec![], 100, 200); + assert!(result.is_empty()); + } + + #[test] + fn test_single_batch_passthrough() { + let batch = make_batch(100); + let bytes = batch.get_array_memory_size(); + // Batch is between min and max — should pass through as-is. + let result = collect_rechunked(vec![batch], bytes / 2, bytes * 2); + assert_eq!(result.len(), 1); + assert_eq!(result[0].num_rows(), 100); + } + + #[test] + fn test_small_batches_concatenated() { + let one_batch_bytes = make_batch(10).get_array_memory_size(); + let batches: Vec<_> = (0..8).map(|_| make_batch(10)).collect(); + // min = 5 batches worth, max = 10 batches worth. + let result = collect_rechunked(batches, one_batch_bytes * 5, one_batch_bytes * 10); + assert_eq!(total_rows(&result), 80); + // Should have been concatenated into fewer batches than the 8 inputs. + assert!( + result.len() < 8, + "expected fewer output batches, got {}", + result.len() + ); + } + + #[test] + fn test_large_batch_sliced() { + let batch = make_batch(1000); + let bytes = batch.get_array_memory_size(); + let result = collect_rechunked(vec![batch], bytes / 8, bytes / 4); + assert_eq!(total_rows(&result), 1000); + assert!( + result.len() >= 4, + "expected at least 4 slices, got {}", + result.len() + ); + } + + #[test] + fn test_sliced_leftovers_are_not_recombined() { + // Key test for the fast-path optimisation. When a large batch is + // sliced, leftover slices should be delivered one-at-a-time without + // being concatenated back together. We verify this by checking that + // every output buffer pointer falls inside the original batch's + // allocation (i.e. they are all zero-copy slices, not fresh copies). + let batch = make_batch(1000); + let bytes = batch.get_array_memory_size(); + let orig_data = batch.column(0).to_data(); + let orig_buf = &orig_data.buffers()[0]; + let orig_start = orig_buf.as_ptr() as usize; + let orig_end = orig_start + orig_buf.len(); + + let result = collect_rechunked(vec![batch], bytes / 8, bytes / 4); + + assert_eq!(total_rows(&result), 1000); + assert!(result.len() >= 4); + + for (i, b) in result.iter().enumerate() { + let ptr = b.column(0).to_data().buffers()[0].as_ptr() as usize; + assert!( + ptr >= orig_start && ptr < orig_end, + "slice {i} buffer at {ptr:#x} is outside the original allocation \ + [{orig_start:#x}, {orig_end:#x}) — it was re-concatenated" + ); + } + } + + #[test] + fn test_flush_remainder_on_stream_end() { + // Data below min_bytes should still be flushed when the stream ends. + let batch = make_batch(10); + let bytes = batch.get_array_memory_size(); + let result = collect_rechunked(vec![batch], bytes * 100, bytes * 200); + assert_eq!(result.len(), 1); + assert_eq!(result[0].num_rows(), 10); + } + + #[test] + fn test_large_then_small_batches() { + // After a large batch is fully drained, subsequent small batches + // should be accumulated normally. + let large = make_batch(1000); + let small_bytes = make_batch(10).get_array_memory_size(); + let batches = vec![ + large, + make_batch(10), + make_batch(10), + make_batch(10), + make_batch(10), + make_batch(10), + ]; + let result = collect_rechunked(batches, small_bytes * 3, small_bytes * 100); + assert_eq!(total_rows(&result), 1050); + // The large batch should appear (possibly sliced) followed by + // concatenated small batches, so we should have fewer output batches + // than the 6 inputs. + assert!(result.len() < 6); + } + + #[test] + fn test_row_preservation_across_slicing() { + // Verify that every input row appears exactly once in the output + // and in the correct order after slicing. + let batch = make_batch(237); // odd count to exercise remainder slice + let bytes = batch.get_array_memory_size(); + let result = collect_rechunked(vec![batch], bytes / 8, bytes / 5); + + assert_eq!(total_rows(&result), 237); + + let values: Vec = result + .iter() + .flat_map(|b| { + b.column(0) + .as_any() + .downcast_ref::() + .unwrap() + .values() + .iter() + .copied() + }) + .collect(); + let expected: Vec = (0..237).collect(); + assert_eq!(values, expected); + } + + #[test] + fn test_min_bytes_zero_still_yields_all_rows() { + // When min_bytes=0, the stream should still yield every batch. + // This is the "chop only, don't coalesce" use case. + let batches: Vec<_> = (0..5).map(|_| make_batch(100)).collect(); + let batch_bytes = batches[0].get_array_memory_size(); + let result = collect_rechunked(batches, 0, batch_bytes * 2); + assert_eq!(total_rows(&result), 500); + } + + #[test] + fn test_min_bytes_zero_slices_oversized() { + // min_bytes=0 with a small max_bytes should still slice large batches. + let batch = make_batch(1000); + let bytes = batch.get_array_memory_size(); + let result = collect_rechunked(vec![batch], 0, bytes / 4); + assert_eq!(total_rows(&result), 1000); + assert!( + result.len() >= 4, + "expected at least 4 slices, got {}", + result.len() + ); + } + + /// Build a batch with one variable-length string column. + /// Every row is `small_size` bytes except the row at index `big_row_idx` + /// which is `big_size` bytes. + fn make_variable_batch( + num_rows: usize, + small_size: usize, + big_row_idx: usize, + big_size: usize, + ) -> RecordBatch { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, false)])); + let values: Vec = (0..num_rows) + .map(|i| { + if i == big_row_idx { + "X".repeat(big_size) + } else { + "x".repeat(small_size) + } + }) + .collect(); + let array = arrow_array::StringArray::from(values); + RecordBatch::try_new(schema, vec![Arc::new(array)]).unwrap() + } + + fn variable_schema() -> SchemaRef { + Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, false)])) + } + + fn collect_rechunked_variable( + batches: Vec, + min_bytes: usize, + max_bytes: usize, + ) -> Vec { + let input = stream::iter(batches.into_iter().map(Ok::<_, ArrowError>)); + let rechunked = + rechunk_stream_by_size_deep_copy(input, variable_schema(), min_bytes, max_bytes); + block_on(rechunked.collect::>()) + .into_iter() + .map(|r| r.unwrap()) + .collect() + } + + #[test] + fn test_oversized_row_at_end() { + // 100 rows: 99 small (64 bytes each) + 1 large (100KiB) at the end. + let batch = make_variable_batch(100, 64, 99, 100 * 1024); + let max_bytes = 64 * 1024; + let result = collect_rechunked_variable(vec![batch], 0, max_bytes); + assert_eq!(total_rows(&result), 100); + for (i, b) in result.iter().enumerate() { + let size = b.get_array_memory_size(); + assert!( + size <= max_bytes || b.num_rows() == 1, + "batch {i} has {size} bytes (max {max_bytes}) and {} rows", + b.num_rows() + ); + } + } + + #[test] + fn test_oversized_row_at_start() { + // 100 rows: 1 large (100KiB) at the start + 99 small (64 bytes each). + let batch = make_variable_batch(100, 64, 0, 100 * 1024); + let max_bytes = 64 * 1024; + let result = collect_rechunked_variable(vec![batch], 0, max_bytes); + assert_eq!(total_rows(&result), 100); + for (i, b) in result.iter().enumerate() { + let size = b.get_array_memory_size(); + assert!( + size <= max_bytes || b.num_rows() == 1, + "batch {i} has {size} bytes (max {max_bytes}) and {} rows", + b.num_rows() + ); + } + } + + #[test] + fn test_oversized_row_in_middle() { + // 100 rows: 1 large (100KiB) in the middle + 99 small (64 bytes each). + let batch = make_variable_batch(100, 64, 50, 100 * 1024); + let max_bytes = 64 * 1024; + let result = collect_rechunked_variable(vec![batch], 0, max_bytes); + assert_eq!(total_rows(&result), 100); + for (i, b) in result.iter().enumerate() { + let size = b.get_array_memory_size(); + assert!( + size <= max_bytes || b.num_rows() == 1, + "batch {i} has {size} bytes (max {max_bytes}) and {} rows", + b.num_rows() + ); + } + } + + #[test] + fn test_error_propagation() { + let input = stream::iter(vec![ + Ok(make_batch(10)), + Err(ArrowError::ComputeError("boom".into())), + Ok(make_batch(10)), + ]); + let rechunked = rechunk_stream_by_size(input, test_schema(), 1, usize::MAX); + let results: Vec> = block_on(rechunked.collect()); + assert!(results.iter().any(|r| r.is_err())); + } +} diff --git a/lance-artifact/rust/lance-arrow/src/struct.rs b/lance-artifact/rust/lance-arrow/src/struct.rs new file mode 100644 index 000000000..4dee5032b --- /dev/null +++ b/lance-artifact/rust/lance-arrow/src/struct.rs @@ -0,0 +1,191 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Extension to arrow struct arrays + +use arrow_array::{Array, StructArray, cast::AsArray, make_array}; +use arrow_buffer::NullBuffer; +use arrow_data::{ArrayData, ArrayDataBuilder}; +use arrow_schema::ArrowError; + +pub trait StructArrayExt { + /// Removes the offset / length of the struct array by pushing it into the children + /// + /// In arrow-rs when slice is called it recursively slices the children. + /// In arrow-cpp when slice is called it just sets the offset/length of + /// the struct array and leaves the children as-is + /// + /// Both are legal approaches (╥﹏╥) + /// + /// This method helps reduce complexity by folding into the arrow-rs approach + fn normalize_slicing(&self) -> Result + where + Self: Sized; + + /// Structs are allowed to mask valid items. For example, a struct array might be: + /// + /// [ {"items": [1, 2, 3]}, NULL, {"items": NULL}] + /// + /// However, the underlying items array might be: [[1, 2, 3], [4, 5], NULL] + /// + /// The [4, 5] list is masked out because the struct array is null. + /// + /// The struct validity would be [true, false, true] and the list validity would be [true, true, false] + /// + /// This method pushes nulls down into all children. In the above example the list validity would become + /// [true, false, false]. + /// + /// This method is not recursive. If a child is a struct array it will not push that child's nulls down. + /// + /// This method does not remove garbage lists. It only updates the validity so a future call to + /// [crate::list::ListArrayExt::filter_garbage_nulls] will remove the garbage lists (without + /// this pushdown it would not) + fn pushdown_nulls(&self) -> Result + where + Self: Sized; +} + +fn normalized_struct_array_data(data: ArrayData) -> Result { + let parent_offset = data.offset(); + let parent_len = data.len(); + let modified_children = data + .child_data() + .iter() + .map(|d| { + let d = normalized_struct_array_data(d.clone())?; + let offset = d.offset(); + let len = d.len(); + if len < parent_len + parent_offset { + return Err(ArrowError::InvalidArgumentError(format!( + "Child array {} has length {} which is less than the parent length {} plus the parent offset {}", + d.data_type(), + len, + parent_len, + parent_offset + ))); + } + let new_offset = offset + parent_offset; + d.into_builder().offset(new_offset) + .len(parent_len) + .build() + }) + .collect::, _>>()?; + ArrayDataBuilder::new(data.data_type().clone()) + .len(parent_len) + .offset(0) + .buffers(data.buffers().to_vec()) + .child_data(modified_children) + .build() +} + +impl StructArrayExt for StructArray { + fn normalize_slicing(&self) -> Result + where + Self: Sized, + { + if self.offset() == 0 && self.columns().iter().all(|c| c.len() == self.len()) { + return Ok(self.clone()); + } + + let data = normalized_struct_array_data(self.to_data())?; + Ok(Self::from(data)) + } + + fn pushdown_nulls(&self) -> Result + where + Self: Sized, + { + let Some(validity) = self.nulls() else { + return Ok(self.clone()); + }; + let data = self.to_data(); + let children = data + .child_data() + .iter() + .map(|c| { + if let Some(child_validity) = c.nulls() { + let new_validity = child_validity.inner() & validity.inner(); + c.clone() + .into_builder() + .nulls(Some(NullBuffer::from(new_validity))) + .build() + } else { + Ok(c.clone() + .into_builder() + .nulls(Some(validity.clone())) + .build()?) + } + }) + .collect::, _>>()?; + let arr = make_array(data.into_builder().child_data(children).build()?); + Ok(arr.as_struct().clone()) + } +} + +#[cfg(test)] +mod tests { + use arrow_array::{Array, Int32Array, StructArray, cast::AsArray, make_array}; + use arrow_schema::{DataType, Field, Fields}; + use std::sync::Arc; + + use crate::r#struct::StructArrayExt; + + #[test] + fn test_normalize_slicing_no_offset() { + let x = Int32Array::from(vec![1, 2, 3]); + let y = Int32Array::from(vec![4, 5, 6]); + let struct_array = StructArray::new( + Fields::from(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int32, true), + ]), + vec![Arc::new(x), Arc::new(y)], + None, + ); + + let normalized = struct_array.normalize_slicing().unwrap(); + assert_eq!(normalized, struct_array); + } + + #[test] + fn test_arrow_rs_slicing() { + let x = Int32Array::from(vec![1, 2, 3, 4]); + let y = Int32Array::from(vec![5, 6, 7, 8]); + let struct_array = StructArray::new( + Fields::from(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int32, true), + ]), + vec![Arc::new(x), Arc::new(y)], + None, + ); + + // Slicing with arrow-rs propagates the slicing to the children so there should + // be no change needed to the struct array + let sliced = struct_array.slice(1, 2); + let normalized = sliced.normalize_slicing().unwrap(); + + assert_eq!(normalized, sliced); + } + + #[test] + fn test_arrow_cpp_slicing() { + let x = Int32Array::from(vec![1, 2, 3, 4]); + let y = Int32Array::from(vec![5, 6, 7, 8]); + let struct_array = StructArray::new( + Fields::from(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int32, true), + ]), + vec![Arc::new(x), Arc::new(y)], + None, + ); + + let data = struct_array.to_data(); + let sliced = data.into_builder().offset(1).len(2).build().unwrap(); + let sliced = make_array(sliced); + let normalized = sliced.as_struct().clone().normalize_slicing().unwrap(); + + assert_eq!(normalized, struct_array.slice(1, 2)); + } +} diff --git a/lance-artifact/rust/lance-core/Cargo.toml b/lance-artifact/rust/lance-core/Cargo.toml new file mode 100644 index 000000000..8e7b99869 --- /dev/null +++ b/lance-artifact/rust/lance-core/Cargo.toml @@ -0,0 +1,72 @@ +[package] +name = "lance-core" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme.workspace = true +description = "Lance Columnar Format -- Core Library" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[dependencies] +arrow-array.workspace = true +arrow-buffer.workspace = true +arrow-data.workspace = true +arrow-schema.workspace = true +async-trait.workspace = true +lance-arrow.workspace = true +blake3.workspace = true +byteorder.workspace = true +bytes.workspace = true +datafusion-common = { workspace = true, optional = true } +datafusion-sql = { workspace = true, optional = true } +lance-derive.workspace = true +futures.workspace = true +itertools.workspace = true +libc.workspace = true +libm.workspace = true +moka.workspace = true +quick_cache = "0.6" +num_cpus = "1.0" +object_store = { workspace = true } +pin-project.workspace = true +prost.workspace = true +rand.workspace = true +roaring.workspace = true +serde_json.workspace = true +snafu.workspace = true +tempfile.workspace = true +tokio.workspace = true +tokio-stream.workspace = true +tokio-util.workspace = true +tracing.workspace = true +twox-hash.workspace = true +url.workspace = true +log.workspace = true + +# This is used to detect CPU features at runtime. +# See src/utils/cpu.rs +[target.'cfg(all(any(target_arch = "aarch64", target_arch = "loongarch64"), target_os = "linux"))'.dependencies] +libc = { version = "0.2" } + +[dev-dependencies] +criterion.workspace = true +proptest.workspace = true +rstest.workspace = true + +[features] +# Capture Rust backtraces in error types. When disabled (the default), +# the backtrace field is zero-sized with no overhead. At runtime, capture +# is still gated by RUST_BACKTRACE=1. +backtrace = [] +datafusion = ["dep:datafusion-common", "dep:datafusion-sql"] + +[[bench]] +name = "cache_keys" +harness = false + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-core/benches/cache_keys.rs b/lance-artifact/rust/lance-core/benches/cache_keys.rs new file mode 100644 index 000000000..10f3917f1 --- /dev/null +++ b/lance-artifact/rust/lance-core/benches/cache_keys.rs @@ -0,0 +1,365 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::any::Any; +use std::borrow::Cow; +use std::hash::{BuildHasher, RandomState}; +use std::hint::black_box; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Weak}; + +use async_trait::async_trait; +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use futures::FutureExt; +use lance_core::cache::{ + CacheKey, CacheKeySchema, CacheNamespace, KeyBuilder, LanceCache, WeakLanceCache, +}; + +struct PageKey { + column_index: u32, + page_index: u64, +} + +impl CacheKey for PageKey { + type ValueType = Vec; + + fn key(&self) -> Cow<'_, str> { + format!("{}-{}", self.column_index, self.page_index).into() + } + + fn type_name() -> &'static str { + "bench.Page" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("bench.page-key", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u32(self.column_index); + builder.write_u64(self.page_index); + } +} + +#[derive(Clone, Eq, Hash, PartialEq)] +struct LegacyPhysicalKey { + namespace: Arc, + logical_key: Arc, + type_name: &'static str, +} + +type LegacyEntryValue = Arc; + +#[derive(Clone)] +struct LegacyEntry { + value: LegacyEntryValue, + size_bytes: usize, +} + +#[async_trait] +trait LegacyBackend: Send + Sync { + async fn get(&self, key: &LegacyPhysicalKey) -> Option; + async fn insert(&self, key: &LegacyPhysicalKey, value: LegacyEntryValue, size_bytes: usize); +} + +struct LegacyMokaBackend { + cache: moka::future::Cache, +} + +impl LegacyMokaBackend { + fn with_capacity(capacity: usize) -> Self { + let cache = moka::future::Cache::builder() + .max_capacity(capacity as u64) + .weigher(|key: &LegacyPhysicalKey, entry: &LegacyEntry| { + std::mem::size_of::() + .saturating_add(key.logical_key.len()) + .saturating_add(entry.size_bytes) + .try_into() + .unwrap_or(u32::MAX) + }) + .support_invalidation_closures() + .build(); + Self { cache } + } +} + +#[async_trait] +impl LegacyBackend for LegacyMokaBackend { + async fn get(&self, key: &LegacyPhysicalKey) -> Option { + self.cache.get(key).await.map(|entry| entry.value) + } + + async fn insert(&self, key: &LegacyPhysicalKey, value: LegacyEntryValue, size_bytes: usize) { + self.cache + .insert(key.clone(), LegacyEntry { value, size_bytes }) + .await; + } +} + +#[derive(Clone)] +struct LegacyCache { + backend: Arc, + namespace: Arc, + hits: Arc, + misses: Arc, +} + +impl LegacyCache { + fn with_capacity(capacity: usize) -> Self { + Self { + backend: Arc::new(LegacyMokaBackend::with_capacity(capacity)), + namespace: Arc::from(""), + hits: Arc::new(AtomicU64::new(0)), + misses: Arc::new(AtomicU64::new(0)), + } + } + + fn with_key_prefix(&self, segment: &str) -> Self { + Self { + backend: self.backend.clone(), + namespace: Arc::from(format!("{}{segment}/", self.namespace)), + hits: self.hits.clone(), + misses: self.misses.clone(), + } + } + + fn physical_key(&self, key: &PageKey) -> LegacyPhysicalKey { + let logical_key = key.key(); + LegacyPhysicalKey { + namespace: self.namespace.clone(), + logical_key: Arc::from(logical_key.as_ref()), + type_name: PageKey::type_name(), + } + } + + async fn insert(&self, key: &PageKey, value: Arc>) { + let size_bytes = std::mem::size_of::>() + + value.capacity() + + std::mem::size_of::() * 2; + self.backend + .insert(&self.physical_key(key), value, size_bytes) + .boxed() + .await; + } + + async fn get(&self, key: &PageKey) -> Option>> { + async { + let Some(value) = self.backend.get(&self.physical_key(key)).await else { + self.misses.fetch_add(1, Ordering::Relaxed); + return None; + }; + match value.downcast::>() { + Ok(value) => { + self.hits.fetch_add(1, Ordering::Relaxed); + Some(value) + } + Err(_) => { + self.misses.fetch_add(1, Ordering::Relaxed); + None + } + } + } + .boxed() + .await + } +} + +struct LegacyWeakCache { + backend: Weak, + namespace: Arc, + hits: Arc, + misses: Arc, +} + +impl LegacyWeakCache { + fn from(cache: &LegacyCache) -> Self { + Self { + backend: Arc::downgrade(&cache.backend), + namespace: cache.namespace.clone(), + hits: cache.hits.clone(), + misses: cache.misses.clone(), + } + } + + async fn get(&self, key: &PageKey) -> Option>> { + let backend = self.backend.upgrade()?; + let logical_key = key.key(); + let physical_key = LegacyPhysicalKey { + namespace: self.namespace.clone(), + logical_key: Arc::from(logical_key.as_ref()), + type_name: PageKey::type_name(), + }; + let Some(value) = backend.get(&physical_key).await else { + self.misses.fetch_add(1, Ordering::Relaxed); + return None; + }; + match value.downcast::>() { + Ok(value) => { + self.hits.fetch_add(1, Ordering::Relaxed); + Some(value) + } + Err(_) => { + self.misses.fetch_add(1, Ordering::Relaxed); + None + } + } + } +} + +fn benchmark_key_preparation(c: &mut Criterion) { + let key = PageKey { + column_index: 17, + page_index: 42, + }; + let outer_hashes = RandomState::new(); + let prefixes: [(&str, Arc); 2] = [ + ("short", Arc::from("dataset")), + ("long", Arc::from("p".repeat(1024))), + ]; + let mut group = c.benchmark_group("cache_key_preparation"); + + for (case, prefix) in prefixes { + let namespace = CacheNamespace::root().child(&prefix); + + group.bench_with_input(BenchmarkId::new("legacy", case), &prefix, |b, prefix| { + b.iter(|| { + let logical_key = key.key(); + let physical_key = LegacyPhysicalKey { + namespace: Arc::clone(prefix), + logical_key: Arc::from(logical_key.as_ref()), + type_name: PageKey::type_name(), + }; + black_box(outer_hashes.hash_one(physical_key)) + }) + }); + + group.bench_function(BenchmarkId::new("blake3_typed", case), |b| { + b.iter(|| { + let mut builder = + KeyBuilder::new(namespace, PageKey::stable_type_id(), PageKey::schema()); + key.write_key(&mut builder); + black_box(outer_hashes.hash_one(builder.finish())) + }) + }); + } + + group.finish(); +} + +fn benchmark_namespace_derivation(c: &mut Criterion) { + let root = CacheNamespace::root(); + let long_segment = "p".repeat(160); + let mut group = c.benchmark_group("cache_namespace_derivation"); + + group.bench_function("root", |b| { + b.iter(|| black_box(CacheNamespace::root())); + }); + group.bench_with_input( + BenchmarkId::new("child", "short"), + &"dataset", + |b, segment| { + b.iter(|| black_box(root.child(black_box(segment)))); + }, + ); + group.bench_with_input( + BenchmarkId::new("child", "long"), + &long_segment, + |b, segment| { + b.iter(|| black_box(root.child(black_box(segment)))); + }, + ); + + group.finish(); +} + +fn benchmark_cache_operations(c: &mut Criterion) { + const CAPACITY: usize = 64 * 1024; + const ROTATING_KEYS: u64 = 512; + + let runtime = tokio::runtime::Runtime::new().unwrap(); + let prefix = "p".repeat(1024); + let legacy = LegacyCache::with_capacity(CAPACITY).with_key_prefix(&prefix); + let legacy_weak = LegacyWeakCache::from(&legacy); + let fixed = LanceCache::with_capacity(CAPACITY).with_key_prefix(&prefix); + let fixed_weak = WeakLanceCache::from(&fixed); + let hit_key = PageKey { + column_index: 17, + page_index: 42, + }; + runtime.block_on(async { + legacy.insert(&hit_key, Arc::new(vec![1_u8; 32])).await; + fixed + .insert_with_key(&hit_key, Arc::new(vec![1_u8; 32])) + .await; + }); + + let mut group = c.benchmark_group("cache_operations"); + group.bench_function(BenchmarkId::new("strong_warmed_hit", "legacy"), |b| { + b.to_async(&runtime) + .iter(|| legacy.get(black_box(&hit_key))); + }); + group.bench_function(BenchmarkId::new("strong_warmed_hit", "fixed"), |b| { + b.to_async(&runtime) + .iter(|| fixed.get_with_key(black_box(&hit_key))); + }); + group.bench_function(BenchmarkId::new("weak_warmed_hit", "legacy"), |b| { + b.to_async(&runtime) + .iter(|| legacy_weak.get(black_box(&hit_key))); + }); + group.bench_function(BenchmarkId::new("weak_warmed_hit", "fixed"), |b| { + b.to_async(&runtime) + .iter(|| fixed_weak.get_with_key(black_box(&hit_key))); + }); + + let values: Vec<_> = (0..16).map(|value| Arc::new(vec![value; 32])).collect(); + let next_legacy_insert = AtomicU64::new(0); + group.bench_function(BenchmarkId::new("bounded_rotating_insert", "legacy"), |b| { + b.to_async(&runtime).iter(|| { + let sequence = next_legacy_insert.fetch_add(1, Ordering::Relaxed); + let page_index = sequence % ROTATING_KEYS; + let value = Arc::clone(&values[sequence as usize % values.len()]); + let legacy = &legacy; + async move { + legacy + .insert( + &PageKey { + column_index: 17, + page_index, + }, + value, + ) + .await; + } + }); + }); + let next_fixed_insert = AtomicU64::new(0); + group.bench_function(BenchmarkId::new("bounded_rotating_insert", "fixed"), |b| { + b.to_async(&runtime).iter(|| { + let sequence = next_fixed_insert.fetch_add(1, Ordering::Relaxed); + let page_index = sequence % ROTATING_KEYS; + let value = Arc::clone(&values[sequence as usize % values.len()]); + let fixed = &fixed; + async move { + fixed + .insert_with_key( + &PageKey { + column_index: 17, + page_index, + }, + value, + ) + .await; + } + }); + }); + + group.finish(); +} + +criterion_group!( + benches, + benchmark_key_preparation, + benchmark_namespace_derivation, + benchmark_cache_operations +); +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-core/proptest-regressions/io/writer/statistics.txt b/lance-artifact/rust/lance-core/proptest-regressions/io/writer/statistics.txt new file mode 100644 index 000000000..a09af1119 --- /dev/null +++ b/lance-artifact/rust/lance-core/proptest-regressions/io/writer/statistics.txt @@ -0,0 +1,12 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc d036403fe9f78dbff2c0719e2319e043d9fa1e95aeecd63f8e69593a22c10ea4 # shrinks to values = [""] +cc e7d31563fa5d35fe809e639d776be444b6042700b05fe695a0f6adc14fb171f7 # shrinks to values = [] +cc 6c525cb87c8a699c192a10bb4064837f4e91ca609cac82e3615da4776227b613 # shrinks to values = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] +cc 6a1991927899b890de6623bf046ebd7e6fd512c73c854a21be5a947bf15f98dd # shrinks to values = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] +cc 1436aa3ba65a78849c52480e744e8ed37a6eb64e793319f36f28e0ffd30c0f34 # shrinks to values = ["𐀀 𐀀aA \u{80}aA\u{b}A \u{b}\00 0𐀀0 A𐀀\0ࠀaࠀAA0¡ 𐀀𐀀¡ 𐀀"] +cc f1974dc3e72ef3d23ce7fd5a983f188a3f26bf9247f43dc25e7e71f46e0bedd9 # shrinks to values = ["\u{7f}", " \u{80}𐀀𐀀\0\0 \u{b}¡𐀀𐀀ࠀ\0 a\u{b} ¡𐀀𐀀0Aa𐀀a00 𐀀a𐀀A"] diff --git a/lance-artifact/rust/lance-core/proptest-regressions/utils/mask.txt b/lance-artifact/rust/lance-core/proptest-regressions/utils/mask.txt new file mode 100644 index 000000000..75868998c --- /dev/null +++ b/lance-artifact/rust/lance-core/proptest-regressions/utils/mask.txt @@ -0,0 +1,9 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 53de4b75e19cb1722e4c49c4f6da7c5b128f2550e2bdb834dd2ef8204641ab5b # shrinks to left_full_fragments = [], left_rows = [6342742539488985088], right_full_fragments = [], right_rows = [13733282843423946305, 8187912200365697975, 2826914340666146713, 346404260948424191, 8795222071059034867, 8747127353892278493, 1468831721133745007, 16891318129280599622, 15324377982129497641, 15226820007695200749, 15042029384397853463, 8652845026784187264, 10331787763784933176, 276201048312694890, 2534976430398968306, 11416396717282536445, 7672722720382349779, 18204822811349568253, 4929671055367878201, 14085723886966335595, 12467037916095647919, 5207803031642043043, 8760013298953053789, 5809609147114629810, 16225080661218755454, 7498829699352808828, 8202174973368140193, 8804400423873537437, 15855936153862474267, 8270040936527830746, 11170802732437518884, 5563288748526670493, 10358898910076544769, 13067718144223787806, 1290646450941382369, 13169158542922612311, 8665645167951235642, 1091413462271850947, 7222205877985222715, 13353452657831555660, 11602932332195399937, 6041176084591465480, 2192824513752415329, 7548823551902649398, 14128204609527865509, 209825311222762991, 716579796384536416, 13283195767619500549, 7207350297315362613, 5972665587198475423, 17368711297169537952, 16174661986928242041, 4883920872997594213, 7060536239980957117, 12814819924027932908, 4286464601194923495, 18236425938320045515, 15120958922042602936, 2188823134122277630, 2663730373951037934, 168823322116957773, 171482964947351277, 8420872594248310957, 13802176086534397206, 6860324615718722703, 2710209065549797293, 10836339832438525373, 15793129351026318560, 6999402962257513466, 1679590599864002654, 542281197128589615, 6042117912976220666, 5562112650713749921, 892479752219343050, 2719699678682480992, 16183523869635315264, 6030280045844295185, 12044311714884516738, 2027234216180904734, 10651998776755631186, 16532449747588313058, 12977139814083135678, 11158224751899280827, 4999399290500350933, 13850740312291029723, 7013571957185385376, 3712183885789447544, 8971080480942832455, 5651190952511168498, 10905864091724386338, 6740811476879151832, 646879762256929312, 1588493226600681858, 436045259057454210, 7261833664967894390, 17636837559136442052, 14524526435438990852, 16889822340594141566, 15310825020779890802, 965185016991844328, 12297990261441456618, 2192998141502958798, 12125171641710366818, 10094374568678016581, 4384969157869746719, 10343908254139191200, 12998324592403633914, 637568095658198117, 8212503576607298140, 7266911639689022903, 18067493675562897980, 17095127428876791495, 1142701023060298838, 10270420569676448331, 12255442189115891721, 1622733462125749191, 10628625171463731388, 1805099576498553164, 8106881252300568487, 17815147598333925982, 13225214881857236793, 17773363691692293625, 17638758071786298497, 9244282442895641553, 10680984208128704554, 3980475612309009729, 15950778978679318863, 6905731364372537346, 11981704927530295505, 6768264081412043555, 5159714764396089912, 13956505341439482871, 10202243731211495096, 15781665333732314013, 3259318619651256721, 12608828407323512974, 1751284343166002692, 4020171776257937824, 8463665762191716162, 17824609831430256275, 4270369827749135973, 10463946411388443781, 13743678300012523455, 2806873590443087011, 1044273387154914036, 77965350712761607, 3130834798402376082, 15007021910815693868, 14880671706450060325, 1634940649133521279, 7045129694581350073, 10423529367311455761, 4353404924195198257, 8985383300410376250, 4111466743230279746, 11351762511751826765, 2763907725027238157, 3069869343944810641, 5540843923453134687, 13085011501419283648, 15941569528455664618, 2160657940628656180, 6879252286355263654, 2704618325953696237, 17469966758155545781, 12397819175798353476, 11011026207001558558, 1506665050149228935, 7098184520666238954, 9767322906705915515, 15353354727205194206, 9866228278435122, 2102367774513341183, 3805962221925304068, 13166539065791475353, 4485566101982551195, 3645491834085192840, 13515648009500005179, 1072241748443355105, 7283932425305648296, 7835465610375812546, 2864733791080953071, 168136695080980486, 3593154177390492293, 1803738505252156683, 2345290162572142467, 17589096842477763889, 13952573583177725056, 16625037181727463146, 8801414446044119888, 6342742543020814281, 4462887087591900863, 8629583278021765474, 9480805335467506318, 1023059858072507169, 17812804134620697230, 13471111245905282711, 6712269206202718456, 14162968057918382420, 5957022394052318570, 3271958901145022957, 5325397640026970521, 7024675086201511551, 9475063037733084495, 18255454461409039125, 14676460598863029935, 11727012523887848580, 9695065792734513840, 16291538706807612177, 17876062475901690743, 17575303988324693038, 8408849602798956319, 16139905881586840583, 6063246168674511204, 2393316904147042967, 5796051366081719090, 15994869378339532426, 15169311665186583460, 11112507622195111985, 15870791659662008484, 6100370410603029618, 14545059720181855236, 6299171068949228220, 10259383482725500970, 10032068577003599733, 4896219609730168908, 8036291497636534896, 11677782109340250791, 17968763060782407111, 3615583488780794732, 6722825758251840602, 9023103009150837460, 12018627587754147659, 9429956690830333242, 6281161582008007039, 3769419278751737239, 15724642631260971815, 7127007386302879086, 6557241278197011071, 11109426557487235477, 6615153397592382540, 5775957236880730439, 12138751564888756615, 16513840252092754541, 6963022000296301659, 2638029040181960306, 1183930527665980069, 13517713242722724060, 8370584284753744692, 11638870871053839037, 2144118614178013276, 3975683220588669066, 1427479859038247714, 9084643451112126012, 12630080380812334452, 10022639250487604700, 7272779302037057915, 2622583550130849756, 7808959037994179807, 17875195572930644536, 4939012547971162367, 14512352907397614278, 246405366835936315, 4934431338226476516, 14984748796323296379, 3521341438724326327, 9560663597855536475, 3383138686691985364, 10603552466045290097, 594877629843474020, 2350594426090317633, 2904059667428434988, 5784722567433487660, 7375360910086521563, 16300197331112906643, 14047510571721932735, 12529348231087924711, 5956481652765075509, 7878169618776137096, 18214238038477008937, 12484123754320264154, 10276178645853708514, 4493380037664311589, 287069083072293717, 17593640950026360501, 14693683868510648759, 15061607965654878946, 15746973705201117636, 13065636121467627552, 11296180334714955298, 11744400753964229947, 14671192746690504631, 6965494992677674643, 17747920263123992750, 61755586993290126, 6058319254859393498, 15410758620392344184, 17919105140707609776, 1063252334769125144, 6877096040065090713, 12354114025427769748, 17611418956492774768, 16314052644669674463, 17389465526496956927, 10951825905360760380, 4892553639173273962, 6381127805530294120, 15026621505198122419, 17654462898827945460, 18334288813378947560, 10447562046962870313, 17787032036949099815, 4208998780119334745, 15559584280214079442, 5473849864048284009, 17050125910403415795, 4739876045594204375, 6456143804710758613, 16263704440898205659, 1707824652709662541, 11920091080587420346, 14802845059144881995, 18240918946198680427, 2416842737049175381, 6487817469511133612, 15888014328054043694, 30089805715740204, 2173667145619914557, 8655502048108414893, 1649555215859854858, 9612948290156418869, 7623979106284848119, 6569182823199151456, 3723228891919878869, 17846173866933315452, 5066181643922906900, 6888112031607755155, 15087536236026433117, 9383821820046739832, 13074705695263645820, 16951895279107926024, 6830621797677580111, 6301906905623557486, 16379453645387888340] +cc 21cad309010e6125a7664e6e66502b28a2311f12378dbe8c39107acbb90225f7 + diff --git a/lance-artifact/rust/lance-core/src/cache/backend.rs b/lance-artifact/rust/lance-core/src/cache/backend.rs new file mode 100644 index 000000000..fdd612f10 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/backend.rs @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Backend interface for cache implementors. +//! +//! This module defines the trait that custom cache backends must implement, +//! along with the entry type they operate on. Most callers should +//! use [`LanceCache`](super::LanceCache) instead of interacting with +//! backends directly. +//! +//! # Migrating custom backends +//! +//! Cache keys are opaque 16-byte values. Store +//! [`InternalCacheKey::as_bytes`] directly instead of decomposing a logical +//! prefix, key string, and Rust type name. The physical namespace must also +//! include [`CACHE_KEY_FORMAT`](super::CACHE_KEY_FORMAT), so a future key +//! protocol produces cold misses instead of aliases. Persistent or tiered +//! backends can route serializable values with [`CacheCodec::type_id`]. +//! +//! Prefix invalidation and key inventory are intentionally not part of this +//! interface: one-way digests cannot support either operation without +//! retaining the logical strings that fixed-size keys are designed to remove. +//! Existing callers should migrate removed symbols as follows: +//! - replace `with_backend_and_prefix(backend, prefix)` with +//! [`LanceCache::with_backend`](super::LanceCache::with_backend) followed by +//! [`LanceCache::with_key_prefix`](super::LanceCache::with_key_prefix); +//! - replace `invalidate_prefix` with [`LanceCache::clear`](super::LanceCache::clear) +//! when clearing the shared backend is acceptable, or rotate a versioned +//! namespace to leave older entries to age out; +//! - remove uses of `prefix`, `keys`, and session key-inventory methods; opaque +//! keys have no readable or enumerable equivalent. + +use std::any::Any; +use std::pin::Pin; +use std::sync::Arc; + +use async_trait::async_trait; +use futures::Future; + +use crate::Result; +use crate::deepsize::Context; + +use super::{CacheCodec, InternalCacheKey}; + +/// A type-erased cache entry. +pub type CacheEntry = Arc; + +/// Low-level pluggable cache backend. +/// +/// Implementations store entries keyed by [`InternalCacheKey`] and return +/// type-erased [`CacheEntry`] values. +/// [`LanceCache`](super::LanceCache) handles key construction and type safety; +/// backend authors only need to implement storage and eviction. +#[async_trait] +pub trait CacheBackend: Send + Sync + std::fmt::Debug { + /// Look up an entry by its key. + /// + /// `codec` is provided so that persistent backends can deserialize the + /// entry from storage. In-memory backends can ignore it. When `codec` + /// is `None`, the entry type does not support serialization yet and + /// must be stored in-memory. + /// + /// The goal is for all cache entry types to eventually have codecs, + /// at which point the `Option` will be removed. + async fn get(&self, key: &InternalCacheKey, codec: Option) -> Option; + + /// Store an entry. `size_bytes` is used for eviction accounting. + /// + /// See [`get`](Self::get) for codec semantics. + async fn insert( + &self, + key: &InternalCacheKey, + entry: CacheEntry, + size_bytes: usize, + codec: Option, + ); + + /// Get an existing entry or compute it from `loader`. + /// + /// Implementations should deduplicate concurrent loads for the same key + /// so the loader runs at most once. + /// + /// Returns `(entry, was_cached)` where `was_cached` is `true` if the entry + /// was already present in the cache (the loader was not invoked). + /// + /// See [`get`](Self::get) for codec semantics. + async fn get_or_insert<'a>( + &self, + key: &InternalCacheKey, + loader: Pin> + Send + 'a>>, + codec: Option, + ) -> Result<(CacheEntry, bool)>; + + /// Remove all entries. + async fn clear(&self); + + /// Number of entries currently stored (may flush pending operations). + async fn num_entries(&self) -> usize; + + /// Total weighted size in bytes of all stored entries (may flush pending operations). + async fn size_bytes(&self) -> usize; + + /// Approximate number of entries, callable from synchronous contexts. + /// Backends that cannot provide this cheaply should return 0. + fn approx_num_entries(&self) -> usize { + 0 + } + + /// Approximate weighted size in bytes, callable from synchronous contexts. + /// Used as a `DeepSizeOf` fallback when exact entry traversal is unavailable. + /// Backends that cannot provide this cheaply should return 0. + /// + /// Assumes entries do not share underlying buffers; if they do, the + /// returned total may overcount. + fn approx_size_bytes(&self) -> usize { + 0 + } + + /// Computes the size of the entries currently held in memory. + /// + /// `size_of_entry` threads a shared [`Context`] through each value so + /// allocations shared by multiple entries are counted once. It returns + /// `None` when the value's concrete type was not registered by + /// [`LanceCache`](super::LanceCache); implementations should use the + /// entry's declared eviction size as a fallback in that case. + /// + /// Backends that can enumerate their in-memory entries should include the + /// physical key footprint in the returned total. The default returns + /// `None`, causing `LanceCache` to use [`approx_size_bytes`](Self::approx_size_bytes). + fn deep_size_of_entries( + &self, + _context: &mut Context, + _size_of_entry: &dyn Fn(&CacheEntry, &mut Context) -> Option, + ) -> Option { + None + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/backend_uri.rs b/lance-artifact/rust/lance-core/src/cache/backend_uri.rs new file mode 100644 index 000000000..33daa6adb --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/backend_uri.rs @@ -0,0 +1,265 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! URI-based configuration for cache backends. +//! +//! [`build_from_uri`] parses a compact string form such as +//! `moka://?capacity=1073741824` into a [`BackendConfig`] and hands it to +//! the registry. This gives Python/Java bindings and configuration files a +//! single-string representation of a backend without having to expose a +//! typed builder for every backend. +//! +//! Grammar (intentionally a subset of RFC 3986 — Lance only needs a +//! predictable, unambiguous form): +//! +//! ```text +//! uri ::= scheme ":" hier ( "?" query )? +//! scheme ::= ALPHA ( ALPHA | DIGIT | "+" | "-" | "." )* +//! hier ::= "//" authority path? -- e.g. moka://?..., other:///path?... +//! | path -- e.g. moka:capacity=... (rare) +//! authority ::= *( any char except "/" | "?" ) +//! path ::= *( any char except "?" ) +//! query ::= pair ( "&" pair )* +//! pair ::= key "=" value -- both percent-decoded +//! ``` +//! +//! Mapping to [`BackendConfig`]: +//! +//! * `scheme` becomes `kind`. +//! * The joined `authority + path` (with any leading `//` stripped) is stored +//! under the option key `path` when non-empty. Empty-authority absolute +//! paths such as `backend:///tmp/cache` keep their leading `/`; host-style +//! paths such as `backend://localhost:6379/0` are stored as +//! `localhost:6379/0`. If the query already contains a `path` key, the +//! URI-supplied path wins and the parser errors on the conflict. +//! * Each `key=value` pair from the query becomes an entry in `options`. +//! Duplicate keys are rejected. + +use std::sync::Arc; + +use super::backend::CacheBackend; +use super::registry::{BackendConfig, build_from_config, normalize_backend_kind}; +use crate::{Error, Result}; + +/// Parse `uri` into a [`BackendConfig`] and build the backend registered +/// under its `scheme`. +/// +/// Returns an error if: +/// * the URI cannot be parsed, +/// * no backend is registered for that scheme, or +/// * the constructor itself fails. +pub fn build_from_uri(uri: &str) -> Result> { + let config = parse_backend_uri(uri)?; + build_from_config(&config) +} + +/// Parse `uri` into a [`BackendConfig`] without touching the registry. +/// +/// See the module docs for the accepted grammar. +pub fn parse_backend_uri(uri: &str) -> Result { + let (scheme, rest) = split_scheme(uri)?; + let (path, query) = split_path_query(rest); + + let mut config = BackendConfig::new(&scheme)?; + + let normalized_path = normalize_path(path); + if !normalized_path.is_empty() { + config.options.insert("path".to_string(), normalized_path); + } + + if let Some(query) = query { + for raw_pair in query.split('&') { + if raw_pair.is_empty() { + continue; + } + let (raw_key, raw_value) = raw_pair.split_once('=').ok_or_else(|| { + Error::invalid_input(format!( + "cache backend uri {:?}: query pair {:?} is missing '='", + uri, raw_pair + )) + })?; + let key = percent_decode(raw_key).map_err(|err| { + Error::invalid_input(format!( + "cache backend uri {:?}: cannot decode query key {:?}: {}", + uri, raw_key, err + )) + })?; + let value = percent_decode(raw_value).map_err(|err| { + Error::invalid_input(format!( + "cache backend uri {:?}: cannot decode query value {:?}: {}", + uri, raw_value, err + )) + })?; + if config.options.contains_key(&key) { + return Err(Error::invalid_input(format!( + "cache backend uri {:?}: option {:?} is set more than once", + uri, key + ))); + } + config.options.insert(key, value); + } + } + + Ok(config) +} + +fn split_scheme(uri: &str) -> Result<(String, &str)> { + let colon = uri.find(':').ok_or_else(|| { + Error::invalid_input(format!("cache backend uri {:?} is missing ':'", uri)) + })?; + let scheme = &uri[..colon]; + let scheme = normalize_backend_kind(scheme) + .map_err(|err| Error::invalid_input(format!("cache backend uri {:?}: {}", uri, err)))?; + Ok((scheme, &uri[colon + 1..])) +} + +fn split_path_query(rest: &str) -> (&str, Option<&str>) { + match rest.split_once('?') { + Some((path, query)) => (path, Some(query)), + None => (rest, None), + } +} + +/// Strip leading `//authority/` boilerplate and return the useful path +/// component. Empty authorities (e.g. `moka://`) yield an empty path, while +/// empty-authority absolute paths (e.g. `disk:///tmp/cache`) retain their +/// leading slash. +fn normalize_path(raw: &str) -> String { + let Some(without_marker) = raw.strip_prefix("//") else { + return raw.to_string(); + }; + if without_marker.is_empty() { + return String::new(); + } + without_marker.to_string() +} + +fn percent_decode(input: &str) -> std::result::Result { + let bytes = input.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + match bytes[i] { + b'%' => { + if i + 2 >= bytes.len() { + return Err(format!("truncated percent-escape at offset {}", i)); + } + let hi = decode_hex_digit(bytes[i + 1])?; + let lo = decode_hex_digit(bytes[i + 2])?; + out.push((hi << 4) | lo); + i += 3; + } + b => { + out.push(b); + i += 1; + } + } + } + String::from_utf8(out).map_err(|err| err.to_string()) +} + +fn decode_hex_digit(b: u8) -> std::result::Result { + match b { + b'0'..=b'9' => Ok(b - b'0'), + b'a'..=b'f' => Ok(10 + b - b'a'), + b'A'..=b'F' => Ok(10 + b - b'A'), + _ => Err(format!( + "invalid hex digit {:?} in percent-escape", + b as char + )), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_authority_only() { + let cfg = parse_backend_uri("moka://?capacity=1073741824").unwrap(); + assert_eq!(cfg.kind, "moka"); + assert_eq!( + cfg.options.get("capacity").map(String::as_str), + Some("1073741824") + ); + assert!(!cfg.options.contains_key("path")); + } + + #[test] + fn test_parse_path_and_query() { + let cfg = parse_backend_uri("example:///var/lance/cache?capacity=10G").unwrap(); + assert_eq!(cfg.kind, "example"); + assert_eq!( + cfg.options.get("path").map(String::as_str), + Some("/var/lance/cache") + ); + assert_eq!(cfg.options.get("capacity").map(String::as_str), Some("10G")); + } + + #[test] + fn test_parse_host_style() { + // Redis-style URI with host:port + path segment. All of it lives + // under the "path" option; the backend is responsible for + // interpreting it. + let cfg = parse_backend_uri("redis://localhost:6379/0?prefix=lance").unwrap(); + assert_eq!(cfg.kind, "redis"); + assert_eq!( + cfg.options.get("path").map(String::as_str), + Some("localhost:6379/0"), + ); + assert_eq!(cfg.options.get("prefix").map(String::as_str), Some("lance")); + } + + #[test] + fn test_scheme_is_lowercased() { + // Different upper/lower cases must resolve to the same registry + // key, otherwise `Moka://` and `moka://` would look up different + // backends. + let cfg = parse_backend_uri("MOKA://?capacity=1").unwrap(); + assert_eq!(cfg.kind, "moka"); + } + + #[test] + fn test_percent_decoding() { + let cfg = parse_backend_uri("kv://?prefix=a%2Fb&name=hello%20world&token=a+b%2Bc").unwrap(); + assert_eq!(cfg.options.get("prefix").map(String::as_str), Some("a/b")); + assert_eq!( + cfg.options.get("name").map(String::as_str), + Some("hello world") + ); + assert_eq!(cfg.options.get("token").map(String::as_str), Some("a+b+c")); + } + + #[test] + fn test_empty_query_pair_is_skipped() { + // A trailing "&" should not cause a spurious "" pair to appear. + let cfg = parse_backend_uri("moka://?capacity=1&").unwrap(); + assert_eq!(cfg.options.len(), 1); + } + + #[test] + fn test_missing_scheme_errors() { + let err = parse_backend_uri("no-scheme-here").unwrap_err(); + assert!(err.to_string().contains("missing ':'")); + } + + #[test] + fn test_invalid_scheme_errors() { + // Digit-leading schemes are invalid per RFC 3986 and would clash + // with URI-like values elsewhere in the config. + let err = parse_backend_uri("1moka://").unwrap_err(); + assert!(err.to_string().contains("must start with an ASCII letter")); + } + + #[test] + fn test_duplicate_option_errors() { + let err = parse_backend_uri("moka://?capacity=1&capacity=2").unwrap_err(); + assert!(err.to_string().contains("more than once")); + } + + #[test] + fn test_query_pair_without_equals_errors() { + let err = parse_backend_uri("moka://?capacity").unwrap_err(); + assert!(err.to_string().contains("missing '='")); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/codec.rs b/lance-artifact/rust/lance-core/src/cache/codec.rs new file mode 100644 index 000000000..eff1b5e2b --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/codec.rs @@ -0,0 +1,553 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Serialization codecs for cache entries. +//! +//! Implement [`CacheCodecImpl`] on concrete types, then use +//! [`CacheCodec::from_impl`] to produce a type-erased codec for the cache. +//! +//! # Wire format +//! +//! Every serialized entry begins with a small hand-framed **envelope** so the +//! reader can validate it before trusting the body: +//! +//! ```text +//! [magic: 4B = b"LCE1"] +//! [envelope_version: u8] +//! [type_id_len: u16 LE][type_id: utf8] # stable, author-assigned +//! [type_version: u32 LE] # per-type body schema version +//! +//! ``` +//! +//! The envelope is deliberately *not* protobuf: it is the most +//! stability-critical part, must parse robustly against arbitrary bytes +//! (including data written by older, pre-stabilization builds), and never +//! changes shape. Bodies use protobuf headers, where field-number evolution +//! pays off. +//! +//! # Decode outcome +//! +//! Deserialization never propagates a parse failure as a hard error into the +//! cache path. Anything the reader cannot confidently interpret — absent or +//! wrong magic, an unknown `envelope_version`, a `type_id` mismatch, an +//! unsupported `type_version`, or a body decode error — becomes +//! [`CacheDecode::Miss`]. A backend turns `Miss` into a normal cache miss and +//! recomputes the value. This is what lets data written by an older format +//! self-heal: it simply fails the magic check and is regenerated. + +use std::io::Write; +use std::sync::Arc; + +use bytes::Bytes; + +use crate::{Error, Result}; + +use super::{CacheEntryReader, CacheEntryWriter}; + +// --------------------------------------------------------------------------- +// Envelope +// --------------------------------------------------------------------------- + +/// Magic bytes that prefix every stabilized cache entry. +/// +/// An ASCII tag (`0x4C 0x43 0x45 0x31`) chosen so it cannot collide with any +/// pre-stabilization blob: those began with either a small little-endian +/// length (tens of bytes) or a small tag byte, never these values. +/// +/// Exported so backends can cheaply identify Lance cache entries (e.g. when +/// scanning a persistent store at startup) without hardcoding the bytes — +/// prefer [`has_cache_envelope`] over comparing against this directly. +pub const MAGIC: [u8; 4] = *b"LCE1"; + +/// Returns `true` if `data` begins with the cache-entry [`MAGIC`]. +/// +/// A cheap prefix check for backends that need to recognize Lance cache +/// entries without fully [`deserialize`](CacheCodec::deserialize)-ing them. A +/// `true` result only means the framing looks like ours; the entry can still +/// decode to a [`Miss`](CacheDecode::Miss) (e.g. wrong `type_id`). +pub fn has_cache_envelope(data: &[u8]) -> bool { + data.get(..MAGIC.len()) == Some(&MAGIC[..]) +} + +/// Version of the envelope framing itself. Bumped only if the outer frame +/// (magic/version/type_id/type_version layout) ever changes — expected never. +const ENVELOPE_VERSION: u8 = 1; + +/// Parsed envelope borrowed from the input bytes. +struct ParsedEnvelope<'a> { + type_id: &'a str, + type_version: u32, + /// Offset of the first body byte within the input. + body_offset: usize, +} + +/// Parse and validate the envelope at the start of `data`. +/// +/// Returns `None` for anything that is not a well-formed envelope this build +/// understands (wrong/absent magic, unknown `envelope_version`, truncation, +/// non-utf8 `type_id`). Callers translate `None` into [`CacheDecode::Miss`]. +fn parse_envelope(data: &Bytes) -> Option> { + let bytes = data.as_ref(); + let mut off = 0usize; + + let magic = bytes.get(off..off + 4)?; + if magic != MAGIC { + return None; + } + off += 4; + + if *bytes.get(off)? != ENVELOPE_VERSION { + return None; + } + off += 1; + + let type_id_len = u16::from_le_bytes(bytes.get(off..off + 2)?.try_into().ok()?) as usize; + off += 2; + + let type_id = std::str::from_utf8(bytes.get(off..off + type_id_len)?).ok()?; + off += type_id_len; + + let type_version = u32::from_le_bytes(bytes.get(off..off + 4)?.try_into().ok()?); + off += 4; + + Some(ParsedEnvelope { + type_id, + type_version, + body_offset: off, + }) +} + +/// Write the envelope for `type_id`/`type_version`, returning the number of +/// bytes written (the body's starting offset). +fn write_envelope(writer: &mut dyn Write, type_id: &str, type_version: u32) -> Result { + let type_id_len = u16::try_from(type_id.len()).map_err(|_| { + Error::io(format!( + "cache codec type_id too long ({} bytes, max {})", + type_id.len(), + u16::MAX + )) + })?; + + writer.write_all(&MAGIC)?; + writer.write_all(&[ENVELOPE_VERSION])?; + writer.write_all(&type_id_len.to_le_bytes())?; + writer.write_all(type_id.as_bytes())?; + writer.write_all(&type_version.to_le_bytes())?; + + Ok(4 + 1 + 2 + type_id.len() + 4) +} + +// --------------------------------------------------------------------------- +// CacheDecode — first-class cache-miss outcome +// --------------------------------------------------------------------------- + +/// Why a cache entry could not be decoded into the expected type. +/// +/// Carried by [`CacheDecode::Miss`] so backends can emit targeted metrics +/// (e.g. distinguish "evicting due to a stale format" from "type collision") +/// without re-parsing. Every reason maps to the same behavior — recompute via +/// the loader — so callers that don't care can ignore it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CacheMissReason { + /// Absent or wrong magic, unknown `envelope_version`, truncated framing, or + /// a non-utf8 `type_id`. Typically an entry written by a pre-stabilization + /// or otherwise foreign build. + InvalidEnvelope, + /// Well-formed envelope, but its `type_id` names a different entry type than + /// the codec reading it. + TypeMismatch, + /// Written by a newer build whose `type_version` this build does not + /// understand and must not attempt to interpret. + VersionTooNew, + /// Envelope validated, but the body failed to decode (truncation, a + /// malformed protobuf header, an IPC error, etc.). + BodyError, +} + +/// Outcome of deserializing a cache entry. +/// +/// `Miss` means the bytes could not be confidently decoded into `T`; the +/// [`CacheMissReason`] says why. A backend treats any `Miss` exactly like a key +/// that was never present: recompute via the loader. +#[derive(Debug)] +pub enum CacheDecode { + Hit(T), + Miss(CacheMissReason), +} + +impl CacheDecode { + pub fn hit(self) -> Option { + match self { + Self::Hit(v) => Some(v), + Self::Miss(_) => None, + } + } +} + +// --------------------------------------------------------------------------- +// CacheCodecImpl — trait for serializable cache entry types +// --------------------------------------------------------------------------- + +/// Serialization trait for cache entries. +/// +/// **Experimental**: the serialized format is not yet covered by a stability +/// guarantee and may change between releases. When it does stabilize, the +/// rules are: `TYPE_ID`, protobuf field numbers, and enum values are +/// append-only forever; format changes that protobuf cannot express +/// transparently bump [`CURRENT_VERSION`](Self::CURRENT_VERSION). +/// +/// Implement this on concrete types that need to survive serialization through +/// a persistent cache backend, then wire it into a +/// [`CacheKey`](super::CacheKey) via [`CacheCodec::from_impl`]. +/// +/// The envelope (magic/version/type_id/type_version) is written and validated +/// by the [`CacheCodec`] wrapper. [`serialize`](Self::serialize) writes only +/// the body — a header followed by sections in a fixed, version-keyed order — +/// and [`deserialize`](Self::deserialize) reads them back in that same order. +/// The read sequence mirroring the write sequence for each `type_version` is +/// the invariant the implementor owns. +pub trait CacheCodecImpl: Send + Sync { + /// Stable identity for this entry type. **Must not change once shipped.** + /// This is a deliberate author-assigned string, not `std::any::type_name` + /// (which is not stable across compiler versions). + const TYPE_ID: &'static str; + + /// Body schema version this build writes. Bump when the body layout + /// changes in a way protobuf field additions cannot express transparently + /// (adding/removing/reordering sections, a raw-blob encoding change, etc.). + const CURRENT_VERSION: u32; + + /// Write the body: a header, then sections in a fixed order. + fn serialize(&self, writer: &mut CacheEntryWriter<'_>) -> Result<()>; + + /// Reconstruct from the body. Branch on + /// [`reader.version()`](CacheEntryReader::version) for backward compat; + /// sections are read in write order. + fn deserialize(reader: &mut CacheEntryReader<'_>) -> Result + where + Self: Sized; +} + +// --------------------------------------------------------------------------- +// CacheCodec — type-erased codec passed to backends +// --------------------------------------------------------------------------- + +pub(crate) type ArcAny = Arc; + +/// Type-erased codec for serializing and deserializing cache entries. +/// +/// `CacheCodec` carries the entry's stable `type_id`/`version` plus two plain +/// function pointers — it is `Copy` and has no heap allocation. Construct one +/// via [`CacheCodec::from_impl`] for types that implement [`CacheCodecImpl`], +/// or [`CacheCodec::new`] for custom cases (e.g. when the orphan rule prevents +/// a direct impl). +#[derive(Copy, Clone)] +pub struct CacheCodec { + type_id: &'static str, + version: u32, + serialize_body: fn(&ArcAny, &mut CacheEntryWriter<'_>) -> Result<()>, + deserialize_body: fn(&mut CacheEntryReader<'_>) -> Result, +} + +impl std::fmt::Debug for CacheCodec { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CacheCodec") + .field("type_id", &self.type_id) + .field("version", &self.version) + .finish_non_exhaustive() + } +} + +fn serialize_via_impl( + any: &ArcAny, + writer: &mut CacheEntryWriter<'_>, +) -> Result<()> { + let val = any + .downcast_ref::() + .expect("CacheCodec::serialize called with wrong type (this is a bug in the cache layer)"); + val.serialize(writer) +} + +fn deserialize_via_impl( + reader: &mut CacheEntryReader<'_>, +) -> Result { + let val = T::deserialize(reader)?; + Ok(Arc::new(val) as ArcAny) +} + +impl CacheCodec { + /// Create a `CacheCodec` from explicit body function pointers. + /// + /// Prefer [`from_impl`](Self::from_impl) when the value type implements + /// [`CacheCodecImpl`]. Use this for types where a direct impl isn't + /// possible (e.g. the orphan rule prevents it). `type_id` and `version` + /// play the same role as the corresponding [`CacheCodecImpl`] constants. + pub fn new( + type_id: &'static str, + version: u32, + serialize_body: fn(&ArcAny, &mut CacheEntryWriter<'_>) -> Result<()>, + deserialize_body: fn(&mut CacheEntryReader<'_>) -> Result, + ) -> Self { + Self { + type_id, + version, + serialize_body, + deserialize_body, + } + } + + /// Create a `CacheCodec` from a [`CacheCodecImpl`] implementation. + pub fn from_impl() -> Self { + Self { + type_id: T::TYPE_ID, + version: T::CURRENT_VERSION, + serialize_body: serialize_via_impl::, + deserialize_body: deserialize_via_impl::, + } + } + + /// Return the stable entry type identity. + /// + /// Persistent and tiered backends can use this metadata to route encoded + /// values without retaining a readable logical cache key. + pub const fn type_id(&self) -> &'static str { + self.type_id + } + + /// Serialize `value` into `writer`: envelope first, then the body. + pub fn serialize(&self, value: &ArcAny, writer: &mut dyn Write) -> Result<()> { + let body_offset = write_envelope(writer, self.type_id, self.version)?; + let mut entry_writer = CacheEntryWriter::with_pos(writer, body_offset); + (self.serialize_body)(value, &mut entry_writer) + } + + /// Deserialize an entry from `data`. + /// + /// Never fails: any non-fatal failure to interpret the bytes becomes a + /// [`CacheDecode::Miss`] with the reason why (see [`CacheMissReason`]). + /// Reading from an in-memory [`Bytes`] cannot do I/O, so there is no fault + /// channel — a miss is the only non-`Hit` outcome. + pub fn deserialize(&self, data: &Bytes) -> CacheDecode { + let Some(envelope) = parse_envelope(data) else { + log::debug!("cache entry rejected: missing or invalid envelope"); + return CacheDecode::Miss(CacheMissReason::InvalidEnvelope); + }; + + if envelope.type_id != self.type_id { + log::debug!( + "cache entry type_id mismatch: got {:?}, expected {:?}", + envelope.type_id, + self.type_id + ); + return CacheDecode::Miss(CacheMissReason::TypeMismatch); + } + + // A version newer than this build writes was produced by a newer build + // whose body layout we cannot assume to understand. Older/equal versions + // are the impl's responsibility to handle (branching on reader.version()). + if envelope.type_version > self.version { + log::debug!( + "cache entry {:?} has unsupported type_version {} (this build writes {})", + self.type_id, + envelope.type_version, + self.version + ); + return CacheDecode::Miss(CacheMissReason::VersionTooNew); + } + + let mut reader = CacheEntryReader::new(data, envelope.body_offset, envelope.type_version); + match (self.deserialize_body)(&mut reader) { + Ok(value) => CacheDecode::Hit(value), + Err(e) => { + log::debug!( + "cache entry {:?} v{} failed to decode: {e}", + self.type_id, + envelope.type_version + ); + CacheDecode::Miss(CacheMissReason::BodyError) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A trivial codec used to exercise the envelope and miss semantics + /// without pulling in arrow-backed payloads. + #[derive(Debug, PartialEq)] + struct Widget { + n: u32, + } + + impl CacheCodecImpl for Widget { + const TYPE_ID: &'static str = "test.Widget"; + const CURRENT_VERSION: u32 = 1; + + fn serialize(&self, writer: &mut CacheEntryWriter<'_>) -> Result<()> { + writer.write_raw(&self.n.to_le_bytes()) + } + + fn deserialize(reader: &mut CacheEntryReader<'_>) -> Result { + let bytes = reader.read_raw()?; + let n = u32::from_le_bytes( + bytes + .as_ref() + .try_into() + .map_err(|_| Error::io("bad widget".to_string()))?, + ); + Ok(Self { n }) + } + } + + fn serialize_widget(widget: &Widget) -> Bytes { + let codec = CacheCodec::from_impl::(); + let any: ArcAny = Arc::new(Widget { n: widget.n }); + let mut buf = Vec::new(); + codec.serialize(&any, &mut buf).unwrap(); + Bytes::from(buf) + } + + /// The miss reason, or `None` if the decode was a hit. + fn miss_reason(data: &Bytes) -> Option { + match deserialize_widget(data) { + CacheDecode::Hit(_) => None, + CacheDecode::Miss(reason) => Some(reason), + } + } + + fn deserialize_widget(data: &Bytes) -> CacheDecode { + let codec = CacheCodec::from_impl::(); + match codec.deserialize(data) { + CacheDecode::Hit(any) => { + CacheDecode::Hit(Arc::try_unwrap(any.downcast::().unwrap()).unwrap()) + } + CacheDecode::Miss(reason) => CacheDecode::Miss(reason), + } + } + + #[test] + fn envelope_roundtrip_hits() { + let bytes = serialize_widget(&Widget { n: 0xDEADBEEF }); + // Sanity: the entry starts with the magic. + assert_eq!(&bytes[..4], b"LCE1"); + let decoded = deserialize_widget(&bytes).hit().unwrap(); + assert_eq!(decoded, Widget { n: 0xDEADBEEF }); + } + + #[test] + fn has_cache_envelope_detects_magic() { + let bytes = serialize_widget(&Widget { n: 1 }); + assert!(has_cache_envelope(&bytes)); + assert!(has_cache_envelope(&MAGIC)); // exactly the magic, nothing after + assert!(!has_cache_envelope(b"LCE")); // too short + assert!(!has_cache_envelope(b"JUNK and more")); + assert!(!has_cache_envelope(&[])); + } + + #[test] + fn wrong_magic_is_miss() { + let mut bytes = serialize_widget(&Widget { n: 7 }).to_vec(); + bytes[0] = b'X'; + assert_eq!( + miss_reason(&Bytes::from(bytes)), + Some(CacheMissReason::InvalidEnvelope) + ); + } + + #[test] + fn pre_stabilization_blob_is_miss() { + // An old unstable blob led with a small u64 LE length prefix (a JSON + // header of tens of bytes) — no magic. It must self-heal to a miss. + let mut blob = Vec::new(); + blob.extend_from_slice(&(42u64).to_le_bytes()); + blob.extend_from_slice(&[0u8; 42]); + assert_eq!( + miss_reason(&Bytes::from(blob)), + Some(CacheMissReason::InvalidEnvelope) + ); + + // A different unstable shape led with a small u8 tag (0/1/2). + assert_eq!( + miss_reason(&Bytes::from(vec![0u8, 1, 2, 3])), + Some(CacheMissReason::InvalidEnvelope) + ); + } + + #[test] + fn unknown_envelope_version_is_miss() { + let mut bytes = serialize_widget(&Widget { n: 7 }).to_vec(); + bytes[4] = 0xFF; // envelope_version byte + assert_eq!( + miss_reason(&Bytes::from(bytes)), + Some(CacheMissReason::InvalidEnvelope) + ); + } + + #[test] + fn type_id_mismatch_is_miss() { + // Hand-build an envelope with a foreign type_id but valid framing. + let mut buf = Vec::new(); + write_envelope(&mut buf, "some.OtherType", 1).unwrap(); + buf.extend_from_slice(&(4u64).to_le_bytes()); + buf.extend_from_slice(&99u32.to_le_bytes()); + assert_eq!( + miss_reason(&Bytes::from(buf)), + Some(CacheMissReason::TypeMismatch) + ); + } + + #[test] + fn unsupported_future_type_version_is_miss() { + // An entry written by a newer build (higher type_version) must miss + // rather than be misread by this build. + let mut buf = Vec::new(); + write_envelope(&mut buf, Widget::TYPE_ID, Widget::CURRENT_VERSION + 1).unwrap(); + lance_arrow::ipc::write_len_prefixed_bytes(&mut buf, &9u32.to_le_bytes()).unwrap(); + assert_eq!( + miss_reason(&Bytes::from(buf)), + Some(CacheMissReason::VersionTooNew) + ); + } + + #[test] + fn truncated_envelope_is_miss() { + let bytes = serialize_widget(&Widget { n: 7 }); + for cut in [0, 1, 4, 5, 7, 9] { + assert_eq!( + miss_reason(&bytes.slice(..cut.min(bytes.len()))), + Some(CacheMissReason::InvalidEnvelope), + "truncating to {cut} bytes should miss as InvalidEnvelope" + ); + } + } + + #[test] + fn body_decode_error_is_miss() { + // Valid envelope, but the body is too short for the widget. + let mut buf = Vec::new(); + write_envelope(&mut buf, Widget::TYPE_ID, Widget::CURRENT_VERSION).unwrap(); + buf.extend_from_slice(&(1u64).to_le_bytes()); + buf.push(0u8); + assert_eq!( + miss_reason(&Bytes::from(buf)), + Some(CacheMissReason::BodyError) + ); + } + + #[test] + fn reader_exposes_envelope_version() { + // type_version travels through the envelope to reader.version(). + let mut buf = Vec::new(); + write_envelope(&mut buf, Widget::TYPE_ID, 7).unwrap(); + let body_off = buf.len(); + // A widget body so the codec can decode it. + lance_arrow::ipc::write_len_prefixed_bytes(&mut buf, &5u32.to_le_bytes()).unwrap(); + let data = Bytes::from(buf); + + let mut r = CacheEntryReader::new(&data, body_off, 7); + assert_eq!(r.version(), 7); + assert_eq!(r.read_raw().unwrap().as_ref(), 5u32.to_le_bytes()); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/entry_io.rs b/lance-artifact/rust/lance-core/src/cache/entry_io.rs new file mode 100644 index 000000000..fe91b11ca --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/entry_io.rs @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Streaming readers/writers for cache entry bodies. +//! +//! [`CacheCodecImpl`](super::CacheCodecImpl) bodies are written and read +//! through these wrappers. They keep serialization streaming (no buffering of +//! the whole entry) and reads zero-copy (sections borrow from the input +//! [`Bytes`]), while tracking the byte position needed to keep Arrow IPC +//! sections 64-byte aligned (see [`lance_arrow::ipc`]). +//! +//! Body layout primitives: +//! +//! ```text +//! HEADER : [header_len: u32 LE][header proto bytes] +//! ARROW_IPC : [pad to 64B][self-delimiting IPC stream] +//! RAW_BLOB : [len: u64 LE][bytes] +//! ``` + +use std::io::Write; + +use arrow_array::RecordBatch; +use bytes::Bytes; +use prost::Message; + +use crate::{Error, Result}; + +/// Writes a cache entry body: a header followed by sections, streaming +/// directly to the underlying writer. +/// +/// The envelope is written by the [`CacheCodec`](super::CacheCodec) wrapper +/// before this writer is handed to +/// [`CacheCodecImpl::serialize`](super::CacheCodecImpl::serialize). +pub struct CacheEntryWriter<'a> { + writer: &'a mut dyn Write, + /// Absolute byte offset within the entry, used to align IPC sections. + pos: usize, +} + +impl<'a> CacheEntryWriter<'a> { + /// Create a writer positioned at the start of an entry (offset 0). + /// + /// Use this for nested serialization into a standalone buffer. The + /// envelope-aware entry point is [`CacheCodec::serialize`](super::CacheCodec::serialize). + pub fn new(writer: &'a mut dyn Write) -> Self { + Self { writer, pos: 0 } + } + + /// Create a writer whose section alignment accounts for `pos` bytes + /// already written ahead of the body (i.e. the envelope). + pub(crate) fn with_pos(writer: &'a mut dyn Write, pos: usize) -> Self { + Self { writer, pos } + } + + /// Write a single discriminant byte (e.g. a variant tag). + pub fn write_u8(&mut self, value: u8) -> Result<()> { + self.writer.write_all(&[value])?; + self.pos += 1; + Ok(()) + } + + /// Write a protobuf header as `[len: u32 LE][bytes]`. + pub fn write_header(&mut self, header: &P) -> Result<()> { + let bytes = header.encode_to_vec(); + let len = u32::try_from(bytes.len()) + .map_err(|_| Error::io(format!("cache header too large: {} bytes", bytes.len())))?; + self.writer.write_all(&len.to_le_bytes())?; + self.writer.write_all(&bytes)?; + self.pos += 4 + bytes.len(); + Ok(()) + } + + /// Write `batch` as a 64-byte-aligned Arrow IPC section. + pub fn write_ipc(&mut self, batch: &RecordBatch) -> Result<()> { + lance_arrow::ipc::write_ipc_section(self.writer, &mut self.pos, batch) + .map_err(|e| Error::io(e.to_string())) + } + + /// Write `batches` as a single 64-byte-aligned multi-batch Arrow IPC + /// section. The iterator must yield at least one batch. + pub fn write_ipc_batches(&mut self, batches: I) -> Result<()> + where + I: IntoIterator, + { + lance_arrow::ipc::write_ipc_section_batches(self.writer, &mut self.pos, batches) + .map_err(|e| Error::io(e.to_string())) + } + + /// Write a raw blob as `[len: u64 LE][bytes]`. + /// + /// Only for byte payloads that already have their own stable, portable + /// encoding (e.g. a roaring bitmap, a varint-packed stream). + pub fn write_raw(&mut self, bytes: &[u8]) -> Result<()> { + lance_arrow::ipc::write_len_prefixed_bytes(self.writer, bytes) + .map_err(|e| Error::io(e.to_string()))?; + self.pos += 8 + bytes.len(); + Ok(()) + } + + /// The underlying writer, for a payload that carries its own framing. + /// + /// Use this only when the codec writes a self-delimiting or whole-body + /// payload — e.g. streaming a roaring bitmap as the entire body, where the + /// length prefix of [`write_raw`](Self::write_raw) would be redundant and + /// buffering to measure that length would force an extra copy. For + /// structured bodies prefer [`write_header`](Self::write_header) / + /// [`write_ipc`](Self::write_ipc) / [`write_raw`](Self::write_raw), which + /// give you versioning and 64-byte IPC alignment. + /// + /// Bytes written through this do **not** advance the section-alignment + /// position, so it must not be interleaved with [`write_ipc`](Self::write_ipc). + pub fn raw_writer(&mut self) -> &mut dyn Write { + self.writer + } +} + +/// Reads a cache entry body, tracking an offset into the input and exposing +/// the entry's `type_version` so implementors can branch for backward compat. +/// +/// All reads are zero-copy: returned [`Bytes`] and the buffers behind decoded +/// [`RecordBatch`]es borrow from the input allocation. +pub struct CacheEntryReader<'a> { + data: &'a Bytes, + offset: usize, + version: u32, +} + +impl<'a> CacheEntryReader<'a> { + /// Create a reader over `data`, starting at body byte `offset`, for an + /// entry written at `version`. + pub fn new(data: &'a Bytes, offset: usize, version: u32) -> Self { + Self { + data, + offset, + version, + } + } + + /// The `type_version` from the envelope. Branch on this for backward compat. + pub fn version(&self) -> u32 { + self.version + } + + /// Read a single discriminant byte written by [`CacheEntryWriter::write_u8`]. + pub fn read_u8(&mut self) -> Result { + let bytes = self.data.as_ref(); + let v = *bytes + .get(self.offset) + .ok_or_else(|| Error::io("cache entry: truncated, missing tag byte".to_string()))?; + self.offset += 1; + Ok(v) + } + + /// Read a protobuf header written by [`CacheEntryWriter::write_header`]. + pub fn read_header(&mut self) -> Result

    { + let bytes = self.data.as_ref(); + let len_end = self + .offset + .checked_add(4) + .filter(|&e| e <= bytes.len()) + .ok_or_else(|| Error::io("cache header: truncated length prefix".to_string()))?; + let len = u32::from_le_bytes(bytes[self.offset..len_end].try_into().unwrap()) as usize; + let data_end = len_end + .checked_add(len) + .filter(|&e| e <= bytes.len()) + .ok_or_else(|| Error::io("cache header: truncated body".to_string()))?; + let msg = P::decode(&bytes[len_end..data_end]) + .map_err(|e| Error::io(format!("cache header decode failed: {e}")))?; + self.offset = data_end; + Ok(msg) + } + + /// Read one [`RecordBatch`] from a 64-byte-aligned IPC section. + pub fn read_ipc(&mut self) -> Result { + lance_arrow::ipc::read_ipc_section_at(self.data, &mut self.offset) + .map_err(|e| Error::io(e.to_string())) + } + + /// Read all [`RecordBatch`]es from a 64-byte-aligned multi-batch IPC + /// section written by [`CacheEntryWriter::write_ipc_batches`]. + pub fn read_ipc_batches(&mut self) -> Result> { + lance_arrow::ipc::read_ipc_section_batches_at(self.data, &mut self.offset) + .map_err(|e| Error::io(e.to_string())) + } + + /// Read a raw blob written by [`CacheEntryWriter::write_raw`], zero-copy. + pub fn read_raw(&mut self) -> Result { + lance_arrow::ipc::read_len_prefixed_bytes_at(self.data, &mut self.offset) + .map_err(|e| Error::io(e.to_string())) + } + + /// The not-yet-consumed body bytes as a zero-copy slice. + /// + /// For a payload that carries its own framing and is parsed with the + /// codec's own cursor — the read counterpart of + /// [`CacheEntryWriter::raw_writer`]. For structured bodies prefer + /// [`read_header`](Self::read_header) / [`read_ipc`](Self::read_ipc) / + /// [`read_raw`](Self::read_raw). + pub fn body(&self) -> Bytes { + self.data.slice(self.offset..) + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/key.rs b/lance-artifact/rust/lance-core/src/cache/key.rs new file mode 100644 index 000000000..b7687d264 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/key.rs @@ -0,0 +1,476 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Canonical fixed-size cache key construction. +//! +//! Cache keys are BLAKE3 digests truncated to 128 bits. Logical key fields are +//! encoded with explicit type tags, fixed-width little-endian integers, and +//! length framing for variable-width values. This makes the pre-hash encoding +//! unambiguous and stable across processes, platforms, and builds. +//! +//! The digest is a cache identity, not an authentication or access-control +//! primitive: namespace derivation keys are deterministic and not secret. +//! Truncating to 128 bits gives generic birthday resistance of approximately +//! 64 bits. This protocol does not introduce a FIPS mode; BLAKE3 is the +//! repository's selected cache-key algorithm. + +use std::fmt; + +/// Storage namespace identifier for canonical cache keys. +/// +/// Persistent backends should include this identifier in their physical +/// namespace so future algorithm or framing changes produce cold misses. +pub const CACHE_KEY_FORMAT: &str = "blake3-128-v1"; + +const KEY_FORMAT_VERSION: u32 = 1; +const NAMESPACE_CONTEXT: &str = "lance-format/lance 2026-07-17 cache namespace v1"; +const NAMESPACE_DOMAIN: &[u8] = b"lance-cache-namespace\0"; +const ENTRY_DOMAIN: &[u8] = b"lance-cache-entry\0"; + +/// One-byte type discriminants in the stable key encoding. +#[derive(Clone, Copy)] +#[repr(u8)] +enum FieldTag { + U8 = 1, + U16 = 2, + U32 = 3, + U64 = 4, + I32 = 5, + I64 = 6, + Bool = 7, + Str = 8, + Bytes = 9, + FixedBytes = 10, + None = 11, + Some = 12, + Variant = 13, + Sequence = 14, +} + +impl FieldTag { + const fn as_u8(self) -> u8 { + self as u8 + } +} + +/// Versioned schema identity for fields emitted by a cache key. +/// +/// Change the version whenever the encoded fields or their meaning changes. +/// The identifier must be stable and globally unique to the logical layout. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct CacheKeySchema { + id: &'static str, + version: u32, +} + +impl CacheKeySchema { + /// Compatibility schema used by the default string-key bridge. + pub const LEGACY_TEXT: Self = Self::new("lance.cache.legacy-text", 1); + + /// Create a stable schema identifier and encoding version. + pub const fn new(id: &'static str, version: u32) -> Self { + Self { id, version } + } + + /// Return the author-assigned schema identifier. + pub const fn id(self) -> &'static str { + self.id + } + + /// Return the schema encoding version. + pub const fn version(self) -> u32 { + self.version + } +} + +/// Opaque 128-bit key passed to cache backends. +/// +/// The byte representation is canonical. It can be persisted directly and is +/// independent of the host's native integer endianness. +#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct InternalCacheKey([u8; 16]); + +impl InternalCacheKey { + /// Reconstruct a key from its canonical bytes. + pub const fn from_bytes(bytes: [u8; 16]) -> Self { + Self(bytes) + } + + /// Borrow the canonical byte representation. + pub const fn as_bytes(&self) -> &[u8; 16] { + &self.0 + } + + /// Consume the key and return its canonical bytes. + pub const fn into_bytes(self) -> [u8; 16] { + self.0 + } +} + +impl fmt::Debug for InternalCacheKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("InternalCacheKey(")?; + for byte in self.0 { + write!(f, "{byte:02x}")?; + } + f.write_str(")") + } +} + +/// Pre-derived namespace key shared by entries in one logical cache scope. +#[derive(Clone, Copy, Debug)] +pub struct CacheNamespace([u8; 32]); + +impl CacheNamespace { + /// Construct the stable root namespace. + pub fn root() -> Self { + Self(blake3::derive_key(NAMESPACE_CONTEXT, b"")) + } + + /// Derive a child namespace from one framed hierarchy segment. + pub fn child(self, segment: &str) -> Self { + let mut hasher = blake3::Hasher::new_keyed(&self.0); + write_framed(&mut hasher, NAMESPACE_DOMAIN); + hasher.update(&KEY_FORMAT_VERSION.to_le_bytes()); + write_framed(&mut hasher, segment.as_bytes()); + Self(hasher.finalize().into()) + } +} + +/// Streams typed logical fields into a canonical cache key. +/// +/// Integer methods use little-endian fixed-width encoding. Variable-width +/// strings and bytes are type-tagged and length-prefixed. There is deliberately +/// no `usize` method because cache identities must not depend on target width. +/// +/// # Examples +/// +/// ``` +/// use lance_core::cache::{CacheKeySchema, CacheNamespace, KeyBuilder}; +/// +/// let namespace = CacheNamespace::root().child("dataset"); +/// let mut builder = KeyBuilder::new( +/// namespace, +/// "example.Page", +/// CacheKeySchema::new("example.page-key", 1), +/// ); +/// builder.write_u32(7); +/// builder.write_str("values"); +/// let key = builder.finish(); +/// assert_eq!(key.as_bytes().len(), 16); +/// ``` +pub struct KeyBuilder { + hasher: blake3::Hasher, +} + +impl KeyBuilder { + /// Start a key in a namespace with a stable value type and key schema. + pub fn new( + namespace: CacheNamespace, + stable_type_id: &'static str, + schema: CacheKeySchema, + ) -> Self { + let mut hasher = blake3::Hasher::new_keyed(&namespace.0); + write_framed(&mut hasher, ENTRY_DOMAIN); + hasher.update(&KEY_FORMAT_VERSION.to_le_bytes()); + write_framed(&mut hasher, stable_type_id.as_bytes()); + write_framed(&mut hasher, schema.id().as_bytes()); + hasher.update(&schema.version().to_le_bytes()); + Self { hasher } + } + + /// Append a tagged, fixed-width `u8`. + #[inline] + pub fn write_u8(&mut self, value: u8) { + self.hasher.update(&[FieldTag::U8.as_u8(), value]); + } + + /// Append a tagged, little-endian `u16`. + #[inline] + pub fn write_u16(&mut self, value: u16) { + let mut encoded = [0; 3]; + encoded[0] = FieldTag::U16.as_u8(); + encoded[1..].copy_from_slice(&value.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append a tagged, little-endian `u32`. + #[inline] + pub fn write_u32(&mut self, value: u32) { + let mut encoded = [0; 5]; + encoded[0] = FieldTag::U32.as_u8(); + encoded[1..].copy_from_slice(&value.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append a tagged, little-endian `u64`. + #[inline] + pub fn write_u64(&mut self, value: u64) { + let mut encoded = [0; 9]; + encoded[0] = FieldTag::U64.as_u8(); + encoded[1..].copy_from_slice(&value.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append a tagged, little-endian `i32`. + #[inline] + pub fn write_i32(&mut self, value: i32) { + let mut encoded = [0; 5]; + encoded[0] = FieldTag::I32.as_u8(); + encoded[1..].copy_from_slice(&value.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append a tagged, little-endian `i64`. + #[inline] + pub fn write_i64(&mut self, value: i64) { + let mut encoded = [0; 9]; + encoded[0] = FieldTag::I64.as_u8(); + encoded[1..].copy_from_slice(&value.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append a tagged boolean. + #[inline] + pub fn write_bool(&mut self, value: bool) { + self.hasher + .update(&[FieldTag::Bool.as_u8(), u8::from(value)]); + } + + /// Append a tagged, length-prefixed UTF-8 string. + #[inline] + pub fn write_str(&mut self, value: &str) { + self.write_variable(FieldTag::Str, value.as_bytes()); + } + + /// Append tagged, length-prefixed bytes. + #[inline] + pub fn write_bytes(&mut self, value: &[u8]) { + self.write_variable(FieldTag::Bytes, value); + } + + /// Append a tagged fixed-size byte array, including its length. + #[inline] + pub fn write_fixed_bytes(&mut self, value: &[u8; N]) { + self.write_variable(FieldTag::FixedBytes, value); + } + + /// Append the canonical marker for an absent optional value. + #[inline] + pub fn write_none(&mut self) { + self.hasher.update(&[FieldTag::None.as_u8()]); + } + + /// Append the canonical marker for a present optional value. + #[inline] + pub fn write_some(&mut self) { + self.hasher.update(&[FieldTag::Some.as_u8()]); + } + + /// Append a tagged enum variant ordinal. + #[inline] + pub fn write_variant(&mut self, variant: u32) { + let mut encoded = [0; 5]; + encoded[0] = FieldTag::Variant.as_u8(); + encoded[1..].copy_from_slice(&variant.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Append the length of a following sequence. + #[inline] + pub fn write_sequence_len(&mut self, len: u64) { + let mut encoded = [0; 9]; + encoded[0] = FieldTag::Sequence.as_u8(); + encoded[1..].copy_from_slice(&len.to_le_bytes()); + self.hasher.update(&encoded); + } + + /// Finalize and return the canonical 128-bit key. + #[inline] + pub fn finish(self) -> InternalCacheKey { + let hash = self.hasher.finalize(); + let mut bytes = [0; 16]; + bytes.copy_from_slice(&hash.as_bytes()[..16]); + InternalCacheKey(bytes) + } + + #[inline] + fn write_variable(&mut self, tag: FieldTag, value: &[u8]) { + self.hasher.update(&[tag.as_u8()]); + self.hasher.update(&encoded_len(value)); + self.hasher.update(value); + } +} + +#[inline] +fn write_framed(hasher: &mut blake3::Hasher, value: &[u8]) { + hasher.update(&encoded_len(value)); + hasher.update(value); +} + +#[inline] +fn encoded_len(value: &[u8]) -> [u8; 8] { + (value.len() as u64).to_le_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + + const SCHEMA: CacheKeySchema = CacheKeySchema::new("test.key", 1); + + fn builder() -> KeyBuilder { + KeyBuilder::new( + CacheNamespace::root().child("s3://bucket/dataset"), + "test.Value", + SCHEMA, + ) + } + + fn key_with(write: impl FnOnce(&mut KeyBuilder)) -> InternalCacheKey { + let mut key = builder(); + write(&mut key); + key.finish() + } + + #[test] + fn key_and_namespace_have_fixed_sizes() { + assert_eq!(std::mem::size_of::(), 16); + assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 1); + } + + #[test] + fn blake3_matches_official_empty_keyed_hash_vector() { + let key = *b"whats the Elvish word for friend"; + assert_eq!( + blake3::keyed_hash(&key, b"").as_bytes(), + &[ + 0x92, 0xb2, 0xb7, 0x56, 0x04, 0xed, 0x3c, 0x76, 0x1f, 0x9d, 0x6f, 0x62, 0x39, 0x2c, + 0x8a, 0x92, 0x27, 0xad, 0x0e, 0xa3, 0xf0, 0x95, 0x73, 0xe7, 0x83, 0xf1, 0x49, 0x8a, + 0x4e, 0xd6, 0x0d, 0x26, + ] + ); + } + + #[test] + fn typed_fields_and_boundaries_are_unambiguous() { + let cases = [ + key_with(|key| { + key.write_str("ab"); + key.write_str("c"); + }), + key_with(|key| { + key.write_str("a"); + key.write_str("bc"); + }), + key_with(|key| key.write_str("")), + key_with(|key| key.write_bytes(b"")), + key_with(|key| key.write_fixed_bytes(b"")), + key_with(|key| key.write_u8(1)), + key_with(|key| key.write_u16(1)), + key_with(|key| key.write_u32(1)), + key_with(|key| key.write_u64(1)), + key_with(|key| key.write_i32(1)), + key_with(|key| key.write_i64(1)), + key_with(|key| key.write_bool(false)), + key_with(|key| key.write_bool(true)), + key_with(KeyBuilder::write_none), + key_with(KeyBuilder::write_some), + key_with(|key| key.write_variant(0)), + key_with(|key| key.write_variant(1)), + ]; + assert_eq!(std::collections::BTreeSet::from(cases).len(), cases.len()); + + assert_ne!( + key_with(|key| { + key.write_sequence_len(2); + key.write_u32(1); + key.write_u32(2); + }), + key_with(|key| { + key.write_u32(1); + key.write_u32(2); + }) + ); + } + + #[test] + fn namespace_type_schema_and_version_are_domain_separated() { + let root = CacheNamespace::root(); + let namespace = root.child("dataset").child("index"); + let nested = KeyBuilder::new(namespace, "test.Value", SCHEMA).finish(); + let combined = KeyBuilder::new(root.child("dataset/index"), "test.Value", SCHEMA).finish(); + assert_ne!(nested, combined); + + assert_ne!( + nested, + KeyBuilder::new(namespace, "test.OtherValue", SCHEMA).finish() + ); + assert_ne!( + nested, + KeyBuilder::new( + namespace, + "test.Value", + CacheKeySchema::new("test.other-key", 1), + ) + .finish() + ); + assert_ne!( + nested, + KeyBuilder::new(namespace, "test.Value", CacheKeySchema::new("test.key", 2),).finish() + ); + + let tenant_a_memory = + KeyBuilder::new(root.child("tenant-a").child("memory"), "test.Value", SCHEMA).finish(); + assert_ne!( + tenant_a_memory, + KeyBuilder::new(root.child("tenant-b").child("memory"), "test.Value", SCHEMA,).finish() + ); + assert_ne!( + tenant_a_memory, + KeyBuilder::new( + root.child("tenant-a").child("persistent"), + "test.Value", + SCHEMA, + ) + .finish() + ); + } + + #[test] + fn integers_use_fixed_width_little_endian_encoding() { + let namespace = CacheNamespace::root().child("endianness"); + let mut key = KeyBuilder::new(namespace, "test.Value", SCHEMA); + key.write_u32(0x0102_0304); + let actual = key.finish(); + + let mut reference = blake3::Hasher::new_keyed(&namespace.0); + write_framed(&mut reference, ENTRY_DOMAIN); + reference.update(&KEY_FORMAT_VERSION.to_le_bytes()); + write_framed(&mut reference, b"test.Value"); + write_framed(&mut reference, SCHEMA.id().as_bytes()); + reference.update(&SCHEMA.version().to_le_bytes()); + reference.update(&[FieldTag::U32.as_u8(), 0x04, 0x03, 0x02, 0x01]); + let mut expected = [0; 16]; + expected.copy_from_slice(&reference.finalize().as_bytes()[..16]); + + assert_eq!(actual, InternalCacheKey::from_bytes(expected)); + } + + #[test] + fn key_has_stable_golden_vector() { + let mut key = builder(); + key.write_u32(7); + key.write_str("page"); + key.write_some(); + key.write_fixed_bytes(&[0xAB; 16]); + assert_eq!( + key.finish().into_bytes(), + [ + 0xc4, 0x38, 0xff, 0x22, 0x30, 0x55, 0x30, 0xfc, 0x74, 0x16, 0x38, 0xe9, 0x7d, 0x45, + 0xa5, 0x68, + ] + ); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/mod.rs b/lance-artifact/rust/lance-core/src/cache/mod.rs new file mode 100644 index 000000000..bdcfa55d2 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/mod.rs @@ -0,0 +1,1435 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Lance cache system. +//! +//! ## For cache users +//! +//! Use [`LanceCache`] (or [`WeakLanceCache`]) to store and retrieve typed +//! values. Define a [`CacheKey`] (or [`UnsizedCacheKey`] for trait objects) to +//! describe what you're caching and its type. +//! +//! To make a value type serializable (so persistent backends can store it), +//! implement [`CacheCodecImpl`] on the type, then override [`CacheKey::codec`]: +//! +//! ```ignore +//! impl CacheCodecImpl for MyData { +//! fn serialize(&self, w: &mut dyn Write) -> Result<()> { /* ... */ } +//! fn deserialize(data: &Bytes) -> Result { /* ... */ } +//! } +//! +//! impl CacheKey for MyDataKey { +//! type ValueType = MyData; +//! fn key(&self) -> Cow<'_, str> { /* ... */ } +//! fn type_name() -> &'static str { "MyData" } +//! fn codec() -> Option { +//! Some(CacheCodec::from_impl::()) +//! } +//! } +//! ``` +//! +//! ## For backend implementors +//! +//! Implement [`CacheBackend`] to provide a custom storage layer (disk, Redis, +//! etc.). Backends receive opaque, fixed-size [`InternalCacheKey`] values and +//! type-erased [`CacheEntry`] values. The typed wrapping is handled by +//! [`LanceCache`]. See the [`backend`] module for migration details. +//! +//! ## Serialization flow +//! +//! When a [`CacheKey`] provides a codec via [`CacheKey::codec`]: +//! +//! 1. [`LanceCache`] wraps the [`CacheCodec`] and passes it to the backend +//! alongside the entry on `insert` and `get` calls. +//! 2. In-memory backends (like [`MokaCacheBackend`]) ignore the codec. +//! 3. Persistent backends use `codec.serialize(entry, writer)` on insert and +//! `codec.deserialize(reader)` on get to persist entries across restarts. + +pub mod backend; +mod backend_uri; +pub mod codec; +mod entry_io; +mod key; +mod moka; +mod quick; +mod registry; + +pub use backend::{CacheBackend, CacheEntry}; +pub use backend_uri::{build_from_uri, parse_backend_uri}; +pub use codec::{ + CacheCodec, CacheCodecImpl, CacheDecode, CacheMissReason, MAGIC, has_cache_envelope, +}; +pub use entry_io::{CacheEntryReader, CacheEntryWriter}; +pub use key::{CACHE_KEY_FORMAT, CacheKeySchema, CacheNamespace, InternalCacheKey, KeyBuilder}; +pub use moka::MokaCacheBackend; +pub use quick::{QuickCacheBackend, recommended_cache_shards}; +pub use registry::{BackendBuildFn, BackendConfig, build_from_config, register_backend}; + +use std::any::TypeId; +use std::borrow::Cow; +use std::collections::HashMap; +use std::sync::{ + Arc, RwLock, Weak, + atomic::{AtomicU64, Ordering}, +}; + +use futures::Future; + +use crate::{Error, Result}; + +pub use crate::deepsize::{Context, DeepSizeOf}; + +// --------------------------------------------------------------------------- +// CacheKey / UnsizedCacheKey — typed key traits for cache users +// --------------------------------------------------------------------------- + +/// Typed cache key for sized value types. +/// +/// Existing implementations can continue returning a logical string from +/// [`key`](Self::key). Performance-sensitive implementations should also +/// provide a stable schema and stream typed fields through +/// [`write_key`](Self::write_key), avoiding construction of that string. +/// +/// # Example +/// +/// ```ignore +/// struct MyKey { id: u64 } +/// +/// impl CacheKey for MyKey { +/// type ValueType = MyData; +/// fn key(&self) -> Cow<'_, str> { self.id.to_string().into() } +/// fn type_name() -> &'static str { "MyData" } +/// } +/// ``` +pub trait CacheKey { + type ValueType: 'static; + + fn key(&self) -> Cow<'_, str>; + + /// Short, stable string identifying this value type. + /// + /// Two `CacheKey` impls that store different `ValueType`s **must** return + /// different type names. + /// + /// Use a short literal (e.g. `"Vec"`), not + /// `std::any::type_name` — the latter is not guaranteed stable across + /// compiler versions or build configurations. + fn type_name() -> &'static str; + + /// Stable identity included in the physical key. + /// + /// The compatibility default preserves existing implementations by using + /// their author-assigned [`type_name`](Self::type_name). + fn stable_type_id() -> &'static str { + Self::type_name() + } + + /// Versioned schema for the logical key fields. + fn schema() -> CacheKeySchema { + CacheKeySchema::LEGACY_TEXT + } + + /// Stream the logical key fields into the canonical key builder. + /// + /// The compatibility default hashes the existing string key. In-tree hot + /// paths override this with typed, allocation-free field encoding. + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_str(self.key().as_ref()); + } + + /// Optional codec for serializing/deserializing this key's value type. + /// + /// Returns `None` by default. Cache backends that support persistence + /// (e.g. disk-backed caches) use this to serialize entries on insert and + /// deserialize on get. Types without a codec will only be stored in-memory. + /// + /// [`CacheCodec`] is `Copy` (two plain function pointers), so returning it + /// by value is cheap — no allocation needed. + fn codec() -> Option { + None + } +} + +/// Like [`CacheKey`] but for unsized value types (e.g. `dyn Trait`). +/// +/// The cache wraps values in an extra `Arc` layer internally; callers pass +/// and receive `Arc` where `T: ?Sized`. +/// +/// Unsized cache entries are always in-memory only (no serialization codec). +/// For serializable entries, use a sized [`CacheKey`] instead. +pub trait UnsizedCacheKey { + type ValueType: 'static + ?Sized; + + fn key(&self) -> Cow<'_, str>; + + /// Short, stable string identifying this value type. + /// See [`CacheKey::type_name`] for requirements. + fn type_name() -> &'static str; + + /// Stable identity included in the physical key. + fn stable_type_id() -> &'static str { + Self::type_name() + } + + /// Versioned schema for the logical key fields. + fn schema() -> CacheKeySchema { + CacheKeySchema::LEGACY_TEXT + } + + /// Stream the logical key fields into the canonical key builder. + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_str(self.key().as_ref()); + } +} + +// --------------------------------------------------------------------------- +// Internal helpers +// --------------------------------------------------------------------------- + +/// Size of a cached `Arc`, accounting for the Arc overhead (two atomic counters). +fn cache_entry_size(value: &T) -> usize { + value.deep_size_of() + std::mem::size_of::() * 2 +} + +type CacheEntrySizeAccessor = fn(&CacheEntry, &mut Context) -> Option; + +fn cache_entry_size_with_context(entry: &CacheEntry, context: &mut Context) -> Option +where + T: DeepSizeOf + Send + Sync + 'static, +{ + let value = entry.downcast_ref::()?; + let entry_ptr = Arc::as_ptr(entry) as *const () as usize; + if !context.mark_seen(entry_ptr) { + return Some(0); + } + Some( + std::mem::size_of_val(value) + + value.deep_size_of_children(context) + + std::mem::size_of::() * 2, + ) +} + +#[derive(Debug)] +struct CacheState { + backend: Arc, + hits: AtomicU64, + misses: AtomicU64, + entry_size_accessors: RwLock>, +} + +impl CacheState { + fn new(backend: Arc) -> Self { + Self { + backend, + hits: AtomicU64::new(0), + misses: AtomicU64::new(0), + entry_size_accessors: RwLock::new(HashMap::new()), + } + } + + fn entry_size(&self, value: &T) -> usize + where + T: DeepSizeOf + Send + Sync + 'static, + { + let type_id = TypeId::of::(); + let is_registered = self + .entry_size_accessors + .read() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .contains_key(&type_id); + if !is_registered { + self.entry_size_accessors + .write() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .entry(type_id) + .or_insert(cache_entry_size_with_context::); + } + cache_entry_size(value) + } +} + +// --------------------------------------------------------------------------- +// LanceCache — typed wrapper around dyn CacheBackend +// --------------------------------------------------------------------------- + +/// Typed cache wrapper that handles key construction and type safety. +/// +/// Internally delegates to a [`CacheBackend`]. The default backend is +/// [`MokaCacheBackend`]; pass a custom backend via [`LanceCache::with_backend`]. +#[derive(Clone)] +pub struct LanceCache { + state: Arc, + namespace: key::CacheNamespace, +} + +impl std::fmt::Debug for LanceCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("LanceCache") + .field("backend", &self.state.backend) + .finish_non_exhaustive() + } +} + +impl DeepSizeOf for LanceCache { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + let state_ptr = Arc::as_ptr(&self.state) as usize; + if !context.mark_seen(state_ptr) { + return 0; + } + + let accessors = self + .state + .entry_size_accessors + .read() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .clone(); + self.state + .backend + .deep_size_of_entries(context, &|entry, context| { + accessors + .get(&entry.as_ref().type_id()) + .and_then(|size_of_entry| size_of_entry(entry, context)) + }) + .unwrap_or_else(|| self.state.backend.approx_size_bytes()) + } +} + +impl LanceCache { + pub fn with_capacity(capacity: usize) -> Self { + Self::with_backend(Arc::new(MokaCacheBackend::with_capacity(capacity))) + } + + /// Create a cache backed by a custom [`CacheBackend`]. + pub fn with_backend(backend: Arc) -> Self { + Self { + state: Arc::new(CacheState::new(backend)), + namespace: key::CacheNamespace::root(), + } + } + + pub fn no_cache() -> Self { + Self::with_backend(Arc::new(MokaCacheBackend::no_cache())) + } + + /// Derive a child namespace for all keys in the returned cache handle. + /// + /// Each call adds one framed hierarchy segment. Consequently, + /// `cache.with_key_prefix("a").with_key_prefix("b")` is deliberately + /// distinct from `cache.with_key_prefix("a/b")`. + pub fn with_key_prefix(&self, prefix: &str) -> Self { + Self { + state: self.state.clone(), + namespace: self.namespace.child(prefix), + } + } + + pub async fn size(&self) -> usize { + self.state.backend.num_entries().await + } + + pub fn approx_size(&self) -> usize { + self.state.backend.approx_num_entries() + } + + pub async fn size_bytes(&self) -> usize { + self.state.backend.size_bytes().await + } + + // -- Stats / clear -------------------------------------------------------- + + pub async fn stats(&self) -> CacheStats { + CacheStats { + hits: self.state.hits.load(Ordering::Relaxed), + misses: self.state.misses.load(Ordering::Relaxed), + num_entries: self.state.backend.num_entries().await, + size_bytes: self.state.backend.size_bytes().await, + } + } + + pub async fn clear(&self) { + self.state.backend.clear().await; + self.state.hits.store(0, Ordering::Relaxed); + self.state.misses.store(0, Ordering::Relaxed); + } + + // -- CacheKey-based methods ----------------------------------------------- + + pub async fn insert_with_key(&self, cache_key: &K, metadata: Arc) + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let size = self.state.entry_size(metadata.as_ref()); + let key = self.sized_key(cache_key); + self.state + .backend + .insert(&key, metadata, size, K::codec()) + .await; + } + + pub async fn get_with_key(&self, cache_key: &K) -> Option> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let key = self.sized_key(cache_key); + let Some(entry) = self.state.backend.get(&key, K::codec()).await else { + self.state.misses.fetch_add(1, Ordering::Relaxed); + return None; + }; + match entry.downcast::() { + Ok(value) => { + self.state.hits.fetch_add(1, Ordering::Relaxed); + Some(value) + } + Err(_) => { + // Type mismatch: the backend returned a different concrete + // type than expected (e.g. a disk cache may store + // intermediate state). Treat as a miss. + log::warn!( + "cache backend returned a value with the wrong concrete type for key type {:?}", + K::stable_type_id() + ); + self.state.misses.fetch_add(1, Ordering::Relaxed); + None + } + } + } + + pub async fn get_or_insert_with_key( + &self, + cache_key: K, + loader: F, + ) -> Result> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + F: FnOnce() -> Fut + Send, + Fut: Future> + Send, + { + self.get_or_insert_with_key_hit(cache_key, loader) + .await + .map(|(value, _)| value) + } + + /// Same as [`get_or_insert_with_key`](Self::get_or_insert_with_key), but + /// also returns a boolean indicating whether the loader was skipped for + /// this call. + /// + /// - `true` means this call did **not** execute the loader. That covers + /// both a true cache hit on an already-populated entry and a coalesced + /// concurrent load where an in-flight loader started by a different + /// caller produced the value. + /// - `false` means the loader ran on this call (a real cache miss). + /// + /// Callers that want strict "served from cache" semantics should treat + /// coalesced loads as misses; the current backend does not distinguish the + /// two cases. Prefer this over rolling a caller-side `Arc` + /// when the caller needs per-query hit/miss counters — the backend already + /// tracks this bit internally and this method just exposes it. + pub async fn get_or_insert_with_key_hit( + &self, + cache_key: K, + loader: F, + ) -> Result<(Arc, bool)> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + F: FnOnce() -> Fut + Send, + Fut: Future> + Send, + { + let key = self.sized_key(&cache_key); + let state = self.state.clone(); + let typed_loader = Box::pin(async move { + let value = Arc::new(loader().await?); + let size = state.entry_size(value.as_ref()); + Ok((value as CacheEntry, size)) + }); + + let (entry, was_cached) = self + .state + .backend + .get_or_insert(&key, typed_loader, K::codec()) + .await?; + let entry = entry.downcast::().map_err(|_| { + self.state.misses.fetch_add(1, Ordering::Relaxed); + Error::io(format!( + "cache backend returned a value with the wrong concrete type for key type {:?}", + K::stable_type_id() + )) + })?; + if was_cached { + self.state.hits.fetch_add(1, Ordering::Relaxed); + } else { + self.state.misses.fetch_add(1, Ordering::Relaxed); + } + Ok((entry, was_cached)) + } + + pub async fn insert_unsized_with_key(&self, cache_key: &K, metadata: Arc) + where + K: UnsizedCacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let metadata = Arc::new(metadata); + let size = self.state.entry_size(metadata.as_ref()); + let key = self.unsized_key(cache_key); + self.state.backend.insert(&key, metadata, size, None).await; + } + + pub async fn get_unsized_with_key(&self, cache_key: &K) -> Option> + where + K: UnsizedCacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let key = self.unsized_key(cache_key); + let Some(entry) = self.state.backend.get(&key, None).await else { + self.state.misses.fetch_add(1, Ordering::Relaxed); + return None; + }; + match entry.downcast::>() { + Ok(value) => { + self.state.hits.fetch_add(1, Ordering::Relaxed); + Some(value.as_ref().clone()) + } + Err(_) => { + // Type mismatch: the backend returned a different concrete + // type than expected (e.g. a disk cache may store + // intermediate state). Treat as a miss. + log::warn!( + "cache backend returned a value with the wrong concrete type for unsized key type {:?}", + K::stable_type_id() + ); + self.state.misses.fetch_add(1, Ordering::Relaxed); + None + } + } + } + + fn sized_key(&self, cache_key: &K) -> InternalCacheKey { + let mut builder = KeyBuilder::new(self.namespace, K::stable_type_id(), K::schema()); + cache_key.write_key(&mut builder); + builder.finish() + } + + fn unsized_key(&self, cache_key: &K) -> InternalCacheKey { + let mut builder = KeyBuilder::new(self.namespace, K::stable_type_id(), K::schema()); + cache_key.write_key(&mut builder); + builder.finish() + } +} + +// --------------------------------------------------------------------------- +// WeakLanceCache +// --------------------------------------------------------------------------- + +/// A weak reference to a LanceCache, used by indices to avoid circular references. +/// When the original cache is dropped, operations on this will gracefully no-op. +#[derive(Clone, Debug)] +pub struct WeakLanceCache { + state: Weak, + namespace: key::CacheNamespace, +} + +impl WeakLanceCache { + pub fn from(cache: &LanceCache) -> Self { + Self { + state: Arc::downgrade(&cache.state), + namespace: cache.namespace, + } + } + + pub fn with_key_prefix(&self, prefix: &str) -> Self { + Self { + state: self.state.clone(), + namespace: self.namespace.child(prefix), + } + } + + pub async fn get_with_key(&self, cache_key: &K) -> Option> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + self.upgrade()?.get_with_key(cache_key).await + } + + pub async fn insert_with_key(&self, cache_key: &K, value: Arc) -> bool + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let Some(cache) = self.upgrade() else { + log::warn!("WeakLanceCache: cache no longer available, unable to insert item"); + return false; + }; + cache.insert_with_key(cache_key, value).await; + true + } + + /// Get or insert an item, computing it if necessary. + /// + /// Deduplication of concurrent loads is handled by the backend. + pub async fn get_or_insert_with_key( + &self, + cache_key: K, + loader: F, + ) -> Result> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + F: FnOnce() -> Fut + Send, + Fut: Future> + Send, + { + self.get_or_insert_with_key_hit(cache_key, loader) + .await + .map(|(value, _)| value) + } + + /// Same as [`get_or_insert_with_key`](Self::get_or_insert_with_key), but + /// also returns a boolean indicating whether the loader was skipped for + /// this call. See [`LanceCache::get_or_insert_with_key_hit`] for the + /// coalesced-load caveat. + pub async fn get_or_insert_with_key_hit( + &self, + cache_key: K, + loader: F, + ) -> Result<(Arc, bool)> + where + K: CacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + F: FnOnce() -> Fut + Send, + Fut: Future> + Send, + { + let Some(cache) = self.upgrade() else { + log::warn!("WeakLanceCache: cache no longer available, computing without caching"); + return loader().await.map(|value| (Arc::new(value), false)); + }; + cache.get_or_insert_with_key_hit(cache_key, loader).await + } + + pub async fn get_unsized_with_key(&self, cache_key: &K) -> Option> + where + K: UnsizedCacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + self.upgrade()?.get_unsized_with_key(cache_key).await + } + + pub async fn insert_unsized_with_key(&self, cache_key: &K, value: Arc) + where + K: UnsizedCacheKey, + K::ValueType: DeepSizeOf + Send + Sync + 'static, + { + let Some(cache) = self.upgrade() else { + log::warn!("WeakLanceCache: cache no longer available, unable to insert unsized item"); + return; + }; + cache.insert_unsized_with_key(cache_key, value).await; + } + + fn upgrade(&self) -> Option { + Some(LanceCache { + state: self.state.upgrade()?, + namespace: self.namespace, + }) + } +} + +// --------------------------------------------------------------------------- +// CacheStats +// --------------------------------------------------------------------------- + +#[derive(Debug, Clone)] +pub struct CacheStats { + /// Number of times `get`, `get_unsized`, or `get_or_insert` found an item in the cache. + pub hits: u64, + /// Number of times `get`, `get_unsized`, or `get_or_insert` did not find an item in the cache. + pub misses: u64, + /// Number of entries currently in the cache. + pub num_entries: usize, + /// Total size in bytes of all entries in the cache. + pub size_bytes: usize, +} + +impl CacheStats { + pub fn hit_ratio(&self) -> f32 { + if self.hits + self.misses == 0 { + 0.0 + } else { + self.hits as f32 / (self.hits + self.misses) as f32 + } + } + + pub fn miss_ratio(&self) -> f32 { + if self.hits + self.misses == 0 { + 0.0 + } else { + self.misses as f32 / (self.hits + self.misses) as f32 + } + } +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + use std::pin::Pin; + use std::sync::{ + atomic::{AtomicUsize, Ordering}, + mpsc, + }; + use std::task::Poll; + use std::thread; + use std::time::Duration; + + use super::*; + + async fn report_first_pending( + future: F, + parked: tokio::sync::oneshot::Sender<()>, + ) -> F::Output + where + F: Future, + { + tokio::pin!(future); + let mut parked = Some(parked); + futures::future::poll_fn(|cx| match future.as_mut().poll(cx) { + Poll::Pending => { + if let Some(parked) = parked.take() { + let _ = parked.send(()); + } + Poll::Pending + } + Poll::Ready(output) => Poll::Ready(output), + }) + .await + } + + #[derive(Clone)] + struct VersionedTestKey { + id: u64, + } + + type TestKey = VersionedTestKey<1>; + type TestKeyV2 = VersionedTestKey<2>; + + impl VersionedTestKey { + fn new(id: u64) -> Self { + Self { id } + } + } + + impl CacheKey for VersionedTestKey { + type ValueType = Vec; + + fn key(&self) -> Cow<'_, str> { + self.id.to_string().into() + } + + fn type_name() -> &'static str { + "test.VecU32" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("test.vec-u32-key", SCHEMA_VERSION) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u64(self.id); + } + } + + struct SharedTestValue { + data: Arc>, + } + + impl DeepSizeOf for SharedTestValue { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.data.deep_size_of_children(context) + } + } + + struct SharedTestKey(u64); + + impl CacheKey for SharedTestKey { + type ValueType = SharedTestValue; + + fn key(&self) -> Cow<'_, str> { + self.0.to_string().into() + } + + fn type_name() -> &'static str { + "test.SharedValue" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("test.shared-value-key", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u64(self.0); + } + } + + struct ReentrantValue(LanceCache); + + impl DeepSizeOf for ReentrantValue { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.0.deep_size_of_children(context) + } + } + + struct ReentrantKey; + + impl CacheKey for ReentrantKey { + type ValueType = ReentrantValue; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed("reentrant") + } + + fn type_name() -> &'static str { + "test.ReentrantValue" + } + } + + #[derive(Clone, Copy, Debug)] + enum TestBackendKind { + Moka, + Quick, + } + + impl TestBackendKind { + fn cache(self, capacity: usize) -> LanceCache { + match self { + Self::Moka => LanceCache::with_capacity(capacity), + Self::Quick => { + LanceCache::with_backend(Arc::new(QuickCacheBackend::with_capacity(capacity))) + } + } + } + } + + struct LegacyBridgeKey(&'static str); + + impl CacheKey for LegacyBridgeKey { + type ValueType = Vec; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed(self.0) + } + + fn type_name() -> &'static str { + "test.LegacyBridge" + } + } + + struct ExplicitBridgeKey(&'static str); + + impl CacheKey for ExplicitBridgeKey { + type ValueType = Vec; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed(self.0) + } + + fn type_name() -> &'static str { + "test.LegacyBridge" + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_str(self.0); + } + } + + trait TestDynValue: DeepSizeOf + Send + Sync { + fn values(&self) -> &[u32]; + } + + impl TestDynValue for Vec { + fn values(&self) -> &[u32] { + self + } + } + + struct LegacyUnsizedBridgeKey(&'static str); + + impl UnsizedCacheKey for LegacyUnsizedBridgeKey { + type ValueType = dyn TestDynValue; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed(self.0) + } + + fn type_name() -> &'static str { + "test.LegacyUnsizedBridge" + } + } + + struct ExplicitUnsizedBridgeKey(&'static str); + + impl UnsizedCacheKey for ExplicitUnsizedBridgeKey { + type ValueType = dyn TestDynValue; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed(self.0) + } + + fn type_name() -> &'static str { + "test.LegacyUnsizedBridge" + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_str(self.0); + } + } + + #[derive(Debug, Default)] + struct HashMapBackend { + entries: tokio::sync::Mutex>, + } + + #[async_trait::async_trait] + impl CacheBackend for HashMapBackend { + async fn get( + &self, + key: &InternalCacheKey, + _codec: Option, + ) -> Option { + self.entries + .lock() + .await + .get(key) + .map(|(entry, _)| entry.clone()) + } + + async fn insert( + &self, + key: &InternalCacheKey, + entry: CacheEntry, + size_bytes: usize, + _codec: Option, + ) { + self.entries.lock().await.insert(*key, (entry, size_bytes)); + } + + async fn get_or_insert<'a>( + &self, + key: &InternalCacheKey, + loader: Pin> + Send + 'a>>, + codec: Option, + ) -> Result<(CacheEntry, bool)> { + if let Some(entry) = self.get(key, codec).await { + return Ok((entry, true)); + } + let (entry, size_bytes) = loader.await?; + self.insert(key, entry.clone(), size_bytes, codec).await; + Ok((entry, false)) + } + + async fn clear(&self) { + self.entries.lock().await.clear(); + } + + async fn num_entries(&self) -> usize { + self.entries.lock().await.len() + } + + async fn size_bytes(&self) -> usize { + self.entries + .lock() + .await + .values() + .map(|(_, size_bytes)| size_bytes) + .sum() + } + } + + #[derive(Debug)] + struct WrongTypeBackend; + + #[async_trait::async_trait] + impl CacheBackend for WrongTypeBackend { + async fn get( + &self, + _key: &InternalCacheKey, + _codec: Option, + ) -> Option { + Some(Arc::new(String::from("wrong type"))) + } + + async fn insert( + &self, + _key: &InternalCacheKey, + _entry: CacheEntry, + _size_bytes: usize, + _codec: Option, + ) { + } + + async fn get_or_insert<'a>( + &self, + _key: &InternalCacheKey, + _loader: Pin> + Send + 'a>>, + _codec: Option, + ) -> Result<(CacheEntry, bool)> { + Ok((Arc::new(String::from("wrong type")), true)) + } + + async fn clear(&self) {} + + async fn num_entries(&self) -> usize { + 0 + } + + async fn size_bytes(&self) -> usize { + 0 + } + } + + #[tokio::test] + async fn typed_roundtrip_stats_clear_and_namespace_isolation() { + let cache = LanceCache::with_capacity(4096); + let left = cache.with_key_prefix("left"); + let right = cache.with_key_prefix("right"); + left.insert_with_key(&TestKey::new(7), Arc::new(vec![1, 2, 3])) + .await; + + assert_eq!( + left.get_with_key(&TestKey::new(7)).await.as_deref(), + Some(&vec![1, 2, 3]) + ); + assert!(right.get_with_key(&TestKey::new(7)).await.is_none()); + let stats = cache.stats().await; + assert_eq!((stats.hits, stats.misses, stats.num_entries), (1, 1, 1)); + + cache.clear().await; + let stats = left.stats().await; + assert_eq!((stats.hits, stats.misses, stats.num_entries), (0, 0, 0)); + } + + #[tokio::test] + async fn strong_and_weak_handles_share_state_and_namespace() { + let cache = LanceCache::with_capacity(4096); + let child = cache.with_key_prefix("child"); + let weak = WeakLanceCache::from(&child); + + assert!( + weak.insert_with_key(&TestKey::new(1), Arc::new(vec![1])) + .await + ); + assert_eq!( + child.get_with_key(&TestKey::new(1)).await.as_deref(), + Some(&vec![1]) + ); + child + .insert_with_key(&TestKey::new(2), Arc::new(vec![2])) + .await; + assert_eq!( + weak.get_with_key(&TestKey::new(2)).await.as_deref(), + Some(&vec![2]) + ); + assert_eq!((cache.stats().await.hits, cache.size().await), (2, 2)); + } + + #[tokio::test] + async fn nested_namespace_segments_do_not_alias_combined_segments() { + let cache = LanceCache::with_capacity(4096); + let nested = cache.with_key_prefix("a").with_key_prefix("b"); + let combined = cache.with_key_prefix("a/b"); + nested + .insert_with_key(&TestKey::new(1), Arc::new(vec![10])) + .await; + assert!(combined.get_with_key(&TestKey::new(1)).await.is_none()); + } + + #[tokio::test] + async fn schema_change_produces_a_cold_miss() { + let cache = LanceCache::with_capacity(4096); + cache + .insert_with_key(&TestKey::new(1), Arc::new(vec![10])) + .await; + assert!(cache.get_with_key(&TestKeyV2::new(1)).await.is_none()); + } + + #[tokio::test] + async fn get_or_insert_with_key_hit_reports_loader_execution() { + let cache = LanceCache::with_capacity(4096); + + // Cold: loader runs, was_cached = false. + let (value, was_cached) = cache + .get_or_insert_with_key_hit(TestKey::new(1), || async { Ok(vec![1, 2, 3]) }) + .await + .unwrap(); + assert_eq!(*value, vec![1, 2, 3]); + assert!(!was_cached); + + // Warm: loader must not run and was_cached = true. + let (value, was_cached) = cache + .get_or_insert_with_key_hit(TestKey::new(1), || async { + panic!("should not be called") + }) + .await + .unwrap(); + assert_eq!(*value, vec![1, 2, 3]); + assert!(was_cached); + } + + #[tokio::test] + async fn default_string_bridge_matches_explicit_legacy_encoding() { + let cache = LanceCache::with_capacity(4096); + cache + .insert_with_key(&LegacyBridgeKey("same"), Arc::new(vec![10])) + .await; + assert_eq!( + cache + .get_with_key(&ExplicitBridgeKey("same")) + .await + .as_deref(), + Some(&vec![10]) + ); + } + + #[tokio::test] + async fn unsized_default_string_bridge_matches_explicit_legacy_encoding() { + let cache = LanceCache::with_capacity(4096); + let value: Arc = Arc::new(vec![10, 20]); + cache + .insert_unsized_with_key(&LegacyUnsizedBridgeKey("same"), value) + .await; + + let cached = cache + .get_unsized_with_key(&ExplicitUnsizedBridgeKey("same")) + .await + .unwrap(); + assert_eq!(cached.values(), &[10, 20]); + } + + #[tokio::test] + async fn custom_backend_receives_opaque_keys_and_shared_clear() { + let backend = Arc::new(HashMapBackend::default()); + let cache = LanceCache::with_backend(backend.clone()); + let child = cache.with_key_prefix("child"); + let value = Arc::new(vec![1, 2, 3]); + let value_size = cache_entry_size(value.as_ref()); + + child.insert_with_key(&TestKey::new(7), value).await; + assert_eq!( + child.get_with_key(&TestKey::new(7)).await.as_deref(), + Some(&vec![1, 2, 3]) + ); + assert_eq!(backend.entries.lock().await.len(), 1); + assert_eq!(cache.size_bytes().await, value_size); + + cache.clear().await; + assert!(backend.entries.lock().await.is_empty()); + assert_eq!(child.stats().await.hits, 0); + } + + #[tokio::test] + async fn backend_type_collisions_are_contextual_misses_or_errors() { + let cache = LanceCache::with_backend(Arc::new(WrongTypeBackend)); + + assert!(cache.get_with_key(&TestKey::new(1)).await.is_none()); + let error = cache + .get_or_insert_with_key(TestKey::new(2), || async { Ok(vec![2]) }) + .await + .unwrap_err(); + assert!(error.to_string().contains("test.VecU32")); + let stats = cache.stats().await; + assert_eq!((stats.hits, stats.misses), (0, 2)); + } + + #[tokio::test] + async fn moka_weight_includes_the_fixed_physical_key() { + let value = Arc::new(vec![0_u32; 3]); + let expected = cache_entry_size(value.as_ref()) + .checked_add(std::mem::size_of::()) + .unwrap(); + let cache = LanceCache::with_capacity(expected * 2); + cache.insert_with_key(&TestKey::new(1), value).await; + assert_eq!(cache.size_bytes().await, expected); + } + + #[rstest::rstest] + #[case::moka(TestBackendKind::Moka)] + #[case::quick(TestBackendKind::Quick)] + #[tokio::test] + async fn deep_size_deduplicates_shared_entry_allocations( + #[case] backend_kind: TestBackendKind, + ) { + let cache = backend_kind.cache(1 << 20); + let shared_data = Arc::new(vec![0_u8; 1024]); + + for id in 0..2 { + let data = shared_data.clone(); + cache + .get_or_insert_with_key(SharedTestKey(id), || async move { + Ok(SharedTestValue { data }) + }) + .await + .unwrap(); + } + + let arc_overhead = std::mem::size_of::() * 2; + let shared_allocation = std::mem::size_of::>() + shared_data.capacity(); + let expected_entries = 2 * std::mem::size_of::() + + 2 * (std::mem::size_of::() + arc_overhead) + + shared_allocation; + + let weighted_size = cache.size_bytes().await; + assert_eq!(weighted_size, expected_entries + shared_allocation); + assert_eq!( + cache.deep_size_of(), + std::mem::size_of::() + expected_entries + ); + + let mut context = Context::new(); + assert_eq!(cache.deep_size_of_children(&mut context), expected_entries); + assert_eq!( + cache + .with_key_prefix("another-handle") + .deep_size_of_children(&mut context), + 0 + ); + } + + #[test] + fn sizing_can_reenter_the_same_cache() { + let (done_tx, done_rx) = mpsc::channel(); + let worker = thread::spawn(move || { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + runtime.block_on(async move { + let cache = LanceCache::with_capacity(4096); + cache + .insert_with_key(&ReentrantKey, Arc::new(ReentrantValue(cache.clone()))) + .await; + done_tx.send(()).unwrap(); + }); + }); + + done_rx + .recv_timeout(Duration::from_secs(1)) + .expect("cache insertion deadlocked during sizing"); + worker.join().unwrap(); + } + + #[tokio::test] + async fn no_cache_computes_each_time() { + let cache = LanceCache::no_cache(); + let loads = Arc::new(AtomicUsize::new(0)); + for _ in 0..2 { + let loads = loads.clone(); + let value = cache + .get_or_insert_with_key(TestKey::new(1), move || async move { + loads.fetch_add(1, Ordering::SeqCst); + Ok(vec![42]) + }) + .await + .unwrap(); + assert_eq!(value.as_slice(), &[42]); + } + assert_eq!(loads.load(Ordering::SeqCst), 2); + assert_eq!(cache.size().await, 0); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn single_flight_coalesces_success_after_contenders_are_parked() { + const CONTENDERS: usize = 4; + + let cache = Arc::new(LanceCache::with_capacity(4096)); + let loader_calls = Arc::new(AtomicUsize::new(0)); + let release = Arc::new(tokio::sync::Notify::new()); + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + + let owner = { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + let release = release.clone(); + tokio::spawn(async move { + cache + .get_or_insert_with_key(TestKey::new(10), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + let _ = started_tx.send(()); + release.notified().await; + Ok(vec![10]) + }) + .await + }) + }; + started_rx.await.unwrap(); + + let mut contenders = Vec::new(); + let mut parked = Vec::new(); + for _ in 0..CONTENDERS { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + let (parked_tx, parked_rx) = tokio::sync::oneshot::channel(); + parked.push(parked_rx); + contenders.push(tokio::spawn(async move { + report_first_pending( + cache.get_or_insert_with_key(TestKey::new(10), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + Ok(vec![99]) + }), + parked_tx, + ) + .await + })); + } + for parked in parked { + parked + .await + .expect("contender completed instead of parking behind owner"); + } + assert_eq!(loader_calls.load(Ordering::SeqCst), 1); + assert!(contenders.iter().all(|handle| !handle.is_finished())); + + release.notify_one(); + assert_eq!(owner.await.unwrap().unwrap().as_slice(), &[10]); + for contender in contenders { + assert_eq!(contender.await.unwrap().unwrap().as_slice(), &[10]); + } + let stats = cache.stats().await; + assert_eq!((stats.hits, stats.misses), (CONTENDERS as u64, 1),); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn single_flight_coalesces_errors_after_contenders_are_parked() { + const CONTENDERS: usize = 4; + + let cache = Arc::new(LanceCache::with_capacity(4096)); + let loader_calls = Arc::new(AtomicUsize::new(0)); + let release = Arc::new(tokio::sync::Notify::new()); + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + + let owner = { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + let release = release.clone(); + tokio::spawn(async move { + cache + .get_or_insert_with_key(TestKey::new(20), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + let _ = started_tx.send(()); + release.notified().await; + Err(Error::timeout("owner loader timed out")) + }) + .await + }) + }; + started_rx.await.unwrap(); + + let mut contenders = Vec::new(); + let mut parked = Vec::new(); + for _ in 0..CONTENDERS { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + let (parked_tx, parked_rx) = tokio::sync::oneshot::channel(); + parked.push(parked_rx); + contenders.push(tokio::spawn(async move { + report_first_pending( + cache.get_or_insert_with_key(TestKey::new(20), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + Err(Error::timeout("contender loader timed out")) + }), + parked_tx, + ) + .await + })); + } + for parked in parked { + parked + .await + .expect("contender completed instead of parking behind owner"); + } + assert_eq!(loader_calls.load(Ordering::SeqCst), 1); + assert!(contenders.iter().all(|handle| !handle.is_finished())); + + release.notify_one(); + assert!(matches!(owner.await.unwrap(), Err(Error::Timeout { .. }))); + for contender in contenders { + assert!(matches!( + contender.await.unwrap(), + Err(Error::Timeout { .. }) + )); + } + assert_eq!(loader_calls.load(Ordering::SeqCst), 1); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn single_flight_retries_after_the_owner_is_cancelled() { + let cache = Arc::new(LanceCache::with_capacity(4096)); + let loader_calls = Arc::new(AtomicUsize::new(0)); + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + + let owner = { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + tokio::spawn(async move { + cache + .get_or_insert_with_key(TestKey::new(30), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + let _ = started_tx.send(()); + std::future::pending::<()>().await; + Ok(vec![30]) + }) + .await + }) + }; + started_rx.await.unwrap(); + + let (parked_tx, parked_rx) = tokio::sync::oneshot::channel(); + let contender = { + let cache = cache.clone(); + let loader_calls = loader_calls.clone(); + tokio::spawn(async move { + report_first_pending( + cache.get_or_insert_with_key(TestKey::new(30), move || async move { + loader_calls.fetch_add(1, Ordering::SeqCst); + Ok(vec![31]) + }), + parked_tx, + ) + .await + }) + }; + parked_rx + .await + .expect("contender completed instead of parking behind owner"); + assert_eq!(loader_calls.load(Ordering::SeqCst), 1); + assert!(!contender.is_finished()); + + owner.abort(); + assert!(owner.await.unwrap_err().is_cancelled()); + let value = tokio::time::timeout(std::time::Duration::from_secs(5), contender) + .await + .expect("contender remained parked after owner cancellation") + .unwrap() + .unwrap(); + assert_eq!(value.as_slice(), &[31]); + assert_eq!(loader_calls.load(Ordering::SeqCst), 2); + } + + #[tokio::test] + async fn expired_weak_cache_degrades_without_retaining_state() { + let cache = LanceCache::with_capacity(4096); + let weak = WeakLanceCache::from(&cache); + drop(cache); + + assert!(weak.get_with_key(&TestKey::new(1)).await.is_none()); + assert!( + !weak + .insert_with_key(&TestKey::new(1), Arc::new(vec![1])) + .await + ); + let value = weak + .get_or_insert_with_key(TestKey::new(1), || async { Ok(vec![7]) }) + .await + .unwrap(); + assert_eq!(value.as_slice(), &[7]); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/moka.rs b/lance-artifact/rust/lance-core/src/cache/moka.rs new file mode 100644 index 000000000..2a93ba6c2 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/moka.rs @@ -0,0 +1,349 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::pin::Pin; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +use async_trait::async_trait; +use futures::Future; + +use crate::Result; +use crate::deepsize::Context; +use crate::error::CloneableError; + +use super::backend::{CacheBackend, CacheEntry}; +use super::{CacheCodec, InternalCacheKey}; + +/// Internal record stored in the moka cache. +#[derive(Clone, Debug)] +struct MokaCacheEntry { + entry: CacheEntry, + size_bytes: usize, +} + +/// Per-entry key cost for eviction. +pub(super) fn key_footprint(_key: &InternalCacheKey) -> usize { + std::mem::size_of::() +} + +fn physical_size(key: &InternalCacheKey, size_bytes: usize) -> usize { + key_footprint(key).saturating_add(size_bytes) +} + +/// Number of physical bytes represented by one Moka weight unit. +/// +/// Moka limits each entry's weight to `u32`, so capacities above 4 GiB need +/// coarser units to account for a single large entry without undercharging it. +fn weight_unit(capacity: usize) -> usize { + capacity.div_ceil(u32::MAX as usize).max(1) +} + +fn entry_weight(key: &InternalCacheKey, size_bytes: usize, weight_unit: usize) -> u32 { + physical_size(key, size_bytes) + .div_ceil(weight_unit) + .try_into() + .unwrap_or(u32::MAX) +} + +/// Default [`CacheBackend`] backed by a [moka](https://crates.io/crates/moka) cache. +/// +/// Provides weighted-capacity eviction and concurrent-load deduplication +/// via moka's built-in `optionally_get_with`. +pub struct MokaCacheBackend { + cache: moka::future::Cache, + capacity: usize, + weight_unit: usize, +} + +impl std::fmt::Debug for MokaCacheBackend { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("MokaCacheBackend") + .field("entry_count", &self.cache.entry_count()) + .finish() + } +} + +impl MokaCacheBackend { + pub fn with_capacity(capacity: usize) -> Self { + let weight_unit = weight_unit(capacity); + let capacity_weight = capacity.div_ceil(weight_unit) as u64; + let cache = moka::future::Cache::builder() + .max_capacity(capacity_weight) + .weigher(move |key: &InternalCacheKey, entry: &MokaCacheEntry| { + entry_weight(key, entry.size_bytes, weight_unit) + }) + .build(); + Self { + cache, + capacity, + weight_unit, + } + } + + pub fn no_cache() -> Self { + Self { + cache: moka::future::Cache::new(0), + capacity: 0, + weight_unit: 1, + } + } + + /// Configured weighted capacity in bytes. + pub fn capacity(&self) -> usize { + self.capacity + } + + fn weighted_size_bytes(&self) -> usize { + self.cache + .weighted_size() + .saturating_mul(self.weight_unit as u64) + .try_into() + .unwrap_or(usize::MAX) + } +} + +#[async_trait] +impl CacheBackend for MokaCacheBackend { + async fn get(&self, key: &InternalCacheKey, _codec: Option) -> Option { + self.cache.get(key).await.map(|r| r.entry) + } + + async fn insert( + &self, + key: &InternalCacheKey, + entry: CacheEntry, + size_bytes: usize, + _codec: Option, + ) { + self.cache + .insert(*key, MokaCacheEntry { entry, size_bytes }) + .await; + } + + async fn get_or_insert<'a>( + &self, + key: &InternalCacheKey, + loader: Pin> + Send + 'a>>, + _codec: Option, + ) -> Result<(CacheEntry, bool)> { + // Track whether the loader actually ran (= cache miss). + let was_miss = Arc::new(AtomicBool::new(false)); + let was_miss_clone = was_miss.clone(); + + let init = async move { + was_miss_clone.store(true, Ordering::Relaxed); + loader + .await + .map(|(entry, size_bytes)| MokaCacheEntry { entry, size_bytes }) + .map_err(CloneableError) + }; + + let owned_key = *key; + match self.cache.try_get_with(owned_key, init).await { + Ok(record) => { + let was_cached = !was_miss.load(Ordering::Relaxed); + Ok((record.entry, was_cached)) + } + Err(error) => Err(Arc::unwrap_or_clone(error).0), + } + } + + async fn clear(&self) { + self.cache.invalidate_all(); + self.cache.run_pending_tasks().await; + } + + async fn num_entries(&self) -> usize { + self.cache.run_pending_tasks().await; + self.cache.entry_count() as usize + } + + async fn size_bytes(&self) -> usize { + self.cache.run_pending_tasks().await; + self.weighted_size_bytes() + } + + fn approx_num_entries(&self) -> usize { + self.cache.entry_count() as usize + } + + fn approx_size_bytes(&self) -> usize { + // `weighted_size()` can be stale without `run_pending_tasks()`, which + // is async and can't be called from this synchronous context. + self.weighted_size_bytes() + } + + fn deep_size_of_entries( + &self, + context: &mut Context, + size_of_entry: &dyn Fn(&CacheEntry, &mut Context) -> Option, + ) -> Option { + Some( + self.cache + .iter() + .map(|(key, record)| { + key_footprint(key.as_ref()) + + size_of_entry(&record.entry, context).unwrap_or(record.size_bytes) + }) + .sum(), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn entry_weights_are_exact_at_byte_granularity() { + let key = InternalCacheKey::from_bytes([0; 16]); + assert_eq!(weight_unit(4096), 1); + assert_eq!(entry_weight(&key, 7, 1), 23); + } + + #[tokio::test] + async fn size_methods_use_constant_time_weighted_accounting() { + let backend = MokaCacheBackend::with_capacity(4096); + let key = InternalCacheKey::from_bytes([0; 16]); + let entry: CacheEntry = Arc::new(()); + let value_size = 7; + let expected = physical_size(&key, value_size); + + backend.insert(&key, entry, value_size, None).await; + + assert_eq!(backend.size_bytes().await, expected); + assert_eq!(backend.approx_size_bytes(), expected); + } + + #[cfg(target_pointer_width = "64")] + #[test] + fn entry_weights_scale_for_capacities_above_four_gibibytes() { + let key = InternalCacheKey::from_bytes([0; 16]); + let capacity = 6 * 1024 * 1024 * 1024; + let weight_unit = weight_unit(capacity); + assert_eq!(weight_unit, 2); + + let size_bytes = u32::MAX as usize + 1024; + let expected = physical_size(&key, size_bytes).div_ceil(weight_unit); + let weight = entry_weight(&key, size_bytes, weight_unit); + assert_eq!(weight as usize, expected); + assert_ne!(weight, u32::MAX); + } +} + +/// Registry identifier for the built-in Moka backend. +pub const MOKA_BACKEND_KIND: &str = "moka"; + +/// [`BackendBuildFn`](super::registry::BackendBuildFn) for [`MokaCacheBackend`]. +/// +/// Recognized options: +/// * `capacity` — total weighted capacity in bytes (`usize`). +/// This must be present and non-empty. +/// +/// Unknown options are rejected so typos surface immediately instead of +/// silently falling through to the default capacity. +pub(super) fn build_moka_backend( + config: &super::registry::BackendConfig, +) -> Result { + let mut capacity: Option = None; + for (key, value) in &config.options { + match key.as_str() { + "capacity" => { + if value.is_empty() { + return Err(crate::Error::invalid_input( + "moka cache backend: capacity must not be empty", + )); + } else { + capacity = Some(value.parse::().map_err(|err| { + crate::Error::invalid_input(format!( + "moka cache backend: cannot parse capacity {:?}: {}", + value, err + )) + })?); + } + } + other => { + return Err(crate::Error::invalid_input(format!( + "moka cache backend: unknown option {:?}", + other + ))); + } + } + } + let capacity = capacity.ok_or_else(|| { + crate::Error::invalid_input( + "moka cache backend: capacity is required; use moka://?capacity=", + ) + })?; + Ok(MokaCacheBackend::with_capacity(capacity)) +} + +pub(super) fn build_moka(config: &super::registry::BackendConfig) -> Result> { + Ok(Arc::new(build_moka_backend(config)?)) +} + +#[cfg(test)] +mod moka_registry_tests { + use super::super::backend_uri::{build_from_uri, parse_backend_uri}; + use super::super::registry::{BackendConfig, build_from_config, registry_test_lock}; + use super::*; + + #[test] + fn test_moka_builds_from_config() { + let _lock = registry_test_lock(); + let cfg = BackendConfig::new("moka") + .unwrap() + .with_option("capacity", "1048576"); + let backend = build_moka_backend(&cfg).unwrap(); + assert_eq!(backend.capacity(), 1048576); + let _backend = build_from_config(&cfg).unwrap(); + } + + #[test] + fn test_moka_builds_from_uri() { + let _lock = registry_test_lock(); + let cfg = parse_backend_uri("moka://?capacity=1048576").unwrap(); + let backend = build_moka_backend(&cfg).unwrap(); + assert_eq!(backend.capacity(), 1048576); + let _backend = build_from_uri("moka://?capacity=1048576").unwrap(); + } + + #[test] + fn test_moka_rejects_unknown_option() { + let _lock = registry_test_lock(); + let cfg = BackendConfig::new("moka") + .unwrap() + .with_option("mystery", "1"); + let err = build_from_config(&cfg).unwrap_err(); + assert!(err.to_string().contains("unknown option")); + } + + #[test] + fn test_moka_rejects_bad_capacity() { + let _lock = registry_test_lock(); + let cfg = BackendConfig::new("moka") + .unwrap() + .with_option("capacity", "not-a-number"); + let err = build_from_config(&cfg).unwrap_err(); + assert!(err.to_string().contains("cannot parse capacity")); + } + + #[test] + fn test_moka_rejects_missing_capacity() { + let _lock = registry_test_lock(); + let cfg = BackendConfig::new("moka").unwrap(); + let err = build_from_config(&cfg).unwrap_err(); + assert!(err.to_string().contains("capacity is required")); + } + + #[test] + fn test_moka_rejects_empty_capacity() { + let _lock = registry_test_lock(); + let cfg = BackendConfig::new("moka") + .unwrap() + .with_option("capacity", ""); + let err = build_from_config(&cfg).unwrap_err(); + assert!(err.to_string().contains("capacity must not be empty")); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/quick.rs b/lance-artifact/rust/lance-core/src/cache/quick.rs new file mode 100644 index 000000000..53e0d0e88 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/quick.rs @@ -0,0 +1,298 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! [`CacheBackend`] backed by [quick_cache](https://crates.io/crates/quick_cache), +//! whose hit path is one atomic bit — no read-op channel or inline +//! housekeeping. Used for the session index and metadata caches; the index +//! cache sees thousands of cache reads per query. + +use std::pin::Pin; + +use async_trait::async_trait; +use futures::Future; + +use super::backend::{CacheBackend, CacheEntry}; +use super::moka::key_footprint; +use super::{CacheCodec, InternalCacheKey}; +use crate::Result; +use crate::deepsize::Context; + +#[derive(Clone)] +struct QuickEntry { + entry: CacheEntry, + size_bytes: usize, +} + +#[derive(Clone)] +struct EntryWeighter; + +impl quick_cache::Weighter for EntryWeighter { + fn weight(&self, key: &InternalCacheKey, value: &QuickEntry) -> u64 { + // Same accounting as the moka backend. + key_footprint(key).saturating_add(value.size_bytes).max(1) as u64 + } +} + +pub struct QuickCacheBackend { + cache: quick_cache::sync::Cache, +} + +impl std::fmt::Debug for QuickCacheBackend { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("QuickCacheBackend") + .field("entry_count", &self.cache.len()) + .finish() + } +} + +/// Minimum weight budget (4 GiB) per shard: shards don't borrow capacity, and +/// an entry heavier than ~its shard's budget is silently refused admission. +const MIN_SHARD_SHARE: usize = 4 << 30; + +/// Recommended shard count: `min(cpus / 2, capacity / 4 GiB)`, power of two +/// in `[1, 1024]`. The cpu term bounds lock contention; the capacity term +/// keeps each shard's budget >= 4 GiB so large entries stay admissible. +/// Rounded down because quick_cache rounds requests up. +pub fn recommended_cache_shards(capacity: usize) -> usize { + let by_cpu = std::thread::available_parallelism() + .map(|n| n.get()) + .unwrap_or(2) + / 2; + let shards = (capacity / MIN_SHARD_SHARE).min(by_cpu).max(1); + let shards = if shards.is_power_of_two() { + shards + } else { + shards.next_power_of_two() / 2 + }; + shards.clamp(1, 1024) +} + +/// Assumed average entry size for pre-allocation sizing. +const ESTIMATED_AVG_ENTRY_BYTES: usize = 64 << 10; + +impl QuickCacheBackend { + /// Create a backend holding up to `capacity` bytes of weighted entries + /// (weight = key footprint + declared size), sharded per + /// [`recommended_cache_shards`]. + pub fn with_capacity(capacity: usize) -> Self { + let shards = recommended_cache_shards(capacity); + // Floor protects the shard count from quick_cache's items-per-shard + // heuristic; ceiling bounds pre-allocation. + let estimated_items = (capacity / ESTIMATED_AVG_ENTRY_BYTES).clamp(shards * 32, 1_000_000); + let options = quick_cache::OptionsBuilder::new() + .estimated_items_capacity(estimated_items) + .weight_capacity(capacity as u64) + .shards(shards) + .build() + // Only errors when weight/item capacity is missing; both are set. + .expect("quick_cache options"); + let cache = quick_cache::sync::Cache::with_options( + options, + EntryWeighter, + Default::default(), + Default::default(), + ); + Self { cache } + } +} + +#[async_trait] +impl CacheBackend for QuickCacheBackend { + async fn get(&self, key: &InternalCacheKey, _codec: Option) -> Option { + self.cache.get(key).map(|v| v.entry) + } + + async fn insert( + &self, + key: &InternalCacheKey, + entry: CacheEntry, + size_bytes: usize, + _codec: Option, + ) { + self.cache.insert(*key, QuickEntry { entry, size_bytes }); + } + + async fn get_or_insert<'a>( + &self, + key: &InternalCacheKey, + loader: Pin> + Send + 'a>>, + _codec: Option, + ) -> Result<(CacheEntry, bool)> { + match self.cache.get_value_or_guard_async(key).await { + Ok(value) => Ok((value.entry, true)), + Err(guard) => { + let (entry, size_bytes) = loader.await?; + let _ = guard.insert(QuickEntry { + entry: entry.clone(), + size_bytes, + }); + Ok((entry, false)) + } + } + } + + async fn clear(&self) { + self.cache.clear(); + } + + async fn num_entries(&self) -> usize { + self.cache.len() + } + + async fn size_bytes(&self) -> usize { + self.cache.weight() as usize + } + + fn approx_num_entries(&self) -> usize { + self.cache.len() + } + + fn approx_size_bytes(&self) -> usize { + self.cache.weight() as usize + } + + fn deep_size_of_entries( + &self, + context: &mut Context, + size_of_entry: &dyn Fn(&CacheEntry, &mut Context) -> Option, + ) -> Option { + Some( + self.cache + .iter() + .map(|(key, record)| { + key_footprint(&key) + + size_of_entry(&record.entry, context).unwrap_or(record.size_bytes) + }) + .sum(), + ) + } +} + +#[cfg(test)] +mod tests { + use std::marker::PhantomData; + use std::sync::Arc; + use std::sync::atomic::{AtomicUsize, Ordering}; + + use super::*; + use crate::cache::{CacheKey, LanceCache}; + + struct TestKey { + key: String, + _phantom: PhantomData, + } + + impl TestKey { + fn new(key: &str) -> Self { + Self { + key: key.to_string(), + _phantom: PhantomData, + } + } + } + + impl CacheKey for TestKey { + type ValueType = T; + fn key(&self) -> std::borrow::Cow<'_, str> { + std::borrow::Cow::Borrowed(&self.key) + } + fn type_name() -> &'static str { + std::any::type_name::() + } + } + + #[test] + fn entry_weight_includes_fixed_key() { + let key = InternalCacheKey::from_bytes([0; 16]); + let entry = QuickEntry { + entry: Arc::new(()), + size_bytes: 7, + }; + assert_eq!( + quick_cache::Weighter::weight(&EntryWeighter, &key, &entry), + 23 + ); + } + + #[tokio::test] + async fn test_quick_backend_roundtrip_singleflight_and_eviction() { + // Capacity must be large relative to one entry: quick_cache shards + // its weight budget, and an entry heavier than its shard's share is + // not admitted at all. + const CAPACITY: usize = 1 << 20; + let item = Arc::new(vec![1u8, 2, 3]); + let cache = LanceCache::with_backend(Arc::new(QuickCacheBackend::with_capacity(CAPACITY))); + + // insert + get roundtrip and weighted accounting + cache + .insert_with_key(&TestKey::>::new("a"), item.clone()) + .await; + assert_eq!( + cache + .get_with_key(&TestKey::>::new("a")) + .await + .as_deref(), + Some(&vec![1u8, 2, 3]) + ); + assert_eq!(cache.approx_size(), 1); + assert!(cache.size_bytes().await > 0); + + // get_or_insert runs the loader only on a miss + let loads = Arc::new(AtomicUsize::new(0)); + for _ in 0..2 { + let loads = loads.clone(); + let value = cache + .get_or_insert_with_key(TestKey::>::new("b"), || async move { + loads.fetch_add(1, Ordering::SeqCst); + Ok(vec![7u8]) + }) + .await + .unwrap(); + assert_eq!(value.as_ref(), &vec![7u8]); + } + assert_eq!(loads.load(Ordering::SeqCst), 1); + + // capacity is enforced: overfill with 4x capacity of 16KiB entries + // and confirm eviction kept the weighted size within budget + for i in 0..256 { + cache + .insert_with_key( + &TestKey::>::new(&format!("fill-{i}")), + Arc::new(vec![0u8; 16 << 10]), + ) + .await; + } + assert!(cache.size_bytes().await <= CAPACITY); + assert!(cache.size().await < 258); + + cache.clear().await; + assert_eq!(cache.size().await, 0); + } + + #[tokio::test] + async fn test_quick_backend_tiny_capacity() { + // A tiny cache must not over-provision item metadata and must still + // admit and evict correctly within its weight budget. + const CAPACITY: usize = 64 << 10; + let cache = LanceCache::with_backend(Arc::new(QuickCacheBackend::with_capacity(CAPACITY))); + for i in 0..64 { + cache + .insert_with_key( + &TestKey::>::new(&format!("k-{i}")), + Arc::new(vec![0u8; 4 << 10]), + ) + .await; + } + assert!(cache.size_bytes().await <= CAPACITY); + assert!(cache.size().await >= 1); + let hit = cache + .get_with_key(&TestKey::>::new("k-63")) + .await + .is_some() + || cache + .get_with_key(&TestKey::>::new("k-62")) + .await + .is_some(); + assert!(hit, "recently inserted entries should be resident"); + } +} diff --git a/lance-artifact/rust/lance-core/src/cache/registry.rs b/lance-artifact/rust/lance-core/src/cache/registry.rs new file mode 100644 index 000000000..e4e72656c --- /dev/null +++ b/lance-artifact/rust/lance-core/src/cache/registry.rs @@ -0,0 +1,364 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Pluggable cache-backend registry. +//! +//! A [`BackendConfig`] identifies which backend to build (`kind`) and carries +//! backend-specific string options. Backends are constructed through a +//! [`BackendBuildFn`] registered under a unique `kind`. Third-party crates +//! integrate by calling [`register_backend`] once at application startup; +//! [`build_from_config`] then locates the constructor and hands it the +//! config. +//! +//! The registry uses `HashMap` for options so it can be +//! represented naturally across FFI (Python `dict[str, str]`, Java +//! `Map`, etc.). + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, MutexGuard, OnceLock}; + +use super::backend::CacheBackend; +use super::moka::{MOKA_BACKEND_KIND, build_moka}; +use crate::{Error, Result}; + +/// Backend-independent configuration passed to a [`BackendBuildFn`]. +/// +/// `kind` selects which registered backend to construct; `options` carries +/// backend-specific key/value settings (e.g. `capacity`, `path`). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct BackendConfig { + /// Registered backend identifier, e.g. `"moka"`. + pub kind: String, + /// Backend-specific string options. + pub options: HashMap, +} + +impl BackendConfig { + /// Build a config with no options. + pub fn new(kind: impl AsRef) -> Result { + Ok(Self { + kind: normalize_backend_kind(kind.as_ref())?, + options: HashMap::new(), + }) + } + + /// Insert a single option and return `self`, enabling chaining. + pub fn with_option(mut self, key: impl Into, value: impl Into) -> Self { + self.options.insert(key.into(), value.into()); + self + } +} + +/// Normalize and validate a cache backend kind. +/// +/// Backend kinds share the same syntax as URI schemes. They are matched +/// case-insensitively and stored as lowercase ASCII so registry lookups, +/// config dictionaries, and URI parsing all address the same key. +pub fn normalize_backend_kind(kind: &str) -> Result { + let mut chars = kind.chars(); + match chars.next() { + Some(c) if c.is_ascii_alphabetic() => {} + _ => { + return Err(Error::invalid_input(format!( + "cache backend kind {:?}: must start with an ASCII letter", + kind + ))); + } + } + for c in chars { + let ok = c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'); + if !ok { + return Err(Error::invalid_input(format!( + "cache backend kind {:?}: invalid character {:?}", + kind, c + ))); + } + } + Ok(kind.to_ascii_lowercase()) +} + +/// Constructor signature for a cache backend. +/// +/// Constructors are synchronous. Backends that need async initialization +/// should surface a `try_new_blocking` shim (or equivalent) and call it here. +pub type BackendBuildFn = fn(&BackendConfig) -> Result>; + +fn registry() -> &'static Mutex> { + static REGISTRY: OnceLock>> = OnceLock::new(); + REGISTRY.get_or_init(|| Mutex::new(HashMap::new())) +} + +fn registry_lock() -> Result>> { + registry() + .lock() + .map_err(|_| Error::internal("cache backend registry mutex is poisoned")) +} + +#[cfg(test)] +fn registry_lock_for_test() -> MutexGuard<'static, HashMap> { + registry() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) +} + +/// Register a constructor for a cache backend under `kind`. +/// +/// Returns `Err` if a non-built-in `kind` is already registered. Built-in +/// backends may be replaced so callers can mask a built-in implementation +/// (for example, a patched `"moka"` backend) without changing URI/config +/// strings elsewhere. +/// +/// Typical usage from a backend crate: +/// +/// ```ignore +/// pub fn register() -> lance_core::Result<()> { +/// lance_core::cache::register_backend("my_backend", build_my_backend) +/// } +/// ``` +pub fn register_backend(kind: &str, build: BackendBuildFn) -> Result<()> { + let kind = normalize_backend_kind(kind)?; + insert_backend(&kind, build, builtin_backend(&kind).is_some()) +} + +fn insert_backend(kind: &str, build: BackendBuildFn, allow_replace: bool) -> Result<()> { + let mut map = registry_lock()?; + if map.contains_key(kind) && !allow_replace { + return Err(Error::invalid_input(format!( + "cache backend {:?} is already registered", + kind + ))); + } + map.insert(kind.to_string(), build); + Ok(()) +} + +fn builtin_backend(kind: &str) -> Option { + match kind { + MOKA_BACKEND_KIND => Some(build_moka), + _ => None, + } +} + +/// Look up the constructor for `config.kind` and build a backend. +/// +/// Returns `Err` if no backend has been registered under that identifier. +pub fn build_from_config(config: &BackendConfig) -> Result> { + ensure_builtin_backends()?; + let kind = normalize_backend_kind(&config.kind)?; + let config = BackendConfig { + kind: kind.clone(), + options: config.options.clone(), + }; + let build = { + let map = registry_lock()?; + map.get(&kind).copied() + }; + match build { + Some(build) => build(&config), + None => Err(Error::invalid_input(format!( + "unknown cache backend kind: {:?}", + kind + ))), + } +} + +/// Idempotently register the backends that ship with `lance-core`. +/// +/// Called by [`build_from_config`] (and, transitively, by +/// [`build_from_uri`](super::backend_uri::build_from_uri)) so a bare Lance +/// installation can build a Moka backend without the caller having to +/// register it. Third-party backends still have to opt in with their own +/// `register()` call. +/// +/// The check is against the current registry contents rather than a +/// process-once flag so that `#[cfg(test)]` helpers which snapshot and +/// restore the registry still see the built-in backend after they take +/// ownership. +fn ensure_builtin_backends() -> Result<()> { + let mut map = registry_lock()?; + if !map.contains_key(MOKA_BACKEND_KIND) + && let Some(build) = builtin_backend(MOKA_BACKEND_KIND) + { + map.insert(MOKA_BACKEND_KIND.to_string(), build); + } + Ok(()) +} + +/// Test-only helper: replace the registry with an empty map so tests can +/// exercise duplicate-registration logic without polluting the global one. +#[cfg(test)] +pub(super) fn take_registry_for_test() -> HashMap { + let mut map = registry_lock_for_test(); + std::mem::take(&mut *map) +} + +/// Test-only helper: restore a previously captured registry state. +#[cfg(test)] +pub(super) fn restore_registry_for_test(saved: HashMap) { + let mut map = registry_lock_for_test(); + *map = saved; +} + +#[cfg(test)] +pub(super) fn registry_test_lock() -> std::sync::MutexGuard<'static, ()> { + static M: OnceLock> = OnceLock::new(); + M.get_or_init(|| std::sync::Mutex::new(())) + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) +} + +#[cfg(test)] +mod tests { + use super::*; + use async_trait::async_trait; + use std::pin::Pin; + + use crate::cache::InternalCacheKey; + use crate::cache::backend::CacheEntry; + use crate::cache::codec::CacheCodec; + use futures::Future; + + // A trivial no-op backend so tests do not depend on Moka or any other + // real backend. Every method returns "empty" / does nothing. + #[derive(Debug, Default)] + struct NullBackend; + + #[async_trait] + impl CacheBackend for NullBackend { + async fn get( + &self, + _key: &InternalCacheKey, + _codec: Option, + ) -> Option { + None + } + + async fn insert( + &self, + _key: &InternalCacheKey, + _entry: CacheEntry, + _size_bytes: usize, + _codec: Option, + ) { + } + + async fn get_or_insert<'a>( + &self, + _key: &InternalCacheKey, + loader: Pin> + Send + 'a>>, + _codec: Option, + ) -> crate::Result<(CacheEntry, bool)> { + let (entry, _size) = loader.await?; + Ok((entry, false)) + } + + async fn clear(&self) {} + async fn num_entries(&self) -> usize { + 0 + } + async fn size_bytes(&self) -> usize { + 0 + } + } + + fn build_null(_cfg: &BackendConfig) -> Result> { + Ok(Arc::new(NullBackend)) + } + + struct RegistryGuard { + // Hold the serialization lock for the full test. + _lock: std::sync::MutexGuard<'static, ()>, + saved: HashMap, + } + impl RegistryGuard { + fn new() -> Self { + Self { + _lock: registry_test_lock(), + saved: take_registry_for_test(), + } + } + } + impl Drop for RegistryGuard { + fn drop(&mut self) { + restore_registry_for_test(std::mem::take(&mut self.saved)); + } + } + + #[test] + fn test_register_and_build() { + let _guard = RegistryGuard::new(); + register_backend("null", build_null).unwrap(); + let backend = build_from_config(&BackendConfig::new("null").unwrap()).unwrap(); + // Backend is opaque; we just check that the constructor ran and + // gave us an Arc. + assert_eq!(Arc::strong_count(&backend), 1); + } + + #[test] + fn test_duplicate_registration_errors() { + let _guard = RegistryGuard::new(); + register_backend("dup", build_null).unwrap(); + let err = register_backend("dup", build_null).unwrap_err(); + assert!(err.to_string().contains("already registered")); + } + + #[test] + fn test_builtin_kind_can_be_overridden() { + let _guard = RegistryGuard::new(); + register_backend("moka", build_null).unwrap(); + let backend = build_from_config(&BackendConfig::new("moka").unwrap()).unwrap(); + assert_eq!(Arc::strong_count(&backend), 1); + } + + #[test] + fn test_unknown_kind_errors() { + let _guard = RegistryGuard::new(); + let err = build_from_config(&BackendConfig::new("missing").unwrap()).unwrap_err(); + assert!(err.to_string().contains("unknown cache backend kind")); + } + + #[test] + fn test_backend_kind_is_normalized() { + let _guard = RegistryGuard::new(); + register_backend("Echo.Backend", build_null).unwrap(); + let backend = build_from_config(&BackendConfig::new("echo.backend").unwrap()).unwrap(); + assert_eq!(Arc::strong_count(&backend), 1); + } + + #[test] + fn test_config_lookup_normalizes_direct_config() { + let _guard = RegistryGuard::new(); + fn build_echo(cfg: &BackendConfig) -> Result> { + assert_eq!(cfg.kind, "echo.backend"); + Ok(Arc::new(NullBackend)) + } + register_backend("echo.backend", build_echo).unwrap(); + let cfg = BackendConfig { + kind: "ECHO.Backend".to_string(), + options: HashMap::new(), + }; + build_from_config(&cfg).unwrap(); + } + + #[test] + fn test_invalid_backend_kind_errors() { + let err = register_backend("not a scheme", build_null).unwrap_err(); + assert!(err.to_string().contains("invalid character")); + let err = BackendConfig::new("1moka").unwrap_err(); + assert!(err.to_string().contains("must start with an ASCII letter")); + } + + #[test] + fn test_options_are_passed_through() { + let _guard = RegistryGuard::new(); + fn build_echo(cfg: &BackendConfig) -> Result> { + assert_eq!(cfg.options.get("capacity").map(String::as_str), Some("42")); + Ok(Arc::new(NullBackend)) + } + register_backend("echo", build_echo).unwrap(); + let cfg = BackendConfig::new("echo") + .unwrap() + .with_option("capacity", "42"); + build_from_config(&cfg).unwrap(); + } +} diff --git a/lance-artifact/rust/lance-core/src/container.rs b/lance-artifact/rust/lance-core/src/container.rs new file mode 100644 index 000000000..f92893bf0 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/container.rs @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod list; diff --git a/lance-artifact/rust/lance-core/src/container/list.rs b/lance-artifact/rust/lance-core/src/container/list.rs new file mode 100644 index 000000000..9d8205cb3 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/container/list.rs @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::collections::LinkedList; + +use crate::deepsize::DeepSizeOf; + +/// A linked list that grows exponentially. It is used to store a large number of +/// elements in a memory-efficient way. The list grows by doubling the capacity of +/// the last element when it's full, the capacity can be limited by the `limit` +/// parameter. The default value is 0, which means no limit. +#[derive(Debug, Clone, Default)] +pub struct ExpLinkedList { + inner: LinkedList>, + len: usize, + // The maximum capacity of single node in the list. + // If the limit is 0, there is no limit. + // We use u16 to save memory because ExpLinkedList should not + // be used if the limit is that large. + limit: u16, +} + +impl ExpLinkedList { + /// Creates a new empty `ExpLinkedList`. + pub fn new() -> Self { + Self { + inner: LinkedList::new(), + len: 0, + limit: 0, + } + } + + pub fn with_capacity(capacity: usize) -> Self { + let mut inner = LinkedList::new(); + inner.push_back(Vec::with_capacity(capacity)); + Self { + inner, + len: 0, + limit: 0, + } + } + + /// Creates a new `ExpLinkedList` with a specified capacity limit. + /// The limit is the maximum capacity of a single node in the list. + /// If the limit is 0, there is no limit. + pub fn with_capacity_limit(mut self, limit: u16) -> Self { + self.limit = limit; + self + } + + /// Pushes a new element into the list. If the last element in the list + /// reaches its capacity, a new node is created with double capacity. + pub fn push(&mut self, v: T) { + match self.inner.back() { + Some(last) => { + if last.len() == last.capacity() { + let new_cap = if self.limit > 0 && last.capacity() * 2 >= self.limit as usize { + self.limit as usize + } else { + last.capacity() * 2 + }; + self.inner.push_back(Vec::with_capacity(new_cap)); + } + } + None => { + self.inner.push_back(Vec::with_capacity(1)); + } + } + self.do_push(v); + } + + fn do_push(&mut self, v: T) { + self.inner.back_mut().unwrap().push(v); + self.len += 1; + } + + /// Removes the last element from the list. + pub fn pop(&mut self) -> Option { + match self.inner.back_mut() { + Some(last) => { + if last.is_empty() { + self.inner.pop_back(); + self.pop() + } else { + self.len -= 1; + last.pop() + } + } + None => None, + } + } + + /// Clears the list, removing all elements. + /// This will free the memory used by the list. + pub fn clear(&mut self) { + self.inner.clear(); + self.len = 0; + } + + /// Returns the number of elements in the list. + pub fn len(&self) -> usize { + self.len + } + + /// Returns whether the list is empty. + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + /// Returns the size of list, including the size of the elements and the + /// size of the list itself, and the unused space. + /// The element size is calculated using `std::mem::size_of::()`, + /// so it is not accurate for all types. + /// For example, for `String`, it will return the size of the pointer, + /// not the size of the string itself. For that you need to use `DeepSizeOf`. + pub fn size(&self) -> usize { + let unused_space = match self.inner.back() { + Some(last) => last.capacity() - last.len(), + None => 0, + }; + (self.len() + unused_space) * std::mem::size_of::() + + std::mem::size_of::() + + self.inner.len() * std::mem::size_of::>() + } + + /// Returns an iterator over the elements in the list. + pub fn iter(&self) -> ExpLinkedListIter<'_, T> { + ExpLinkedListIter::new(self) + } + + pub fn block_iter(&self) -> impl Iterator { + self.inner.iter().map(|v| v.as_slice()) + } +} + +impl DeepSizeOf for ExpLinkedList { + fn deep_size_of_children(&self, context: &mut crate::deepsize::Context) -> usize { + self.inner + .iter() + .map(|v| v.deep_size_of_children(context)) + .sum() + } +} + +impl FromIterator for ExpLinkedList { + fn from_iter>(iter: I) -> Self { + let iter = iter.into_iter(); + let size_hint = iter.size_hint().0; + let cap = if size_hint > 0 { size_hint } else { 1 }; + let mut list = Self::with_capacity(cap); + for item in iter { + list.push(item); + } + list + } +} + +impl PartialEq for ExpLinkedList +where + T: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.iter().zip(other.iter()).all(|(a, b)| a == b) + } +} + +impl Eq for ExpLinkedList where T: Eq {} + +pub struct ExpLinkedListIter<'a, T> { + inner: std::collections::linked_list::Iter<'a, Vec>, + inner_iter: Option>, + len: usize, +} + +impl<'a, T> ExpLinkedListIter<'a, T> { + pub fn new(inner: &'a ExpLinkedList) -> Self { + Self { + inner: inner.inner.iter(), + inner_iter: None, + len: inner.len(), + } + } +} + +impl<'a, T> Iterator for ExpLinkedListIter<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option { + if let Some(inner_iter) = &mut self.inner_iter + && let Some(v) = inner_iter.next() + { + return Some(v); + } + if let Some(inner) = self.inner.next() { + self.inner_iter = Some(inner.iter()); + return self.next(); + } + None + } + + fn size_hint(&self) -> (usize, Option) { + (self.len, Some(self.len)) + } +} + +pub struct ExpLinkedListIntoIter { + inner: std::collections::linked_list::IntoIter>, + inner_iter: Option>, + len: usize, +} + +impl ExpLinkedListIntoIter { + pub fn new(list: ExpLinkedList) -> Self { + let len = list.len(); + Self { + inner: list.inner.into_iter(), + inner_iter: None, + len, + } + } +} + +impl Iterator for ExpLinkedListIntoIter { + type Item = T; + + fn next(&mut self) -> Option { + if let Some(inner_iter) = &mut self.inner_iter + && let Some(v) = inner_iter.next() + { + return Some(v); + } + if let Some(inner) = self.inner.next() { + self.inner_iter = Some(inner.into_iter()); + return self.next(); + } + None + } + + fn size_hint(&self) -> (usize, Option) { + (self.len, Some(self.len)) + } +} + +impl IntoIterator for ExpLinkedList { + type Item = T; + type IntoIter = ExpLinkedListIntoIter; + + fn into_iter(self) -> Self::IntoIter { + ExpLinkedListIntoIter::new(self) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_exp_linked_list(list: &mut ExpLinkedList) { + assert_eq!(list.len(), 100); + assert!(!list.is_empty()); + + // removes the last 50 elements + for i in 0..50 { + assert_eq!(list.pop(), Some(99 - i)); + } + assert_eq!(list.len(), 50); + assert!(!list.is_empty()); + + // iterate over the list + for (i, v) in list.iter().enumerate() { + assert_eq!(*v, i); + } + + // clear the list + list.clear(); + assert_eq!(list.len(), 0); + assert!(list.is_empty()); + assert_eq!(list.pop(), None); + } + + #[test] + fn test_exp_linked_list_basic() { + let mut list = ExpLinkedList::new(); + for i in 0..100 { + list.push(i); + assert_eq!(list.len(), i + 1); + } + test_exp_linked_list(&mut list); + } + + #[test] + fn test_exp_linked_list_from() { + let mut list = (0..100).collect(); + test_exp_linked_list(&mut list); + } + + #[test] + fn test_exp_linked_list_with_capacity_limit() { + let mut list = ExpLinkedList::new().with_capacity_limit(10); + for i in 0..100 { + list.push(i); + assert_eq!(list.len(), i + 1); + } + assert_eq!(list.inner.back().unwrap().capacity(), 10); + test_exp_linked_list(&mut list); + } +} diff --git a/lance-artifact/rust/lance-core/src/datatypes.rs b/lance-artifact/rust/lance-core/src/datatypes.rs new file mode 100644 index 000000000..98eeebde5 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/datatypes.rs @@ -0,0 +1,687 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Lance data types, [Schema] and [Field] + +use std::collections::HashMap; +use std::fmt::{self, Debug, Formatter}; +use std::sync::{Arc, LazyLock}; + +use crate::deepsize::DeepSizeOf; +use arrow_array::ArrayRef; +use arrow_schema::{DataType, Field as ArrowField, Fields, TimeUnit}; +use lance_arrow::bfloat16::{BFLOAT16_EXT_NAME, is_bfloat16_field}; +use lance_arrow::{ARROW_EXT_META_KEY, ARROW_EXT_NAME_KEY}; + +mod field; +mod schema; + +use crate::{Error, Result}; +pub use field::{ + BlobVersion, Encoding, Field, LANCE_UNENFORCED_CLUSTERING_KEY_POSITION, + LANCE_UNENFORCED_PRIMARY_KEY, LANCE_UNENFORCED_PRIMARY_KEY_POSITION, NullabilityComparison, + OnTypeMismatch, SchemaCompareOptions, +}; +pub use schema::{ + BlobHandling, FieldRef, OnMissing, Projectable, Projection, Schema, + escape_field_path_for_project, format_field_path, format_field_path_minimal, parse_field_path, + validate_fixed_size_list_dimensions, +}; + +pub static BLOB_DESC_FIELDS: LazyLock = LazyLock::new(|| { + Fields::from(vec![ + ArrowField::new("position", DataType::UInt64, true), + ArrowField::new("size", DataType::UInt64, true), + ]) +}); + +pub static BLOB_DESC_TYPE: LazyLock = + LazyLock::new(|| DataType::Struct(BLOB_DESC_FIELDS.clone())); + +pub static BLOB_DESC_FIELD: LazyLock = LazyLock::new(|| { + ArrowField::new("description", BLOB_DESC_TYPE.clone(), true).with_metadata(HashMap::from([( + lance_arrow::BLOB_META_KEY.to_string(), + "true".to_string(), + )])) +}); + +pub static BLOB_DESC_LANCE_FIELD: LazyLock = + LazyLock::new(|| Field::try_from(&*BLOB_DESC_FIELD).unwrap()); + +/// The minimal logical blob v2 fields accepted from writers. +/// +/// Logical values may also use [`BLOB_V2_LOGICAL_FIELDS`] when an external +/// object range is present. +pub static BLOB_V2_LOGICAL_MINIMAL_FIELDS: LazyLock = LazyLock::new(|| { + Fields::from(vec![ + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ]) +}); + +/// The complete logical blob v2 fields used for writer input and rewrite output. +/// +/// `position` and `size` are an optional range within the external object named +/// by `uri`. They do not describe Lance-managed data, packed, or dedicated +/// storage. +pub static BLOB_V2_LOGICAL_FIELDS: LazyLock = LazyLock::new(|| { + let mut fields = BLOB_V2_LOGICAL_MINIMAL_FIELDS + .iter() + .cloned() + .collect::>(); + fields.extend([ + Arc::new(ArrowField::new("position", DataType::UInt64, true)), + Arc::new(ArrowField::new("size", DataType::UInt64, true)), + ]); + Fields::from(fields) +}); + +/// The complete logical blob v2 struct type. +pub static BLOB_V2_LOGICAL_TYPE: LazyLock = + LazyLock::new(|| DataType::Struct(BLOB_V2_LOGICAL_FIELDS.clone())); + +/// Writer-prepared blob v2 fields consumed by the structural encoder. +/// +/// The populated fields depend on [`BlobKind`]: +/// +/// - [`BlobKind::Inline`] carries `data`; the encoder derives the stored +/// `position` and `size` from the out-of-line buffer it creates. +/// - [`BlobKind::Packed`] carries `blob_id`, `position`, and `blob_size`. +/// - [`BlobKind::Dedicated`] carries `blob_id` and `blob_size`; its stored +/// `position` is zero. +/// - [`BlobKind::External`] carries `uri`, optional `blob_id`, `position`, and +/// `blob_size`. A zero `blob_size` is resolved to the complete external object +/// length when read. +/// +/// `blob_size` is distinct from the logical `size`, which is only an optional +/// external-object range before preparation. For external blobs, `uri` is +/// normalized into the stable stored `blob_uri` field. +pub static BLOB_V2_PREPARED_FIELDS: LazyLock = LazyLock::new(|| { + Fields::from(vec![ + ArrowField::new("kind", DataType::UInt8, true), + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ArrowField::new("blob_id", DataType::UInt32, true), + ArrowField::new("blob_size", DataType::UInt64, true), + ArrowField::new("position", DataType::UInt64, true), + ]) +}); + +/// The writer-prepared blob v2 struct type. +pub static BLOB_V2_PREPARED_TYPE: LazyLock = + LazyLock::new(|| DataType::Struct(BLOB_V2_PREPARED_FIELDS.clone())); + +/// Stored blob v2 descriptor fields. +/// +/// These field names are part of the stable file format. Their meaning depends +/// on `kind`: +/// +/// - [`BlobKind::Inline`]: `position` and `size` locate an out-of-line buffer in +/// the Lance data file. +/// - [`BlobKind::Packed`]: `blob_id` identifies a shared packed blob file, and +/// `position` and `size` locate a range within it. +/// - [`BlobKind::Dedicated`]: `blob_id` identifies a dedicated raw blob file, +/// `position` is zero, and `size` is the complete file length. +/// - [`BlobKind::External`]: `blob_uri` and `blob_id` identify the object, while +/// `position` and `size` select a range. A zero `size` is resolved to the +/// object's complete length when read. +pub static BLOB_V2_DESC_FIELDS: LazyLock = LazyLock::new(|| { + Fields::from(vec![ + ArrowField::new("kind", DataType::UInt8, false), + ArrowField::new("position", DataType::UInt64, false), + ArrowField::new("size", DataType::UInt64, false), + ArrowField::new("blob_id", DataType::UInt32, false), + ArrowField::new("blob_uri", DataType::Utf8, false), + ]) +}); + +pub static BLOB_V2_DESC_TYPE: LazyLock = + LazyLock::new(|| DataType::Struct(BLOB_V2_DESC_FIELDS.clone())); + +pub static BLOB_V2_DESC_FIELD: LazyLock = LazyLock::new(|| { + ArrowField::new("description", BLOB_V2_DESC_TYPE.clone(), false).with_metadata(HashMap::from([ + (lance_arrow::BLOB_META_KEY.to_string(), "true".to_string()), + ("lance-encoding:packed".to_string(), "true".to_string()), + ])) +}); + +pub static BLOB_V2_DESC_LANCE_FIELD: LazyLock = + LazyLock::new(|| Field::try_from(&*BLOB_V2_DESC_FIELD).unwrap()); + +/// The in-memory representation of a blob v2 struct. +#[non_exhaustive] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlobV2Layout { + /// Writer input or rewrite output. + /// + /// Both the minimal `data, uri` fields and the complete + /// `data, uri, position, size` fields have this layout. + Logical, + /// Kind-aware writer intermediate consumed by the structural encoder. + Prepared, + /// Stable descriptor stored in Lance files and returned by descriptor scans. + Descriptor, +} + +impl BlobV2Layout { + /// Classify blob v2 child fields by name, type, order, and layout-specific + /// nullability requirements. + /// + /// Child metadata is not part of the representation. The complete logical + /// layout also accepts non-nullable `position` and `size` fields, matching + /// the existing writer-input contract. Descriptor child nullability is + /// ignored because it changed across released schemas; row nullness has + /// been represented by either parent struct validity or a nullable `kind` + /// child. + pub fn classify(fields: &Fields) -> Option { + if logical_blob_v2_fields_match(fields) { + Some(Self::Logical) + } else if blob_v2_fields_match(fields, &BLOB_V2_PREPARED_FIELDS, true) { + Some(Self::Prepared) + } else if blob_v2_fields_match(fields, &BLOB_V2_DESC_FIELDS, false) { + Some(Self::Descriptor) + } else { + None + } + } +} + +impl fmt::Display for BlobV2Layout { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Logical => write!(f, "logical"), + Self::Prepared => write!(f, "prepared"), + Self::Descriptor => write!(f, "descriptor"), + } + } +} + +fn blob_v2_field_matches( + actual: &ArrowField, + expected: &ArrowField, + compare_nullability: bool, +) -> bool { + actual.name() == expected.name() + && actual.data_type() == expected.data_type() + && (!compare_nullability || actual.is_nullable() == expected.is_nullable()) +} + +fn blob_v2_fields_match(actual: &Fields, expected: &Fields, compare_nullability: bool) -> bool { + actual.len() == expected.len() + && actual + .iter() + .zip(expected.iter()) + .all(|(actual, expected)| { + blob_v2_field_matches(actual.as_ref(), expected.as_ref(), compare_nullability) + }) +} + +fn logical_blob_v2_fields_match(fields: &Fields) -> bool { + if blob_v2_fields_match(fields, &BLOB_V2_LOGICAL_MINIMAL_FIELDS, true) { + return true; + } + fields.len() == BLOB_V2_LOGICAL_FIELDS.len() + && fields + .iter() + .zip(BLOB_V2_LOGICAL_FIELDS.iter()) + .enumerate() + .all(|(index, (actual, expected))| { + blob_v2_field_matches(actual.as_ref(), expected.as_ref(), index < 2) + }) +} + +/// Deprecated name for [`BLOB_V2_LOGICAL_FIELDS`]. +#[deprecated(note = "use BLOB_V2_LOGICAL_FIELDS")] +pub use self::BLOB_V2_LOGICAL_FIELDS as BLOB_V2_USER_FIELDS; + +/// Deprecated name for [`BLOB_V2_LOGICAL_TYPE`]. +#[deprecated(note = "use BLOB_V2_LOGICAL_TYPE")] +pub use self::BLOB_V2_LOGICAL_TYPE as BLOB_V2_USER_TYPE; + +pub const BLOB_LOGICAL_TYPE: &str = "blob"; + +/// LogicalType is a string presentation of arrow type. +/// to be serialized into protobuf. +#[derive(Debug, Clone, PartialEq, DeepSizeOf)] +pub struct LogicalType(String); + +impl fmt::Display for LogicalType { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +impl LogicalType { + fn is_list(&self) -> bool { + self.0 == "list" || self.0 == "list.struct" + } + + fn is_large_list(&self) -> bool { + self.0 == "large_list" || self.0 == "large_list.struct" + } + + fn is_fixed_size_list_struct(&self) -> bool { + self.0.starts_with("fixed_size_list:struct:") + } + + pub fn is_struct(&self) -> bool { + self.0 == "struct" + } + + fn is_blob(&self) -> bool { + self.0 == BLOB_LOGICAL_TYPE + } + + fn is_map(&self) -> bool { + self.0 == "map" + } +} + +impl From<&str> for LogicalType { + fn from(s: &str) -> Self { + Self(s.to_string()) + } +} + +fn timeunit_to_str(unit: &TimeUnit) -> &'static str { + match unit { + TimeUnit::Second => "s", + TimeUnit::Millisecond => "ms", + TimeUnit::Microsecond => "us", + TimeUnit::Nanosecond => "ns", + } +} + +fn is_supported_fixed_size_list_child(data_type: &DataType, nested: bool) -> bool { + match data_type { + DataType::Struct(_) => !nested, + DataType::List(_) | DataType::LargeList(_) | DataType::Map(_, _) => false, + DataType::FixedSizeList(field, _) => { + is_supported_fixed_size_list_child(field.data_type(), true) + } + _ => true, + } +} + +fn parse_timeunit(unit: &str) -> Result { + match unit { + "s" => Ok(TimeUnit::Second), + "ms" => Ok(TimeUnit::Millisecond), + "us" => Ok(TimeUnit::Microsecond), + "ns" => Ok(TimeUnit::Nanosecond), + _ => Err(Error::arrow(format!("Unsupported TimeUnit: {unit}"))), + } +} + +impl TryFrom<&DataType> for LogicalType { + type Error = Error; + + fn try_from(dt: &DataType) -> Result { + let type_str = match dt { + DataType::Null => "null".to_string(), + DataType::Boolean => "bool".to_string(), + DataType::Int8 => "int8".to_string(), + DataType::UInt8 => "uint8".to_string(), + DataType::Int16 => "int16".to_string(), + DataType::UInt16 => "uint16".to_string(), + DataType::Int32 => "int32".to_string(), + DataType::UInt32 => "uint32".to_string(), + DataType::Int64 => "int64".to_string(), + DataType::UInt64 => "uint64".to_string(), + DataType::Float16 => "halffloat".to_string(), + DataType::Float32 => "float".to_string(), + DataType::Float64 => "double".to_string(), + DataType::Decimal128(precision, scale) => format!("decimal:128:{precision}:{scale}"), + DataType::Decimal256(precision, scale) => format!("decimal:256:{precision}:{scale}"), + DataType::Utf8 | DataType::Utf8View => "string".to_string(), + DataType::Binary | DataType::BinaryView => "binary".to_string(), + DataType::LargeUtf8 => "large_string".to_string(), + DataType::LargeBinary => "large_binary".to_string(), + DataType::Date32 => "date32:day".to_string(), + DataType::Date64 => "date64:ms".to_string(), + DataType::Time32(tu) => format!("time32:{}", timeunit_to_str(tu)), + DataType::Time64(tu) => format!("time64:{}", timeunit_to_str(tu)), + DataType::Timestamp(tu, tz) => format!( + "timestamp:{}:{}", + timeunit_to_str(tu), + tz.as_ref() + .map(|v| v.to_string()) + .unwrap_or("-".to_string()) + ), + DataType::Duration(tu) => format!("duration:{}", timeunit_to_str(tu)), + DataType::Struct(_) => "struct".to_string(), + DataType::Dictionary(key_type, value_type) => { + format!( + "dict:{}:{}:{}", + Self::try_from(value_type.as_ref())?.0, + Self::try_from(key_type.as_ref())?.0, + // Arrow C++ Dictionary has "ordered:bool" field, but it does not exist in `arrow-rs`. + false + ) + } + DataType::List(elem) => match elem.data_type() { + DataType::Struct(_) => "list.struct".to_string(), + _ => "list".to_string(), + }, + DataType::LargeList(elem) => match elem.data_type() { + DataType::Struct(_) => "large_list.struct".to_string(), + _ => "large_list".to_string(), + }, + DataType::FixedSizeList(field, len) => { + if is_bfloat16_field(field) { + // Don't want to directly use `bfloat16`, in case a built-in type is added + // that isn't identical to our extension type. + format!("fixed_size_list:lance.bfloat16:{}", *len) + } else if !is_supported_fixed_size_list_child(field.data_type(), false) { + return Err(Error::schema(format!("Unsupported data type: {:?}", dt))); + } else { + format!( + "fixed_size_list:{}:{}", + Self::try_from(field.data_type())?.0, + *len + ) + } + } + DataType::FixedSizeBinary(len) => format!("fixed_size_binary:{}", *len), + DataType::Map(_, keys_sorted) => { + // TODO: We only support keys_sorted=false for now, + // because converting a rust arrow map field to the python arrow field will + // lose the keys_sorted property. + if *keys_sorted { + return Err(Error::schema(format!( + "Unsupported map data type with keys_sorted=true: {:?}", + dt + ))); + } + "map".to_string() + } + _ => { + return Err(Error::schema(format!("Unsupported data type: {:?}", dt))); + } + }; + + Ok(Self(type_str)) + } +} + +impl TryFrom<&LogicalType> for DataType { + type Error = Error; + + fn try_from(lt: &LogicalType) -> Result { + use DataType::*; + if let Some(t) = match lt.0.as_str() { + "null" => Some(Null), + "bool" => Some(Boolean), + "int8" => Some(Int8), + "uint8" => Some(UInt8), + "int16" => Some(Int16), + "uint16" => Some(UInt16), + "int32" => Some(Int32), + "uint32" => Some(UInt32), + "int64" => Some(Int64), + "uint64" => Some(UInt64), + "halffloat" => Some(Float16), + "float" => Some(Float32), + "double" => Some(Float64), + "string" => Some(Utf8), + "binary" => Some(Binary), + "large_string" => Some(LargeUtf8), + "large_binary" => Some(LargeBinary), + BLOB_LOGICAL_TYPE => Some(LargeBinary), + "json" => Some(LargeBinary), + "date32:day" => Some(Date32), + "date64:ms" => Some(Date64), + "time32:s" => Some(Time32(TimeUnit::Second)), + "time32:ms" => Some(Time32(TimeUnit::Millisecond)), + "time64:us" => Some(Time64(TimeUnit::Microsecond)), + "time64:ns" => Some(Time64(TimeUnit::Nanosecond)), + "duration:s" => Some(Duration(TimeUnit::Second)), + "duration:ms" => Some(Duration(TimeUnit::Millisecond)), + "duration:us" => Some(Duration(TimeUnit::Microsecond)), + "duration:ns" => Some(Duration(TimeUnit::Nanosecond)), + _ => None, + } { + Ok(t) + } else { + let splits = lt.0.split(':').collect::>(); + match splits[0] { + "fixed_size_list" => { + if splits.len() < 3 { + return Err(Error::schema(format!("Unsupported logical type: {}", lt))); + } + + let size: i32 = splits + .last() + .unwrap() + .parse::() + .map_err(|e: _| Error::schema(e.to_string()))?; + + let inner_type = splits[1..splits.len() - 1].join(":"); + + match inner_type.as_str() { + BFLOAT16_EXT_NAME => { + let field = ArrowField::new("item", Self::FixedSizeBinary(2), true) + .with_metadata( + [ + (ARROW_EXT_NAME_KEY.into(), BFLOAT16_EXT_NAME.into()), + (ARROW_EXT_META_KEY.into(), "".into()), + ] + .into(), + ); + Ok(FixedSizeList(Arc::new(field), size)) + } + data_type => { + let elem_type = (&LogicalType(data_type.to_string())).try_into()?; + + Ok(FixedSizeList( + Arc::new(ArrowField::new("item", elem_type, true)), + size, + )) + } + } + } + "fixed_size_binary" => { + if splits.len() != 2 { + Err(Error::schema(format!("Unsupported logical type: {}", lt))) + } else { + let size: i32 = splits[1] + .parse::() + .map_err(|e: _| Error::schema(e.to_string()))?; + Ok(FixedSizeBinary(size)) + } + } + "dict" => { + if splits.len() != 4 { + Err(Error::schema(format!( + "Unsupported dictionary type: {}", + lt + ))) + } else { + let value_type: Self = (&LogicalType::from(splits[1])).try_into()?; + let index_type: Self = (&LogicalType::from(splits[2])).try_into()?; + Ok(Dictionary(Box::new(index_type), Box::new(value_type))) + } + } + "decimal" => { + if splits.len() != 4 { + Err(Error::schema(format!("Unsupported decimal type: {}", lt))) + } else { + let bits: i16 = splits[1] + .parse::() + .map_err(|err| Error::schema(err.to_string()))?; + let precision: u8 = splits[2] + .parse::() + .map_err(|err| Error::schema(err.to_string()))?; + let scale: i8 = splits[3] + .parse::() + .map_err(|err| Error::schema(err.to_string()))?; + + if bits == 128 { + Ok(Decimal128(precision, scale)) + } else if bits == 256 { + Ok(Decimal256(precision, scale)) + } else { + Err(Error::schema(format!( + "Only Decimal128 and Decimal256 is supported. Found {bits}" + ))) + } + } + } + "timestamp" => { + if splits.len() != 3 { + Err(Error::schema(format!("Unsupported timestamp type: {}", lt))) + } else { + let timeunit = parse_timeunit(splits[1])?; + let tz: Option> = if splits[2] == "-" { + None + } else { + Some(splits[2].into()) + }; + Ok(Timestamp(timeunit, tz)) + } + } + _ => Err(Error::schema(format!("Unsupported logical type: {}", lt))), + } + } + } +} + +#[derive(Debug, Clone, Default)] +pub struct Dictionary { + pub offset: usize, + + pub length: usize, + + pub values: Option, +} + +impl DeepSizeOf for Dictionary { + fn deep_size_of_children(&self, context: &mut crate::deepsize::Context) -> usize { + self.values + .as_ref() + .map(|v| (v.as_ref() as &dyn arrow_array::Array).deep_size_of_children(context)) + .unwrap_or(0) + } +} + +impl PartialEq for Dictionary { + fn eq(&self, other: &Self) -> bool { + match (&self.values, &other.values) { + (Some(a), Some(b)) => a == b, + _ => false, + } + } +} + +/// Physical storage mode for blob v2 descriptors (one byte, stored in the packed struct column). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum BlobKind { + /// Stored in the main data file’s out-of-line buffer; `position`/`size` point into that file. + Inline = 0, + /// Stored in a shared packed blob file; `position`/`size` locate the slice, `blob_id` selects the file. + Packed = 1, + /// Stored in a dedicated raw blob file; `blob_id` identifies the file, `size` is the full file length. + Dedicated = 2, + /// Not stored by Lance data files. + /// + /// For external blobs: + /// - `blob_id == 0` means `blob_uri` is an absolute external URI. + /// - `blob_id > 0` means `blob_uri` is a path relative to `manifest.base_paths[blob_id]`. + /// + /// External blobs can have a position and a size. If the position is not set, + /// it defaults to 0, which points to the beginning of the blob. + External = 3, +} + +impl TryFrom for BlobKind { + type Error = Error; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(Self::Inline), + 1 => Ok(Self::Packed), + 2 => Ok(Self::Dedicated), + 3 => Ok(Self::External), + other => Err(Error::invalid_input_source( + format!("Unknown blob kind {other:?}").into(), + )), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_classify_blob_v2_layouts() { + assert_eq!( + BlobV2Layout::classify(&BLOB_V2_LOGICAL_MINIMAL_FIELDS), + Some(BlobV2Layout::Logical) + ); + assert_eq!( + BlobV2Layout::classify(&BLOB_V2_LOGICAL_FIELDS), + Some(BlobV2Layout::Logical) + ); + assert_eq!( + BlobV2Layout::classify(&BLOB_V2_PREPARED_FIELDS), + Some(BlobV2Layout::Prepared) + ); + assert_eq!( + BlobV2Layout::classify(&BLOB_V2_DESC_FIELDS), + Some(BlobV2Layout::Descriptor) + ); + } + + #[test] + fn test_classify_blob_v2_layout_uses_structural_contract() { + let logical_with_required_range = Fields::from(vec![ + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ArrowField::new("position", DataType::UInt64, false), + ArrowField::new("size", DataType::UInt64, false), + ]); + assert_eq!( + BlobV2Layout::classify(&logical_with_required_range), + Some(BlobV2Layout::Logical) + ); + + let prepared_with_child_metadata = + Fields::from( + BLOB_V2_PREPARED_FIELDS + .iter() + .map(|field| { + Arc::new(field.as_ref().clone().with_metadata(HashMap::from([( + "source".to_string(), + "test".to_string(), + )]))) + }) + .collect::>(), + ); + assert_eq!( + BlobV2Layout::classify(&prepared_with_child_metadata), + Some(BlobV2Layout::Prepared) + ); + + let nullable_descriptor = Fields::from( + BLOB_V2_DESC_FIELDS + .iter() + .map(|field| Arc::new(field.as_ref().clone().with_nullable(true))) + .collect::>(), + ); + assert_eq!( + BlobV2Layout::classify(&nullable_descriptor), + Some(BlobV2Layout::Descriptor) + ); + + let malformed_descriptor = Fields::from(vec![ + ArrowField::new("kind", DataType::UInt8, false), + ArrowField::new("position", DataType::UInt64, false), + ArrowField::new("size", DataType::UInt32, false), + ArrowField::new("blob_id", DataType::UInt32, false), + ArrowField::new("blob_uri", DataType::Utf8, false), + ]); + assert_eq!(BlobV2Layout::classify(&malformed_descriptor), None); + } +} diff --git a/lance-artifact/rust/lance-core/src/datatypes/field.rs b/lance-artifact/rust/lance-core/src/datatypes/field.rs new file mode 100644 index 000000000..d5eb89dcc --- /dev/null +++ b/lance-artifact/rust/lance-core/src/datatypes/field.rs @@ -0,0 +1,2044 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Lance Schema Field + +use std::{ + cmp::{Ordering, max}, + collections::{HashMap, VecDeque}, + fmt, + sync::Arc, +}; + +use crate::deepsize::DeepSizeOf; +use arrow_array::{ + ArrayRef, + cast::AsArray, + types::{ + Int8Type, Int16Type, Int32Type, Int64Type, UInt8Type, UInt16Type, UInt32Type, UInt64Type, + }, +}; +use arrow_schema::{DataType, Field as ArrowField}; +use lance_arrow::{ + ARROW_EXT_NAME_KEY, BLOB_META_KEY, BLOB_V2_EXT_NAME, DataTypeExt, + json::{is_arrow_json_field, is_json_field}, +}; + +use super::{ + Dictionary, LogicalType, Projection, + schema::{compare_fields, explain_fields_difference}, +}; +use crate::{ + Error, Result, + datatypes::{BLOB_DESC_LANCE_FIELD, BLOB_V2_DESC_LANCE_FIELD}, +}; + +/// Use this config key in Arrow field metadata to indicate a column is a part of the primary key. +/// The value can be any true values like `true`, `1`, `yes` (case-insensitive). +/// A primary key column must satisfy: +/// (1) The field, and all its ancestors must not be nullable. +/// (2) The field must be a leaf without child (i.e. it is a primitive data type). +/// (3) The field must not be within a list type. +pub const LANCE_UNENFORCED_PRIMARY_KEY: &str = "lance-schema:unenforced-primary-key"; + +/// Use this config key in Arrow field metadata to specify the position of a primary key column. +/// The value is a 1-based integer indicating the order within the composite primary key. +/// When specified, primary key fields are ordered by this position value. +/// When not specified, primary key fields are ordered by their schema field id. +pub const LANCE_UNENFORCED_PRIMARY_KEY_POSITION: &str = + "lance-schema:unenforced-primary-key:position"; + +/// Use this config key in Arrow field metadata to specify the position of a clustering key column. +/// The value is a 1-based integer indicating the order within the composite clustering key. +/// Clustering key fields are ordered by this position value. +pub const LANCE_UNENFORCED_CLUSTERING_KEY_POSITION: &str = + "lance-schema:unenforced-clustering-key:position"; + +/// Use this config key in Arrow field metadata to specify the field id of the lance field. +/// The value should be non-negative i32 value. Any negative value will be seen as -1. +pub const LANCE_FIELD_ID_KEY: &str = "lance:field_id"; + +const PACKED_KEYS: [&str; 2] = ["packed", "lance-encoding:packed"]; + +fn has_blob_v2_extension(field: &ArrowField) -> bool { + field + .metadata() + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == BLOB_V2_EXT_NAME) + .unwrap_or(false) +} + +#[derive(Debug, Default)] +pub enum NullabilityComparison { + // If the nullabilities don't match then the fields don't match + #[default] + Strict, + // If the expected schema is nullable then a non-nullable version of the field is allowed + OneWay, + // Nullability is ignored when comparing fields + Ignore, +} + +#[derive(Default)] +pub struct SchemaCompareOptions { + /// Should the metadata be compared (default false) + pub compare_metadata: bool, + /// Should the dictionaries be compared (default false) + pub compare_dictionary: bool, + /// Should the field ids be compared (default false) + pub compare_field_ids: bool, + /// Should nullability be compared (default Strict) + pub compare_nullability: NullabilityComparison, + /// Allow fields in the expected schema to be missing from the schema being tested if + /// they are nullable (default false) + /// + /// Fields in the schema being tested must always be present in the expected schema + /// regardless of this flag. + pub allow_missing_if_nullable: bool, + /// Allow out of order fields (default false) + pub ignore_field_order: bool, + /// Allow the source schema to be a subset of the target schema (default false) + pub allow_subschema: bool, +} + +/// Blob column format version. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum BlobVersion { + /// Legacy blob format (position / size only). + #[default] + V1, + /// Blob v2 struct format. + V2, +} +/// Encoding enum. +#[derive(Debug, Clone, PartialEq, Eq, DeepSizeOf)] +pub enum Encoding { + /// Plain encoding. + Plain, + /// Binary encoding. + VarBinary, + /// Dictionary encoding. + Dictionary, + /// RLE encoding. + RLE, +} + +/// What to do on a merge operation if the types of the fields don't match +#[derive(Debug, Clone, Copy, PartialEq, Eq, DeepSizeOf)] +pub enum OnTypeMismatch { + TakeSelf, + Error, +} + +/// Lance Schema Field +/// +#[derive(Debug, Clone, PartialEq, DeepSizeOf)] +pub struct Field { + pub name: String, + pub id: i32, + // TODO: Find way to move these next three fields to private + pub parent_id: i32, + pub logical_type: LogicalType, + pub metadata: HashMap, + pub encoding: Option, + pub nullable: bool, + + pub children: Vec, + + /// Dictionary value array if this field is dictionary. + pub dictionary: Option, + + /// Position of this field in the primary key (1-based). + /// None means the field is not part of the primary key. + /// Some(n) means this field is the nth column in the primary key. + pub unenforced_primary_key_position: Option, + + /// Position of this field in the clustering key (1-based). + /// None means the field is not part of the clustering key. + /// Some(n) means this field is the nth column in the clustering key. + pub unenforced_clustering_key_position: Option, +} + +impl Field { + /// Shortcut for creating a field with no field id (i.e. from the same info + /// needed to create an Arrow field) + pub fn new_arrow(name: &str, data_type: DataType, nullable: bool) -> Result { + Self::try_from(ArrowField::new(name, data_type, nullable)) + } + + /// Returns arrow data type. + pub fn data_type(&self) -> DataType { + match &self.logical_type { + lt if lt.is_list() => DataType::List(Arc::new(ArrowField::from(&self.children[0]))), + lt if lt.is_large_list() => { + DataType::LargeList(Arc::new(ArrowField::from(&self.children[0]))) + } + lt if lt.is_fixed_size_list_struct() => { + // Parse size from "fixed_size_list:struct:N" + let size: i32 = + lt.0.split(':') + .next_back() + .expect("fixed_size_list:struct logical type missing size suffix") + .parse() + .expect("fixed_size_list:struct logical type has invalid size"); + DataType::FixedSizeList(Arc::new(ArrowField::from(&self.children[0])), size) + } + lt if lt.is_struct() => { + DataType::Struct(self.children.iter().map(ArrowField::from).collect()) + } + lt if lt.is_map() => { + DataType::Map(Arc::new(ArrowField::from(&self.children[0])), false) + } + lt => DataType::try_from(lt).unwrap(), + } + } + + pub fn has_dictionary_types(&self) -> bool { + matches!(self.data_type(), DataType::Dictionary(_, _)) + || self.children.iter().any(Self::has_dictionary_types) + } + + /// Merge a field with another field using a reference field to ensure + /// the correct order of fields + /// + /// For each child in the reference field we look for a matching child + /// in self and other. + /// + /// If we find a match in both we recursively merge the children. + /// If we find a match in one but not the other we take the matching child. + /// + /// Primitive fields we simply clone self and return. + /// + /// Matches are determined using field names and so ids are not required. + pub fn merge_with_reference(&self, other: &Self, reference: &Self) -> Self { + let mut new_children = Vec::with_capacity(reference.children.len()); + let mut self_children_itr = self.children.iter().peekable(); + let mut other_children_itr = other.children.iter().peekable(); + for ref_child in &reference.children { + match (self_children_itr.peek(), other_children_itr.peek()) { + (Some(&only_child), None) => { + // other is exhausted so just check if self matches + if only_child.name == ref_child.name { + new_children.push(only_child.clone()); + self_children_itr.next(); + } + } + (None, Some(&only_child)) => { + // Self is exhausted so just check if other matches + if only_child.name == ref_child.name { + new_children.push(only_child.clone()); + other_children_itr.next(); + } + } + (Some(&self_child), Some(&other_child)) => { + // Both iterators have potential, see if any match + match ( + ref_child.name.cmp(&self_child.name), + ref_child.name.cmp(&other_child.name), + ) { + (Ordering::Equal, Ordering::Equal) => { + // Both match, recursively merge + new_children + .push(self_child.merge_with_reference(other_child, ref_child)); + self_children_itr.next(); + other_children_itr.next(); + } + (Ordering::Equal, _) => { + // Self matches, other doesn't, use self as-is + new_children.push(self_child.clone()); + self_children_itr.next(); + } + (_, Ordering::Equal) => { + // Other matches, self doesn't, use other as-is + new_children.push(other_child.clone()); + other_children_itr.next(); + } + _ => { + // Neither match, field is projected out + } + } + } + (None, None) => { + // Both iterators are exhausted, we can quit, all remaining fields projected out + break; + } + } + } + Self { + children: new_children, + ..self.clone() + } + } + + pub fn apply_projection(&self, projection: &Projection) -> Option { + // Maps and blob descriptors are atomic physical layouts. Map children + // must remain together, while projected blob descriptor children may + // have synthetic IDs that cannot be selected independently. + let is_atomic_layout = self.logical_type.is_map() || self.is_blob(); + if is_atomic_layout && !projection.contains_field_id(self.id) { + return None; + } + + let children = if is_atomic_layout { + self.children.clone() + } else { + self.children + .iter() + .filter_map(|c| c.apply_projection(projection)) + .collect::>() + }; + + // The following case is invalid: + // - This is a nested field (has children) + // - All children were projected away + // - Caller is asking for the parent field + assert!( + // One of the following must be true + !children.is_empty() // Some children were projected + || !projection.contains_field_id(self.id) // Caller is not asking for this field + || self.children.is_empty() // This isn't a nested field + ); + + if children.is_empty() && !projection.contains_field_id(self.id) { + None + } else { + let mut new_field = self.clone(); + new_field.children = children; + Some(projection.blob_handling.unload_if_needed(new_field)) + } + } + + pub(crate) fn explain_differences( + &self, + expected: &Self, + options: &SchemaCompareOptions, + path: Option<&str>, + ) -> Vec { + let mut differences = Vec::new(); + let self_name = path + .map(|path| { + let mut self_name = path.to_owned(); + self_name.push('.'); + self_name.push_str(&self.name); + self_name + }) + .unwrap_or_else(|| self.name.clone()); + if self.name != expected.name { + let expected_path = path + .map(|path| { + let mut expected_path = path.to_owned(); + expected_path.push('.'); + expected_path.push_str(&expected.name); + expected_path + }) + .unwrap_or_else(|| expected.name.clone()); + differences.push(format!( + "expected name '{}' but name was '{}'", + expected_path, self_name + )); + } + if options.compare_field_ids && self.id != expected.id { + differences.push(format!( + "`{}` should have id {} but id was {}", + self_name, expected.id, self.id + )); + } + if self.logical_type != expected.logical_type { + differences.push(format!( + "`{}` should have type {} but type was {}", + self_name, expected.logical_type, self.logical_type + )); + } + if !Self::compare_nullability(expected.nullable, self.nullable, options) { + differences.push(format!( + "`{}` should have nullable={} but nullable={}", + self_name, expected.nullable, self.nullable + )) + } + if options.compare_dictionary && self.dictionary != expected.dictionary { + differences.push(format!( + "dictionary for `{}` did not match expected dictionary", + self_name + )); + } + if options.compare_metadata && self.metadata != expected.metadata { + differences.push(format!( + "metadata for `{}` did not match expected metadata", + self_name + )); + } + let children_differences = explain_fields_difference( + &self.children, + &expected.children, + options, + Some(&self_name), + ); + if !children_differences.is_empty() { + let children_differences = format!( + "`{}` had mismatched children: {}", + self_name, + children_differences.join(", ") + ); + differences.push(children_differences); + } + differences + } + + pub fn explain_difference( + &self, + expected: &Self, + options: &SchemaCompareOptions, + ) -> Option { + let differences = self.explain_differences(expected, options, None); + if differences.is_empty() { + None + } else { + Some(differences.join(", ")) + } + } + + pub fn compare_nullability( + expected_nullability: bool, + actual_nullability: bool, + options: &SchemaCompareOptions, + ) -> bool { + match options.compare_nullability { + NullabilityComparison::Strict => expected_nullability == actual_nullability, + NullabilityComparison::OneWay => expected_nullability || !actual_nullability, + NullabilityComparison::Ignore => true, + } + } + + pub fn compare_with_options(&self, expected: &Self, options: &SchemaCompareOptions) -> bool { + self.name == expected.name + && self.logical_type == expected.logical_type + && Self::compare_nullability(expected.nullable, self.nullable, options) + && compare_fields(&self.children, &expected.children, options) + && (!options.compare_field_ids || self.id == expected.id) + && (!options.compare_dictionary || self.dictionary == expected.dictionary) + && (!options.compare_metadata || self.metadata == expected.metadata) + } + + pub fn extension_name(&self) -> Option<&str> { + self.metadata.get(ARROW_EXT_NAME_KEY).map(String::as_str) + } + + pub fn child(&self, name: &str) -> Option<&Self> { + self.children.iter().find(|f| f.name == name) + } + + pub fn child_mut(&mut self, name: &str) -> Option<&mut Self> { + self.children.iter_mut().find(|f| f.name == name) + } + + /// Attach the Dictionary's value array, so that we can later serialize + /// the dictionary to the manifest. + pub fn set_dictionary_values(&mut self, arr: &ArrayRef) { + assert!(self.data_type().is_dictionary()); + // offset / length are set to 0 and recomputed when the dictionary is persisted to disk + self.dictionary = Some(Dictionary { + offset: 0, + length: 0, + values: Some(arr.clone()), + }); + } + + pub fn set_dictionary(&mut self, arr: &ArrayRef) { + let data_type = self.data_type(); + match data_type { + DataType::Dictionary(key_type, _) => match key_type.as_ref() { + DataType::Int8 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::Int16 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::Int32 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::Int64 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::UInt8 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::UInt16 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::UInt32 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + DataType::UInt64 => { + self.set_dictionary_values(arr.as_dictionary::().values()) + } + _ => { + panic!("Unsupported dictionary key type: {}", key_type); + } + }, + DataType::Struct(subfields) => { + for (i, f) in subfields.iter().enumerate() { + let lance_field = self + .children + .iter_mut() + .find(|c| c.name == *f.name()) + .unwrap(); + let struct_arr = arr.as_struct(); + lance_field.set_dictionary(struct_arr.column(i)); + } + } + DataType::List(_) => { + let list_arr = arr.as_list::(); + self.children[0].set_dictionary(list_arr.values()); + } + DataType::LargeList(_) => { + let list_arr = arr.as_list::(); + self.children[0].set_dictionary(list_arr.values()); + } + _ => { + // Field types that don't support dictionaries + } + } + } + + pub fn sub_field(&self, path_components: &[&str]) -> Option<&Self> { + if path_components.is_empty() { + Some(self) + } else { + let first = path_components[0]; + self.children + .iter() + .find(|c| c.name == first) + .and_then(|c| c.sub_field(&path_components[1..])) + } + } + + pub fn sub_field_mut(&mut self, path_components: &[&str]) -> Option<&mut Self> { + if path_components.is_empty() { + Some(self) + } else { + let first = path_components[0]; + self.children + .iter_mut() + .find(|c| c.name == first) + .and_then(|c| c.sub_field_mut(&path_components[1..])) + } + } + + /// Check if the user has labeled the field as a blob + /// + /// Blob fields will load descriptions by default + pub fn is_blob(&self) -> bool { + self.metadata.contains_key(BLOB_META_KEY) + || self + .metadata + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == BLOB_V2_EXT_NAME) + .unwrap_or(false) + } + + /// Returns true if the field is explicitly marked as blob v2 extension. + pub fn is_blob_v2(&self) -> bool { + self.metadata + .get(ARROW_EXT_NAME_KEY) + .map(|name| name == BLOB_V2_EXT_NAME) + .unwrap_or(false) + || self.is_blob_v2_descriptor() + } + + fn is_blob_v2_descriptor(&self) -> bool { + self.metadata.contains_key(BLOB_META_KEY) + && self.logical_type == BLOB_V2_DESC_LANCE_FIELD.logical_type + && self.children.len() == BLOB_V2_DESC_LANCE_FIELD.children.len() + && self + .children + .iter() + .zip(BLOB_V2_DESC_LANCE_FIELD.children.iter()) + .all(|(child, expected)| { + child.name == expected.name && child.data_type() == expected.data_type() + }) + } + + // Blob columns intentionally have two schema representations: + // the loaded value view (legacy LargeBinary or blob v2 struct) and the unloaded + // descriptor view used by projection/planning. Schema set operations need to + // treat them as the same logical column instead of ordinary incompatible types. + fn is_compatible_blob_projection(&self, other: &Self) -> bool { + self.is_blob() && other.is_blob() && self.is_blob_v2() == other.is_blob_v2() + } + + /// If the field is a blob, update this field with the same name and id + /// but with the data type set to a struct of the blob description fields. + /// + /// If the field is not a blob, return the field itself. + pub fn unloaded_mut(&mut self) { + if self.is_blob_v2() { + self.logical_type = BLOB_V2_DESC_LANCE_FIELD.logical_type.clone(); + self.children = BLOB_V2_DESC_LANCE_FIELD.children.clone(); + self.metadata = BLOB_V2_DESC_LANCE_FIELD.metadata.clone(); + } else if self.is_blob() { + self.logical_type = BLOB_DESC_LANCE_FIELD.logical_type.clone(); + self.children = BLOB_DESC_LANCE_FIELD.children.clone(); + self.metadata = BLOB_DESC_LANCE_FIELD.metadata.clone(); + } + } + + /// Convert a blob field to the materialized binary payload view. + /// + /// The field keeps its name and id but uses `LargeBinary` with no children. + /// Blob v2 fields retain their extension marker internally so scan planning + /// can recognize the binary view before exposing a plain Arrow binary field. + pub fn binary_blob_mut(&mut self) { + if !self.is_blob() { + return; + } + let is_blob_v2 = self.is_blob_v2(); + + self.logical_type = LogicalType::try_from(&DataType::LargeBinary) + .expect("LargeBinary is always a valid logical type"); + self.children.clear(); + self.encoding = Some(Encoding::VarBinary); + if is_blob_v2 { + self.metadata.remove(BLOB_META_KEY); + for key in PACKED_KEYS { + self.metadata.remove(key); + } + self.metadata + .insert(ARROW_EXT_NAME_KEY.to_string(), BLOB_V2_EXT_NAME.to_string()); + } + } + + /// Convert blob v2 fields in this field tree to their descriptor view. + pub fn unload_blobs_recursive(&mut self) { + if self.is_blob_v2() { + self.unloaded_mut(); + return; + } + + for child in &mut self.children { + child.unload_blobs_recursive(); + } + } + + pub fn project(&self, path_components: &[&str]) -> Result { + let mut f = Self { + name: self.name.clone(), + id: self.id, + parent_id: self.parent_id, + logical_type: self.logical_type.clone(), + metadata: self.metadata.clone(), + encoding: self.encoding.clone(), + nullable: self.nullable, + children: vec![], + dictionary: self.dictionary.clone(), + unenforced_primary_key_position: self.unenforced_primary_key_position, + unenforced_clustering_key_position: self.unenforced_clustering_key_position, + }; + if path_components.is_empty() { + // Project stops here, copy all the remaining children. + f.children.clone_from(&self.children) + } else { + let first = path_components[0]; + for c in self.children.as_slice() { + if c.name == first { + let projected = c.project(&path_components[1..])?; + f.children.push(projected); + break; + } + } + } + Ok(f) + } + + /// Create a new field by removing all fields that do not match the filter. + /// + /// If a child field matches the filter then the parent will be kept even if + /// it does not match the filter. + /// + /// Returns None if the field itself does not match the filter. + pub fn project_by_filter bool>(&self, filter: &F) -> Option { + let children = self + .children + .iter() + .filter_map(|c| c.project_by_filter(filter)) + .collect::>(); + if !children.is_empty() || filter(self) { + Some(Self { + children, + ..self.clone() + }) + } else { + None + } + } + + /// Create a new field by selecting fields by their ids. + /// + /// If a field has it's id in the list of ids then it will be included + /// in the new field. If a field is selected, all of it's parents will be + /// and all of it's children will be included. + /// + /// For example, for the schema: + /// + /// ```text + /// 0: x struct { + /// 1: y int32 + /// 2: l list { + /// 3: z int32 + /// } + /// } + /// ``` + /// + /// If the ids are `[2]`, then this will include the parent `0` and the + /// child `3`. + pub(crate) fn project_by_ids(&self, ids: &[i32], include_all_children: bool) -> Option { + let children = self + .children + .iter() + .filter_map(|c| c.project_by_ids(ids, include_all_children)) + .collect::>(); + if ids.contains(&self.id) && (children.is_empty() || include_all_children) { + Some(self.clone()) + } else if !children.is_empty() { + Some(Self { + children, + ..self.clone() + }) + } else { + None + } + } + + /// Project by a field. + /// + pub fn project_by_field(&self, other: &Self, on_type_mismatch: OnTypeMismatch) -> Result { + if self.name != other.name { + return Err(Error::schema(format!( + "Attempt to project field by different names: {} and {}", + self.name, other.name, + ))); + }; + + if self.is_compatible_blob_projection(other) { + return Ok(self.clone()); + } + + match (self.data_type(), other.data_type()) { + (DataType::Boolean, DataType::Boolean) => Ok(self.clone()), + (dt, other_dt) + if (dt.is_primitive() && other_dt.is_primitive()) + || (dt.is_binary_like() && other_dt.is_binary_like()) => + { + if dt != other_dt { + return Err(Error::schema(format!( + "Attempt to project field by different types: {} and {}", + dt, other_dt, + ))); + } + Ok(self.clone()) + } + (DataType::Struct(_), DataType::Struct(_)) => { + // Blob v2 columns are special: they can have different struct layouts + // (logical input vs. descriptor struct). We treat blob v2 structs like primitive + // fields (e.g. a binary column) during schema set operations (union/subtract). + if self.is_blob() { + return Ok(self.clone()); + } + let mut fields = vec![]; + for other_field in other.children.iter() { + let Some(child) = self.child(&other_field.name) else { + return Err(Error::schema(format!( + "Attempt to project non-existed field: {} on {}", + other_field.name, self, + ))); + }; + fields.push(child.project_by_field(other_field, on_type_mismatch)?); + } + let mut cloned = self.clone(); + cloned.children = fields; + Ok(cloned) + } + (DataType::List(_), DataType::List(_)) + | (DataType::LargeList(_), DataType::LargeList(_)) + | (DataType::Map(_, _), DataType::Map(_, _)) => { + let projected = + self.children[0].project_by_field(&other.children[0], on_type_mismatch)?; + let mut cloned = self.clone(); + cloned.children = vec![projected]; + Ok(cloned) + } + (DataType::FixedSizeList(dt, n), DataType::FixedSizeList(other_dt, m)) + if dt == other_dt && n == m => + { + Ok(self.clone()) + } + ( + DataType::Dictionary(self_key, self_value), + DataType::Dictionary(other_key, other_value), + ) if self_key == other_key && self_value == other_value => Ok(self.clone()), + (DataType::Null, DataType::Null) => Ok(self.clone()), + (DataType::FixedSizeBinary(self_width), DataType::FixedSizeBinary(other_width)) + if self_width == other_width => + { + Ok(self.clone()) + } + _ => match on_type_mismatch { + OnTypeMismatch::Error => Err(Error::schema(format!( + "Attempt to project incompatible fields: {} and {}", + self, other + ))), + OnTypeMismatch::TakeSelf => Ok(self.clone()), + }, + } + } + + pub(crate) fn resolve<'a>( + &'a self, + split: &mut VecDeque<&str>, + fields: &mut Vec<&'a Self>, + ) -> bool { + fields.push(self); + if split.is_empty() { + return true; + } + let first = split.pop_front().unwrap(); + if let Some(child) = self.children.iter().find(|c| c.name == first) { + child.resolve(split, fields) + } else { + false + } + } + + /// Case-insensitive version of resolve. + /// First tries exact match for each child, then falls back to case-insensitive. + pub(crate) fn resolve_case_insensitive<'a>( + &'a self, + split: &mut VecDeque<&str>, + fields: &mut Vec<&'a Self>, + ) -> bool { + fields.push(self); + if split.is_empty() { + return true; + } + let first = split.pop_front().unwrap(); + // Try exact match first + if let Some(child) = self.children.iter().find(|c| c.name == first) { + return child.resolve_case_insensitive(split, fields); + } + // Fall back to case-insensitive match + if let Some(child) = self + .children + .iter() + .find(|c| c.name.eq_ignore_ascii_case(first)) + { + return child.resolve_case_insensitive(split, fields); + } + false + } + + pub(crate) fn do_intersection(&self, other: &Self, ignore_types: bool) -> Result { + if self.name != other.name { + return Err(Error::arrow(format!( + "Attempt to intersect different fields: {} and {}", + self.name, other.name, + ))); + } + + if self.is_blob() != other.is_blob() { + if ignore_types { + return Ok(if self.id >= 0 { + self.clone() + } else { + other.clone() + }); + } + return Err(Error::arrow(format!( + "Attempt to intersect blob and non-blob field: {}", + self.name + ))); + } + + if self.is_compatible_blob_projection(other) { + return Ok(if self.id >= 0 { + self.clone() + } else { + other.clone() + }); + } + + let self_type = self.data_type(); + let other_type = other.data_type(); + + if matches!( + (&self_type, &other_type), + (DataType::Struct(_), DataType::Struct(_)) + | (DataType::List(_), DataType::List(_)) + | (DataType::Map(_, _), DataType::Map(_, _)) + ) { + // Blob v2 uses a struct logical type for descriptors, which differs from the logical + // input struct (data/uri). When intersecting schemas for projection we want to keep + // the projected blob layout instead of intersecting by child names. + if self.is_blob() { + return Ok(self.clone()); + } + + let children = self + .children + .iter() + .filter_map(|c| { + if let Some(other_child) = other.child(&c.name) { + let intersection = c.do_intersection(other_child, ignore_types).ok()?; + Some(intersection) + } else { + None + } + }) + .collect::>(); + let f = Self { + name: self.name.clone(), + id: if self.id >= 0 { self.id } else { other.id }, + parent_id: self.parent_id, + logical_type: self.logical_type.clone(), + metadata: self.metadata.clone(), + encoding: self.encoding.clone(), + nullable: self.nullable, + children, + dictionary: self.dictionary.clone(), + unenforced_primary_key_position: self.unenforced_primary_key_position, + unenforced_clustering_key_position: self.unenforced_clustering_key_position, + }; + return Ok(f); + } + + if (!ignore_types && self_type != other_type) || self.name != other.name { + return Err(Error::arrow(format!( + "Attempt to intersect different fields: ({}, {}) and ({}, {})", + self.name, self_type, other.name, other_type + ))); + } + + Ok(if self.id >= 0 { + self.clone() + } else { + other.clone() + }) + } + + /// Intersection of two [`Field`]s. + /// + pub fn intersection(&self, other: &Self) -> Result { + self.do_intersection(other, false) + } + + /// Intersection of two [`Field`]s, ignoring data types. + pub fn intersection_ignore_types(&self, other: &Self) -> Result { + self.do_intersection(other, true) + } + + pub fn exclude(&self, other: &Self) -> Option { + if !self.data_type().is_nested() { + return None; + } + let children = self + .children + .iter() + .map(|c| { + if let Some(other_child) = other.child(&c.name) { + c.exclude(other_child) + } else { + Some(c.clone()) + } + }) + .filter(Option::is_some) + .flatten() + .collect::>(); + if children.is_empty() { + None + } else { + Some(Self { + name: self.name.clone(), + id: self.id, + parent_id: self.parent_id, + logical_type: self.logical_type.clone(), + metadata: self.metadata.clone(), + encoding: self.encoding.clone(), + nullable: self.nullable, + children, + dictionary: self.dictionary.clone(), + unenforced_primary_key_position: self.unenforced_primary_key_position, + unenforced_clustering_key_position: self.unenforced_clustering_key_position, + }) + } + } + + /// Merge the children of other field into this one. + pub(super) fn merge(&mut self, other: &Self) -> Result<()> { + match (self.data_type(), other.data_type()) { + (DataType::Struct(_), DataType::Struct(_)) => { + for other_child in other.children.as_slice() { + if let Some(field) = self.child_mut(&other_child.name) { + field.merge(other_child)?; + } else { + self.children.push(other_child.clone()); + } + } + } + (DataType::List(_), DataType::List(_)) + | (DataType::LargeList(_), DataType::LargeList(_)) => { + self.children[0].merge(&other.children[0])?; + } + ( + DataType::FixedSizeList(_, self_list_size), + DataType::FixedSizeList(_, other_list_size), + ) if self_list_size == other_list_size => { + // do nothing + } + (DataType::FixedSizeBinary(self_size), DataType::FixedSizeBinary(other_size)) + if self_size == other_size => + { + // do nothing + } + _ => { + if self.data_type() != other.data_type() { + return Err(Error::schema(format!( + "Attempt to merge incompatible fields: {} and {}", + self, other + ))); + } + } + } + Ok(()) + } + + // Get the max field id of itself and all children. + pub(super) fn max_id(&self) -> i32 { + max( + self.id, + self.children.iter().map(|c| c.max_id()).max().unwrap_or(-1), + ) + } + + /// Recursively set field ID and parent ID for this field and all its children. + pub fn set_id(&mut self, parent_id: i32, id_seed: &mut i32) { + self.parent_id = parent_id; + if self.id < 0 { + self.id = *id_seed; + *id_seed += 1; + } + self.children + .iter_mut() + .for_each(|f| f.set_id(self.id, id_seed)); + } + + /// Recursively reset field ID for this field and all its children. + pub(super) fn reset_id(&mut self) { + self.id = -1; + self.children.iter_mut().for_each(Self::reset_id); + } + + pub fn field_by_id_mut(&mut self, id: impl Into) -> Option<&mut Self> { + let id = id.into(); + for child in self.children.as_mut_slice() { + if child.id == id { + return Some(child); + } + if let Some(grandchild) = child.field_by_id_mut(id) { + return Some(grandchild); + } + } + None + } + + pub fn field_by_id(&self, id: impl Into) -> Option<&Self> { + let id = id.into(); + for child in self.children.as_slice() { + if child.id == id { + return Some(child); + } + if let Some(grandchild) = child.field_by_id(id) { + return Some(grandchild); + } + } + None + } + + // Find any nested child with a specific field id + pub(super) fn mut_field_by_id(&mut self, id: impl Into) -> Option<&mut Self> { + let id = id.into(); + for child in self.children.as_mut_slice() { + if child.id == id { + return Some(child); + } + if let Some(grandchild) = child.mut_field_by_id(id) { + return Some(grandchild); + } + } + None + } + + // Check if field has metadata `packed` set to true, this check is case insensitive. + pub fn is_packed_struct(&self) -> bool { + PACKED_KEYS.iter().any(|key| { + self.metadata + .get(*key) + .map(|value| value.eq_ignore_ascii_case("true")) + .unwrap_or(false) + }) + } + + /// Return true if the field is a leaf field. + /// + /// A leaf field is a field that is not a struct or a list. + pub fn is_leaf(&self) -> bool { + self.children.is_empty() + } + + /// Return true if the field is part of the (unenforced) primary key. + pub fn is_unenforced_primary_key(&self) -> bool { + self.unenforced_primary_key_position.is_some() + } + + pub fn is_unenforced_clustering_key(&self) -> bool { + self.unenforced_clustering_key_position.is_some() + } +} + +impl fmt::Display for Field { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Field(id={}, name={}, type={}", + self.id, self.name, self.logical_type.0, + )?; + + if let Some(dictionary) = &self.dictionary { + write!(f, ", dictionary={:?}", dictionary)?; + } + + if !self.children.is_empty() { + write!(f, ", children=[")?; + for child in self.children.iter() { + write!(f, "{}, ", child)?; + } + write!(f, "]")?; + } + + write!(f, ")") + } +} + +impl TryFrom<&ArrowField> for Field { + type Error = Error; + + fn try_from(field: &ArrowField) -> Result { + let mut metadata = field.metadata().clone(); + let id = match metadata.remove(LANCE_FIELD_ID_KEY) { + Some(val) => val + .parse::() + .map_err(|e| Error::invalid_input(e.to_string()))? + .max(-1), + None => -1, + }; + + let children = match field.data_type() { + DataType::Struct(children) => children + .iter() + .map(|f| Self::try_from(f.as_ref())) + .collect::>()?, + DataType::List(item) => vec![Self::try_from(item.as_ref())?], + DataType::LargeList(item) => vec![Self::try_from(item.as_ref())?], + DataType::FixedSizeList(item, _) if matches!(item.data_type(), DataType::Struct(_)) => { + vec![Self::try_from(item.as_ref())?] + } + DataType::Map(entries, keys_sorted) => { + // TODO: We only support keys_sorted=false for now, + // because converting a rust arrow map field to the python arrow field will + // lose the keys_sorted property. + if *keys_sorted { + return Err(Error::schema( + "Unsupported map field with keys_sorted=true".to_string(), + )); + } + // Validate Map entries follow Arrow specification + let DataType::Struct(struct_fields) = entries.data_type() else { + return Err(Error::schema( + "Map entries field must be a Struct".to_string(), + )); + }; + if struct_fields.len() < 2 { + return Err(Error::schema( + "Map entries struct must contain both key and value fields".to_string(), + )); + } + let key_field = &struct_fields[0]; + if key_field.is_nullable() { + return Err(Error::schema(format!( + "Map key field '{}' must be non-nullable according to Arrow Map specification", + key_field.name() + ))); + } + vec![Self::try_from(entries.as_ref())?] + } + _ => vec![], + }; + let unenforced_primary_key_position = metadata + .get(LANCE_UNENFORCED_PRIMARY_KEY_POSITION) + .and_then(|s| s.parse::().ok()) + .or_else(|| { + // Backward compatibility: use 0 for legacy boolean flag + metadata + .get(LANCE_UNENFORCED_PRIMARY_KEY) + .filter(|s| matches!(s.to_lowercase().as_str(), "true" | "1" | "yes")) + .map(|_| 0) + }); + let unenforced_clustering_key_position = metadata + .get(LANCE_UNENFORCED_CLUSTERING_KEY_POSITION) + .and_then(|s| s.parse::().ok()); + let is_blob_v2 = has_blob_v2_extension(field); + + if is_blob_v2 { + metadata + .entry(ARROW_EXT_NAME_KEY.to_string()) + .or_insert_with(|| BLOB_V2_EXT_NAME.to_string()); + } + + // Check for JSON extension types (both Arrow and Lance) + let logical_type = if is_arrow_json_field(field) || is_json_field(field) { + LogicalType::from("json") + } else if is_blob_v2 { + LogicalType::from("struct") + } else { + LogicalType::try_from(field.data_type())? + }; + + Ok(Self { + id, + parent_id: -1, + name: field.name().clone(), + logical_type, + encoding: match field.data_type() { + dt if dt.is_fixed_stride() => Some(Encoding::Plain), + dt if dt.is_binary_like() => Some(Encoding::VarBinary), + DataType::Dictionary(_, _) => Some(Encoding::Dictionary), + // Use plain encoder to store the offsets of list and map. + DataType::List(_) | DataType::LargeList(_) | DataType::Map(_, _) => { + Some(Encoding::Plain) + } + _ => None, + }, + metadata, + nullable: field.is_nullable(), + children, + dictionary: None, + unenforced_primary_key_position, + unenforced_clustering_key_position, + }) + } +} + +impl TryFrom for Field { + type Error = Error; + + fn try_from(field: ArrowField) -> Result { + Self::try_from(&field) + } +} + +impl From<&Field> for ArrowField { + fn from(field: &Field) -> Self { + let out = Self::new(&field.name, field.data_type(), field.nullable); + let mut metadata = field.metadata.clone(); + + if field.logical_type.is_blob() { + metadata + .entry(BLOB_META_KEY.to_string()) + .or_insert_with(|| "true".to_string()); + } + + // Add JSON extension metadata if this is a JSON field + if field.logical_type.0 == "json" { + metadata.insert( + ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::json::JSON_EXT_NAME.to_string(), + ); + } + + out.with_metadata(metadata) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use arrow_array::{DictionaryArray, StringArray, UInt32Array}; + use arrow_schema::{Fields, TimeUnit}; + use lance_arrow::BLOB_META_KEY; + use std::collections::HashMap; + + #[test] + fn arrow_field_to_field_metadata() { + let mut metadata = HashMap::new(); + metadata.insert(LANCE_FIELD_ID_KEY.to_string(), "42".to_string()); + metadata.insert("custom".to_string(), "value".to_string()); + + let arrow_field = + ArrowField::new("a", DataType::Int32, false).with_metadata(metadata.clone()); + let field = Field::try_from(&arrow_field).unwrap(); + + assert_eq!(field.id, 42); + assert!(!field.metadata.contains_key(LANCE_FIELD_ID_KEY)); + assert_eq!( + field.metadata.get("custom").map(String::as_str), + Some("value") + ); + } + + #[test] + fn arrow_field_to_field() { + for (name, data_type) in [ + ("null", DataType::Null), + ("bool", DataType::Boolean), + ("int8", DataType::Int8), + ("uint8", DataType::UInt8), + ("int16", DataType::Int16), + ("uint16", DataType::UInt16), + ("int32", DataType::Int32), + ("uint32", DataType::UInt32), + ("int64", DataType::Int64), + ("uint64", DataType::UInt64), + ("float16", DataType::Float16), + ("float32", DataType::Float32), + ("float64", DataType::Float64), + ("decimal128:7:3", DataType::Decimal128(7, 3)), + ("timestamp:s:-", DataType::Timestamp(TimeUnit::Second, None)), + ( + "timestamp:ms:-", + DataType::Timestamp(TimeUnit::Millisecond, None), + ), + ( + "timestamp:us:-", + DataType::Timestamp(TimeUnit::Microsecond, None), + ), + ( + "timestamp:ns:-", + DataType::Timestamp(TimeUnit::Nanosecond, None), + ), + ( + "timestamp:s:America/New_York", + DataType::Timestamp(TimeUnit::Second, Some("America/New_York".into())), + ), + ("time32:s", DataType::Time32(TimeUnit::Second)), + ("time32:ms", DataType::Time32(TimeUnit::Millisecond)), + ("time64:us", DataType::Time64(TimeUnit::Microsecond)), + ("time64:ns", DataType::Time64(TimeUnit::Nanosecond)), + ("duration:s", DataType::Duration(TimeUnit::Second)), + ("duration:ms", DataType::Duration(TimeUnit::Millisecond)), + ("duration:us", DataType::Duration(TimeUnit::Microsecond)), + ("duration:ns", DataType::Duration(TimeUnit::Nanosecond)), + ("fixed_size_binary:100", DataType::FixedSizeBinary(100)), + ( + "fixed_size_list:int32:10", + DataType::FixedSizeList( + Arc::new(ArrowField::new("item", DataType::Int32, true)), + 10, + ), + ), + ] { + let arrow_field = ArrowField::new(name, data_type.clone(), true); + let field = Field::try_from(&arrow_field).unwrap(); + assert_eq!(field.name, name); + assert_eq!(field.data_type(), data_type); + assert_eq!(ArrowField::from(&field), arrow_field); + } + } + + #[test] + fn test_view_types_stored_as_lance_base_types() { + let field = Field::try_from(&ArrowField::new("s", DataType::Utf8View, true)).unwrap(); + assert_eq!(field.data_type(), DataType::Utf8); + assert_eq!( + LogicalType::try_from(&DataType::Utf8View).unwrap().0, + "string" + ); + + let field = Field::try_from(&ArrowField::new("b", DataType::BinaryView, true)).unwrap(); + assert_eq!(field.data_type(), DataType::Binary); + assert_eq!( + LogicalType::try_from(&DataType::BinaryView).unwrap().0, + "binary" + ); + } + + #[test] + fn test_nested_types() { + assert_eq!( + LogicalType::try_from(&DataType::List(Arc::new(ArrowField::new( + "item", + DataType::Binary, + false + )))) + .unwrap() + .0, + "list" + ); + assert_eq!( + LogicalType::try_from(&DataType::List(Arc::new(ArrowField::new( + "item", + DataType::Struct(Fields::empty()), + false + )))) + .unwrap() + .0, + "list.struct" + ); + assert_eq!( + LogicalType::try_from(&DataType::Struct(Fields::from(vec![ArrowField::new( + "item", + DataType::Binary, + false + )]))) + .unwrap() + .0, + "struct" + ); + + assert_eq!( + LogicalType::try_from(&DataType::Map( + Arc::new(ArrowField::new( + "entries", + DataType::Struct(Fields::from(vec![ + ArrowField::new("key", DataType::Utf8, false), + ArrowField::new("value", DataType::Int32, true), + ])), + true + )), + false + )) + .unwrap() + .0, + "map" + ); + } + + #[test] + fn struct_field() { + let arrow_field = ArrowField::new( + "struct", + DataType::Struct(Fields::from(vec![ArrowField::new( + "a", + DataType::Int32, + true, + )])), + false, + ); + let field = Field::try_from(&arrow_field).unwrap(); + assert_eq!(field.name, "struct"); + assert_eq!(&field.data_type(), arrow_field.data_type()); + assert_eq!(ArrowField::from(&field), arrow_field); + } + + #[test] + fn map_key_must_be_non_nullable() { + let entries_field = Arc::new(ArrowField::new( + "entries", + DataType::Struct(Fields::from(vec![ + ArrowField::new("key", DataType::Utf8, true), // invalid: nullable key + ArrowField::new("value", DataType::Int32, true), + ])), + false, + )); + let arrow_field = ArrowField::new("props", DataType::Map(entries_field, false), true); + + let result = Field::try_from(&arrow_field); + assert!(result.is_err(), "Nullable map key should be rejected"); + } + + #[test] + fn map_keys_sorted_unsupported() { + let entries_field = Arc::new(ArrowField::new( + "entries", + DataType::Struct(Fields::from(vec![ + ArrowField::new("key", DataType::Utf8, false), + ArrowField::new("value", DataType::Int32, true), + ])), + false, + )); + + // Test that keys_sorted=true is rejected + let arrow_field_sorted = ArrowField::new( + "map_field", + DataType::Map(entries_field.clone(), true), + true, + ); + let result = Field::try_from(&arrow_field_sorted); + assert!(result.is_err(), "keys_sorted=true should be rejected"); + assert!(result.unwrap_err().to_string().contains("keys_sorted=true")); + + // Test that keys_sorted=false is supported + let arrow_field_unsorted = + ArrowField::new("map_field", DataType::Map(entries_field, false), true); + let lance_field_unsorted = Field::try_from(&arrow_field_unsorted).unwrap(); + + // Verify conversion back to ArrowField preserves keys_sorted=false + let converted_field_unsorted = ArrowField::from(&lance_field_unsorted); + match converted_field_unsorted.data_type() { + DataType::Map(_, keys_sorted) => assert!(!keys_sorted, "keys_sorted should be false"), + _ => panic!("Expected Map type"), + } + } + + #[test] + fn map_entries_must_be_struct() { + let entries_field = Arc::new(ArrowField::new("entries", DataType::Utf8, false)); + let arrow_field = ArrowField::new("map_field", DataType::Map(entries_field, false), true); + + let err = Field::try_from(&arrow_field).unwrap_err(); + assert!( + err.to_string() + .contains("Map entries field must be a Struct"), + "Expected struct requirement error, got {err}" + ); + } + + #[test] + fn map_entries_struct_needs_key_and_value() { + let entries_field = Arc::new(ArrowField::new( + "entries", + DataType::Struct(Fields::from(vec![ArrowField::new( + "key", + DataType::Utf8, + false, + )])), + false, + )); + let arrow_field = ArrowField::new("map_field", DataType::Map(entries_field, false), true); + + let err = Field::try_from(&arrow_field).unwrap_err(); + assert!( + err.to_string().contains("must contain both key and value"), + "Expected both fields requirement error, got {err}" + ); + } + + #[test] + fn test_project_by_field_null_type() { + let f1: Field = ArrowField::new("a", DataType::Null, true) + .try_into() + .unwrap(); + let f2: Field = ArrowField::new("a", DataType::Null, true) + .try_into() + .unwrap(); + let p1 = f1.project_by_field(&f2, OnTypeMismatch::Error).unwrap(); + + assert_eq!(p1, f1); + + let f3: Field = ArrowField::new("b", DataType::Null, true) + .try_into() + .unwrap(); + assert!(f1.project_by_field(&f3, OnTypeMismatch::Error).is_err()); + + let f4: Field = ArrowField::new("a", DataType::Int32, true) + .try_into() + .unwrap(); + assert!(f1.project_by_field(&f4, OnTypeMismatch::Error).is_err()); + } + + #[test] + fn test_field_intersection() { + let f1: Field = ArrowField::new("a", DataType::Int32, true) + .try_into() + .unwrap(); + let f2: Field = ArrowField::new("a", DataType::Int32, true) + .try_into() + .unwrap(); + let i1 = f1.intersection(&f2).unwrap(); + + assert_eq!(i1, f1); + + let f3: Field = ArrowField::new("b", DataType::Int32, true) + .try_into() + .unwrap(); + assert!(f1.intersection(&f3).is_err()); + } + + #[test] + fn test_struct_field_intersection() { + let f1: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("b", DataType::Int32, true), + ArrowField::new("c", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + let f2: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("c", DataType::Int32, true), + ArrowField::new("a", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + let actual = f1.intersection(&f2).unwrap(); + + let expected: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ArrowField::new( + "c", + DataType::Int32, + true, + )])), + true, + ) + .try_into() + .unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn test_compare() { + let opts = SchemaCompareOptions::default(); + + let mut expected: Field = ArrowField::new( + "a", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + true, + ) + .try_into() + .unwrap(); + let keys = UInt32Array::from_iter_values(vec![0, 1]); + let values: ArrayRef = Arc::new(StringArray::from_iter_values([ + "a".to_string(), + "b".to_string(), + ])); + let dictionary: ArrayRef = Arc::new(DictionaryArray::new(keys, values)); + expected.set_dictionary(&dictionary); + + let no_dictionary: Field = ArrowField::new( + "a", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + true, + ) + .try_into() + .unwrap(); + + // By default, do not compare dictionary + assert!(no_dictionary.compare_with_options(&expected, &opts)); + + let compare_dict = SchemaCompareOptions { + compare_dictionary: true, + ..Default::default() + }; + assert!(!no_dictionary.compare_with_options(&expected, &compare_dict)); + + let metadata = HashMap::<_, _>::from_iter(vec![("foo".to_string(), "bar".to_string())]); + let expected: Field = ArrowField::new("a", DataType::UInt32, true) + .with_metadata(metadata) + .try_into() + .unwrap(); + + let no_metadata: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + + // By default, do not compare metadata + assert!(no_metadata.compare_with_options(&expected, &opts)); + + let compare_metadata = SchemaCompareOptions { + compare_metadata: true, + ..Default::default() + }; + assert!(!no_metadata.compare_with_options(&expected, &compare_metadata)); + + let mut expected: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + let mut seed = 0; + expected.set_id(-1, &mut seed); + + let no_id: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + // Do not compare ids by default + assert!(no_id.compare_with_options(&expected, &opts)); + + let compare_ids = SchemaCompareOptions { + compare_field_ids: true, + ..Default::default() + }; + assert!(!no_id.compare_with_options(&expected, &compare_ids)); + } + + #[test] + fn test_explain_difference() { + let expected: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("b", DataType::Int32, true), + ArrowField::new("c", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + + let opts = SchemaCompareOptions::default(); + assert_eq!(expected.explain_difference(&expected, &opts), None); + + let wrong_name: Field = ArrowField::new( + "b", + DataType::Struct(Fields::from(vec![ + ArrowField::new("b", DataType::Int32, true), + ArrowField::new("c", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + + assert_eq!( + wrong_name.explain_difference(&expected, &opts), + Some("expected name 'a' but name was 'b'".to_string()) + ); + + let wrong_child: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("b", DataType::Int32, false), + ArrowField::new("c", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + assert_eq!( + wrong_child.explain_difference(&expected, &opts), + Some( + "`a` had mismatched children: `a.b` should have nullable=true but nullable=false" + .to_string() + ) + ); + + let mismatched_children: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("d", DataType::Int32, false), + ArrowField::new("b", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + assert_eq!( + mismatched_children.explain_difference(&expected, &opts), + Some("`a` had mismatched children: fields did not match, missing=[a.c], unexpected=[a.d]".to_string()) + ); + + let reordered_children: Field = ArrowField::new( + "a", + DataType::Struct(Fields::from(vec![ + ArrowField::new("c", DataType::Int32, true), + ArrowField::new("b", DataType::Int32, true), + ])), + true, + ) + .try_into() + .unwrap(); + assert_eq!( + reordered_children.explain_difference(&expected, &opts), + Some("`a` had mismatched children: fields in different order, expected: [b, c], actual: [c, b]".to_string()) + ); + + let multiple_wrongs: Field = ArrowField::new( + "c", + DataType::Struct(Fields::from(vec![ + ArrowField::new("b", DataType::Int32, true), + ArrowField::new("c", DataType::Float32, true), + ])), + true, + ) + .try_into() + .unwrap(); + assert_eq!( + multiple_wrongs.explain_difference(&expected, &opts), + Some( + "expected name 'a' but name was 'c', `c` had mismatched children: `c.c` should have type int32 but type was float" + .to_string() + ) + ); + + let mut expected: Field = ArrowField::new( + "a", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + true, + ) + .try_into() + .unwrap(); + let keys = UInt32Array::from_iter_values(vec![0, 1]); + let values: ArrayRef = Arc::new(StringArray::from_iter_values([ + "a".to_string(), + "b".to_string(), + ])); + let dictionary: ArrayRef = Arc::new(DictionaryArray::new(keys, values)); + expected.set_dictionary(&dictionary); + + let no_dictionary: Field = ArrowField::new( + "a", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + true, + ) + .try_into() + .unwrap(); + + // By default, do not compare dictionary + assert_eq!(no_dictionary.explain_difference(&expected, &opts), None); + + let compare_dict = SchemaCompareOptions { + compare_dictionary: true, + ..Default::default() + }; + assert_eq!( + no_dictionary.explain_difference(&expected, &compare_dict), + Some("dictionary for `a` did not match expected dictionary".to_string()) + ); + + let metadata = HashMap::<_, _>::from_iter(vec![("foo".to_string(), "bar".to_string())]); + let expected: Field = ArrowField::new("a", DataType::UInt32, true) + .with_metadata(metadata) + .try_into() + .unwrap(); + + let no_metadata: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + + // By default, do not compare metadata + assert_eq!(no_metadata.explain_difference(&expected, &opts), None); + + let compare_metadata = SchemaCompareOptions { + compare_metadata: true, + ..Default::default() + }; + assert_eq!( + no_metadata.explain_difference(&expected, &compare_metadata), + Some("metadata for `a` did not match expected metadata".to_string()) + ); + + let mut expected: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + let mut seed = 0; + expected.set_id(-1, &mut seed); + + let no_id: Field = ArrowField::new("a", DataType::UInt32, true) + .try_into() + .unwrap(); + // Do not compare ids by default + assert_eq!(no_id.explain_difference(&expected, &opts), None); + + let compare_ids = SchemaCompareOptions { + compare_field_ids: true, + ..Default::default() + }; + assert_eq!( + no_id.explain_difference(&expected, &compare_ids), + Some("`a` should have id 0 but id was -1".to_string()) + ); + } + + #[test] + pub fn test_nullability_comparison() { + let f1 = Field::try_from(&ArrowField::new("a", DataType::Int32, true)).unwrap(); + let f2 = Field::try_from(&ArrowField::new("a", DataType::Int32, false)).unwrap(); + + // By default, nullability difference is not allowed + assert!(!f1.compare_with_options(&f2, &SchemaCompareOptions::default())); + + let ignore_nullability = SchemaCompareOptions { + compare_nullability: NullabilityComparison::Ignore, + ..Default::default() + }; + let oneway_nullability = SchemaCompareOptions { + compare_nullability: NullabilityComparison::OneWay, + ..Default::default() + }; + let strict_nullability = SchemaCompareOptions { + compare_nullability: NullabilityComparison::Strict, + ..Default::default() + }; + + // By default, nullability difference is not allowed + assert!(!f1.compare_with_options(&f2, &strict_nullability)); + assert!(!f2.compare_with_options(&f1, &strict_nullability)); + // One way nullability will allow the difference if expected is nullable + assert!(!f1.compare_with_options(&f2, &oneway_nullability)); + assert!(f2.compare_with_options(&f1, &oneway_nullability)); + // Finally, ignore will ignore + assert!(f1.compare_with_options(&f2, &ignore_nullability)); + assert!(f2.compare_with_options(&f1, &ignore_nullability)); + } + + #[test] + fn blob_unloaded_mut_selects_layout_from_metadata() { + let metadata = HashMap::from([(BLOB_META_KEY.to_string(), "true".to_string())]); + let mut binary_field: Field = ArrowField::new("blob", DataType::LargeBinary, true) + .with_metadata(metadata.clone()) + .try_into() + .unwrap(); + binary_field.binary_blob_mut(); + assert!(binary_field.metadata.contains_key(BLOB_META_KEY)); + assert!(!binary_field.is_blob_v2()); + + let mut field: Field = ArrowField::new("blob", DataType::LargeBinary, true) + .with_metadata(metadata) + .try_into() + .unwrap(); + field.unloaded_mut(); + assert_eq!(field.children.len(), 2); + assert_eq!(field.logical_type, BLOB_DESC_LANCE_FIELD.logical_type); + assert!(field.is_blob()); + assert!(!field.is_blob_v2()); + field.unloaded_mut(); + assert_eq!(field.children.len(), 2); + assert_eq!(field.logical_type, BLOB_DESC_LANCE_FIELD.logical_type); + assert!(!field.is_blob_v2()); + + let metadata = + HashMap::from([(ARROW_EXT_NAME_KEY.to_string(), BLOB_V2_EXT_NAME.to_string())]); + let mut field: Field = ArrowField::new( + "blob", + DataType::Struct( + vec![ + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ] + .into(), + ), + true, + ) + .with_metadata(metadata) + .try_into() + .unwrap(); + field.unloaded_mut(); + assert_eq!(field.children.len(), 5); + assert_eq!(field.logical_type, BLOB_V2_DESC_LANCE_FIELD.logical_type); + assert!(!field.metadata.contains_key(ARROW_EXT_NAME_KEY)); + assert!(field.is_blob_v2()); + field.unloaded_mut(); + assert_eq!(field.children.len(), 5); + assert_eq!(field.logical_type, BLOB_V2_DESC_LANCE_FIELD.logical_type); + assert!(!field.metadata.contains_key(ARROW_EXT_NAME_KEY)); + } + + #[test] + fn unload_blobs_recursive_only_unloads_blob_v2() { + let legacy_metadata = HashMap::from([(BLOB_META_KEY.to_string(), "true".to_string())]); + let blob_v2_metadata = + HashMap::from([(ARROW_EXT_NAME_KEY.to_string(), BLOB_V2_EXT_NAME.to_string())]); + + let mut field: Field = ArrowField::new( + "parent", + DataType::Struct(Fields::from(vec![ + ArrowField::new("legacy_blob", DataType::LargeBinary, true) + .with_metadata(legacy_metadata), + ArrowField::new( + "blob_v2", + DataType::Struct( + vec![ + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ] + .into(), + ), + true, + ) + .with_metadata(blob_v2_metadata), + ])), + true, + ) + .try_into() + .unwrap(); + + field.unload_blobs_recursive(); + + let legacy_blob = field + .children + .iter() + .find(|f| f.name == "legacy_blob") + .unwrap(); + assert_eq!( + legacy_blob.logical_type, + LogicalType::try_from(&DataType::LargeBinary).unwrap() + ); + assert_eq!(legacy_blob.children.len(), 0); + assert!(legacy_blob.metadata.contains_key(BLOB_META_KEY)); + + let blob_v2 = field.children.iter().find(|f| f.name == "blob_v2").unwrap(); + assert_eq!(blob_v2.logical_type, BLOB_V2_DESC_LANCE_FIELD.logical_type); + assert_eq!(blob_v2.children.len(), 5); + } + + #[test] + fn project_by_field_accepts_blob_descriptor_projection() { + let metadata = HashMap::from([(BLOB_META_KEY.to_string(), "true".to_string())]); + let field: Field = ArrowField::new("blob", DataType::LargeBinary, true) + .with_metadata(metadata) + .try_into() + .unwrap(); + let mut unloaded = field.clone(); + unloaded.unloaded_mut(); + + let projected = field + .project_by_field(&unloaded, OnTypeMismatch::Error) + .unwrap(); + assert_eq!(projected, field); + + let unloaded_projected = unloaded + .project_by_field(&field, OnTypeMismatch::Error) + .unwrap(); + assert_eq!(unloaded_projected, unloaded); + } + + #[test] + fn blob_descriptor_projection_preserves_synthetic_children() { + let metadata = + HashMap::from([(ARROW_EXT_NAME_KEY.to_string(), BLOB_V2_EXT_NAME.to_string())]); + let mut blob: Field = ArrowField::new( + "blob", + DataType::Struct( + vec![ + ArrowField::new("data", DataType::LargeBinary, true), + ArrowField::new("uri", DataType::Utf8, true), + ] + .into(), + ), + true, + ) + .with_metadata(metadata) + .try_into() + .unwrap(); + let mut next_id = 0; + blob.set_id(-1, &mut next_id); + + let schema = Arc::new(crate::datatypes::Schema { + fields: vec![blob], + metadata: HashMap::new(), + }); + let descriptor_schema = Projection::full(schema) + .with_blob_handling(crate::datatypes::BlobHandling::BlobsDescriptions) + .to_bare_schema(); + assert!( + descriptor_schema.fields[0] + .children + .iter() + .all(|child| child.id == -1) + ); + + let projected = Projection::full(Arc::new(descriptor_schema)).to_bare_schema(); + assert_eq!(projected.fields[0].children.len(), 5); + } +} diff --git a/lance-artifact/rust/lance-core/src/datatypes/schema.rs b/lance-artifact/rust/lance-core/src/datatypes/schema.rs new file mode 100644 index 000000000..2e328c88d --- /dev/null +++ b/lance-artifact/rust/lance-core/src/datatypes/schema.rs @@ -0,0 +1,3163 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Schema + +use std::{ + collections::{HashMap, HashSet, VecDeque}, + fmt::{self, Debug, Formatter}, + sync::Arc, +}; + +use crate::deepsize::DeepSizeOf; +use arrow_array::RecordBatch; +use arrow_schema::{DataType, Field as ArrowField, Schema as ArrowSchema}; +use lance_arrow::*; + +use super::field::{Field, OnTypeMismatch, SchemaCompareOptions}; +use crate::{ + Error, ROW_ADDR, ROW_ADDR_FIELD, ROW_CREATED_AT_VERSION, ROW_CREATED_AT_VERSION_FIELD, ROW_ID, + ROW_ID_FIELD, ROW_LAST_UPDATED_AT_VERSION, ROW_LAST_UPDATED_AT_VERSION_FIELD, ROW_OFFSET, + ROW_OFFSET_FIELD, Result, WILDCARD, +}; + +/// Lance Schema. +#[derive(Default, Debug, Clone, DeepSizeOf)] +pub struct Schema { + /// Top-level fields in the dataset. + pub fields: Vec, + /// Metadata of the schema + pub metadata: HashMap, +} + +/// Reference to a field in a schema, either by ID or by path. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum FieldRef<'a> { + /// Reference by field ID + ById(i32), + /// Reference by field path (e.g., "struct_field.sub_field") + ByPath(&'a str), +} + +impl FieldRef<'_> { + /// Convert this field reference to a field ID by looking it up in the schema. + pub fn into_id(self, schema: &Schema) -> Result { + match self { + FieldRef::ById(id) => { + if schema.field_by_id(id).is_none() { + return Err(Error::invalid_input_source( + format!("Field ID {} not found in schema", id).into(), + )); + } + Ok(id) + } + FieldRef::ByPath(path) => { + let field = schema + .field(path) + .ok_or_else(|| Error::field_not_found(path, schema.field_paths()))?; + Ok(field.id) + } + } + } +} + +impl From for FieldRef<'_> { + fn from(id: i32) -> Self { + FieldRef::ById(id) + } +} + +impl<'a> From<&'a str> for FieldRef<'a> { + fn from(path: &'a str) -> Self { + FieldRef::ByPath(path) + } +} + +impl<'a> From<&'a String> for FieldRef<'a> { + fn from(path: &'a String) -> Self { + FieldRef::ByPath(path.as_str()) + } +} + +/// State for a pre-order DFS iterator over the fields of a schema. +struct SchemaFieldIterPreOrder<'a> { + field_stack: Vec<&'a Field>, +} + +impl<'a> SchemaFieldIterPreOrder<'a> { + fn new(schema: &'a Schema) -> Self { + let mut field_stack = Vec::with_capacity(schema.fields.len() * 2); + for field in schema.fields.iter().rev() { + field_stack.push(field); + } + Self { field_stack } + } +} + +/// Iterator implementation for a pre-order traversal of fields +impl<'a> Iterator for SchemaFieldIterPreOrder<'a> { + type Item = &'a Field; + + fn next(&mut self) -> Option { + if let Some(next_field) = self.field_stack.pop() { + for child in next_field.children.iter().rev() { + self.field_stack.push(child); + } + Some(next_field) + } else { + None + } + } +} + +/// Reject `FixedSizeList` types whose dimension is not a positive integer. +/// +/// The row count of a fixed-size list is derived by dividing the number of +/// child items by the dimension, so a zero dimension panics with a +/// divide-by-zero further down the write path (see issue #5102). A +/// `FixedSizeList` of a `FixedSizeList` over a primitive collapses into a +/// single leaf field, so the pre-order field walk never visits the inner list; +/// recurse through the nested list types here to catch an inner zero dimension. +/// +/// Shared by [`Schema::validate`] on the write path and the decoder's +/// field-scheduler builders on the read path. +pub fn validate_fixed_size_list_dimensions(field_name: &str, data_type: &DataType) -> Result<()> { + if let DataType::FixedSizeList(inner, dimension) = data_type { + if *dimension <= 0 { + return Err(Error::schema(format!( + "Field \"{field_name}\" contains a FixedSizeList with dimension {dimension}; dimension must be a positive integer" + ))); + } + validate_fixed_size_list_dimensions(field_name, inner.data_type())?; + } + Ok(()) +} + +impl Schema { + /// The unenforced primary key fields in the schema, ordered by position. + /// + /// Fields with explicit positions (1, 2, 3, ...) are ordered by their position value. + /// Fields without explicit positions (using the legacy boolean flag) are ordered + /// by their schema field id and come after fields with explicit positions. + pub fn unenforced_primary_key(&self) -> Vec<&Field> { + let mut pk_fields: Vec<&Field> = self + .fields_pre_order() + .filter(|f| f.is_unenforced_primary_key()) + .collect(); + + pk_fields.sort_by_key(|f| { + let pk_position = f.unenforced_primary_key_position.unwrap_or(0); + if pk_position > 0 { + (false, pk_position as i32, f.id) + } else { + (true, f.id, f.id) + } + }); + + pk_fields + } + + /// The unenforced clustering key fields in the schema, ordered by position. + /// + /// Fields are ordered by their explicit position value (1-based). + pub fn unenforced_clustering_key(&self) -> Vec<&Field> { + let mut ck_fields: Vec<&Field> = self + .fields_pre_order() + .filter(|f| f.is_unenforced_clustering_key()) + .collect(); + + ck_fields.sort_by_key(|f| f.unenforced_clustering_key_position.unwrap_or(0)); + + ck_fields + } + + pub fn compare_with_options(&self, expected: &Self, options: &SchemaCompareOptions) -> bool { + compare_fields(&self.fields, &expected.fields, options) + && (!options.compare_metadata || self.metadata == expected.metadata) + } + + pub fn explain_difference( + &self, + expected: &Self, + options: &SchemaCompareOptions, + ) -> Option { + let mut differences = + explain_fields_difference(&self.fields, &expected.fields, options, None); + + if options.compare_metadata + && let Some(difference) = + explain_metadata_difference(&self.metadata, &expected.metadata) + { + differences.push(difference); + } + + if differences.is_empty() { + None + } else { + Some(differences.join(", ")) + } + } + + pub fn has_dictionary_types(&self) -> bool { + self.fields.iter().any(|f| f.has_dictionary_types()) + } + + pub fn check_compatible(&self, expected: &Self, options: &SchemaCompareOptions) -> Result<()> { + if !self.compare_with_options(expected, options) { + let difference = self.explain_difference(expected, options); + // unknown reason is messy but this shouldn't happen. + Err(Error::schema_mismatch( + difference.unwrap_or("unknown reason".to_string()), + )) + } else { + Ok(()) + } + } + + /// Convert to a compact string representation. + /// + /// This is intended for display purposes and not for serialization. + pub fn to_compact_string(&self, indent: Indentation) -> String { + ArrowSchema::from(self).to_compact_string(indent) + } + + /// Given a string column reference, resolve the path of fields + /// + /// For example, given a.b.c we will return the fields [a, b, c] + /// Field names containing dots must be quoted: parent."child.with.dot" + /// + /// Returns None if we can't find a segment at any point + pub fn resolve(&self, column: impl AsRef) -> Option> { + let split = parse_field_path(column.as_ref()).ok()?; + if split.is_empty() { + return None; + } + + if split.len() == 1 { + let field_name = &split[0]; + if let Some(field) = self.fields.iter().find(|f| &f.name == field_name) { + return Some(vec![field]); + } + return None; + } + + // Multiple segments - resolve as a nested field path + let mut fields = Vec::with_capacity(split.len()); + let first = &split[0]; + + // Find the first field + let field = self.fields.iter().find(|f| &f.name == first)?; + + let mut split_refs: VecDeque<&str> = split[1..].iter().map(|s| s.as_str()).collect(); + if field.resolve(&mut split_refs, &mut fields) { + Some(fields) + } else { + None + } + } + + fn do_project>( + &self, + columns: &[T], + err_on_missing: bool, + preserve_system_columns: bool, + ) -> Result { + let mut candidates: Vec = vec![]; + for col in columns { + let split = parse_field_path(col.as_ref())?; + let first = split[0].as_str(); + if let Some(field) = self.field(first) { + let split_refs: Vec<&str> = split[1..].iter().map(|s| s.as_str()).collect(); + let projected_field = field.project(&split_refs)?; + if let Some(candidate_field) = candidates.iter_mut().find(|f| f.name == first) { + candidate_field.merge(&projected_field)?; + } else { + candidates.push(projected_field) + } + } else if crate::is_system_column(first) { + if preserve_system_columns { + if first == ROW_ID { + candidates.push(Field::try_from(ROW_ID_FIELD.clone())?); + } else if first == ROW_ADDR { + candidates.push(Field::try_from(ROW_ADDR_FIELD.clone())?); + } else if first == ROW_OFFSET { + candidates.push(Field::try_from(ROW_OFFSET_FIELD.clone())?); + } else if first == ROW_CREATED_AT_VERSION { + candidates.push(Field::try_from(ROW_CREATED_AT_VERSION_FIELD.clone())?); + } else if first == ROW_LAST_UPDATED_AT_VERSION { + candidates + .push(Field::try_from(ROW_LAST_UPDATED_AT_VERSION_FIELD.clone())?); + } else { + return Err(Error::schema(format!( + "System column {} is currently not supported in projection", + first + ))); + } + } + } else if err_on_missing { + return Err(Error::field_not_found(col.as_ref(), self.field_paths())); + } + } + + Ok(Self { + fields: candidates, + metadata: self.metadata.clone(), + }) + } + + /// Project the columns over the schema. + /// + /// ```ignore + /// let schema = Schema::from(...); + /// let projected = schema.project(&["col1", "col2.sub_col3.field4"])?; + /// ``` + pub fn project>(&self, columns: &[T]) -> Result { + self.do_project(columns, true, false) + } + + /// Project the columns over the schema, dropping unrecognized columns + pub fn project_or_drop>(&self, columns: &[T]) -> Result { + self.do_project(columns, false, false) + } + + /// Project the columns over the schema, preserving system columns. + pub fn project_preserve_system_columns>(&self, columns: &[T]) -> Result { + self.do_project(columns, true, true) + } + + /// Check that the top level fields don't contain `.` in their names + /// to distinguish from nested fields. + // TODO: pub(crate) + pub fn validate(&self) -> Result<()> { + let mut seen_names = HashSet::new(); + + for field in self.fields.iter() { + if field.name.contains('.') { + return Err(Error::schema(format!( + "Top level field {} cannot contain `.`. Maybe you meant to create a struct field?", + field.name.clone() + ))); + } + + if !seen_names.insert(field.name.as_str()) { + return Err(Error::schema(format!( + "Duplicate field name \"{}\" in schema:\n {:#?}", + field.name, self + ))); + } + } + + // Check for duplicate field ids + let mut seen_ids = HashSet::new(); + for field in self.fields_pre_order() { + if field.id < 0 { + return Err(Error::schema(format!( + "Field {} has a negative id {}", + field.name, field.id + ))); + } + if !seen_ids.insert(field.id) { + return Err(Error::schema(format!( + "Duplicate field id {} in schema {:?}", + field.id, self + ))); + } + // The row count of a fixed-size list is derived by dividing the + // number of items by the dimension, so a zero dimension would + // panic with a divide-by-zero further down the write path. + validate_fixed_size_list_dimensions(&field.name, &field.data_type())?; + } + + Ok(()) + } + + /// Intersection between two [`Schema`]. + pub fn intersection(&self, other: &Self) -> Result { + self.do_intersection(other, false) + } + + /// Intersection between two [`Schema`], ignoring data types. + pub fn intersection_ignore_types(&self, other: &Self) -> Result { + self.do_intersection(other, true) + } + + fn do_intersection(&self, other: &Self, ignore_types: bool) -> Result { + let mut candidates: Vec = vec![]; + for field in other.fields.iter() { + if let Some(candidate_field) = self.field(&field.name) { + candidates.push(candidate_field.do_intersection(field, ignore_types)?); + } + } + + Ok(Self { + fields: candidates, + metadata: self.metadata.clone(), + }) + } + + /// Iterates over the fields using a pre-order traversal + /// + /// This is a DFS traversal where the parent is visited + /// before its children + pub fn fields_pre_order(&self) -> impl Iterator { + SchemaFieldIterPreOrder::new(self) + } + + /// Get all field paths in the schema as a list of strings. + /// + /// This returns all field paths in the schema, including nested fields. + /// For example, if there's a struct field "user" with a field "name", + /// this will return "user.name" as one of the paths. + pub fn field_paths(&self) -> Vec { + let mut paths = Vec::new(); + for field in self.fields_pre_order() { + let ancestry = self.field_ancestry_by_id(field.id); + if let Some(ancestry) = ancestry { + let path = ancestry + .iter() + .map(|f| f.name.as_str()) + .collect::>() + .join("."); + paths.push(path); + } + } + paths + } + + /// Returns a new schema that only contains the fields in `column_ids`. + /// + /// This projection can filter out both top-level and nested fields + /// + /// If `include_all_children` is true, then if a parent field id is passed, + /// then all children of that field will be included in the projection + /// regardless of whether their ids were passed. If this is false, then + /// only the child fields with the passed ids will be included. + pub fn project_by_ids(&self, column_ids: &[i32], include_all_children: bool) -> Self { + let filtered_fields = self + .fields + .iter() + .filter_map(|f| f.project_by_ids(column_ids, include_all_children)) + .collect(); + Self { + fields: filtered_fields, + metadata: self.metadata.clone(), + } + } + + fn apply_projection(&self, projection: &Projection) -> Self { + let filtered_fields = self + .fields + .iter() + .filter_map(|f| f.apply_projection(projection)) + .collect(); + Self { + fields: filtered_fields, + metadata: self.metadata.clone(), + } + } + + /// Project the schema by another schema, and preserves field metadata, i.e., Field IDs. + /// + /// Parameters + /// - `projection`: The schema to project by. Can be [`arrow_schema::Schema`] or [`Schema`]. + pub fn project_by_schema>( + &self, + projection: S, + on_missing: OnMissing, + on_type_mismatch: OnTypeMismatch, + ) -> Result { + let projection = projection.try_into()?; + let mut new_fields = vec![]; + for field in projection.fields.iter() { + // Ensure the field is a top-level field (no dots in the name) + if field.name.contains('.') { + return Err(Error::schema(format!( + "Field '{}' contains dots. project_by_schema only accepts top-level fields. \ + Use project() method for nested field paths.", + field.name + ))); + } + + if let Some(self_field) = self.field(&field.name) { + new_fields.push(self_field.project_by_field(field, on_type_mismatch)?); + } else if matches!(on_missing, OnMissing::Error) { + return Err(Error::schema(format!("Field {} not found", field.name))); + } + } + Ok(Self { + fields: new_fields, + metadata: self.metadata.clone(), + }) + } + + /// Exclude the fields from `other` Schema, and returns a new Schema. + pub fn exclude + Debug>(&self, schema: T) -> Result { + let other = schema.try_into().map_err(|_| { + Error::schema("The other schema is not compatible with this schema".to_string()) + })?; + let mut fields = vec![]; + for field in self.fields.iter() { + if let Some(other_field) = other.field(&field.name) { + if field.data_type().is_nested() + && let Some(f) = field.exclude(other_field) + { + fields.push(f) + } + } else { + fields.push(field.clone()); + } + } + Ok(Self { + fields, + metadata: self.metadata.clone(), + }) + } + + /// Get a field by its path. Return `None` if the field does not exist. + /// Field names containing dots must be quoted: parent."child.with.dot" + pub fn field(&self, name: &str) -> Option<&Field> { + self.resolve(name).and_then(|fields| fields.last().copied()) + } + + /// Get a field by its path, with case-insensitive matching. + /// + /// This first tries an exact match, then falls back to case-insensitive matching. + /// Returns the actual field from the schema (preserving original case). + /// Field names containing dots must be quoted: parent."child.with.dot" + pub fn field_case_insensitive(&self, name: &str) -> Option<&Field> { + self.resolve_case_insensitive(name) + .and_then(|fields| fields.last().copied()) + } + + /// Given a string column reference, resolve the path of fields with case-insensitive matching. + /// + /// This first tries an exact match, then falls back to case-insensitive matching. + /// Returns the actual fields from the schema (preserving original case). + pub fn resolve_case_insensitive(&self, column: impl AsRef) -> Option> { + let split = parse_field_path(column.as_ref()).ok()?; + if split.is_empty() { + return None; + } + + if split.len() == 1 { + let field_name = &split[0]; + // Try exact match first + if let Some(field) = self.fields.iter().find(|f| &f.name == field_name) { + return Some(vec![field]); + } + // Fall back to case-insensitive match + if let Some(field) = self + .fields + .iter() + .find(|f| f.name.eq_ignore_ascii_case(field_name)) + { + return Some(vec![field]); + } + return None; + } + + // Multiple segments - resolve as a nested field path + let mut fields = Vec::with_capacity(split.len()); + let first = &split[0]; + + // Find the first field (try exact match, then case-insensitive) + let field = self.fields.iter().find(|f| &f.name == first).or_else(|| { + self.fields + .iter() + .find(|f| f.name.eq_ignore_ascii_case(first)) + })?; + + let mut split_refs: VecDeque<&str> = split[1..].iter().map(|s| s.as_str()).collect(); + if field.resolve_case_insensitive(&mut split_refs, &mut fields) { + Some(fields) + } else { + None + } + } + + // TODO: This is not a public API, change to pub(crate) after refactor is done. + pub fn field_id(&self, column: &str) -> Result { + self.field(column) + .map(|f| f.id) + .ok_or_else(|| Error::schema("Vector column not in schema".to_string())) + } + + pub fn top_level_field_ids(&self) -> Vec { + self.fields.iter().map(|f| f.id).collect() + } + + // Recursively collect all the field IDs, in pre-order traversal order. + // TODO: pub(crate) + pub fn field_ids(&self) -> Vec { + self.fields_pre_order().map(|f| f.id).collect() + } + + /// Get field by its id. + pub fn field_by_id_mut(&mut self, id: impl Into) -> Option<&mut Field> { + let id = id.into(); + for field in self.fields.iter_mut() { + if field.id == id { + return Some(field); + } + if let Some(grandchild) = field.field_by_id_mut(id) { + return Some(grandchild); + } + } + None + } + + pub fn field_by_id(&self, id: impl Into) -> Option<&Field> { + let id = id.into(); + for field in self.fields.iter() { + if field.id == id { + return Some(field); + } + if let Some(grandchild) = field.field_by_id(id) { + return Some(grandchild); + } + } + None + } + + /// Get the sequence of fields from the root to the field with the given id. + pub fn field_ancestry_by_id(&self, id: i32) -> Option> { + let mut to_visit = self.fields.iter().map(|f| vec![f]).collect::>(); + while let Some(path) = to_visit.pop() { + let field = path.last().unwrap(); + if field.id == id { + return Some(path); + } + for child in field.children.iter() { + let mut new_path = path.clone(); + new_path.push(child); + to_visit.push(new_path); + } + } + None + } + + pub fn mut_field_by_id(&mut self, id: impl Into) -> Option<&mut Field> { + let id = id.into(); + for field in self.fields.as_mut_slice() { + if field.id == id { + return Some(field); + } + if let Some(grandchild) = field.mut_field_by_id(id) { + return Some(grandchild); + } + } + None + } + + // TODO: pub(crate) + /// Get the maximum field id in the schema. + /// + /// Note: When working with Datasets, you should prefer `Manifest::max_field_id()` + /// over this method. This method does not take into account the field IDs + /// of dropped fields. + pub fn max_field_id(&self) -> Option { + self.fields.iter().map(|f| f.max_id()).max() + } + + /// Recursively attach set up dictionary values to the dictionary fields. + // TODO: pub(crate) + pub fn set_dictionary(&mut self, batch: &RecordBatch) -> Result<()> { + for field in self.fields.as_mut_slice() { + let column = batch.column_by_name(&field.name).ok_or_else(|| { + Error::schema(format!( + "column '{}' does not exist in the record batch", + field.name + )) + })?; + field.set_dictionary(column); + } + Ok(()) + } + + /// Walk through the fields and assign a new field id to each field that does + /// not have one (e.g. is set to -1) + /// + /// If this schema is on an existing dataset, pass the result of + /// `Manifest::max_field_id` to `max_existing_id`. If for some reason that + /// id is lower than the maximum field id in this schema, the field IDs will + /// be reassigned starting from the maximum field id in this schema. + /// + /// If this schema is not associated with a dataset, pass `None` to + /// `max_existing_id`. This is the same as passing [Self::max_field_id()]. + pub fn set_field_id(&mut self, max_existing_id: Option) { + let schema_max_id = self.max_field_id().unwrap_or(-1); + let max_existing_id = max_existing_id.unwrap_or(-1); + let mut current_id = schema_max_id.max(max_existing_id) + 1; + self.fields + .iter_mut() + .for_each(|f| f.set_id(-1, &mut current_id)); + } + + fn reset_id(&mut self) { + self.fields.iter_mut().for_each(|f| f.reset_id()); + } + + /// Create a new schema by adding fields to the end of this schema + pub fn extend(&mut self, fields: &[ArrowField]) -> Result<()> { + let new_fields = fields + .iter() + .map(Field::try_from) + .collect::>>()?; + self.fields.extend(new_fields); + // Validate this addition does not create any duplicate field names + let field_names = self.fields.iter().map(|f| &f.name).collect::>(); + if field_names.len() != self.fields.len() { + Err(Error::internal(format!( + "Attempt to add fields [{:?}] would lead to duplicate field names", + fields.iter().map(|f| f.name()).collect::>() + ))) + } else { + Ok(()) + } + } + + /// Merge this schema from the other schema. + /// + /// After merging, the field IDs from `other` schema will be reassigned, + /// following the fields in `self`. Schema metadata is combined, with values + /// from `self` taking precedence when both schemas contain the same key. + pub fn merge>(&self, other: S) -> Result { + let mut other: Self = other.try_into()?; + other.reset_id(); + + let mut merged_fields: Vec = vec![]; + for mut field in self.fields.iter().cloned() { + if let Some(other_field) = other.field(&field.name) { + // if both are struct types, then merge the fields + field.merge(other_field)?; + } + merged_fields.push(field); + } + + // we already checked for overlap so just need to add new top-level fields + // in the incoming schema + for field in other.fields.as_slice() { + if !merged_fields.iter().any(|f| f.name == field.name) { + merged_fields.push(field.clone()); + } + } + let mut metadata = other.metadata; + metadata.extend( + self.metadata + .iter() + .map(|(key, value)| (key.clone(), value.clone())), + ); + let schema = Self { + fields: merged_fields, + metadata, + }; + Ok(schema) + } + + /// Returns the properly formatted path from root to the field. + /// Field names containing dots are quoted (e.g., struct.`field.with.dot`) + /// + /// The result is suitable for SQL parsing. For a human-readable path + /// (e.g. for display in index metadata), use [`Self::field_path_minimal`]. + pub fn field_path(&self, field_id: i32) -> Result { + self.field_ancestry_by_id(field_id) + .map(|ancestry| { + let field_refs: Vec<&str> = ancestry.iter().map(|f| f.name.as_str()).collect(); + format_field_path(&field_refs) + }) + .ok_or_else(|| { + Error::index(format!("Could not find field ancestry for id {}", field_id)) + }) + } + + /// Returns the path from root to the field using *minimal* quoting. + /// + /// A segment is wrapped in backticks only when it contains a character that + /// [`parse_field_path`] treats specially (a `.` separator or a `` ` `` quote); + /// any other character — including hyphens — is left bare. Unlike + /// [`Self::field_path`] (which quotes for SQL-expression safety and so wraps + /// e.g. `my-col` in backticks), the result here is both human-readable and + /// round-trips back through `parse_field_path`, so it is safe to feed into + /// field-path APIs such as `drop_columns` / `update_field_metadata`. + /// + /// This is what should be exposed as the column name in index metadata. + /// + /// ``` + /// use arrow_schema::{DataType, Field, Fields, Schema as ArrowSchema}; + /// use lance_core::datatypes::{parse_field_path, Schema}; + /// + /// let arrow = ArrowSchema::new(vec![ + /// Field::new("my-col", DataType::Int32, false), + /// Field::new( + /// "parent", + /// DataType::Struct(Fields::from(vec![Field::new("child.x", DataType::Int32, true)])), + /// true, + /// ), + /// ]); + /// let schema = Schema::try_from(&arrow).unwrap(); + /// + /// // A hyphen is not special to `parse_field_path`, so it is left bare + /// // (unlike `field_path`, which would quote it as `` `my-col` ``). + /// let hyphen_id = schema.field("my-col").unwrap().id; + /// assert_eq!(schema.field_path_minimal(hyphen_id).unwrap(), "my-col"); + /// + /// // A `.` in a segment forces quoting so the path still round-trips. + /// let dotted_id = schema.field("parent").unwrap().children[0].id; + /// let path = schema.field_path_minimal(dotted_id).unwrap(); + /// assert_eq!(path, "parent.`child.x`"); + /// assert_eq!( + /// parse_field_path(&path).unwrap(), + /// vec!["parent".to_string(), "child.x".to_string()], + /// ); + /// ``` + pub fn field_path_minimal(&self, field_id: i32) -> Result { + self.field_ancestry_by_id(field_id) + .map(|ancestry| { + let field_refs: Vec<&str> = ancestry.iter().map(|f| f.name.as_str()).collect(); + format_field_path_minimal(&field_refs) + }) + .ok_or_else(|| { + Error::index(format!("Could not find field ancestry for id {}", field_id)) + }) + } + + pub fn verify_primary_key(&self) -> Result<()> { + let pk = self.unenforced_primary_key(); + for pk_col in pk.into_iter() { + if !pk_col.is_leaf() { + return Err(Error::schema(format!( + "Primary key column must be a leaf: {}", + pk_col + ))); + } + + if let Some(ancestors) = self.field_ancestry_by_id(pk_col.id) { + for ancestor in ancestors { + if ancestor.nullable { + return Err(Error::schema(format!( + "Primary key column and all its ancestors must not be nullable: {}", + ancestor + ))); + } + + if ancestor.logical_type.is_list() || ancestor.logical_type.is_large_list() { + return Err(Error::schema(format!( + "Primary key column must not be in a list type: {}", + ancestor + ))); + } + + if ancestor.logical_type.is_map() { + return Err(Error::schema(format!( + "Primary key column must not be in a map type: {}", + ancestor + ))); + } + } + } + } + Ok(()) + } +} + +impl PartialEq for Schema { + fn eq(&self, other: &Self) -> bool { + self.fields == other.fields + } +} + +impl fmt::Display for Schema { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + for field in self.fields.iter() { + writeln!(f, "{field}")? + } + Ok(()) + } +} + +/// Convert `arrow2::datatype::Schema` to Lance +impl TryFrom<&ArrowSchema> for Schema { + type Error = Error; + + fn try_from(schema: &ArrowSchema) -> Result { + let mut schema = Self { + fields: schema + .fields + .iter() + .map(|f| Field::try_from(f.as_ref())) + .collect::>()?, + metadata: schema.metadata.clone(), + }; + schema.set_field_id(None); + schema.validate()?; + + schema.verify_primary_key()?; + + Ok(schema) + } +} + +/// Convert Lance Schema to Arrow Schema +impl From<&Schema> for ArrowSchema { + fn from(schema: &Schema) -> Self { + Self { + fields: schema.fields.iter().map(ArrowField::from).collect(), + metadata: schema.metadata.clone(), + } + } +} + +/// Make API cleaner to accept both [`Schema`] and Arrow Schema. +impl TryFrom<&Self> for Schema { + type Error = Error; + + fn try_from(schema: &Self) -> Result { + Ok(schema.clone()) + } +} + +pub fn compare_fields( + fields: &[Field], + expected: &[Field], + options: &SchemaCompareOptions, +) -> bool { + if options.allow_missing_if_nullable || options.ignore_field_order || options.allow_subschema { + let expected_names = expected + .iter() + .map(|f| f.name.as_str()) + .collect::>(); + for field in fields { + if !expected_names.contains(field.name.as_str()) { + // Extra field + return false; + } + } + + let field_mapping = fields + .iter() + .enumerate() + .map(|(pos, f)| (f.name.as_str(), (f, pos))) + .collect::>(); + let mut cumulative_position = 0; + for expected_field in expected { + if let Some((field, pos)) = field_mapping.get(expected_field.name.as_str()) { + if !field.compare_with_options(expected_field, options) { + return false; + } + if !options.ignore_field_order && *pos < cumulative_position { + return false; + } + cumulative_position = *pos; + } else if options.allow_subschema { + // allow_subschema: allow missing any field + continue; + } else if options.allow_missing_if_nullable && expected_field.nullable { + continue; + } else { + return false; + } + } + true + } else { + // Fast path: we can just zip + fields.len() == expected.len() + && fields + .iter() + .zip(expected.iter()) + .all(|(lhs, rhs)| lhs.compare_with_options(rhs, options)) + } +} + +pub fn explain_fields_difference( + fields: &[Field], + expected: &[Field], + options: &SchemaCompareOptions, + path: Option<&str>, +) -> Vec { + let field_names = fields + .iter() + .map(|f| f.name.as_str()) + .collect::>(); + let expected_names = expected + .iter() + .map(|f| f.name.as_str()) + .collect::>(); + + let prepend_path = |f: &str| { + if let Some(path) = path { + format!("{}.{}", path, f) + } else { + f.to_string() + } + }; + + // Check there are no extra fields or missing fields + let unexpected_fields = field_names + .difference(&expected_names) + .cloned() + .map(prepend_path) + .collect::>(); + let missing_fields = expected_names.difference(&field_names); + let missing_fields = if options.allow_subschema { + // allow_subschema: don't report any missing fields + Vec::new() + } else if options.allow_missing_if_nullable { + missing_fields + .filter(|f| { + let expected_field = expected.iter().find(|ef| ef.name == **f).unwrap(); + !expected_field.nullable + }) + .cloned() + .map(prepend_path) + .collect::>() + } else { + missing_fields + .cloned() + .map(prepend_path) + .collect::>() + }; + + let mut differences = vec![]; + if !missing_fields.is_empty() || !unexpected_fields.is_empty() { + differences.push(format!( + "fields did not match, missing=[{}], unexpected=[{}]", + missing_fields.join(", "), + unexpected_fields.join(", ") + )); + } + + // Map the expected fields to position of field + let field_mapping = expected + .iter() + .filter_map(|ef| { + fields + .iter() + .position(|f| ef.name == f.name) + .map(|pos| (ef, pos)) + }) + .collect::>(); + + // Check the fields are in the same order + if !options.ignore_field_order { + let fields_out_of_order = field_mapping.windows(2).any(|w| w[0].1 > w[1].1); + if fields_out_of_order { + let expected_order = expected.iter().map(|f| f.name.as_str()).collect::>(); + let actual_order = fields.iter().map(|f| f.name.as_str()).collect::>(); + differences.push(format!( + "fields in different order, expected: [{}], actual: [{}]", + expected_order.join(", "), + actual_order.join(", ") + )); + } + } + + // Check for individual differences in the fields + for (expected_field, field_pos) in field_mapping.iter() { + let field = &fields[*field_pos]; + debug_assert_eq!(field.name, expected_field.name); + let field_diffs = field.explain_differences(expected_field, options, path); + if !field_diffs.is_empty() { + differences.push(field_diffs.join(", ")) + } + } + + differences +} + +fn explain_metadata_difference( + metadata: &HashMap, + expected: &HashMap, +) -> Option { + if metadata != expected { + Some(format!( + "metadata did not match, expected: {:?}, actual: {:?}", + expected, metadata + )) + } else { + None + } +} + +/// What to do when a column is missing in the schema +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum OnMissing { + Error, + Ignore, +} + +/// A trait for something that we can project fields from. +pub trait Projectable: Debug + Send + Sync { + fn schema(&self) -> &Schema; +} + +impl Projectable for Schema { + fn schema(&self) -> &Schema { + self + } +} + +/// Specifies how to handle blob columns when projecting +#[derive(Debug, Clone, Default, PartialEq)] +pub enum BlobHandling { + /// Read all blobs as binary + AllBinary, + #[default] + /// Read all blobs as descriptions and other binary columns as binary + BlobsDescriptions, + /// Read all binary columns as descriptions + AllDescriptions, + /// Read specific blobs as binary and the rest as descriptions + /// + /// Non-blob binary columns will be read as binary + /// + /// The set contains the field ids that should be read as binary + SomeBlobsBinary(HashSet), + /// Read specific columns as binary and all other binary columns as descriptions + /// + /// The set contains the field ids that should be read as binary + SomeBinary(HashSet), +} + +impl BlobHandling { + fn should_load_binary(&self, field: &Field) -> bool { + if !field.is_blob() { + return false; + } + match self { + Self::AllBinary => true, + Self::SomeBlobsBinary(set) | Self::SomeBinary(set) => set.contains(&(field.id as u32)), + Self::BlobsDescriptions | Self::AllDescriptions => false, + } + } + + fn should_unload(&self, field: &Field) -> bool { + // Blob v2 columns are Structs, so we need to treat any blob-marked field as unloadable + // even if the physical data type is not binary-like. + if !(field.data_type().is_binary_like() || field.is_blob()) { + return false; + } + match self { + Self::AllBinary => false, + Self::BlobsDescriptions => field.is_blob(), + Self::AllDescriptions => true, + Self::SomeBlobsBinary(set) => field.is_blob() && !set.contains(&(field.id as u32)), + Self::SomeBinary(set) => !set.contains(&(field.id as u32)), + } + } + + /// Whether `field` will be projected as a lightweight blob *description* + /// (offset + size) rather than its full binary value under this handling. + /// + /// A description is tiny and cheap to read eagerly; the full binary value is + /// not. Materialization heuristics use this to decide early vs late loading. + pub fn returns_description(&self, field: &Field) -> bool { + self.should_unload(field) + } + + /// Apply this blob handling policy to a projected field tree. + /// + /// Blob descriptor modes convert blob leaves to descriptor views. Binary + /// modes convert selected blob leaves to `LargeBinary`. Non-blob nested + /// fields are preserved while their children are handled recursively. + pub fn unload_if_needed(&self, mut field: Field) -> Field { + if self.should_load_binary(&field) { + field.binary_blob_mut(); + return field; + } + if self.should_unload(&field) { + field.unloaded_mut(); + return field; + } + field.children = field + .children + .into_iter() + .map(|child| self.unload_if_needed(child)) + .collect(); + field + } +} + +/// A projection is a selection of fields in a schema +/// +/// In addition we record whether the row_id or row_addr are +/// selected (these fields have no field id) +#[derive(Clone)] +pub struct Projection { + base: Arc, + pub field_ids: HashSet, + pub with_row_id: bool, + pub with_row_addr: bool, + pub with_row_last_updated_at_version: bool, + pub with_row_created_at_version: bool, + pub blob_handling: BlobHandling, +} + +impl Debug for Projection { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("Projection") + .field("field_ids", &self.field_ids) + .field("with_row_id", &self.with_row_id) + .field("with_row_addr", &self.with_row_addr) + .field( + "with_row_last_updated_at_version", + &self.with_row_last_updated_at_version, + ) + .field( + "with_row_created_at_version", + &self.with_row_created_at_version, + ) + .field("blob_handling", &self.blob_handling) + .finish() + } +} + +impl Projection { + /// Create a new empty projection + pub fn empty(base: Arc) -> Self { + Self { + base, + field_ids: HashSet::new(), + with_row_id: false, + with_row_addr: false, + with_row_last_updated_at_version: false, + with_row_created_at_version: false, + blob_handling: BlobHandling::default(), + } + } + + pub fn full(base: Arc) -> Self { + let schema = base.schema().clone(); + Self::empty(base).union_schema(&schema) + } + + pub fn with_row_id(mut self) -> Self { + self.with_row_id = true; + self + } + + pub fn with_row_addr(mut self) -> Self { + self.with_row_addr = true; + self + } + + pub fn with_row_last_updated_at_version(mut self) -> Self { + self.with_row_last_updated_at_version = true; + self + } + + pub fn with_row_created_at_version(mut self) -> Self { + self.with_row_created_at_version = true; + self + } + + pub fn with_blob_handling(mut self, blob_handling: BlobHandling) -> Self { + self.blob_handling = blob_handling; + self + } + + fn add_field_children(field_ids: &mut HashSet, field: &Field) { + for child in &field.children { + field_ids.insert(child.id); + Self::add_field_children(field_ids, child); + } + } + + /// Add a column to the projection from a string reference + /// + /// The string reference can be a dotted field path (x.y.z) to reference inner struct fields + /// + /// Parent fields will automatically be added. If the specified field has any children then + /// those will be added to. Siblings, aunts, etc. are not automatically added + pub fn union_column(mut self, column: impl AsRef, on_missing: OnMissing) -> Result { + let column = column.as_ref(); + if column == ROW_ID { + self.with_row_id = true; + return Ok(self); + } else if column == ROW_ADDR { + self.with_row_addr = true; + return Ok(self); + } else if column == crate::ROW_LAST_UPDATED_AT_VERSION { + self.with_row_last_updated_at_version = true; + return Ok(self); + } else if column == crate::ROW_CREATED_AT_VERSION { + self.with_row_created_at_version = true; + return Ok(self); + } + + if let Some(fields) = self.base.schema().resolve(column) { + self.field_ids.extend(fields.iter().map(|f| f.id)); + if let Some(last_field) = fields.last() { + Self::add_field_children(&mut self.field_ids, last_field); + } + } else if matches!(on_missing, OnMissing::Error) { + return Err(Error::invalid_input_source( + format!("Column {} does not exist", column).into(), + )); + } + Ok(self) + } + + /// True if the projection selects the given field id + pub fn contains_field_id(&self, id: i32) -> bool { + self.field_ids.contains(&id) + } + + /// True if the projection selects fields other than the row id / addr + pub fn has_data_fields(&self) -> bool { + !self.field_ids.is_empty() + } + + /// Add multiple columns (and their parents) to the projection + pub fn union_columns( + mut self, + columns: impl IntoIterator>, + on_missing: OnMissing, + ) -> Result { + for column in columns { + self = self.union_column(column, on_missing)?; + } + Ok(self) + } + + /// Adds all fields from the base schema satisfying a predicate + pub fn union_predicate(mut self, predicate: impl Fn(&Field) -> bool) -> Self { + for field in self.base.schema().fields_pre_order() { + if predicate(field) { + self.field_ids.insert(field.id); + } + } + self + } + + /// Removes all fields in the base schema satisfying a predicate + pub fn subtract_predicate(mut self, predicate: impl Fn(&Field) -> bool) -> Self { + for field in self.base.schema().fields_pre_order() { + if predicate(field) { + self.field_ids.remove(&field.id); + } + } + self + } + + /// Creates a new projection that is the intersection of this projection and another + pub fn intersect(mut self, other: &Self) -> Self { + self.field_ids = HashSet::from_iter(self.field_ids.intersection(&other.field_ids).copied()); + self.with_row_id = self.with_row_id && other.with_row_id; + self.with_row_addr = self.with_row_addr && other.with_row_addr; + self.with_row_last_updated_at_version = + self.with_row_last_updated_at_version && other.with_row_last_updated_at_version; + self.with_row_created_at_version = + self.with_row_created_at_version && other.with_row_created_at_version; + self + } + + /// Adds all fields from the provided schema to the projection + /// + /// Fields are only added if they exist in the base schema, otherwise they + /// are ignored. + /// + /// Will panic if a field in the given schema has a non-negative id and is not in the base schema. + pub fn union_schema(mut self, other: &Schema) -> Self { + for field in other.fields_pre_order() { + if field.id >= 0 { + self.field_ids.insert(field.id); + } else if field.name == ROW_ID { + self.with_row_id = true; + } else if field.name == ROW_ADDR { + self.with_row_addr = true; + } else if field.name == crate::ROW_LAST_UPDATED_AT_VERSION { + self.with_row_last_updated_at_version = true; + } else if field.name == crate::ROW_CREATED_AT_VERSION { + self.with_row_created_at_version = true; + } else { + // If a field is not in our schema then it should probably have an id of -1. If it isn't -1 + // that probably implies some kind of weird schema mixing is going on and we should panic. + debug_assert_eq!(field.id, -1); + } + } + self + } + + /// Creates a new projection that is the union of this projection and another + pub fn union_projection(mut self, other: &Self) -> Self { + self.field_ids.extend(&other.field_ids); + self.with_row_id = self.with_row_id || other.with_row_id; + self.with_row_addr = self.with_row_addr || other.with_row_addr; + self.with_row_last_updated_at_version = + self.with_row_last_updated_at_version || other.with_row_last_updated_at_version; + self.with_row_created_at_version = + self.with_row_created_at_version || other.with_row_created_at_version; + self + } + + /// Adds all fields from the given schema to the projection + /// + /// on_missing controls what happen to fields that are not in the base schema + /// + /// Name based matching is used to determine if a field is in the base schema. + pub fn union_arrow_schema( + mut self, + other: &ArrowSchema, + on_missing: OnMissing, + ) -> Result { + self.with_row_id |= other.fields().iter().any(|f| f.name() == ROW_ID); + self.with_row_addr |= other.fields().iter().any(|f| f.name() == ROW_ADDR); + self.with_row_last_updated_at_version |= other + .fields() + .iter() + .any(|f| f.name() == crate::ROW_LAST_UPDATED_AT_VERSION); + self.with_row_created_at_version |= other + .fields() + .iter() + .any(|f| f.name() == crate::ROW_CREATED_AT_VERSION); + let other = + self.base + .schema() + .project_by_schema(other, on_missing, OnTypeMismatch::TakeSelf)?; + Ok(self.union_schema(&other)) + } + + /// Removes all fields from the projection that are in the given schema + /// + /// on_missing controls what happen to fields that are not in the base schema + /// + /// Name based matching is used to determine if a field is in the base schema. + pub fn subtract_arrow_schema( + mut self, + other: &ArrowSchema, + on_missing: OnMissing, + ) -> Result { + self.with_row_id &= !other.fields().iter().any(|f| f.name() == ROW_ID); + self.with_row_addr &= !other.fields().iter().any(|f| f.name() == ROW_ADDR); + self.with_row_last_updated_at_version &= !other + .fields() + .iter() + .any(|f| f.name() == crate::ROW_LAST_UPDATED_AT_VERSION); + self.with_row_created_at_version &= !other + .fields() + .iter() + .any(|f| f.name() == crate::ROW_CREATED_AT_VERSION); + let other = + self.base + .schema() + .project_by_schema(other, on_missing, OnTypeMismatch::TakeSelf)?; + Ok(self.subtract_schema(&other)) + } + + /// Removes all fields from this projection that are present in the given projection + pub fn subtract_projection(mut self, other: &Self) -> Self { + self.field_ids = self + .field_ids + .difference(&other.field_ids) + .copied() + .collect(); + self.with_row_addr = self.with_row_addr && !other.with_row_addr; + self.with_row_id = self.with_row_id && !other.with_row_id; + self.with_row_last_updated_at_version = + self.with_row_last_updated_at_version && !other.with_row_last_updated_at_version; + self.with_row_created_at_version = + self.with_row_created_at_version && !other.with_row_created_at_version; + self + } + + /// Removes all fields from the projection that are in the given schema + /// + /// Fields are only removed if they exist in the base schema, otherwise they + /// are ignored. + /// + /// Will panic if a field in the given schema has a non-negative id and is not in the base schema. + pub fn subtract_schema(mut self, other: &Schema) -> Self { + for field in other.fields_pre_order() { + if field.id >= 0 { + self.field_ids.remove(&field.id); + } else if field.name == ROW_ID { + self.with_row_id = false; + } else if field.name == ROW_ADDR { + self.with_row_addr = false; + } else if field.name == crate::ROW_LAST_UPDATED_AT_VERSION { + self.with_row_last_updated_at_version = false; + } else if field.name == crate::ROW_CREATED_AT_VERSION { + self.with_row_created_at_version = false; + } else { + debug_assert_eq!(field.id, -1); + } + } + self + } + + /// True if the projection does not select any fields or take the row id / addr + pub fn is_empty(&self) -> bool { + self.field_ids.is_empty() + && !self.with_row_addr + && !self.with_row_id + && !self.with_row_last_updated_at_version + && !self.with_row_created_at_version + } + + /// True if the projection is only the row_id or row_addr columns + /// + /// Note: this will return false for a completely empty projection + pub fn is_metadata_only(&self) -> bool { + self.field_ids.is_empty() + && (self.with_row_addr + || self.with_row_id + || self.with_row_last_updated_at_version + || self.with_row_created_at_version) + } + + /// True if the projection has at least one non-metadata column + pub fn has_non_meta_cols(&self) -> bool { + !self.field_ids.is_empty() + } + + /// Convert the projection to a schema that does not include metadata columns + pub fn to_bare_schema(&self) -> Schema { + self.base.schema().apply_projection(self) + } + + /// Convert the projection to a schema + /// + /// Includes the _rowid and _rowaddr columns if requested + pub fn to_schema(&self) -> Schema { + let mut schema = self.to_bare_schema(); + let mut extra_fields = Vec::new(); + if self.with_row_id { + extra_fields.push(ROW_ID_FIELD.clone()); + } + if self.with_row_addr { + extra_fields.push(ROW_ADDR_FIELD.clone()); + } + if self.with_row_last_updated_at_version { + extra_fields.push(crate::ROW_LAST_UPDATED_AT_VERSION_FIELD.clone()); + } + if self.with_row_created_at_version { + extra_fields.push(crate::ROW_CREATED_AT_VERSION_FIELD.clone()); + } + schema.extend(&extra_fields).unwrap(); + schema + } + + /// Convert the projection to a schema + pub fn into_schema(self) -> Schema { + self.to_schema() + } + + /// Convert the projection to a schema reference + pub fn into_schema_ref(self) -> Arc { + Arc::new(self.into_schema()) + } + + /// Convert the projection into an Arrow schema + pub fn to_arrow_schema(&self) -> arrow_schema::Schema { + (&self.to_schema()).into() + } +} + +/// Parse a field path that may contain quoted field names. +/// +/// Field names containing dots must be quoted with backticks. +/// For example: "parent.`child.with.dot`" parses to ["parent", "child.with.dot"] +/// +/// Backticks within quoted fields must be escaped by doubling them. +/// For example: "`field``with``backticks`" represents the field name "field`with`backticks" +/// +/// Returns an error if: +/// - The input path is empty +/// - The path has malformed quotes (unclosed, misplaced, etc.) +/// - The path has empty segments (e.g., "parent..child" or "parent.") +/// +/// The result is guaranteed to contain at least one element. +pub fn parse_field_path(path: &str) -> Result> { + if path.is_empty() { + return Err(Error::schema("Field path cannot be empty".to_string())); + } + + let mut result = Vec::new(); + let mut current = String::new(); + let mut in_quotes = false; + let mut chars = path.chars().peekable(); + + while let Some(ch) = chars.next() { + match ch { + '`' => { + if in_quotes { + // Check if this is an escaped backtick (double backtick) + if chars.peek() == Some(&'`') { + // Consume the second backtick and add a single backtick to current + chars.next(); + current.push('`'); + } else { + // End of quoted field + in_quotes = false; + // After closing quote, we should either see a dot or end of string + if let Some(&next_ch) = chars.peek() + && next_ch != '.' + { + return Err(Error::schema(format!( + "Invalid field path '{}': expected '.' or end of string after closing quote", + path + ))); + } + } + } else if current.is_empty() { + // Start of quoted field + in_quotes = true; + } else { + // Quote in the middle of unquoted field name + return Err(Error::schema(format!( + "Invalid field path '{}': unexpected quote in the middle of field name", + path + ))); + } + } + '.' if !in_quotes => { + if current.is_empty() { + return Err(Error::schema(format!( + "Invalid field path '{}': empty field name", + path + ))); + } + result.push(current); + current = String::new(); + } + _ => { + current.push(ch); + } + } + } + + if in_quotes { + return Err(Error::schema(format!( + "Invalid field path '{}': unclosed quote", + path + ))); + } + + if !current.is_empty() { + result.push(current); + } else if !result.is_empty() { + return Err(Error::schema(format!( + "Invalid field path '{}': trailing dot", + path + ))); + } + + // This check is now redundant since we check for empty input at the beginning, + // but keeping it for extra safety + if result.is_empty() { + return Err(Error::schema(format!("Invalid field path '{}'", path))); + } + + Ok(result) +} + +/// Format a field path, quoting field names that require escaping. +/// +/// Field names are quoted if they contain any character that is not alphanumeric +/// or underscore, to ensure safe SQL parsing. +/// +/// For example: ["parent", "child.with.dot"] formats to "parent.`child.with.dot`" +/// For example: ["meta-data", "user-id"] formats to "`meta-data`.`user-id`" +/// Backticks in field names are escaped by doubling them. +/// For example: \["field`with`backticks"\] formats to "`field``with``backticks`" +pub fn format_field_path(fields: &[&str]) -> String { + fields + .iter() + .map(|field| { + // Quote if the field contains any non-identifier character + // (i.e., anything other than alphanumeric or underscore) + let needs_quoting = field.chars().any(|c| !c.is_alphanumeric() && c != '_'); + if needs_quoting { + // Escape backticks by doubling them (PostgreSQL style) + let escaped = field.replace('`', "``"); + format!("`{}`", escaped) + } else { + field.to_string() + } + }) + .collect::>() + .join(".") +} + +/// Like [`format_field_path`], but quotes a segment only when strictly required +/// for the result to round-trip back through [`parse_field_path`]. +/// +/// `parse_field_path` only treats `.` (segment separator) and `` ` `` (quote) +/// specially, so those are the only characters that force quoting here. Notably +/// a hyphen does NOT force quoting (`my-col` stays `my-col`), unlike +/// `format_field_path` which quotes any non-identifier character for +/// SQL-expression safety. Use this for human-readable, round-trippable paths +/// (e.g. column names in index metadata); use `format_field_path` when the +/// result will be embedded in a SQL expression. +/// +/// ``` +/// use lance_core::datatypes::{format_field_path_minimal, parse_field_path}; +/// +/// // Plain identifiers and hyphenated names are left bare. +/// assert_eq!(format_field_path_minimal(&["parent", "my-col"]), "parent.my-col"); +/// // A `.` in a segment forces quoting. +/// assert_eq!(format_field_path_minimal(&["parent", "child.x"]), "parent.`child.x`"); +/// // Embedded backticks are escaped by doubling them. +/// assert_eq!(format_field_path_minimal(&["child`x"]), "`child``x`"); +/// +/// // Whatever it produces round-trips back through `parse_field_path`. +/// for segments in [vec!["parent", "my-col"], vec!["parent", "child.x"], vec!["child`x"]] { +/// let path = format_field_path_minimal(&segments); +/// assert_eq!(parse_field_path(&path).unwrap(), segments); +/// } +/// ``` +pub fn format_field_path_minimal(fields: &[&str]) -> String { + fields + .iter() + .map(|field| { + let needs_quoting = field.contains('.') || field.contains('`'); + if needs_quoting { + // Escape embedded backticks by doubling them, matching parse_field_path. + let escaped = field.replace('`', "``"); + format!("`{}`", escaped) + } else { + field.to_string() + } + }) + .collect::>() + .join(".") +} + +/// Escape a field path for project +/// +/// Parses the field path and formats it for SQL usage. +/// Always quotes all segments with backticks to prevent special characters. +/// +/// For example: +/// - "parent.child" -> “`parent`.`child`” +/// - "parent.`child.with.dot`" -> “`parent`.`child.with.dot`” +pub fn escape_field_path_for_project(name: &str) -> String { + if name == WILDCARD { + return name.to_string(); + } + let segments = parse_field_path(name).unwrap_or_else(|_| vec![name.to_string()]); + segments + .iter() + .map(|s| { + let escaped = s.replace('`', "``"); + format!("`{}`", escaped) + }) + .collect::>() + .join(".") +} + +#[cfg(test)] +mod tests { + use arrow_schema::{DataType as ArrowDataType, Fields as ArrowFields}; + use std::{collections::HashMap, sync::Arc}; + + use super::*; + + #[test] + fn test_resolve_with_quoted_fields() { + // Create a schema with fields containing dots + let field_with_dots = Field::try_from(&ArrowField::new( + "simple.name.with.dot", + ArrowDataType::Int32, + false, + )) + .unwrap(); + let normal_field = + Field::try_from(&ArrowField::new("normal", ArrowDataType::Int32, false)).unwrap(); + let nested_field = Field::try_from(&ArrowField::new( + "parent", + ArrowDataType::Struct(ArrowFields::from(vec![ + ArrowField::new("child.with.dot", ArrowDataType::Int32, false), + ArrowField::new("normal_child", ArrowDataType::Int32, false), + ])), + false, + )) + .unwrap(); + + let schema = Schema { + fields: vec![field_with_dots, normal_field, nested_field], + metadata: HashMap::new(), + }; + + // Test 1: Resolving a field with dots using quotes + let resolved = schema.resolve("`simple.name.with.dot`"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 1); + assert_eq!(fields[0].name, "simple.name.with.dot"); + + // Test 2: Resolving a normal field + let resolved = schema.resolve("normal"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 1); + assert_eq!(fields[0].name, "normal"); + + // Test 3: Resolving a nested field with dots + let resolved = schema.resolve("parent.`child.with.dot`"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name, "parent"); + assert_eq!(fields[1].name, "child.with.dot"); + + // Test 4: Resolving a normal nested field + let resolved = schema.resolve("parent.normal_child"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name, "parent"); + assert_eq!(fields[1].name, "normal_child"); + + // Test 5: Non-existent field should return None + let resolved = schema.resolve("\"non.existent\""); + assert!(resolved.is_none()); + + // Test 6: Schema::field should work the same way + let field = schema.field("`simple.name.with.dot`"); + assert!(field.is_some()); + assert_eq!(field.unwrap().name, "simple.name.with.dot"); + + let field = schema.field("parent.`child.with.dot`"); + assert!(field.is_some()); + assert_eq!(field.unwrap().name, "child.with.dot"); + + let field = schema.field("parent.normal_child"); + assert!(field.is_some()); + assert_eq!(field.unwrap().name, "normal_child"); + } + + #[test] + fn test_field_path_parsing() { + // Simple paths without quotes + assert_eq!( + parse_field_path("a.b.c").unwrap(), + vec!["a".to_string(), "b".to_string(), "c".to_string()] + ); + + // Single quoted field with dots + assert_eq!( + parse_field_path("`simple.name.with.dot`").unwrap(), + vec!["simple.name.with.dot".to_string()] + ); + + // Path with quoted field containing dots + assert_eq!( + parse_field_path("parent.`child.with.dot`.normal").unwrap(), + vec![ + "parent".to_string(), + "child.with.dot".to_string(), + "normal".to_string() + ] + ); + + // Quoted field at the beginning + assert_eq!( + parse_field_path("`field.with.dot`.child").unwrap(), + vec!["field.with.dot".to_string(), "child".to_string()] + ); + + // Simple field + assert_eq!( + parse_field_path("simple").unwrap(), + vec!["simple".to_string()] + ); + + assert_eq!( + parse_field_path("tags[*]").unwrap(), + vec!["tags[*]".to_string()] + ); + + // Quoted field at the end + assert_eq!( + parse_field_path("parent.`field.with.dot`").unwrap(), + vec!["parent".to_string(), "field.with.dot".to_string()] + ); + + // Field with escaped backticks (PostgreSQL style - double backticks) + assert_eq!( + parse_field_path("parent.`field``with``backticks`").unwrap(), + vec!["parent".to_string(), "field`with`backticks".to_string()] + ); + + // Invalid: unclosed quote + assert!(parse_field_path("parent.`unclosed").is_err()); + + // Invalid: quote in middle of unquoted field + assert!(parse_field_path("par`ent.child").is_err()); + + // Invalid: empty field name + assert!(parse_field_path("parent..child").is_err()); + + // Invalid: trailing dot + assert!(parse_field_path("parent.").is_err()); + + // Test formatting + assert_eq!( + format_field_path(&["parent", "child.with.dot", "normal"]), + "parent.`child.with.dot`.normal" + ); + + assert_eq!( + format_field_path(&["field`with`backticks"]), + "`field``with``backticks`" + ); + } + + #[test] + fn test_validate_top_level_names_without_field_id_lookup() { + let mut first = Field::new_arrow("first", DataType::Int32, false).unwrap(); + first.id = 0; + let mut second = Field::new_arrow("second", DataType::Int32, false).unwrap(); + second.id = 0; + let schema = Schema { + fields: vec![first, second], + metadata: HashMap::new(), + }; + + let error = schema.validate().unwrap_err(); + assert!(matches!(&error, Error::Schema { .. })); + assert!(error.to_string().contains("Duplicate field id 0")); + } + + #[test] + fn test_resolve_quoted_fields() { + // Test that top-level fields with dots are rejected during validation + let arrow_schema_with_dots = ArrowSchema::new(vec![ArrowField::new( + "field.with.dots", + DataType::Int32, + false, + )]); + + // Schema creation should fail due to validation + let schema_result = Schema::try_from(&arrow_schema_with_dots); + assert!(schema_result.is_err()); + let err = schema_result.unwrap_err(); + assert!( + err.to_string() + .contains("Top level field field.with.dots cannot contain `.`") + ); + + // Test that nested fields with dots are allowed + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("regular_field", DataType::Int32, false), + ArrowField::new( + "parent", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("child.with.dot", DataType::Utf8, true), + ArrowField::new("normal_child", DataType::Int32, false), + ])), + false, + ), + ]); + + let schema = Schema::try_from(&arrow_schema).unwrap(); + + // Test resolving regular field + let resolved = schema.resolve("regular_field"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 1); + assert_eq!(fields[0].name, "regular_field"); + + // Test resolving nested field with dots using quotes + let resolved = schema.resolve("parent.`child.with.dot`"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name, "parent"); + assert_eq!(fields[1].name, "child.with.dot"); + + // Test resolving normal nested field + let resolved = schema.resolve("parent.normal_child"); + assert!(resolved.is_some()); + let fields = resolved.unwrap(); + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name, "parent"); + assert_eq!(fields[1].name, "normal_child"); + } + + use arrow_schema::DataType; + + #[test] + fn test_schema_projection() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let projected = schema.project(&["b.f1", "b.f3", "c"]).unwrap(); + + let expected_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + } + + #[test] + fn test_schema_projection_preserving_system_columns() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let projected = schema + .project_preserve_system_columns(&["b.f1", "b.f3", "_rowid", "c"]) + .unwrap(); + + let expected_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("_rowid", DataType::UInt64, true), + ArrowField::new("c", DataType::Float64, false), + ]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + } + + #[test] + fn test_schema_project_by_ids() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let mut schema = Schema::try_from(&arrow_schema).unwrap(); + schema.set_field_id(None); + let projected = schema.project_by_ids(&[2, 4, 5], true); + + let expected_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + + let projected = schema.project_by_ids(&[2], true); + let expected_arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f1", + DataType::Utf8, + true, + )])), + true, + )]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + + let projected = schema.project_by_ids(&[1], true); + let expected_arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + )]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + + let projected = schema.project_by_ids(&[1, 2], false); + let expected_arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f1", + DataType::Utf8, + true, + )])), + true, + )]); + assert_eq!(ArrowSchema::from(&projected), expected_arrow_schema); + } + + #[test] + fn test_schema_project_by_schema() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ArrowField::new("s", DataType::Utf8, false), + ArrowField::new( + "l", + DataType::List(Arc::new(ArrowField::new("le", DataType::Int32, false))), + false, + ), + ArrowField::new( + "fixed_l", + DataType::List(Arc::new(ArrowField::new("elem", DataType::Float32, false))), + false, + ), + ArrowField::new( + "d", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + false, + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let projection = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f1", + DataType::Utf8, + true, + )])), + true, + ), + ArrowField::new("s", DataType::Utf8, false), + ArrowField::new( + "l", + DataType::List(Arc::new(ArrowField::new("le", DataType::Int32, false))), + false, + ), + ArrowField::new( + "fixed_l", + DataType::List(Arc::new(ArrowField::new("elem", DataType::Float32, false))), + false, + ), + ArrowField::new( + "d", + DataType::Dictionary(Box::new(DataType::UInt32), Box::new(DataType::Utf8)), + false, + ), + ]); + let projected = schema + .project_by_schema(&projection, OnMissing::Error, OnTypeMismatch::TakeSelf) + .unwrap(); + + assert_eq!(ArrowSchema::from(&projected), projection); + } + + #[test] + fn test_get_nested_field() { + let arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + )]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let field = schema.field("b.f2").unwrap(); + assert_eq!(field.data_type(), DataType::Boolean); + } + + #[test] + fn test_exclude_fields() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let projection = schema.project(&["a", "b.f2", "b.f3"]).unwrap(); + let excluded = schema.exclude(&projection).unwrap(); + + let expected_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f1", + DataType::Utf8, + true, + )])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + assert_eq!(ArrowSchema::from(&excluded), expected_arrow_schema); + } + + #[test] + fn test_intersection() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ArrowField::new("d", DataType::Utf8, false), + ]); + let other = Schema::try_from(&arrow_schema).unwrap(); + + let actual: ArrowSchema = (&schema.intersection(&other).unwrap()).into(); + + let expected = ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + assert_eq!(actual, expected); + + let schema_with_list_struct = ArrowSchema::new(vec![ArrowField::new( + "struct_list", + DataType::List(Arc::new(ArrowField::new( + "item", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ])), + true, + ))), + true, + )]); + let schema_with_list_struct = Schema::try_from(&schema_with_list_struct).unwrap(); + + let with_missing_field = schema_with_list_struct.project_by_ids(&[1, 3], false); + let intersection = schema_with_list_struct + .intersection_ignore_types(&with_missing_field) + .unwrap(); + assert_eq!(intersection, with_missing_field); + let intersection = with_missing_field + .intersection_ignore_types(&schema_with_list_struct) + .unwrap(); + assert_eq!(intersection, with_missing_field); + } + + #[test] + fn test_merge_schemas_and_assign_field_ids() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + assert_eq!(schema.max_field_id(), Some(5)); + + let to_merged_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("d", DataType::Int32, false), + ArrowField::new("e", DataType::Binary, false), + ]); + let to_merged = Schema::try_from(&to_merged_arrow_schema).unwrap(); + // It is already assigned with field ids. + assert_eq!(to_merged.max_field_id(), Some(1)); + + let mut merged = schema.merge(&to_merged).unwrap(); + assert_eq!(merged.max_field_id(), Some(5)); + + let field = merged.field("d").unwrap(); + assert_eq!(field.id, -1); + let field = merged.field("e").unwrap(); + assert_eq!(field.id, -1); + + // Need to explicitly assign field ids. Testing we can pass a larger + // field id to set_field_id. + merged.set_field_id(Some(7)); + let field = merged.field("d").unwrap(); + assert_eq!(field.id, 8); + let field = merged.field("e").unwrap(); + assert_eq!(field.id, 9); + assert_eq!(merged.max_field_id(), Some(9)); + } + + #[test] + fn test_merge_schema_metadata_preserves_self_values() { + let schema = Schema { + metadata: HashMap::from([ + ("shared".to_string(), "left".to_string()), + ("left_only".to_string(), "left".to_string()), + ]), + ..Default::default() + }; + let other = Schema { + metadata: HashMap::from([ + ("shared".to_string(), "right".to_string()), + ("right_only".to_string(), "right".to_string()), + ]), + ..Default::default() + }; + + let merged = schema.merge(&other).unwrap(); + + assert_eq!( + merged.metadata, + HashMap::from([ + ("shared".to_string(), "left".to_string()), + ("left_only".to_string(), "left".to_string()), + ("right_only".to_string(), "right".to_string()), + ]) + ); + } + + #[test] + fn test_merge_arrow_schema() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + assert_eq!(schema.max_field_id(), Some(5)); + + let to_merged_arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("d", DataType::Int32, false), + ArrowField::new("e", DataType::Binary, false), + ]); + let mut merged = schema.merge(&to_merged_arrow_schema).unwrap(); + merged.set_field_id(None); + assert_eq!(merged.max_field_id(), Some(7)); + + let field = merged.field("d").unwrap(); + assert_eq!(field.id, 6); + let field = merged.field("e").unwrap(); + assert_eq!(field.id, 7); + } + + #[test] + fn test_merge_nested_field() { + let arrow_schema1 = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new( + "f1", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f11", + DataType::Utf8, + true, + )])), + true, + ), + ArrowField::new("f2", DataType::Float32, false), + ])), + true, + )]); + let schema1 = Schema::try_from(&arrow_schema1).unwrap(); + + let arrow_schema2 = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new( + "f1", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f22", + DataType::Utf8, + true, + )])), + true, + ), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + )]); + let schema2 = Schema::try_from(&arrow_schema2).unwrap(); + + let expected_arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new( + "f1", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f11", DataType::Utf8, true), + ArrowField::new("f22", DataType::Utf8, true), + ])), + true, + ), + ArrowField::new("f2", DataType::Float32, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + )]); + let mut expected_schema = Schema::try_from(&expected_arrow_schema).unwrap(); + expected_schema.fields[0] + .child_mut("f1") + .unwrap() + .child_mut("f22") + .unwrap() + .id = 4; + expected_schema.fields[0].child_mut("f2").unwrap().id = 3; + + let mut result = schema1.merge(&schema2).unwrap(); + result.set_field_id(None); + assert_eq!(result, expected_schema); + } + + #[test] + fn test_field_by_id() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let field = schema.field_by_id(1).unwrap(); + assert_eq!(field.name, "b"); + + let field = schema.field_by_id(3).unwrap(); + assert_eq!(field.name, "f2"); + } + + #[test] + fn test_explain_difference() { + let expected = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let expected = Schema::try_from(&expected).unwrap(); + + let mismatched = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, true), + ]); + let mismatched = Schema::try_from(&mismatched).unwrap(); + + assert_eq!( + mismatched.explain_difference(&expected, &SchemaCompareOptions::default()), + Some( + "`b` had mismatched children: fields did not match, missing=[b.f2], \ + unexpected=[], `c` should have nullable=false but nullable=true" + .to_string() + ) + ); + } + + #[test] + fn test_schema_difference_subschema() { + let expected = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, true), + ]); + let expected = Schema::try_from(&expected).unwrap(); + + // Can omit nullable fields and subfields + let subschema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ]); + let subschema = Schema::try_from(&subschema).unwrap(); + + assert!(!subschema.compare_with_options(&expected, &SchemaCompareOptions::default())); + assert_eq!( + subschema.explain_difference(&expected, &SchemaCompareOptions::default()), + Some( + "fields did not match, missing=[c], unexpected=[], `b` had mismatched \ + children: fields did not match, missing=[b.f1], unexpected=[]" + .to_string() + ) + ); + let options = SchemaCompareOptions { + allow_missing_if_nullable: true, + ..Default::default() + }; + assert!(subschema.compare_with_options(&expected, &options)); + let res = subschema.explain_difference(&expected, &options); + assert!(res.is_none(), "Expected None, got {:?}", res); + + // Omitting non-nullable fields should fail + let subschema = ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f2", + DataType::Boolean, + false, + )])), + true, + )]); + let subschema = Schema::try_from(&subschema).unwrap(); + assert!(!subschema.compare_with_options(&expected, &options)); + assert_eq!( + subschema.explain_difference(&expected, &options), + Some( + "fields did not match, missing=[a], unexpected=[], `b` had mismatched \ + children: fields did not match, missing=[b.f3], unexpected=[]" + .to_string() + ) + ); + + let out_of_order = ArrowSchema::new(vec![ + ArrowField::new("c", DataType::Float64, true), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f3", DataType::Float32, false), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f1", DataType::Utf8, true), + ])), + true, + ), + ArrowField::new("a", DataType::Int32, false), + ]); + let out_of_order = Schema::try_from(&out_of_order).unwrap(); + assert!(!out_of_order.compare_with_options(&expected, &options)); + assert_eq!( + subschema.explain_difference(&expected, &options), + Some( + "fields did not match, missing=[a], unexpected=[], `b` had mismatched \ + children: fields did not match, missing=[b.f3], unexpected=[]" + .to_string() + ) + ); + + let options = SchemaCompareOptions { + ignore_field_order: true, + ..Default::default() + }; + assert!(out_of_order.compare_with_options(&expected, &options)); + let res = out_of_order.explain_difference(&expected, &options); + assert!(res.is_none(), "Expected None, got {:?}", res); + } + + #[test] + fn test_schema_unenforced_primary_key() { + let cases = vec![ + ArrowSchema::new(vec![ArrowField::new("a", DataType::Int32, false)]), + ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]), + ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ])), + false, + ), + ]), + ]; + let expected = [ + vec![], + vec!["a".to_owned()], + vec!["a".to_owned(), "f1".to_owned()], + ]; + + for (idx, case) in cases.into_iter().enumerate() { + let schema = Schema::try_from(&case).unwrap(); + assert_eq!( + schema + .unenforced_primary_key() + .iter() + .map(|f| f.name.clone()) + .collect::>(), + expected[idx] + ); + } + } + + #[test] + fn test_schema_unenforced_primary_key_failures() { + let cases = vec![ + ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, true).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]), + ArrowSchema::new(vec![ + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ArrowField::new( + "f1", + DataType::Utf8, + false, + )])), + false, + ) + .with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]), + ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ])), + true, + )]), + ArrowSchema::new(vec![ArrowField::new( + "b", + DataType::List(Arc::new( + ArrowField::new("f1", DataType::Utf8, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + )), + false, + )]), + ]; + let error_message_contains = [ + "Primary key column and all its ancestors must not be nullable", + "Primary key column must be a leaf", + "Primary key column and all its ancestors must not be nullable", + "Primary key column must not be in a list type", + ]; + + for (idx, case) in cases.into_iter().enumerate() { + let result = Schema::try_from(&case); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains(error_message_contains[idx]) + ); + } + } + + #[test] + fn test_schema_unenforced_primary_key_ordering() { + use crate::datatypes::field::LANCE_UNENFORCED_PRIMARY_KEY_POSITION; + + // When positions are specified, fields are ordered by their position values + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false).with_metadata( + vec![ + ( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + ), + ( + LANCE_UNENFORCED_PRIMARY_KEY_POSITION.to_owned(), + "2".to_owned(), + ), + ] + .into_iter() + .collect::>(), + ), + ArrowField::new("b", DataType::Int64, false).with_metadata( + vec![ + ( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + ), + ( + LANCE_UNENFORCED_PRIMARY_KEY_POSITION.to_owned(), + "1".to_owned(), + ), + ] + .into_iter() + .collect::>(), + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let pk_fields = schema.unenforced_primary_key(); + assert_eq!(pk_fields.len(), 2); + assert_eq!(pk_fields[0].name, "b"); + assert_eq!(pk_fields[1].name, "a"); + + // When positions are not specified, fields are ordered by their schema field id + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("c", DataType::Int32, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("d", DataType::Int64, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let pk_fields = schema.unenforced_primary_key(); + assert_eq!(pk_fields.len(), 2); + assert_eq!(pk_fields[0].name, "c"); + assert_eq!(pk_fields[1].name, "d"); + + // Fields with explicit positions are ordered before fields without + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("e", DataType::Int32, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("f", DataType::Int64, false).with_metadata( + vec![ + ( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + ), + ( + LANCE_UNENFORCED_PRIMARY_KEY_POSITION.to_owned(), + "1".to_owned(), + ), + ] + .into_iter() + .collect::>(), + ), + ArrowField::new("g", DataType::Utf8, false).with_metadata( + vec![( + "lance-schema:unenforced-primary-key".to_owned(), + "true".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let pk_fields = schema.unenforced_primary_key(); + assert_eq!(pk_fields.len(), 3); + assert_eq!(pk_fields[0].name, "f"); + assert_eq!(pk_fields[1].name, "e"); + assert_eq!(pk_fields[2].name, "g"); + } + + #[test] + fn test_project_with_suggestion() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("vector", ArrowDataType::Float32, false), + ArrowField::new("label", ArrowDataType::Utf8, true), + ArrowField::new("score", ArrowDataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + // Typo: "vectr" is close to "vector" → should get suggestion + let err = schema.project(&["vectr"]).unwrap_err(); + let msg = err.to_string(); + assert!( + msg.contains("Did you mean 'vector'?"), + "Expected suggestion for 'vectr', got: {}", + msg + ); + // Should also list available fields + assert!( + msg.contains("Available fields:"), + "Expected available fields list, got: {}", + msg + ); + + // Completely wrong name → no suggestion but still lists fields + let err = schema.project(&["nonexistent_column"]).unwrap_err(); + let msg = err.to_string(); + assert!( + !msg.contains("Did you mean"), + "Should not suggest for completely different name, got: {}", + msg + ); + assert!( + msg.contains("Available fields:"), + "Expected available fields list even without suggestion, got: {}", + msg + ); + } + + #[test] + fn test_field_paths() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("id", ArrowDataType::Int32, false), + ArrowField::new("vector", ArrowDataType::Float32, false), + ArrowField::new("name", ArrowDataType::Utf8, true), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let paths = schema.field_paths(); + assert!(paths.contains(&"id".to_string())); + assert!(paths.contains(&"vector".to_string())); + assert!(paths.contains(&"name".to_string())); + } + + #[test] + fn test_field_path_minimal() { + // A struct child whose own NAME contains a dot is the case that makes + // "just strip all backticks" wrong: it must stay quoted to round-trip. + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("mycol", ArrowDataType::Int32, false), + ArrowField::new("my_col", ArrowDataType::Int32, false), + ArrowField::new("my-col", ArrowDataType::Int32, false), + ArrowField::new( + "parent", + ArrowDataType::Struct(ArrowFields::from(vec![ + ArrowField::new("child-field", ArrowDataType::Int32, true), + ArrowField::new("child.x", ArrowDataType::Int32, true), + ArrowField::new("child`x", ArrowDataType::Int32, true), + ])), + true, + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let id_of = |path: &str| schema.field(path).unwrap().id; + let child_id = |name: &str| { + schema + .field("parent") + .unwrap() + .children + .iter() + .find(|c| c.name == name) + .unwrap() + .id + }; + + // Plain identifiers: unchanged by either method. + assert_eq!(schema.field_path_minimal(id_of("mycol")).unwrap(), "mycol"); + assert_eq!( + schema.field_path_minimal(id_of("my_col")).unwrap(), + "my_col" + ); + + // Hyphen is NOT special to parse_field_path, so minimal quoting leaves it + // bare (field_path would quote it for SQL safety). + assert_eq!(schema.field_path(id_of("my-col")).unwrap(), "`my-col`"); + assert_eq!( + schema.field_path_minimal(id_of("my-col")).unwrap(), + "my-col" + ); + + // Nested hyphenated leaf: bare under minimal quoting. + assert_eq!( + schema.field_path_minimal(child_id("child-field")).unwrap(), + "parent.child-field" + ); + + // Nested leaf whose NAME contains a dot: MUST stay quoted so it + // round-trips through parse_field_path (this is the regression guard). + let dotted = schema.field_path_minimal(child_id("child.x")).unwrap(); + assert_eq!(dotted, "parent.`child.x`"); + assert_eq!( + parse_field_path(&dotted).unwrap(), + vec!["parent".to_string(), "child.x".to_string()] + ); + + // Nested leaf whose NAME contains a backtick: it must be quoted AND the + // backtick doubled so it round-trips through parse_field_path. + let backticked = schema.field_path_minimal(child_id("child`x")).unwrap(); + assert_eq!(backticked, "parent.`child``x`"); + assert_eq!( + parse_field_path(&backticked).unwrap(), + vec!["parent".to_string(), "child`x".to_string()] + ); + } + + #[test] + fn test_validate_rejects_zero_dimension_fixed_size_list() { + // A zero dimension divides-by-zero further down the write path (#5102) + let fsl = |dimension: i32| { + ArrowDataType::FixedSizeList( + Arc::new(ArrowField::new("item", ArrowDataType::Float32, true)), + dimension, + ) + }; + + let arrow_schema = ArrowSchema::new(vec![ArrowField::new("vec", fsl(0), true)]); + let err = Schema::try_from(&arrow_schema).unwrap_err(); + assert!( + err.to_string() + .contains("dimension must be a positive integer"), + "unexpected error: {}", + err + ); + + // Nested inside a struct is rejected too + let arrow_schema = ArrowSchema::new(vec![ArrowField::new( + "outer", + ArrowDataType::Struct(ArrowFields::from(vec![ArrowField::new( + "vec", + fsl(0), + true, + )])), + true, + )]); + let err = Schema::try_from(&arrow_schema).unwrap_err(); + assert!( + err.to_string() + .contains("dimension must be a positive integer"), + "unexpected error: {}", + err + ); + + // A zero-dimension FixedSizeList nested inside a positive-dimension + // FixedSizeList collapses into a single leaf field, so the inner + // dimension is not visited by the pre-order field walk and must still + // be rejected: FixedSizeList(FixedSizeList(Float32, 0), 4). + let nested = + ArrowDataType::FixedSizeList(Arc::new(ArrowField::new("inner", fsl(0), true)), 4); + let arrow_schema = ArrowSchema::new(vec![ArrowField::new("vec", nested, true)]); + let err = Schema::try_from(&arrow_schema).unwrap_err(); + assert!( + err.to_string() + .contains("dimension must be a positive integer"), + "unexpected error: {}", + err + ); + + // A positive dimension still validates, including nested lists + let arrow_schema = ArrowSchema::new(vec![ArrowField::new("vec", fsl(2), true)]); + assert!(Schema::try_from(&arrow_schema).is_ok()); + let nested_ok = + ArrowDataType::FixedSizeList(Arc::new(ArrowField::new("inner", fsl(2), true)), 4); + let arrow_schema = ArrowSchema::new(vec![ArrowField::new("vec", nested_ok, true)]); + assert!(Schema::try_from(&arrow_schema).is_ok()); + } + + #[test] + fn test_schema_unenforced_clustering_key() { + use crate::datatypes::field::LANCE_UNENFORCED_CLUSTERING_KEY_POSITION; + + // No clustering key fields + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new("b", DataType::Utf8, true), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + assert!(schema.unenforced_clustering_key().is_empty()); + + // Single clustering key field + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false).with_metadata( + vec![( + LANCE_UNENFORCED_CLUSTERING_KEY_POSITION.to_owned(), + "1".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("b", DataType::Utf8, true), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let ck = schema.unenforced_clustering_key(); + assert_eq!(ck.len(), 1); + assert_eq!(ck[0].name, "a"); + + // Clustering key fields can be nullable (unlike primary keys) + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, true).with_metadata( + vec![( + LANCE_UNENFORCED_CLUSTERING_KEY_POSITION.to_owned(), + "1".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + assert_eq!(schema.unenforced_clustering_key().len(), 1); + } + + #[test] + fn test_schema_unenforced_clustering_key_ordering() { + use crate::datatypes::field::LANCE_UNENFORCED_CLUSTERING_KEY_POSITION; + + // Fields ordered by position regardless of schema column order + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("c", DataType::Utf8, true).with_metadata( + vec![( + LANCE_UNENFORCED_CLUSTERING_KEY_POSITION.to_owned(), + "3".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("a", DataType::Int32, false).with_metadata( + vec![( + LANCE_UNENFORCED_CLUSTERING_KEY_POSITION.to_owned(), + "1".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("b", DataType::Int64, false).with_metadata( + vec![( + LANCE_UNENFORCED_CLUSTERING_KEY_POSITION.to_owned(), + "2".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("d", DataType::Float64, true), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + let ck = schema.unenforced_clustering_key(); + assert_eq!(ck.len(), 3); + assert_eq!(ck[0].name, "a"); + assert_eq!(ck[1].name, "b"); + assert_eq!(ck[2].name, "c"); + } +} diff --git a/lance-artifact/rust/lance-core/src/deepsize.rs b/lance-artifact/rust/lance-core/src/deepsize.rs new file mode 100644 index 000000000..350c52b14 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/deepsize.rs @@ -0,0 +1,467 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub use lance_derive::DeepSizeOf; + +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::mem::{size_of, size_of_val}; +use std::sync::atomic::{AtomicU64, AtomicUsize}; +use std::sync::{Arc, Mutex, RwLock}; + +use arrow_array::{Array, RecordBatch}; +use arrow_buffer::ArrowNativeType; +use arrow_data::ArrayData; + +pub struct Context { + seen: HashSet, +} + +impl Default for Context { + fn default() -> Self { + Self::new() + } +} + +impl Context { + pub fn new() -> Self { + Self { + seen: HashSet::new(), + } + } + + /// Returns true if this pointer was NOT previously seen (i.e., it's new). + pub fn mark_seen(&mut self, ptr: usize) -> bool { + self.seen.insert(ptr) + } +} + +pub trait DeepSizeOf { + fn deep_size_of(&self) -> usize { + size_of_val(self) + self.deep_size_of_children(&mut Context::new()) + } + + fn deep_size_of_children(&self, context: &mut Context) -> usize; +} + +// Primitives — no heap children +macro_rules! impl_deep_size_primitive { + ($($t:ty),*) => { + $( + impl DeepSizeOf for $t { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + 0 + } + } + )* + }; +} + +impl_deep_size_primitive!( + u8, + u16, + u32, + u64, + u128, + usize, + i8, + i16, + i32, + i64, + i128, + isize, + f32, + f64, + bool, + () +); + +impl DeepSizeOf for str { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + 0 + } +} + +impl DeepSizeOf for String { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + self.capacity() + } +} + +impl DeepSizeOf for bytes::Bytes { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + if context.mark_seen(self.as_ptr() as usize) { + self.len() + } else { + 0 + } + } +} + +impl DeepSizeOf for AtomicU64 { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + 0 + } +} + +impl DeepSizeOf for AtomicUsize { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + 0 + } +} + +impl DeepSizeOf for [T; N] { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.iter() + .map(|item| item.deep_size_of_children(context)) + .sum() + } +} + +impl DeepSizeOf for [T] { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + // The slice's own element bytes are accounted for by the owner (e.g. the + // `size_of_val` in the `Arc`/`Box` impls); here we only sum the heap + // children of each element. + self.iter() + .map(|item| item.deep_size_of_children(context)) + .sum() + } +} + +impl DeepSizeOf for RwLock { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.read() + .map(|val| val.deep_size_of_children(context)) + .unwrap_or(0) + } +} + +impl DeepSizeOf for Mutex { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.lock() + .map(|val| val.deep_size_of_children(context)) + .unwrap_or(0) + } +} + +// Tuples +macro_rules! impl_deep_size_tuple { + ($($name:ident),+) => { + impl<$($name: DeepSizeOf),+> DeepSizeOf for ($($name,)+) { + #[allow(non_snake_case)] + fn deep_size_of_children(&self, context: &mut Context) -> usize { + let ($($name,)+) = self; + 0 $(+ $name.deep_size_of_children(context))+ + } + } + }; +} + +impl_deep_size_tuple!(A, B); +impl_deep_size_tuple!(A, B, C); +impl_deep_size_tuple!(A, B, C, D); +impl_deep_size_tuple!(A, B, C, D, E); +impl_deep_size_tuple!(A, B, C, D, E, F); + +impl DeepSizeOf for Vec { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.capacity() * size_of::() + + self + .iter() + .map(|item| item.deep_size_of_children(context)) + .sum::() + } +} + +impl DeepSizeOf for Box { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + size_of_val(&**self) + (**self).deep_size_of_children(context) + } +} + +impl DeepSizeOf for Arc { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + if context.mark_seen(Self::as_ptr(self) as *const () as usize) { + size_of_val(&**self) + (**self).deep_size_of_children(context) + } else { + 0 + } + } +} + +impl DeepSizeOf for Option { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + match self { + Some(val) => val.deep_size_of_children(context), + None => 0, + } + } +} + +impl DeepSizeOf for HashMap { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + // Each bucket holds a key-value pair plus hash metadata (~1 byte control per bucket). + // Robin hood / Swiss table capacity is always a power of 2. + let capacity_bytes = self.capacity() * (size_of::() + size_of::() + 1); + let children: usize = self + .iter() + .map(|(k, v)| k.deep_size_of_children(context) + v.deep_size_of_children(context)) + .sum(); + capacity_bytes + children + } +} + +impl DeepSizeOf for HashSet { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + let capacity_bytes = self.capacity() * (size_of::() + 1); + let children: usize = self.iter().map(|k| k.deep_size_of_children(context)).sum(); + capacity_bytes + children + } +} + +impl DeepSizeOf for BTreeMap { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + // BTreeMap nodes have ~11 entries each. Rough estimate: per-entry overhead ~3 pointers. + let per_entry = size_of::() + size_of::() + 3 * size_of::(); + let overhead = self.len() * per_entry; + let children: usize = self + .iter() + .map(|(k, v)| k.deep_size_of_children(context) + v.deep_size_of_children(context)) + .sum(); + overhead + children + } +} + +impl DeepSizeOf for BTreeSet { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + let per_entry = size_of::() + 3 * size_of::(); + let overhead = self.len() * per_entry; + let children: usize = self.iter().map(|k| k.deep_size_of_children(context)).sum(); + overhead + children + } +} + +// Arrow types + +fn record_array_data(context: &mut Context, data: &ArrayData) -> usize { + let mut total = 0; + for buffer in data.buffers() { + if context.mark_seen(buffer.as_ptr() as usize) { + total += buffer.capacity(); + } + } + if let Some(nulls) = data.nulls() { + let null_buf = nulls.inner().inner(); + if context.mark_seen(null_buf.as_ptr() as usize) { + total += null_buf.capacity(); + } + } + for child in data.child_data() { + total += record_array_data(context, child); + } + total +} + +impl DeepSizeOf for dyn Array { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + // `to_data()` only clones Arc refs (no data copy) and allocates a small + // ArrayData metadata struct. This lets us walk buffer pointers for dedup. + // Cost is O(number_of_buffers), not O(data_size). + let data = self.to_data(); + record_array_data(context, &data) + } +} + +impl DeepSizeOf for RecordBatch { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.columns() + .iter() + .map(|col| col.deep_size_of_children(context)) + .sum() + } +} + +impl DeepSizeOf for arrow_buffer::ScalarBuffer +where + T: ArrowNativeType, +{ + fn deep_size_of_children(&self, context: &mut Context) -> usize { + // Track the underlying buffer pointer to avoid double-counting shared allocations. + // Use capacity() rather than len() * size_of::() because sliced buffers retain + // their full original allocation. + let buf = self.inner(); + if context.mark_seen(buf.as_ptr() as usize) { + buf.capacity() + } else { + 0 + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow_array::{Int32Array, StringArray, StructArray}; + use arrow_schema::{DataType, Field, Fields, Schema}; + + #[test] + fn test_basic_record_batch() { + let batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], + ) + .unwrap(); + + let size = batch.deep_size_of(); + // Should at least include the buffer for 3 i32s + assert!(size >= 3 * size_of::()); + } + + #[test] + fn test_same_batch_dedup() { + let batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]))], + ) + .unwrap(); + + let mut ctx = Context::new(); + let size_a = batch.deep_size_of_children(&mut ctx); + let size_b = batch.deep_size_of_children(&mut ctx); + + // First measurement should report buffer sizes + assert!(size_a > 0); + // Second measurement of the same batch should add nothing (buffers already seen) + assert_eq!(size_b, 0); + } + + #[test] + fn test_arc_dedup() { + let batch = Arc::new( + RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], + ) + .unwrap(), + ); + let clone = Arc::clone(&batch); + + let mut ctx = Context::new(); + let size_a = batch.deep_size_of_children(&mut ctx); + let size_b = clone.deep_size_of_children(&mut ctx); + + assert!(size_a > 0); + assert_eq!(size_b, 0); + } + + #[test] + fn test_multi_column_shared_array() { + // Two columns pointing to the same Arc + let array: Arc = Arc::new(Int32Array::from(vec![10, 20, 30])); + let schema = Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int32, false), + Field::new("b", DataType::Int32, false), + ])); + + // Single-column batch for reference + let one_col = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![array.clone()], + ) + .unwrap(); + + // Two-column batch with the same Arc shared + let two_col = RecordBatch::try_new(schema, vec![array.clone(), array]).unwrap(); + + let mut ctx1 = Context::new(); + let size_one = one_col.deep_size_of_children(&mut ctx1); + + let mut ctx2 = Context::new(); + let size_two = two_col.deep_size_of_children(&mut ctx2); + + // Both should report the same size since the second column's Arc is + // already seen and contributes nothing + assert_eq!(size_one, size_two); + } + + #[test] + fn test_nested_struct_array() { + let int_array = Int32Array::from(vec![1, 2, 3]); + let str_array = StringArray::from(vec!["a", "b", "c"]); + let struct_array = StructArray::from(vec![ + ( + Arc::new(Field::new("x", DataType::Int32, false)), + Arc::new(int_array) as Arc, + ), + ( + Arc::new(Field::new("y", DataType::Utf8, false)), + Arc::new(str_array) as Arc, + ), + ]); + + let batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new( + "s", + DataType::Struct(Fields::from(vec![ + Field::new("x", DataType::Int32, false), + Field::new("y", DataType::Utf8, false), + ])), + false, + )])), + vec![Arc::new(struct_array)], + ) + .unwrap(); + + let size = batch.deep_size_of(); + // Should include buffers for both child arrays + assert!(size > 3 * size_of::()); + } + + #[test] + fn test_std_types() { + assert_eq!(42u32.deep_size_of(), size_of::()); + + let s = String::from("hello"); + assert!(s.deep_size_of() >= size_of::() + 5); + + let v = vec![1u32, 2, 3]; + assert!(v.deep_size_of() >= size_of::>() + 3 * size_of::()); + + let a = Arc::new(42u32); + let b = Arc::clone(&a); + let mut ctx = Context::new(); + let size_a = a.deep_size_of_children(&mut ctx); + let size_b = b.deep_size_of_children(&mut ctx); + assert_eq!(size_a, size_of::()); + assert_eq!(size_b, 0); + } + + #[test] + fn test_derive_macro() { + use lance_derive::DeepSizeOf; + + #[derive(DeepSizeOf)] + struct Outer { + count: u64, + label: String, + inner: Inner, + } + + #[derive(DeepSizeOf)] + struct Inner { + values: Vec, + } + + let val = Outer { + count: 7, + label: String::from("hello"), + inner: Inner { + values: vec![1, 2, 3], + }, + }; + + let size = val.deep_size_of(); + // Must be at least the stack size + heap allocations for label + values + assert!(size >= std::mem::size_of::() + 5 + 3 * std::mem::size_of::()); + } +} diff --git a/lance-artifact/rust/lance-core/src/error.rs b/lance-artifact/rust/lance-core/src/error.rs new file mode 100644 index 000000000..648a4881f --- /dev/null +++ b/lance-artifact/rust/lance-core/src/error.rs @@ -0,0 +1,1506 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::fmt; + +use arrow_schema::ArrowError; +use snafu::{IntoError as _, Location, Snafu}; + +type BoxedError = Box; + +#[cfg(feature = "backtrace")] +mod backtrace_support { + use std::backtrace::Backtrace; + + use snafu::{AsBacktrace, GenerateImplicitData}; + + #[derive(Debug)] + pub struct MaybeBacktrace(pub Option); + + impl GenerateImplicitData for MaybeBacktrace { + fn generate() -> Self { + Self(>::generate()) + } + } + + impl AsBacktrace for MaybeBacktrace { + fn as_backtrace(&self) -> Option<&Backtrace> { + self.0.as_ref() + } + } +} + +#[cfg(not(feature = "backtrace"))] +mod backtrace_support { + use std::backtrace::Backtrace; + + use snafu::{AsBacktrace, GenerateImplicitData}; + + #[derive(Debug)] + pub struct MaybeBacktrace; + + impl GenerateImplicitData for MaybeBacktrace { + fn generate() -> Self { + Self + } + } + + impl AsBacktrace for MaybeBacktrace { + fn as_backtrace(&self) -> Option<&Backtrace> { + None + } + } +} + +use backtrace_support::MaybeBacktrace; + +/// Error for when a requested field is not found in a schema. +/// +/// This error computes suggestions lazily (only when displayed) to avoid +/// computing Levenshtein distance when the error is created but never shown. +#[derive(Debug)] +pub struct FieldNotFoundError { + pub field_name: String, + pub candidates: Vec, +} + +impl fmt::Display for FieldNotFoundError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Field '{}' not found.", self.field_name)?; + let suggestion = + crate::levenshtein::find_best_suggestion(&self.field_name, &self.candidates); + if let Some(suggestion) = suggestion { + write!(f, " Did you mean '{}'?", suggestion)?; + } + write!(f, "\nAvailable fields: [")?; + for (i, candidate) in self.candidates.iter().take(10).enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "'{}'", candidate)?; + } + if self.candidates.len() > 10 { + let remaining = self.candidates.len() - 10; + write!(f, ", ... and {} more]", remaining)?; + } else { + write!(f, "]")?; + } + Ok(()) + } +} + +impl std::error::Error for FieldNotFoundError {} + +/// A manifest commit returned an error and its final outcome could not be +/// determined safely. +/// +/// This is wrapped in [`Error::Wrapped`] so Lance can expose a structured +/// source without adding a variant to the exhaustive public [`Error`] enum. +#[derive(Debug)] +pub struct CommitStatusUnknownError { + version: u64, + source: BoxedError, +} + +impl CommitStatusUnknownError { + /// Return the manifest version whose commit outcome is unknown. + pub fn version(&self) -> u64 { + self.version + } +} + +impl std::fmt::Display for CommitStatusUnknownError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Commit result for version {} is unknown: the commit may or may not have been \ + applied; check the table state before retrying: {}", + self.version, self.source + ) + } +} + +impl std::error::Error for CommitStatusUnknownError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(self.source.as_ref()) + } +} + +/// Allocates error on the heap and then places `e` into it. +#[inline] +pub fn box_error(e: impl std::error::Error + Send + Sync + 'static) -> BoxedError { + Box::new(e) +} + +/// Why a writer is fenced. Both reasons are terminal, but callers must tell them +/// apart (a peer takeover vs. our own failure) rather than parse the message. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum FenceReason { + /// A successor writer claimed a higher epoch; this writer lost ownership. + PeerClaimedEpoch, + /// Our own WAL persistence failed, so in-memory state may have diverged from + /// the durable WAL. The writer must be reopened to replay. + PersistenceFailure, +} + +impl std::fmt::Display for FenceReason { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // Stable strings — surfaced in error messages. + let s = match self { + Self::PeerClaimedEpoch => "peer claimed epoch", + Self::PersistenceFailure => "persistence failure", + }; + f.write_str(s) + } +} + +#[derive(Debug, Snafu)] +#[snafu(visibility(pub))] +pub enum Error { + #[snafu(display("Invalid user input: {source}, {location}"))] + InvalidInput { + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Dataset already exists: {uri}, {location}"))] + DatasetAlreadyExists { + uri: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Append with different schema: {difference}, location: {location}"))] + SchemaMismatch { + difference: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Dataset at path {path} was not found: {source}, {location}"))] + DatasetNotFound { + path: String, + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Encountered corrupt file {path}: {source}, {location}"))] + CorruptFile { + path: object_store::path::Path, + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Not supported: {source}, {location}"))] + NotSupported { + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Commit conflict for version {version}: {source}, {location}"))] + CommitConflict { + version: u64, + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Incompatible transaction: {source}, {location}"))] + IncompatibleTransaction { + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Retryable commit conflict for version {version}: {source}, {location}"))] + RetryableCommitConflict { + version: u64, + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Too many concurrent writers. {message}, {location}"))] + TooMuchWriteContention { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Operation timed out: {message}, {location}"))] + Timeout { + message: String, + #[snafu(implicit)] + location: Location, + }, + #[snafu(display( + "Encountered internal error. Please file a bug report at https://github.com/lance-format/lance/issues. {message}, {location}" + ))] + Internal { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("A prerequisite task failed: {message}, {location}"))] + PrerequisiteFailed { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Unprocessable: {message}, {location}"))] + Unprocessable { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("LanceError(Arrow): {message}, {location}"))] + Arrow { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("LanceError(Schema): {message}, {location}"))] + Schema { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Not found: {uri}, {location}"))] + NotFound { + uri: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("LanceError(IO): {source}, {location}"))] + IO { + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("LanceError(Index): {message}, {location}"))] + Index { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Lance index not found: {identity}, {location}"))] + IndexNotFound { + identity: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Cannot infer storage location from: {message}"))] + InvalidTableLocation { message: String }, + /// Stream early stop + Stop, + #[snafu(display("Wrapped error: {error}, {location}"))] + Wrapped { + #[snafu(source)] + error: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Cloned error: {message}, {location}"))] + Cloned { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Query Execution error: {message}, {location}"))] + Execution { + message: String, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Ref is invalid: {message}"))] + InvalidRef { message: String }, + #[snafu(display("Ref conflict error: {message}"))] + RefConflict { message: String }, + #[snafu(display("Ref not found error: {message}"))] + RefNotFound { message: String }, + #[snafu(display("Cleanup error: {message}"))] + Cleanup { message: String }, + #[snafu(display("Version not found error: {message}"))] + VersionNotFound { message: String }, + #[snafu(display("Version conflict error: {message}"))] + VersionConflict { + message: String, + major_version: u16, + minor_version: u16, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + #[snafu(display("Namespace error: {source}, {location}"))] + Namespace { + source: BoxedError, + #[snafu(implicit)] + location: Location, + #[snafu(implicit)] + backtrace: MaybeBacktrace, + }, + /// External error passed through from user code. + /// + /// This variant preserves errors that users pass into Lance APIs (e.g., via streams + /// with custom error types). The original error can be recovered using [`Error::into_external`] + /// or inspected using [`Error::external_source`]. + #[snafu(transparent)] + External { source: BoxedError }, + + /// A requested field was not found in a schema. + #[snafu(transparent)] + FieldNotFound { source: FieldNotFoundError }, + + #[snafu(display( + "Spill disk cap of {cap_bytes} bytes exceeded; currently using {used_bytes} bytes, {location}" + ))] + DiskCapExceeded { + cap_bytes: u64, + used_bytes: u64, + #[snafu(implicit)] + location: Location, + }, + /// A writer has been fenced and must stop (see [`FenceReason`]). The message + /// keeps the `Writer fenced` prefix for legacy string consumers; new code + /// should match on [`Error::fence_reason`]. + #[snafu(display("Writer fenced ({reason}): {message}, {location}"))] + Fenced { + reason: FenceReason, + message: String, + #[snafu(implicit)] + location: Location, + }, +} + +impl Error { + /// Returns the captured Rust backtrace, if available. + /// + /// Requires the `backtrace` feature to be enabled at compile time + /// and `RUST_BACKTRACE=1` at runtime. + #[cfg(feature = "backtrace")] + pub fn backtrace(&self) -> Option<&std::backtrace::Backtrace> { + match self { + Self::InvalidInput { backtrace, .. } + | Self::DatasetAlreadyExists { backtrace, .. } + | Self::SchemaMismatch { backtrace, .. } + | Self::DatasetNotFound { backtrace, .. } + | Self::CorruptFile { backtrace, .. } + | Self::NotSupported { backtrace, .. } + | Self::CommitConflict { backtrace, .. } + | Self::IncompatibleTransaction { backtrace, .. } + | Self::RetryableCommitConflict { backtrace, .. } + | Self::TooMuchWriteContention { backtrace, .. } + | Self::Internal { backtrace, .. } + | Self::PrerequisiteFailed { backtrace, .. } + | Self::Unprocessable { backtrace, .. } + | Self::Arrow { backtrace, .. } + | Self::Schema { backtrace, .. } + | Self::NotFound { backtrace, .. } + | Self::IO { backtrace, .. } + | Self::Index { backtrace, .. } + | Self::IndexNotFound { backtrace, .. } + | Self::Wrapped { backtrace, .. } + | Self::Cloned { backtrace, .. } + | Self::Execution { backtrace, .. } + | Self::VersionConflict { backtrace, .. } + | Self::Namespace { backtrace, .. } => { + use snafu::AsBacktrace; + backtrace.as_backtrace() + } + // Variants without a backtrace field — listed explicitly so that + // adding a new variant with a backtrace field triggers a compiler error. + Self::InvalidTableLocation { .. } + | Self::Stop + | Self::InvalidRef { .. } + | Self::RefConflict { .. } + | Self::RefNotFound { .. } + | Self::Cleanup { .. } + | Self::VersionNotFound { .. } + | Self::External { .. } + | Self::FieldNotFound { .. } + | Self::Timeout { .. } + | Self::DiskCapExceeded { .. } + | Self::Fenced { .. } => None, + } + } + + /// Returns the captured Rust backtrace, if available. + /// + /// Always returns `None` when the `backtrace` feature is not enabled. + #[cfg(not(feature = "backtrace"))] + pub fn backtrace(&self) -> Option<&std::backtrace::Backtrace> { + None + } + + #[track_caller] + pub fn corrupt_file(path: object_store::path::Path, message: impl Into) -> Self { + CorruptFileSnafu { path }.into_error(message.into().into()) + } + + /// Reports a corrupt file when the caller only has a logical/section name + /// rather than the real file path (for example, a decoder that validates an + /// in-memory buffer and does not know where it came from). + /// + /// `name` is carried in the `path` field of the resulting [`Error::CorruptFile`] + /// variant and is NOT a filesystem path; callers that have the real path should + /// use [`Self::corrupt_file`] instead. + #[track_caller] + pub fn corrupt_file_named(name: &str, message: impl Into) -> Self { + Self::corrupt_file(object_store::path::Path::from(name), message) + } + + #[track_caller] + pub fn invalid_input(message: impl Into) -> Self { + InvalidInputSnafu.into_error(message.into().into()) + } + + #[track_caller] + pub fn invalid_input_source(source: BoxedError) -> Self { + InvalidInputSnafu.into_error(source) + } + + #[track_caller] + pub fn io(message: impl Into) -> Self { + IOSnafu.into_error(message.into().into()) + } + + /// A successor writer claimed a higher epoch; this writer lost ownership. + #[track_caller] + pub fn fenced_by_peer(message: impl Into) -> Self { + FencedSnafu { + reason: FenceReason::PeerClaimedEpoch, + message: message.into(), + } + .build() + } + + /// Our WAL persistence failed; in-memory state may have diverged from the + /// durable WAL, so the writer must be reopened to replay. + #[track_caller] + pub fn writer_poisoned(message: impl Into) -> Self { + FencedSnafu { + reason: FenceReason::PersistenceFailure, + message: message.into(), + } + .build() + } + + /// The [`FenceReason`] if this is [`Error::Fenced`], else `None`. Prefer this + /// over matching the error message to decide how to react to a fence. + pub fn fence_reason(&self) -> Option { + match self { + Self::Fenced { reason, .. } => Some(*reason), + _ => None, + } + } + + #[track_caller] + pub fn io_source(source: BoxedError) -> Self { + IOSnafu.into_error(source) + } + + #[track_caller] + pub fn dataset_already_exists(uri: impl Into) -> Self { + DatasetAlreadyExistsSnafu { uri: uri.into() }.build() + } + + #[track_caller] + pub fn dataset_not_found(path: impl Into, source: BoxedError) -> Self { + DatasetNotFoundSnafu { path: path.into() }.into_error(source) + } + + #[track_caller] + pub fn version_conflict( + message: impl Into, + major_version: u16, + minor_version: u16, + ) -> Self { + VersionConflictSnafu { + message: message.into(), + major_version, + minor_version, + } + .build() + } + + #[track_caller] + pub fn not_found(uri: impl Into) -> Self { + NotFoundSnafu { uri: uri.into() }.build() + } + + /// Return whether this error or one of its typed sources is a missing object. + pub fn is_not_found(&self) -> bool { + match self { + Self::NotFound { .. } => true, + Self::Wrapped { error, .. } + if error.downcast_ref::().is_some() => + { + false + } + Self::IO { source, .. } | Self::Wrapped { error: source, .. } => { + error_source_is_not_found(source.as_ref()) + } + _ => false, + } + } + + #[track_caller] + pub fn wrapped(error: BoxedError) -> Self { + WrappedSnafu.into_error(error) + } + + #[track_caller] + pub fn schema(message: impl Into) -> Self { + SchemaSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn not_supported(message: impl Into) -> Self { + NotSupportedSnafu.into_error(message.into().into()) + } + + #[track_caller] + pub fn not_supported_source(source: BoxedError) -> Self { + NotSupportedSnafu.into_error(source) + } + + #[track_caller] + pub fn internal(message: impl Into) -> Self { + InternalSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn timeout(message: impl Into) -> Self { + TimeoutSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn namespace(message: impl Into) -> Self { + NamespaceSnafu.into_error(message.into().into()) + } + + #[track_caller] + pub fn namespace_source(source: Box) -> Self { + NamespaceSnafu.into_error(source) + } + + #[track_caller] + pub fn arrow(message: impl Into) -> Self { + ArrowSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn execution(message: impl Into) -> Self { + ExecutionSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn cloned(message: impl Into) -> Self { + ClonedSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn schema_mismatch(difference: impl Into) -> Self { + SchemaMismatchSnafu { + difference: difference.into(), + } + .build() + } + + #[track_caller] + pub fn unprocessable(message: impl Into) -> Self { + UnprocessableSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn too_much_write_contention(message: impl Into) -> Self { + TooMuchWriteContentionSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn prerequisite_failed(message: impl Into) -> Self { + PrerequisiteFailedSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn index(message: impl Into) -> Self { + IndexSnafu { + message: message.into(), + } + .build() + } + + #[track_caller] + pub fn index_not_found(identity: impl Into) -> Self { + IndexNotFoundSnafu { + identity: identity.into(), + } + .build() + } + + #[track_caller] + pub fn commit_conflict_source(version: u64, source: BoxedError) -> Self { + CommitConflictSnafu { version }.into_error(source) + } + + #[track_caller] + pub fn retryable_commit_conflict_source(version: u64, source: BoxedError) -> Self { + RetryableCommitConflictSnafu { version }.into_error(source) + } + + #[track_caller] + pub fn commit_status_unknown_source(version: u64, source: BoxedError) -> Self { + Self::wrapped(box_error(CommitStatusUnknownError { version, source })) + } + + /// Return whether this error represents a commit whose final outcome could + /// not be determined safely. + pub fn is_commit_status_unknown(&self) -> bool { + matches!( + self, + Self::Wrapped { error, .. } + if error.downcast_ref::().is_some() + ) + } + + #[track_caller] + pub fn incompatible_transaction_source(source: BoxedError) -> Self { + IncompatibleTransactionSnafu.into_error(source) + } + + #[track_caller] + pub fn disk_cap_exceeded(cap_bytes: u64, used_bytes: u64) -> Self { + DiskCapExceededSnafu { + cap_bytes, + used_bytes, + } + .build() + } + + /// Create an External error from a boxed error source. + pub fn external(source: BoxedError) -> Self { + Self::External { source } + } + + /// Create a FieldNotFound error with the given field name and available candidates. + pub fn field_not_found(field_name: impl Into, candidates: Vec) -> Self { + Self::FieldNotFound { + source: FieldNotFoundError { + field_name: field_name.into(), + candidates, + }, + } + } + + /// Returns a reference to the external error source if this is an `External` variant. + /// + /// This allows downcasting to recover the original error type. + pub fn external_source(&self) -> Option<&BoxedError> { + match self { + Self::External { source } => Some(source), + _ => None, + } + } + + /// Consumes the error and returns the external source if this is an `External` variant. + /// + /// Returns `Err(self)` if this is not an `External` variant, allowing for chained handling. + pub fn into_external(self) -> std::result::Result { + match self { + Self::External { source } => Ok(source), + other => Err(other), + } + } +} + +fn error_source_is_not_found(source: &(dyn std::error::Error + 'static)) -> bool { + if let Some(error) = source.downcast_ref::() { + return error.is_not_found(); + } + if let Some(error) = source.downcast_ref::() { + return matches!(error, object_store::Error::NotFound { .. }) + || std::error::Error::source(error).is_some_and(error_source_is_not_found); + } + source.source().is_some_and(error_source_is_not_found) +} + +pub trait LanceOptionExt { + /// Unwraps an option, returning an internal error if the option is None. + /// + /// Can be used when an option is expected to have a value. + fn expect_ok(self) -> Result; +} + +impl LanceOptionExt for Option { + #[track_caller] + fn expect_ok(self) -> Result { + self.ok_or_else(|| Error::internal("Expected option to have value")) + } +} + +pub type Result = std::result::Result; +pub type ArrowResult = std::result::Result; +#[cfg(feature = "datafusion")] +pub type DataFusionResult = std::result::Result; + +impl From for Error { + #[track_caller] + fn from(e: ArrowError) -> Self { + match e { + ArrowError::ExternalError(source) => { + // Try to downcast to lance_core::Error first to recover the original + match source.downcast::() { + Ok(lance_err) => *lance_err, + Err(source) => Self::External { source }, + } + } + other => Self::arrow(other.to_string()), + } + } +} + +impl From<&ArrowError> for Error { + #[track_caller] + fn from(e: &ArrowError) -> Self { + Self::arrow(e.to_string()) + } +} + +impl From for Error { + #[track_caller] + fn from(e: std::io::Error) -> Self { + // A lance `Error` may have been wrapped in an `io::Error` (e.g. via + // `io::Error::other(Error::...)`) to cross an `AsyncWrite`/`AsyncRead` + // boundary. Recover it so typed errors such as `DiskCapExceeded` + // survive the round-trip instead of collapsing into an opaque `IO`. + if e.get_ref().is_some_and(|inner| inner.is::()) { + return *e + .into_inner() + .expect("checked Some above") + .downcast::() + .expect("checked type above"); + } + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: object_store::Error) -> Self { + match e { + // source intentionally dropped; Error::NotFound carries only the path + object_store::Error::NotFound { path, .. } => Self::not_found(path), + other => Self::io_source(box_error(other)), + } + } +} + +impl From for Error { + #[track_caller] + fn from(e: prost::DecodeError) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: prost::EncodeError) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: prost::UnknownEnumValue) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: tokio::task::JoinError) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: object_store::path::Error) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: url::ParseError) -> Self { + Self::io_source(box_error(e)) + } +} + +impl From for Error { + #[track_caller] + fn from(e: serde_json::Error) -> Self { + Self::arrow(e.to_string()) + } +} + +impl From for ArrowError { + fn from(value: Error) -> Self { + match value { + // Pass through external errors directly + Error::External { source } => Self::ExternalError(source), + // Preserve schema errors with their specific type + Error::Schema { message, .. } => Self::SchemaError(message), + // Wrap all other lance errors so they can be recovered + e => Self::ExternalError(Box::new(e)), + } + } +} + +#[cfg(feature = "datafusion")] +impl From for Error { + #[track_caller] + fn from(e: datafusion_sql::sqlparser::parser::ParserError) -> Self { + Self::io_source(box_error(e)) + } +} + +#[cfg(feature = "datafusion")] +impl From for Error { + #[track_caller] + fn from(e: datafusion_sql::sqlparser::tokenizer::TokenizerError) -> Self { + Self::io_source(box_error(e)) + } +} + +#[cfg(feature = "datafusion")] +impl From for datafusion_common::DataFusionError { + #[track_caller] + fn from(e: Error) -> Self { + Self::External(Box::new(e)) + } +} + +#[cfg(feature = "datafusion")] +impl From for Error { + #[track_caller] + fn from(e: datafusion_common::DataFusionError) -> Self { + match e { + datafusion_common::DataFusionError::SQL(..) + | datafusion_common::DataFusionError::Plan(..) + | datafusion_common::DataFusionError::Configuration(..) + | datafusion_common::DataFusionError::SchemaError(..) => { + Self::invalid_input_source(box_error(e)) + } + datafusion_common::DataFusionError::ArrowError(arrow_err, _) => Self::from(*arrow_err), + datafusion_common::DataFusionError::NotImplemented(..) => { + Self::not_supported_source(box_error(e)) + } + datafusion_common::DataFusionError::Execution(..) => Self::execution(e.to_string()), + datafusion_common::DataFusionError::Shared(shared) => { + // DataFusion shares an error across consumers (e.g. a join's + // build-side error fanned out to every probe partition) behind an + // `Arc`. If we are the sole owner we can recurse for full fidelity; + // otherwise the inner error can't be moved out, so we preserve its + // message under the execution category (its concrete type is lost). + match std::sync::Arc::try_unwrap(shared) { + Ok(inner) => Self::from(inner), + Err(shared) => Self::execution(shared.to_string()), + } + } + datafusion_common::DataFusionError::External(source) => { + // Try to downcast to lance_core::Error first + match source.downcast::() { + Ok(lance_err) => *lance_err, + Err(source) => Self::External { source }, + } + } + _ => Self::io_source(box_error(e)), + } + } +} + +// This is a bit odd but some object_store functions only accept +// Stream> and so we need to convert +// to ObjectStoreError to call the methods. +impl From for object_store::Error { + fn from(err: Error) -> Self { + Self::Generic { + store: "N/A", + source: Box::new(err), + } + } +} + +#[track_caller] +pub fn get_caller_location() -> &'static std::panic::Location<'static> { + std::panic::Location::caller() +} + +/// Wrap an error in a new error type that implements Clone +/// +/// This is useful when two threads/streams share a common fallible source +/// Definite not-found errors preserve typed source-chain detection and their +/// human-readable representation. Timeout and I/O errors preserve their error +/// categories. Other cloned results use Error::Cloned with the string +/// representation of the base error. +pub struct CloneableError(pub Error); + +struct DisplayError(Error); + +impl fmt::Debug for DisplayError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for DisplayError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +impl std::error::Error for DisplayError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.0) + } +} + +impl Clone for CloneableError { + #[track_caller] + fn clone(&self) -> Self { + match &self.0 { + Error::NotFound { uri, .. } => Self(Error::wrapped(Box::new(DisplayError( + Error::not_found(uri.clone()), + )))), + error if error.is_not_found() => Self(Error::wrapped(Box::new(DisplayError( + Error::not_found(error.to_string()), + )))), + Error::Timeout { message, .. } => Self(Error::timeout(message.clone())), + Error::IO { source, .. } => Self(Error::io(source.to_string())), + error => Self(Error::cloned(error.to_string())), + } + } +} + +#[derive(Clone)] +pub struct CloneableResult(pub std::result::Result); + +impl From> for CloneableResult { + fn from(result: Result) -> Self { + Self(result.map_err(CloneableError)) + } +} + +#[cfg(test)] +mod test { + use super::*; + use std::error::Error as _; + use std::fmt; + + #[test] + fn cloneable_error_preserves_not_found_contract() { + let original = CloneableError(Error::not_found("metadata.lance")); + let cloned = original.clone(); + let cloned_again = cloned.clone(); + assert!(matches!(original.0, Error::NotFound { .. })); + assert!(cloned.0.is_not_found()); + assert!(cloned_again.0.is_not_found()); + assert!(cloned.0.to_string().to_lowercase().contains("not found")); + assert!( + cloned_again + .0 + .to_string() + .to_lowercase() + .contains("not found") + ); + assert!( + format!("{:?}", cloned.0) + .to_lowercase() + .contains("not found") + ); + assert!(cloned.0.source().is_some_and(|source| source.is::() + || source.source().is_some_and(|source| source.is::()))); + let downstream_error = Error::wrapped(Box::new(Error::io_source(Box::new( + object_store::Error::Generic { + store: "N/A", + source: Box::new(cloned.0), + }, + )))); + assert!(downstream_error.is_not_found()); + assert!( + format!("{downstream_error:?}") + .to_lowercase() + .contains("not found") + ); + + let original = CloneableError(Error::timeout("metadata read timed out")); + let cloned = original.clone(); + assert!(matches!(original.0, Error::Timeout { .. })); + assert!(matches!(cloned.0, Error::Timeout { .. })); + + let original = CloneableError(Error::io("metadata read was denied")); + let cloned = original.clone(); + assert!(matches!(original.0, Error::IO { .. })); + assert!(matches!(cloned.0, Error::IO { .. })); + } + + #[test] + fn test_caller_location_capture() { + let current_fn = get_caller_location(); + // make sure ? captures the correct location + // .into() WILL NOT capture the correct location + let f: Box Result<()>> = Box::new(|| { + Err(object_store::Error::Generic { + store: "", + source: "".into(), + })?; + Ok(()) + }); + match f().unwrap_err() { + Error::IO { location, .. } => { + // +4 is the beginning of object_store::Error::Generic... + assert_eq!(location.line(), current_fn.line() + 4, "{}", location) + } + #[allow(unreachable_patterns)] + _ => panic!("expected ObjectStore error"), + } + } + + #[test] + fn test_caller_location_capture_not_found() { + let current_fn = get_caller_location(); + let f: Box Result<()>> = Box::new(|| { + Err(object_store::Error::NotFound { + path: "some/path".to_string(), + source: "not found".into(), + })?; + Ok(()) + }); + match f().unwrap_err() { + Error::NotFound { location, .. } => { + // +2 is the beginning of object_store::Error::NotFound... + assert_eq!(location.line(), current_fn.line() + 2, "{}", location) + } + #[allow(unreachable_patterns)] + other => panic!("expected NotFound, got {:?}", other), + } + } + + #[test] + fn test_object_store_not_found_converts_to_not_found() { + let os_err = object_store::Error::NotFound { + path: "test/path".to_string(), + source: "no such file".into(), + }; + let lance_err: Error = os_err.into(); + match lance_err { + Error::NotFound { uri, .. } => { + assert_eq!(uri, "test/path"); + } + other => panic!("Expected NotFound, got {:?}", other), + } + } + + #[derive(Debug)] + struct MyCustomError { + code: i32, + message: String, + } + + impl fmt::Display for MyCustomError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "MyCustomError({}): {}", self.code, self.message) + } + } + + impl std::error::Error for MyCustomError {} + + #[test] + fn test_io_error_recovers_wrapped_lance_error() { + // A lance Error wrapped in io::Error::other should round-trip back to + // the original variant rather than collapsing into Error::IO. + let io_err = std::io::Error::other(Error::disk_cap_exceeded(100, 50)); + let recovered: Error = io_err.into(); + match recovered { + Error::DiskCapExceeded { + cap_bytes, + used_bytes, + .. + } => { + assert_eq!(cap_bytes, 100); + assert_eq!(used_bytes, 50); + } + other => panic!("expected DiskCapExceeded, got {other:?}"), + } + } + + #[test] + fn test_io_error_without_lance_error_stays_io() { + // A plain io::Error (no wrapped lance Error) should become Error::IO. + let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing"); + let converted: Error = io_err.into(); + assert!(matches!(converted, Error::IO { .. })); + } + + #[test] + fn test_commit_status_unknown_is_structured_without_masking_as_not_found() { + let error = Error::commit_status_unknown_source( + 42, + box_error(Error::not_found("temporarily invisible manifest")), + ); + + assert!(error.is_commit_status_unknown()); + assert!(!error.is_not_found()); + assert!(error.to_string().contains("version 42 is unknown")); + let Error::Wrapped { error, .. } = error else { + panic!("commit-status-unknown must use the semver-compatible wrapper") + }; + let status = error + .downcast_ref::() + .expect("wrapper must retain the typed commit status"); + assert_eq!(status.version(), 42); + } + + #[test] + fn test_external_error_creation() { + let custom_err = MyCustomError { + code: 42, + message: "test error".to_string(), + }; + let err = Error::external(Box::new(custom_err)); + + match &err { + Error::External { source } => { + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 42); + assert_eq!(recovered.message, "test error"); + } + _ => panic!("Expected External variant"), + } + } + + #[test] + fn test_external_source_method() { + let custom_err = MyCustomError { + code: 123, + message: "source test".to_string(), + }; + let err = Error::external(Box::new(custom_err)); + + let source = err.external_source().expect("should have external source"); + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 123); + + // Test that non-External variants return None + let io_err = Error::io("test"); + assert!(io_err.external_source().is_none()); + } + + #[test] + fn test_into_external_method() { + let custom_err = MyCustomError { + code: 456, + message: "into test".to_string(), + }; + let err = Error::external(Box::new(custom_err)); + + match err.into_external() { + Ok(source) => { + let recovered = source.downcast::().unwrap(); + assert_eq!(recovered.code, 456); + } + Err(_) => panic!("Expected Ok"), + } + + // Test that non-External variants return Err(self) + let io_err = Error::io("test"); + match io_err.into_external() { + Err(Error::IO { .. }) => {} + _ => panic!("Expected Err with IO variant"), + } + } + + #[test] + fn test_arrow_external_error_conversion() { + let custom_err = MyCustomError { + code: 789, + message: "arrow test".to_string(), + }; + let arrow_err = ArrowError::ExternalError(Box::new(custom_err)); + let lance_err: Error = arrow_err.into(); + + match lance_err { + Error::External { source } => { + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 789); + } + _ => panic!("Expected External variant, got {:?}", lance_err), + } + } + + #[test] + fn test_external_to_arrow_roundtrip() { + let custom_err = MyCustomError { + code: 999, + message: "roundtrip".to_string(), + }; + let lance_err = Error::external(Box::new(custom_err)); + let arrow_err: ArrowError = lance_err.into(); + + match arrow_err { + ArrowError::ExternalError(source) => { + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 999); + } + _ => panic!("Expected ExternalError variant"), + } + } + + #[cfg(feature = "datafusion")] + #[test] + fn test_datafusion_schema_error_is_invalid_input() { + // Schema errors from DataFusion (e.g., a filter referencing an unknown + // column) are user-input failures, not internal lance failures. They + // must surface as `Error::InvalidInput` so downstream FFI/Python + // bindings can map them to the right user-facing error code. + use datafusion_common::Column; + + let schema_err = datafusion_common::SchemaError::FieldNotFound { + field: Box::new(Column::from_name("missing_col")), + valid_fields: vec![], + }; + let df_err = + datafusion_common::DataFusionError::SchemaError(Box::new(schema_err), Box::new(None)); + let lance_err: Error = df_err.into(); + + match lance_err { + Error::InvalidInput { .. } => { + assert!( + lance_err.to_string().contains("missing_col"), + "expected the column name to survive in the error message, got: {lance_err}" + ); + } + _ => panic!("Expected InvalidInput variant, got {:?}", lance_err), + } + } + + #[cfg(feature = "datafusion")] + #[test] + fn test_datafusion_external_error_conversion() { + let custom_err = MyCustomError { + code: 111, + message: "datafusion test".to_string(), + }; + let df_err = datafusion_common::DataFusionError::External(Box::new(custom_err)); + let lance_err: Error = df_err.into(); + + match lance_err { + Error::External { source } => { + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 111); + } + _ => panic!("Expected External variant"), + } + } + + #[cfg(feature = "datafusion")] + #[test] + fn test_datafusion_arrow_external_error_conversion() { + // Test the nested case: ArrowError::ExternalError inside DataFusionError::ArrowError + let custom_err = MyCustomError { + code: 222, + message: "nested test".to_string(), + }; + let arrow_err = ArrowError::ExternalError(Box::new(custom_err)); + let df_err = datafusion_common::DataFusionError::ArrowError(Box::new(arrow_err), None); + let lance_err: Error = df_err.into(); + + match lance_err { + Error::External { source } => { + let recovered = source.downcast_ref::().unwrap(); + assert_eq!(recovered.code, 222); + } + _ => panic!("Expected External variant, got {:?}", lance_err), + } + } + + /// Test that lance_core::Error round-trips through ArrowError. + /// + /// This simulates the case where a user defines an iterator in terms of + /// lance_core::Error, and the error goes through Arrow's error type + /// (e.g., via RecordBatchIterator) before being converted back. + #[test] + fn test_lance_error_roundtrip_through_arrow() { + let original = Error::invalid_input("test validation error"); + + // Simulate what happens when using ? in an Arrow context + let arrow_err: ArrowError = original.into(); + + // Convert back to lance error (as happens when Lance consumes the stream) + let recovered: Error = arrow_err.into(); + + // Should get back the original lance error directly (not wrapped in External) + match recovered { + Error::InvalidInput { .. } => { + assert!(recovered.to_string().contains("test validation error")); + } + _ => panic!("Expected InvalidInput variant, got {:?}", recovered), + } + } + + /// Test that lance_core::Error round-trips through DataFusionError. + /// + /// This simulates the case where a user defines a stream in terms of + /// lance_core::Error, and the error goes through DataFusion's error type + /// (e.g., via SendableRecordBatchStream) before being converted back. + #[cfg(feature = "datafusion")] + #[test] + fn test_lance_error_roundtrip_through_datafusion() { + let original = Error::invalid_input("test validation error"); + + // Simulate what happens when using ? in a DataFusion context + let df_err: datafusion_common::DataFusionError = original.into(); + + // Convert back to lance error (as happens when Lance consumes the stream) + let recovered: Error = df_err.into(); + + // Should get back the original lance error directly (not wrapped in External) + match recovered { + Error::InvalidInput { .. } => { + assert!(recovered.to_string().contains("test validation error")); + } + _ => panic!("Expected InvalidInput variant, got {:?}", recovered), + } + } + + #[test] + fn test_backtrace_accessor() { + // Verify that backtrace() returns the expected result based on feature state + let err = Error::io("test backtrace"); + let bt = err.backtrace(); + #[cfg(feature = "backtrace")] + { + // With the backtrace feature enabled, whether a backtrace is captured + // depends on the RUST_BACKTRACE env var at runtime. We just verify + // the accessor doesn't panic and returns a valid Option. + let _ = bt; + } + #[cfg(not(feature = "backtrace"))] + { + // Without the backtrace feature, this must always be None. + assert!(bt.is_none()); + } + } + + #[test] + fn test_backtrace_captured_when_feature_enabled() { + // Test that backtrace is actually captured when the feature is on and + // RUST_BACKTRACE=1 is set in the environment before the process starts. + // + // NOTE: std::backtrace::Backtrace caches the RUST_BACKTRACE env check, + // so set_var at runtime does not reliably enable capture. This test + // verifies the accessor works correctly in both cases: + // - If RUST_BACKTRACE=1 was set before the test binary started, we get Some. + // - If not, we get None (even with the feature on), which is expected. + #[cfg(feature = "backtrace")] + { + let err = Error::io("backtrace capture test"); + if std::env::var("RUST_BACKTRACE").is_ok() { + assert!( + err.backtrace().is_some(), + "Expected a backtrace when RUST_BACKTRACE=1 and backtrace feature is enabled" + ); + } + // When RUST_BACKTRACE is not set, backtrace() may return None even + // with the feature enabled — this is correct runtime gating behavior. + } + #[cfg(not(feature = "backtrace"))] + { + let err = Error::io("backtrace capture test"); + assert!(err.backtrace().is_none()); + } + } + + #[test] + fn test_backtrace_returns_none_for_variants_without_location() { + let err = Error::InvalidTableLocation { + message: "test".to_string(), + }; + assert!(err.backtrace().is_none()); + + let err = Error::InvalidRef { + message: "test".to_string(), + }; + assert!(err.backtrace().is_none()); + + let err = Error::Stop; + assert!(err.backtrace().is_none()); + } +} diff --git a/lance-artifact/rust/lance-core/src/levenshtein.rs b/lance-artifact/rust/lance-core/src/levenshtein.rs new file mode 100644 index 000000000..ebf5d8901 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/levenshtein.rs @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Calculate the Levenshtein distance between two strings. +/// +/// The Levenshtein distance is a measure of the number of single-character edits +/// (insertions, deletions, or substitutions) required to change one word into the other. +/// +/// # Examples +/// +/// ``` +/// use lance_core::levenshtein::levenshtein_distance; +/// +/// assert_eq!(levenshtein_distance("kitten", "sitting"), 3); +/// assert_eq!(levenshtein_distance("hello", "hello"), 0); +/// ``` +pub fn levenshtein_distance(s1: &str, s2: &str) -> usize { + let s1_chars: Vec = s1.chars().collect(); + let s2_chars: Vec = s2.chars().collect(); + let m = s1_chars.len(); + let n = s2_chars.len(); + + if m == 0 { + return n; + } + if n == 0 { + return m; + } + + // Use two rows instead of full matrix for space efficiency + let mut prev_row: Vec = (0..=n).collect(); + let mut curr_row: Vec = vec![0; n + 1]; + + for (i, s1_char) in s1_chars.iter().enumerate() { + curr_row[0] = i + 1; + for (j, s2_char) in s2_chars.iter().enumerate() { + let cost = if s1_char == s2_char { 0 } else { 1 }; + curr_row[j + 1] = (prev_row[j + 1] + 1) + .min(curr_row[j] + 1) + .min(prev_row[j] + cost); + } + std::mem::swap(&mut prev_row, &mut curr_row); + } + + prev_row[n] +} + +/// Find the best suggestion from a list of options based on Levenshtein distance. +/// +/// Returns `Some(suggestion)` if there's an option where the Levenshtein distance +/// is at most 1/3 of the length of the input string (integer division). +/// Otherwise returns `None`. +/// +/// # Examples +/// +/// ``` +/// use lance_core::levenshtein::find_best_suggestion; +/// +/// let options = vec!["vector", "id", "name"]; +/// assert_eq!(find_best_suggestion("vacter", &options), Some("vector")); +/// assert_eq!(find_best_suggestion("hello", &options), None); +/// ``` +pub fn find_best_suggestion<'a, 'b>( + input: &'a str, + options: &'b [impl AsRef], +) -> Option<&'b str> { + let input_len = input.chars().count(); + if input_len == 0 { + return None; + } + + let threshold = input_len / 3; + let mut best_option: Option<(&'b str, usize)> = None; + for option in options { + let distance = levenshtein_distance(input, option.as_ref()); + if distance <= threshold { + match &best_option { + None => best_option = Some((option.as_ref(), distance)), + Some((_, best_distance)) => { + if distance < *best_distance { + best_option = Some((option.as_ref(), distance)); + } + } + } + } + } + + best_option.map(|(option, _)| option) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_levenshtein_distance() { + assert_eq!(levenshtein_distance("", ""), 0); + assert_eq!(levenshtein_distance("a", ""), 1); + assert_eq!(levenshtein_distance("", "a"), 1); + assert_eq!(levenshtein_distance("abc", "abc"), 0); + assert_eq!(levenshtein_distance("abc", ""), 3); + assert_eq!(levenshtein_distance("", "abc"), 3); + assert_eq!(levenshtein_distance("kitten", "sitting"), 3); + assert_eq!(levenshtein_distance("saturday", "sunday"), 3); + assert_eq!(levenshtein_distance("vector", "vectr"), 1); + assert_eq!(levenshtein_distance("vector", "vextor"), 1); + assert_eq!(levenshtein_distance("vector", "vvector"), 1); + assert_eq!(levenshtein_distance("abc", "xyz"), 3); + } + + #[test] + fn test_find_best_suggestion() { + let options = vec!["vector", "id", "name", "column", "table"]; + + assert_eq!(find_best_suggestion("vacter", &options), Some("vector")); + assert_eq!(find_best_suggestion("vectr", &options), Some("vector")); + assert_eq!(find_best_suggestion("tble", &options), Some("table")); + + // Should return None if no good match + assert_eq!(find_best_suggestion("hello", &options), None); + assert_eq!(find_best_suggestion("xyz", &options), None); + + // Should return None if input is too short + assert_eq!(find_best_suggestion("v", &options), None); + assert_eq!(find_best_suggestion("", &options), None); + + // Picks closest when multiple are close + assert_eq!( + find_best_suggestion("vecor", &["vector", "vendor"]), + Some("vector") + ); + } +} diff --git a/lance-artifact/rust/lance-core/src/lib.rs b/lance-artifact/rust/lance-core/src/lib.rs new file mode 100644 index 000000000..32fb34ad5 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/lib.rs @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors +#![cfg_attr(coverage, feature(coverage_attribute))] + +// Allow the derive macro to reference `lance_core::deepsize` from within this crate. +extern crate self as lance_core; + +use arrow_schema::{DataType, Field as ArrowField}; +use std::sync::LazyLock; + +pub mod cache; +pub mod container; +pub mod datatypes; +pub mod deepsize; +pub mod error; +pub mod levenshtein; +pub mod traits; +pub mod utils; + +pub use error::{ArrowResult, Error, FenceReason, Result, box_error}; + +/// Wildcard to indicate all non-system columns +pub const WILDCARD: &str = "*"; +/// Column name for the meta row ID. +pub const ROW_ID: &str = "_rowid"; +/// Column name for the meta row address. +pub const ROW_ADDR: &str = "_rowaddr"; +/// Column name for the meta row offset. +pub const ROW_OFFSET: &str = "_rowoffset"; +/// Column name for the row's last updated at dataset version. +pub const ROW_LAST_UPDATED_AT_VERSION: &str = "_row_last_updated_at_version"; +/// Column name for the row's created at dataset version. +pub const ROW_CREATED_AT_VERSION: &str = "_row_created_at_version"; + +/// Row ID field. This is nullable because its validity bitmap is sometimes used +/// as a selection vector. +pub static ROW_ID_FIELD: LazyLock = + LazyLock::new(|| ArrowField::new(ROW_ID, DataType::UInt64, true)); +/// Row address field. This is nullable because its validity bitmap is sometimes used +/// as a selection vector. +pub static ROW_ADDR_FIELD: LazyLock = + LazyLock::new(|| ArrowField::new(ROW_ADDR, DataType::UInt64, true)); +/// Row offset field. This is nullable merely for compatibility with the other +/// fields. +pub static ROW_OFFSET_FIELD: LazyLock = + LazyLock::new(|| ArrowField::new(ROW_OFFSET, DataType::UInt64, true)); +/// Row last updated at version field. +pub static ROW_LAST_UPDATED_AT_VERSION_FIELD: LazyLock = + LazyLock::new(|| ArrowField::new(ROW_LAST_UPDATED_AT_VERSION, DataType::UInt64, true)); +/// Row created at version field. +pub static ROW_CREATED_AT_VERSION_FIELD: LazyLock = + LazyLock::new(|| ArrowField::new(ROW_CREATED_AT_VERSION, DataType::UInt64, true)); + +/// Check if a column name is a system column. +/// +/// System columns are virtual columns that are computed at read time and don't +/// exist in the physical data files. They include: +/// - `_rowid`: The row ID +/// - `_rowaddr`: The row address +/// - `_rowoffset`: The row offset +/// - `_row_last_updated_at_version`: The version when the row was last updated +/// - `_row_created_at_version`: The version when the row was created +pub fn is_system_column(column_name: &str) -> bool { + matches!( + column_name, + ROW_ID | ROW_ADDR | ROW_OFFSET | ROW_LAST_UPDATED_AT_VERSION | ROW_CREATED_AT_VERSION + ) +} diff --git a/lance-artifact/rust/lance-core/src/traits.rs b/lance-artifact/rust/lance-core/src/traits.rs new file mode 100644 index 000000000..cc43d7e0d --- /dev/null +++ b/lance-artifact/rust/lance-core/src/traits.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Dataset Traits + +use std::fmt::Debug; + +use arrow_array::RecordBatch; + +use crate::{Result, datatypes::Schema}; + +/// `TakeRow` trait. +/// +/// It offers a lightweight trait to use `take_rows()` over a dataset, without +/// depending on the `lance` trait. +/// +///

    +/// Internal API +///
    +#[async_trait::async_trait] +pub trait DatasetTakeRows: Debug + Send + Sync { + /// The schema of the dataset. + fn schema(&self) -> &Schema; + + /// Take rows by the internal ROW ids. + async fn take_rows(&self, row_ids: &[u64], projection: &Schema) -> Result; +} diff --git a/lance-artifact/rust/lance-core/src/utils.rs b/lance-artifact/rust/lance-core/src/utils.rs new file mode 100644 index 000000000..51d72c3da --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils.rs @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod address; +pub mod aimd; +pub mod assume; +pub mod backoff; +pub mod bit; +pub mod blob; +pub mod bloomfilter; +pub mod cpu; +pub mod deletion; +pub mod futures; +pub mod hash; +pub mod io_stats; +pub mod parse; +pub mod path; +pub mod row_addr_remap; +pub mod tempfile; +pub mod testing; +pub mod tokio; +pub mod tracing; diff --git a/lance-artifact/rust/lance-core/src/utils/address.rs b/lance-artifact/rust/lance-core/src/utils/address.rs new file mode 100644 index 000000000..37512ca1e --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/address.rs @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::ops::Range; + +/// A row address encodes a fragment ID (upper 32 bits) and row offset (lower 32 bits). +/// +/// ``` +/// use lance_core::utils::address::RowAddress; +/// +/// let addr = RowAddress::new_from_parts(5, 100); +/// assert_eq!(addr.fragment_id(), 5); +/// assert_eq!(addr.row_offset(), 100); +/// +/// // Convert to/from u64 +/// let raw: u64 = addr.into(); +/// let addr2: RowAddress = raw.into(); +/// assert_eq!(addr, addr2); +/// +/// // Display format +/// assert_eq!(format!("{}", addr), "(5, 100)"); +/// ``` +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct RowAddress(u64); + +impl RowAddress { + pub const FRAGMENT_SIZE: u64 = 1 << 32; + /// A fragment id that will never be used. + pub const TOMBSTONE_FRAG: u32 = 0xffffffff; + /// A row id that will never be used. + pub const TOMBSTONE_ROW: u64 = 0xffffffffffffffff; + + pub fn new_from_u64(row_addr: u64) -> Self { + Self(row_addr) + } + + pub fn new_from_parts(fragment_id: u32, row_offset: u32) -> Self { + Self(((fragment_id as u64) << 32) | row_offset as u64) + } + + /// Returns the address for the first row of a fragment. + pub fn first_row(fragment_id: u32) -> Self { + Self::new_from_parts(fragment_id, 0) + } + + /// Returns the range of u64 addresses for a given fragment. + /// + /// ``` + /// use lance_core::utils::address::RowAddress; + /// + /// let range = RowAddress::address_range(2); + /// assert_eq!(range.start, 2 * RowAddress::FRAGMENT_SIZE); + /// assert_eq!(range.end, 3 * RowAddress::FRAGMENT_SIZE); + /// ``` + pub fn address_range(fragment_id: u32) -> Range { + u64::from(Self::first_row(fragment_id))..u64::from(Self::first_row(fragment_id + 1)) + } + + pub fn fragment_id(&self) -> u32 { + (self.0 >> 32) as u32 + } + + pub fn row_offset(&self) -> u32 { + self.0 as u32 + } +} + +impl From for u64 { + fn from(row_addr: RowAddress) -> Self { + row_addr.0 + } +} + +impl From for RowAddress { + fn from(row_addr: u64) -> Self { + Self(row_addr) + } +} + +impl std::fmt::Debug for RowAddress { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", self) // use Display + } +} + +impl std::fmt::Display for RowAddress { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {})", self.fragment_id(), self.row_offset()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_row_address() { + // new_from_u64 (not in doctest) + let addr = RowAddress::new_from_u64(0x0000_0001_0000_0002); + assert_eq!(addr.fragment_id(), 1); + assert_eq!(addr.row_offset(), 2); + + // address_range uses first_row internally (coverage) + let range = RowAddress::address_range(3); + assert_eq!(range.start, 3 * RowAddress::FRAGMENT_SIZE); + + // From impls with different values than doctest + let addr2 = RowAddress::new_from_parts(7, 8); + let raw: u64 = addr2.into(); + let addr3: RowAddress = raw.into(); + assert_eq!(addr2, addr3); + + // Debug format (doctest only tests Display) + assert_eq!(format!("{:?}", addr), "(1, 2)"); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/aimd.rs b/lance-artifact/rust/lance-core/src/utils/aimd.rs new file mode 100644 index 000000000..0cbae68ca --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/aimd.rs @@ -0,0 +1,623 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! AIMD (Additive Increase / Multiplicative Decrease) rate controller. +//! +//! This module provides a reusable AIMD algorithm for dynamically adjusting +//! request rates. On success windows, the rate increases additively. On +//! windows with throttle signals, the rate decreases multiplicatively. +//! +//! The algorithm operates in discrete time windows. At the end of each window, +//! the throttle ratio (throttled / total) is compared against a threshold: +//! - Above threshold: `rate = max(rate * decrease_factor, min_rate)` +//! - At or below threshold: `rate = min(rate + additive_increment, max_rate)` + +use std::sync::Mutex; +use std::time::Duration; + +use crate::Result; + +/// Configuration for the AIMD rate controller. +/// +/// Use builder methods to customize. Defaults are tuned for cloud object stores +/// and will start at about 40% of the max rate and require 10 seconds to reach +/// the max rate. +/// +/// - initial_rate: 2000 req/s +/// - min_rate: 1 req/s +/// - max_rate: 5000 req/s (0.0 disables ceiling) +/// - decrease_factor: 0.5 (halve on throttle) +/// - additive_increment: 300 req/s per success window +/// - window_duration: 1 second +/// - throttle_threshold: 0.0 (any throttle triggers decrease) +#[derive(Debug, Clone)] +pub struct AimdConfig { + pub initial_rate: f64, + pub min_rate: f64, + pub max_rate: f64, + pub decrease_factor: f64, + pub additive_increment: f64, + pub window_duration: Duration, + pub throttle_threshold: f64, +} + +impl Default for AimdConfig { + fn default() -> Self { + Self { + initial_rate: 2000.0, + min_rate: 1.0, + max_rate: 5000.0, + decrease_factor: 0.5, + additive_increment: 300.0, + window_duration: Duration::from_secs(1), + throttle_threshold: 0.0, + } + } +} + +impl AimdConfig { + pub fn with_initial_rate(self, initial_rate: f64) -> Self { + Self { + initial_rate, + ..self + } + } + + pub fn with_min_rate(self, min_rate: f64) -> Self { + Self { min_rate, ..self } + } + + pub fn with_max_rate(self, max_rate: f64) -> Self { + Self { max_rate, ..self } + } + + pub fn with_decrease_factor(self, decrease_factor: f64) -> Self { + Self { + decrease_factor, + ..self + } + } + + pub fn with_additive_increment(self, additive_increment: f64) -> Self { + Self { + additive_increment, + ..self + } + } + + pub fn with_window_duration(self, window_duration: Duration) -> Self { + Self { + window_duration, + ..self + } + } + + pub fn with_throttle_threshold(self, throttle_threshold: f64) -> Self { + Self { + throttle_threshold, + ..self + } + } + + /// Validate that the configuration values are sensible. + pub fn validate(&self) -> Result<()> { + if self.initial_rate <= 0.0 { + return Err(crate::Error::invalid_input(format!( + "initial_rate must be positive, got {}", + self.initial_rate + ))); + } + if self.min_rate <= 0.0 { + return Err(crate::Error::invalid_input(format!( + "min_rate must be positive, got {}", + self.min_rate + ))); + } + if self.max_rate < 0.0 { + return Err(crate::Error::invalid_input(format!( + "max_rate must be non-negative (0.0 = no ceiling), got {}", + self.max_rate + ))); + } + if self.max_rate > 0.0 && self.min_rate > self.max_rate { + return Err(crate::Error::invalid_input(format!( + "min_rate ({}) must not exceed max_rate ({})", + self.min_rate, self.max_rate + ))); + } + if self.decrease_factor <= 0.0 || self.decrease_factor >= 1.0 { + return Err(crate::Error::invalid_input(format!( + "decrease_factor must be in (0, 1), got {}", + self.decrease_factor + ))); + } + if self.additive_increment <= 0.0 { + return Err(crate::Error::invalid_input(format!( + "additive_increment must be positive, got {}", + self.additive_increment + ))); + } + if self.window_duration.is_zero() { + return Err(crate::Error::invalid_input( + "window_duration must be non-zero", + )); + } + if !(0.0..=1.0).contains(&self.throttle_threshold) { + return Err(crate::Error::invalid_input(format!( + "throttle_threshold must be in [0.0, 1.0], got {}", + self.throttle_threshold + ))); + } + if self.max_rate > 0.0 && self.initial_rate > self.max_rate { + return Err(crate::Error::invalid_input(format!( + "initial_rate ({}) must not exceed max_rate ({})", + self.initial_rate, self.max_rate + ))); + } + if self.initial_rate < self.min_rate { + return Err(crate::Error::invalid_input(format!( + "initial_rate ({}) must not be below min_rate ({})", + self.initial_rate, self.min_rate + ))); + } + Ok(()) + } +} + +/// Outcome of a single request, used to feed the AIMD controller. +/// +/// Non-throttle errors (e.g. 404, network timeout) should be mapped to +/// `Success` since they don't indicate capacity problems. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RequestOutcome { + Success, + Throttled, +} + +struct AimdState { + rate: f64, + window_start: std::time::Instant, + success_count: u64, + throttle_count: u64, +} + +/// AIMD rate controller. +/// +/// Thread-safe: uses an internal `Mutex` to protect state. The lock is held +/// only briefly during `record_outcome` and `current_rate`. +pub struct AimdController { + config: AimdConfig, + state: Mutex, +} + +impl std::fmt::Debug for AimdController { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AimdController") + .field("config", &self.config) + .field("rate", &self.current_rate()) + .finish() + } +} + +impl AimdController { + /// Create a new AIMD controller with the given configuration. + pub fn new(config: AimdConfig) -> Result { + config.validate()?; + let rate = config.initial_rate; + Ok(Self { + config, + state: Mutex::new(AimdState { + rate, + window_start: std::time::Instant::now(), + success_count: 0, + throttle_count: 0, + }), + }) + } + + /// Record a request outcome and return the current rate. + /// + /// If the current time window has expired, the rate is adjusted before + /// recording the new outcome in a fresh window. + pub fn record_outcome(&self, outcome: RequestOutcome) -> f64 { + let mut state = self.state.lock().unwrap(); + self.record_outcome_inner(&mut state, outcome, std::time::Instant::now()) + } + + fn record_outcome_inner( + &self, + state: &mut AimdState, + outcome: RequestOutcome, + now: std::time::Instant, + ) -> f64 { + // Check if the window has expired + let elapsed = now.duration_since(state.window_start); + if elapsed >= self.config.window_duration { + let total = state.success_count + state.throttle_count; + if total > 0 { + let throttle_ratio = state.throttle_count as f64 / total as f64; + if throttle_ratio > self.config.throttle_threshold { + // Multiplicative decrease + state.rate = + (state.rate * self.config.decrease_factor).max(self.config.min_rate); + } else { + // Additive increase + state.rate += self.config.additive_increment; + if self.config.max_rate > 0.0 { + state.rate = state.rate.min(self.config.max_rate); + } + } + } + // Reset window + state.window_start = now; + state.success_count = 0; + state.throttle_count = 0; + } + + // Record this outcome + match outcome { + RequestOutcome::Success => state.success_count += 1, + RequestOutcome::Throttled => state.throttle_count += 1, + } + + state.rate + } + + /// Get the current rate without recording an outcome. + pub fn current_rate(&self) -> f64 { + self.state.lock().unwrap().rate + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rstest::rstest; + + #[rstest] + #[case::zero_initial_rate( + AimdConfig::default().with_initial_rate(0.0), + "initial_rate must be positive" + )] + #[case::negative_min_rate( + AimdConfig::default().with_min_rate(-1.0), + "min_rate must be positive" + )] + #[case::negative_max_rate( + AimdConfig::default().with_max_rate(-1.0), + "max_rate must be non-negative" + )] + #[case::min_exceeds_max( + AimdConfig::default().with_min_rate(100.0).with_max_rate(10.0), + "min_rate (100) must not exceed max_rate (10)" + )] + #[case::decrease_factor_zero( + AimdConfig::default().with_decrease_factor(0.0), + "decrease_factor must be in (0, 1)" + )] + #[case::decrease_factor_one( + AimdConfig::default().with_decrease_factor(1.0), + "decrease_factor must be in (0, 1)" + )] + #[case::decrease_factor_over_one( + AimdConfig::default().with_decrease_factor(1.5), + "decrease_factor must be in (0, 1)" + )] + #[case::zero_additive_increment( + AimdConfig::default().with_additive_increment(0.0), + "additive_increment must be positive" + )] + #[case::zero_window_duration( + AimdConfig::default().with_window_duration(Duration::ZERO), + "window_duration must be non-zero" + )] + #[case::threshold_over_one( + AimdConfig::default().with_throttle_threshold(1.1), + "throttle_threshold must be in [0.0, 1.0]" + )] + #[case::threshold_negative( + AimdConfig::default().with_throttle_threshold(-0.1), + "throttle_threshold must be in [0.0, 1.0]" + )] + #[case::initial_exceeds_max( + AimdConfig::default().with_initial_rate(6000.0), + "initial_rate (6000) must not exceed max_rate (5000)" + )] + #[case::initial_below_min( + AimdConfig::default().with_initial_rate(0.5).with_min_rate(1.0), + "initial_rate (0.5) must not be below min_rate (1)" + )] + fn test_config_validation_rejects_invalid( + #[case] config: AimdConfig, + #[case] expected_msg: &str, + ) { + let err = config.validate().unwrap_err(); + let msg = err.to_string(); + assert!( + msg.contains(expected_msg), + "Expected error containing '{}', got: {}", + expected_msg, + msg + ); + } + + #[test] + fn test_default_config_is_valid() { + AimdConfig::default().validate().unwrap(); + } + + #[test] + fn test_no_ceiling_config_is_valid() { + AimdConfig::default().with_max_rate(0.0).validate().unwrap(); + } + + #[test] + fn test_additive_increase_on_success_window() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_additive_increment(10.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + // Record some successes in the first window + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, start); + } + + // Advance past the window boundary and record another success + let after_window = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, after_window); + } + + // Rate should have increased by additive_increment + assert_eq!(controller.current_rate(), 110.0); + } + + #[test] + fn test_multiplicative_decrease_on_throttle_window() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_decrease_factor(0.5) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + } + + // Advance past window + let after_window = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, after_window); + } + + assert_eq!(controller.current_rate(), 50.0); + } + + #[test] + fn test_floor_enforcement() { + let config = AimdConfig::default() + .with_initial_rate(2.0) + .with_min_rate(1.0) + .with_decrease_factor(0.5) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + } + + // After decrease: 2.0 * 0.5 = 1.0 (at floor) + let t1 = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, t1); + } + assert_eq!(controller.current_rate(), 1.0); + + // Another decrease should stay at floor + let t2 = t1 + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t2); + } + assert_eq!(controller.current_rate(), 1.0); + } + + #[test] + fn test_ceiling_enforcement() { + let config = AimdConfig::default() + .with_initial_rate(4990.0) + .with_max_rate(5000.0) + .with_additive_increment(20.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, start); + } + + let t1 = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t1); + } + // 4990 + 20 = 5010, clamped to 5000 + assert_eq!(controller.current_rate(), 5000.0); + } + + #[test] + fn test_no_ceiling_allows_unbounded_growth() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_max_rate(0.0) + .with_additive_increment(50.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + let mut t = start; + + for _ in 0..5 { + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t); + } + t += Duration::from_millis(150); + } + + // Trigger final window evaluation + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t); + } + + // 100 + 50*5 = 350 + assert_eq!(controller.current_rate(), 350.0); + } + + #[test] + fn test_empty_window_no_adjustment() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + // Don't record anything in the first window, just advance time + let start = std::time::Instant::now(); + let after = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + // First outcome in a new window after empty window + controller.record_outcome_inner(&mut state, RequestOutcome::Success, after); + } + // No adjustment because the expired window had 0 total + assert_eq!(controller.current_rate(), 100.0); + } + + #[test] + fn test_throttle_threshold_filtering() { + // With threshold 0.5, less than 50% throttles should still increase + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_throttle_threshold(0.5) + .with_additive_increment(10.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + // 1 throttle out of 3 = 33% < 50% threshold + controller.record_outcome_inner(&mut state, RequestOutcome::Success, start); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, start); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + } + + // Advance past window + let t1 = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t1); + } + + // Should have increased because 33% <= 50% + assert_eq!(controller.current_rate(), 110.0); + } + + #[test] + fn test_throttle_threshold_triggers_decrease() { + // With threshold 0.5, >= 50% throttles should decrease + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_throttle_threshold(0.5) + .with_decrease_factor(0.5) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + { + let mut state = controller.state.lock().unwrap(); + // 2 throttle out of 3 = 67% > 50% threshold + controller.record_outcome_inner(&mut state, RequestOutcome::Success, start); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + } + + let t1 = start + Duration::from_millis(150); + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t1); + } + + assert_eq!(controller.current_rate(), 50.0); + } + + #[test] + fn test_recovery_after_decrease() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_decrease_factor(0.5) + .with_additive_increment(10.0) + .with_window_duration(Duration::from_millis(100)); + let controller = AimdController::new(config).unwrap(); + + let start = std::time::Instant::now(); + + // Window 1: throttle → decrease to 50 + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Throttled, start); + } + let t1 = start + Duration::from_millis(150); + + // Window 2: success → increase to 60 + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t1); + } + let t2 = t1 + Duration::from_millis(150); + + // Window 3: success → increase to 70 + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t2); + } + let t3 = t2 + Duration::from_millis(150); + + // Trigger final evaluation + { + let mut state = controller.state.lock().unwrap(); + controller.record_outcome_inner(&mut state, RequestOutcome::Success, t3); + } + + assert_eq!(controller.current_rate(), 70.0); + } + + #[test] + fn test_within_window_no_adjustment() { + let config = AimdConfig::default() + .with_initial_rate(100.0) + .with_window_duration(Duration::from_secs(10)); + let controller = AimdController::new(config).unwrap(); + + // Record many outcomes but all within the same window + for _ in 0..100 { + controller.record_outcome(RequestOutcome::Throttled); + } + + // Rate should still be initial since window hasn't expired + assert_eq!(controller.current_rate(), 100.0); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/assume.rs b/lance-artifact/rust/lance-core/src/utils/assume.rs new file mode 100644 index 000000000..2560e9bf3 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/assume.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// A macro that combines debug_assert and std::hint::assert_unchecked for optimized assertions +/// +/// In debug builds, this will perform a normal assertion check. +/// In release builds, this will use hint::assert_unchecked which tells the compiler to assume +/// the condition is true without actually checking it. +/// +/// # Safety +/// +/// This macro is unsafe in release builds since it uses hint::assert_unchecked. +/// The caller must ensure the condition will always be true. +#[macro_export] +macro_rules! assume { + ($cond:expr) => { + debug_assert!($cond); + // SAFETY: The debug_assert ensures this is true in debug builds. + // In release builds, caller must ensure the condition holds. + unsafe { std::hint::assert_unchecked($cond); } + }; + ($cond:expr, $($arg:tt)+) => { + debug_assert!($cond, $($arg)+); + // SAFETY: The debug_assert ensures this is true in debug builds. + // In release builds, caller must ensure the condition holds. + unsafe { std::hint::assert_unchecked($cond); } + }; +} + +/// Helper macro for equality assumptions. +#[macro_export] +macro_rules! assume_eq { + ($left:expr, $right:expr) => { + debug_assert_eq!($left, $right); + unsafe { std::hint::assert_unchecked($left == $right); } + }; + ($left:expr, $right:expr, $($arg:tt)+) => { + debug_assert_eq!($left, $right, $($arg)+); + unsafe { std::hint::assert_unchecked($left == $right); } + }; +} diff --git a/lance-artifact/rust/lance-core/src/utils/backoff.rs b/lance-artifact/rust/lance-core/src/utils/backoff.rs new file mode 100644 index 000000000..2b1f81e16 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/backoff.rs @@ -0,0 +1,291 @@ +use rand::{Rng, SeedableRng}; +use std::time::Duration; + +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Computes backoff as +/// +/// ```text +/// backoff = base^attempt * unit + jitter +/// ``` +/// +/// The defaults are base=2, unit=50ms, jitter=50ms, min=0ms, max=5s. This gives +/// a backoff of 50ms, 100ms, 200ms, 400ms, 800ms, 1.6s, 3.2s, 5s, (not including jitter). +/// +/// You can have non-exponential backoff by setting base=1. +pub struct Backoff { + base: u32, + unit: u32, + jitter: i32, + min: u32, + max: u32, + attempt: u32, +} + +impl Default for Backoff { + fn default() -> Self { + Self { + base: 2, + unit: 50, + jitter: 50, + min: 0, + max: 5000, + attempt: 0, + } + } +} + +impl Backoff { + pub fn with_base(self, base: u32) -> Self { + Self { base, ..self } + } + + pub fn with_unit(self, unit: u32) -> Self { + Self { unit, ..self } + } + + pub fn with_jitter(self, jitter: i32) -> Self { + Self { jitter, ..self } + } + + pub fn with_min(self, min: u32) -> Self { + Self { min, ..self } + } + + pub fn with_max(self, max: u32) -> Self { + Self { max, ..self } + } + + pub fn next_backoff(&mut self) -> Duration { + let backoff = self + .base + .saturating_pow(self.attempt) + .saturating_mul(self.unit); + let jitter = rand::rng().random_range(-self.jitter..=self.jitter); + let backoff = (backoff.saturating_add_signed(jitter)).clamp(self.min, self.max); + self.attempt += 1; + Duration::from_millis(backoff as u64) + } + + pub fn attempt(&self) -> u32 { + self.attempt + } + + pub fn reset(&mut self) { + self.attempt = 0; + } +} + +/// Upper bound on the number of retry slots. +/// +/// Slots double each attempt to spread contending writers apart, but a hundred +/// or so already exceeds any realistic number of concurrent committers, so +/// further doubling only inflates the wait without reducing collisions. Capping +/// the count also bounds a single backoff to `(MAX_SLOTS - 1) * unit` instead of +/// letting it grow without limit as `attempt` climbs. +const MAX_SLOTS: u32 = 128; + +/// SlotBackoff is a backoff strategy that randomly chooses a time slot to retry. +/// +/// This is useful when you have multiple tasks that can't overlap, and each +/// task takes roughly the same amount of time. +/// +/// The `unit` represents the time it takes to complete one attempt. Future attempts +/// are divided into time slots, and a random slot is chosen for the retry. The number +/// of slots increases exponentially with each attempt. Initially, there are 4 slots, +/// then 8, then 16, and so on, up to a fixed cap. +/// +/// Example: +/// Suppose you have 10 tasks that can't overlap, each taking 1 second. The tasks +/// don't know about each other and can't coordinate. Each task randomly picks a +/// time slot to retry. Here's how it might look: +/// +/// First round (4 slots): +/// ```text +/// task id | 1, 2, 3 | 4, 5, 6 | 7, 8, 9 | 10 | +/// status | x, x, ✓ | x, x, ✓ | x, x, ✓ | ✓ | +/// timeline | 0s | 1s | 2s | 3s | +/// ``` +/// Each slot can have one success. Here, tasks 3, 6, 9, and 10 succeed. +/// In the next round, the number of slots doubles (8): +/// +/// Second round (8 slots): +/// ```text +/// task id | 1 | 2 | | 4, 5 | 7 | 8 | | | +/// status | ✓ | ✓ | | x, ✓ | ✓ | ✓ | | | +/// timeline | 0s | 1s | 2s | 3s | 4s | 5s | 6s | 7s | +/// ``` +/// Most tasks are done now, except for task 4. It will succeed in the next round. +pub struct SlotBackoff { + base: u32, + unit: u32, + starting_i: u32, + attempt: u32, + rng: rand::rngs::SmallRng, +} + +impl Default for SlotBackoff { + fn default() -> Self { + Self { + base: 2, + unit: 50, + starting_i: 2, // start with 4 slots + attempt: 0, + rng: rand::rngs::SmallRng::from_os_rng(), + } + } +} + +impl SlotBackoff { + pub fn with_unit(self, unit: u32) -> Self { + Self { unit, ..self } + } + + pub fn attempt(&self) -> u32 { + self.attempt + } + + pub fn next_backoff(&mut self) -> Duration { + let num_slots = self + .base + .saturating_pow(self.attempt.saturating_add(self.starting_i)) + .min(MAX_SLOTS); + let slot_i = self.rng.random_range(0..num_slots); + self.attempt = self.attempt.saturating_add(1); + // Widen before multiplying: `unit` is the first-attempt latency, which + // can be large enough that a `u32` slot * unit product would overflow. + Duration::from_millis(slot_i as u64 * self.unit as u64) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_backoff() { + let mut backoff = Backoff::default().with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 50); + assert_eq!(backoff.attempt(), 1); + assert_eq!(backoff.next_backoff().as_millis(), 100); + assert_eq!(backoff.attempt(), 2); + assert_eq!(backoff.next_backoff().as_millis(), 200); + assert_eq!(backoff.attempt(), 3); + assert_eq!(backoff.next_backoff().as_millis(), 400); + assert_eq!(backoff.attempt(), 4); + } + + #[test] + fn test_backoff_with_base() { + let mut backoff = Backoff::default().with_base(3).with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 50); // 3^0 * 50 + assert_eq!(backoff.next_backoff().as_millis(), 150); // 3^1 * 50 + assert_eq!(backoff.next_backoff().as_millis(), 450); // 3^2 * 50 + } + + #[test] + fn test_backoff_with_unit() { + let mut backoff = Backoff::default().with_unit(100).with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 100); // 2^0 * 100 + assert_eq!(backoff.next_backoff().as_millis(), 200); // 2^1 * 100 + } + + #[test] + fn test_backoff_with_min() { + let mut backoff = Backoff::default().with_min(100).with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 100); // clamped to min + } + + #[test] + fn test_backoff_with_max() { + let mut backoff = Backoff::default().with_max(75).with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 50); + assert_eq!(backoff.next_backoff().as_millis(), 75); // clamped to max + } + + #[test] + fn test_backoff_reset() { + let mut backoff = Backoff::default().with_jitter(0); + assert_eq!(backoff.next_backoff().as_millis(), 50); + assert_eq!(backoff.attempt(), 1); + backoff.reset(); + assert_eq!(backoff.attempt(), 0); + assert_eq!(backoff.next_backoff().as_millis(), 50); + } + + #[test] + fn test_slot_backoff() { + #[cfg_attr(coverage, coverage(off))] + fn assert_in(value: u128, expected: &[u128]) { + assert!( + expected.contains(&value), + "value {} not in {:?}", + value, + expected + ); + } + + for _ in 0..10 { + let mut backoff = SlotBackoff::default().with_unit(100); + assert_in(backoff.next_backoff().as_millis(), &[0, 100, 200, 300]); + assert_eq!(backoff.attempt(), 1); + assert_in( + backoff.next_backoff().as_millis(), + &[0, 100, 200, 300, 400, 500, 600, 700], + ); + assert_eq!(backoff.attempt(), 2); + assert_in( + backoff.next_backoff().as_millis(), + &(0..16).map(|i| i * 100).collect::>(), + ); + assert_eq!(backoff.attempt(), 3); + } + } + + #[test] + fn test_slot_backoff_high_attempt_is_bounded() { + // Without the slot cap the wait grows unbounded with `attempt`. The cap + // holds every backoff to `(MAX_SLOTS - 1) * unit`. + let unit = 100_000; // 100s first attempt + let mut backoff = SlotBackoff::default().with_unit(unit); + let max_backoff = Duration::from_millis((MAX_SLOTS - 1) as u64 * unit as u64); + for _ in 0..40 { + assert!(backoff.next_backoff() <= max_backoff); + } + assert_eq!(backoff.attempt(), 40); + } + + #[test] + fn test_slot_backoff_large_unit_does_not_overflow() { + // With unit = u32::MAX, any slot >= 2 makes the old u32 `slot_i * unit` + // product overflow: a debug panic, or in release a wrap to a value that + // is no longer a multiple of unit. The u64 widening keeps every backoff + // an exact multiple of unit. Seed the RNG so the drawn slots — and thus + // this check — are deterministic rather than dependent on random draws. + let unit = u32::MAX; + let mut backoff = SlotBackoff::default().with_unit(unit); + backoff.rng = rand::rngs::SmallRng::seed_from_u64(0); + let mut saw_high_slot = false; + for _ in 0..64 { + let backoff_ms = backoff.next_backoff().as_millis(); + // `slot_i * unit` is always a multiple of unit; a wrapped u32 + // product is not. + assert_eq!(backoff_ms % unit as u128, 0, "{backoff_ms} wrapped"); + saw_high_slot |= backoff_ms >= 2 * unit as u128; + } + assert!(saw_high_slot, "expected a slot >= 2 in 64 seeded draws"); + } + + #[test] + fn test_slot_backoff_attempt_saturates() { + // At u32::MAX the counter must stay put rather than panic (debug) or + // wrap to 0 (release), which would restart the low-slot distribution. + let mut backoff = SlotBackoff { + attempt: u32::MAX, + ..Default::default() + }; + let _ = backoff.next_backoff(); + assert_eq!(backoff.attempt(), u32::MAX); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/bit.rs b/lance-artifact/rust/lance-core/src/utils/bit.rs new file mode 100644 index 000000000..d0c9eaf5a --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/bit.rs @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Returns true if the given number is a power of two. +/// +/// ``` +/// use lance_core::utils::bit::is_pwr_two; +/// +/// assert!(is_pwr_two(1)); +/// assert!(is_pwr_two(2)); +/// assert!(is_pwr_two(1024)); +/// assert!(!is_pwr_two(3)); +/// assert!(!is_pwr_two(1000)); +/// ``` +pub fn is_pwr_two(n: u64) -> bool { + n & (n - 1) == 0 +} + +/// Returns the number of padding bytes needed to align `n` to `ALIGN`. +/// +/// ``` +/// use lance_core::utils::bit::pad_bytes; +/// +/// assert_eq!(pad_bytes::<8>(0), 0); +/// assert_eq!(pad_bytes::<8>(1), 7); +/// assert_eq!(pad_bytes::<8>(8), 0); +/// assert_eq!(pad_bytes::<8>(9), 7); +/// ``` +pub fn pad_bytes(n: usize) -> usize { + debug_assert!(is_pwr_two(ALIGN as u64)); + (ALIGN - (n & (ALIGN - 1))) & (ALIGN - 1) +} + +/// Returns the number of padding bytes needed to align `n` to `align`. +/// +/// ``` +/// use lance_core::utils::bit::pad_bytes_to; +/// +/// assert_eq!(pad_bytes_to(0, 8), 0); +/// assert_eq!(pad_bytes_to(1, 8), 7); +/// assert_eq!(pad_bytes_to(8, 8), 0); +/// assert_eq!(pad_bytes_to(9, 8), 7); +/// ``` +pub fn pad_bytes_to(n: usize, align: usize) -> usize { + debug_assert!(is_pwr_two(align as u64)); + (align - (n & (align - 1))) & (align - 1) +} + +/// Returns the number of padding bytes needed to align `n` to `ALIGN` (u64 version). +/// +/// ``` +/// use lance_core::utils::bit::pad_bytes_u64; +/// +/// assert_eq!(pad_bytes_u64::<8>(0), 0); +/// assert_eq!(pad_bytes_u64::<8>(1), 7); +/// assert_eq!(pad_bytes_u64::<8>(8), 0); +/// assert_eq!(pad_bytes_u64::<8>(9), 7); +/// ``` +pub fn pad_bytes_u64(n: u64) -> u64 { + debug_assert!(is_pwr_two(ALIGN)); + (ALIGN - (n & (ALIGN - 1))) & (ALIGN - 1) +} + +// This is a lookup table for the log2 of the first 256 numbers +const LOG_TABLE_256: [u8; 256] = [ + 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, +]; + +/// Returns the number of bits needed to represent the given number. +/// +/// Inspired by +/// +/// ``` +/// use lance_core::utils::bit::log_2_ceil; +/// +/// assert_eq!(log_2_ceil(1), 1); +/// assert_eq!(log_2_ceil(2), 2); +/// assert_eq!(log_2_ceil(255), 8); +/// assert_eq!(log_2_ceil(256), 9); +/// ``` +pub fn log_2_ceil(val: u32) -> u32 { + assert!(val > 0); + let upper_half = val >> 16; + if upper_half == 0 { + let third_quarter = val >> 8; + if third_quarter == 0 { + // Use lowest 8 bits (upper 24 are 0) + LOG_TABLE_256[val as usize] as u32 + } else { + // Use bits 16..24 (0..16 are 0) + LOG_TABLE_256[third_quarter as usize] as u32 + 8 + } + } else { + let first_quarter = upper_half >> 8; + if first_quarter == 0 { + // Use bits 8..16 (0..8 are 0) + 16 + LOG_TABLE_256[upper_half as usize] as u32 + } else { + // Use most significant bits (it's a big number!) + 24 + LOG_TABLE_256[first_quarter as usize] as u32 + } + } +} + +#[cfg(test)] +mod tests { + use crate::utils::bit::{is_pwr_two, log_2_ceil, pad_bytes, pad_bytes_to, pad_bytes_u64}; + + #[test] + fn test_bit_utils() { + // Test values not in doctests + assert!(is_pwr_two(4)); + assert!(is_pwr_two(1024)); + assert!(!is_pwr_two(5)); + + // Test different alignment (64) not shown in doctests + assert_eq!(pad_bytes::<64>(100), 28); + assert_eq!(pad_bytes_to(100, 64), 28); + assert_eq!(pad_bytes_u64::<64>(100), 28); + } + + #[test] + fn test_log_2_ceil() { + #[cfg_attr(coverage, coverage(off))] + fn classic_approach(mut val: u32) -> u32 { + let mut counter = 0; + while val > 0 { + val >>= 1; + counter += 1; + } + counter + } + + for i in 1..(16 * 1024) { + assert_eq!(log_2_ceil(i), classic_approach(i)); + } + assert_eq!(log_2_ceil(50 * 1024), classic_approach(50 * 1024)); + assert_eq!( + log_2_ceil(1024 * 1024 * 1024), + classic_approach(1024 * 1024 * 1024) + ); + // Cover the branch where upper_half != 0 but first_quarter == 0 + // (value between 2^16 and 2^24) + assert_eq!(log_2_ceil(100_000), classic_approach(100_000)); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/blob.rs b/lance-artifact/rust/lance-core/src/utils/blob.rs new file mode 100644 index 000000000..faf97f2d3 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/blob.rs @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use object_store::path::Path; + +/// Format a blob sidecar path for a data file. +/// +/// Layout: `//.blob` +/// - `base` is typically the dataset's data directory. +/// - `data_file_key` is the stem of the data file (without extension). +/// - `blob_id` is transformed via `reverse_bits()` before binary formatting. +pub fn blob_path(base: &Path, data_file_key: &str, blob_id: u32) -> Path { + let file_name = format!("{:032b}.blob", blob_id.reverse_bits()); + base.clone().join(data_file_key).join(file_name.as_str()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_blob_path_formatting() { + let base = Path::from("base"); + let path = blob_path(&base, "deadbeef", 2); + assert_eq!( + path.to_string(), + "base/deadbeef/01000000000000000000000000000000.blob" + ); + } + + #[test] + fn test_blob_path_scattered_prefixes_for_sequential_ids() { + let base = Path::from("base"); + let p1 = blob_path(&base, "deadbeef", 1); + let p2 = blob_path(&base, "deadbeef", 2); + assert_ne!(p1.to_string(), p2.to_string()); + assert_eq!( + p1.to_string(), + "base/deadbeef/10000000000000000000000000000000.blob" + ); + assert_eq!( + p2.to_string(), + "base/deadbeef/01000000000000000000000000000000.blob" + ); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/bloomfilter.rs b/lance-artifact/rust/lance-core/src/utils/bloomfilter.rs new file mode 100644 index 000000000..46cc272a6 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/bloomfilter.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Generic bloom filter primitives. +//! +//! These are storage-agnostic data structures with no Lance semantics, used by +//! higher-level crates (e.g. the bloom filter scalar index in `lance-index`). + +pub mod as_bytes; +pub mod sbbf; diff --git a/lance-artifact/rust/lance-core/src/utils/bloomfilter/as_bytes.rs b/lance-artifact/rust/lance-core/src/utils/bloomfilter/as_bytes.rs new file mode 100644 index 000000000..86b9632ce --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/bloomfilter/as_bytes.rs @@ -0,0 +1,164 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Local implementation of AsBytes trait +//! +//! This trait provides conversion from primitive types to byte slices, +//! similar to parquet::data_type::AsBytes but without the external dependency. + +/// Trait to convert primitive types to byte slices +/// Reference: +pub trait AsBytes { + /// Convert the value to a byte slice + fn as_bytes(&self) -> impl AsRef<[u8]>; +} + +impl AsBytes for i32 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for i64 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for f32 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for f64 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for u8 { + fn as_bytes(&self) -> impl AsRef<[Self]> { + [*self] + } +} + +impl AsBytes for u16 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for u32 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for u64 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for i8 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + [*self as u8] + } +} + +impl AsBytes for i16 { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self.to_le_bytes() + } +} + +impl AsBytes for str { + fn as_bytes(&self) -> impl AsRef<[u8]> { + Self::as_bytes(self) + } +} + +impl AsBytes for [u8] { + fn as_bytes(&self) -> impl AsRef<[u8]> { + self + } +} + +impl AsBytes for bool { + fn as_bytes(&self) -> impl AsRef<[u8]> { + if *self { [1u8] } else { [0u8] } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_i32_as_bytes() { + let val = 0x12345678i32; + let bytes = val.as_bytes(); + assert_eq!(bytes.as_ref().len(), 4); + // Check that we get the expected bytes in little-endian format + assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]); + } + + #[test] + fn test_i64_as_bytes() { + let val = 0x123456789ABCDEF0i64; + let bytes = val.as_bytes(); + assert_eq!(bytes.as_ref().len(), 8); + // Check that we get the expected bytes in little-endian format + assert_eq!( + bytes.as_ref(), + &[0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12] + ); + } + + #[test] + fn test_f32_as_bytes() { + let val = 1.0f32; + let bytes = val.as_bytes(); + assert_eq!(bytes.as_ref().len(), 4); + // f32 representation of 1.0 is [0x00, 0x00, 0x80, 0x3F] in little-endian + assert_eq!(bytes.as_ref(), &[0x00, 0x00, 0x80, 0x3F]); + } + + #[test] + fn test_f64_as_bytes() { + let val = 1.0f64; + let bytes = val.as_bytes(); + assert_eq!(bytes.as_ref().len(), 8); + // f64 representation of 1.0 is [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] in little-endian + assert_eq!( + bytes.as_ref(), + &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] + ); + } + + #[test] + fn test_str_as_bytes() { + let val = "hello"; + let bytes = AsBytes::as_bytes(val); + assert_eq!(bytes.as_ref(), b"hello"); + } + + #[test] + fn test_slice_as_bytes() { + let val: &[u8] = &[1, 2, 3, 4, 5]; + let bytes = val.as_bytes(); + assert_eq!(bytes.as_ref(), &[1, 2, 3, 4, 5]); + } + + #[test] + fn test_bool_as_bytes() { + let val_true = true; + let bytes_true = val_true.as_bytes(); + assert_eq!(bytes_true.as_ref(), &[1u8]); + + let val_false = false; + let bytes_false = val_false.as_bytes(); + assert_eq!(bytes_false.as_ref(), &[0u8]); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/bloomfilter/sbbf.rs b/lance-artifact/rust/lance-core/src/utils/bloomfilter/sbbf.rs new file mode 100644 index 000000000..06df26410 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/bloomfilter/sbbf.rs @@ -0,0 +1,620 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Split Block Bloom Filter (SBBF) implementation for Lance +//! +//! Based on the Apache Arrow Parquet SBBF implementation but with public APIs +//! for use in Lance indexing. This implementation follows the Parquet spec +//! +//! for SBBF as described in +//! FIXME: Make the upstream SBBF implementation public so that this file could be +//! removed from Lance. +//! + +use super::as_bytes::AsBytes; +use libm::lgamma; +use std::error::Error; +use std::fmt; +use std::io::Write; +use twox_hash::XxHash64; + +#[derive(Debug)] +pub enum SbbfError { + InvalidFpp { fpp: f64 }, + WriteError { source: std::io::Error }, + InvalidData { message: String }, +} + +impl fmt::Display for SbbfError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidFpp { fpp } => { + write!( + f, + "False positive probability must be between 0.0 and 1.0, got {}", + fpp + ) + } + Self::WriteError { source } => { + write!(f, "Failed to write bloom filter: {}", source) + } + Self::InvalidData { message } => { + write!(f, "Invalid bloom filter data: {}", message) + } + } + } +} + +impl Error for SbbfError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self { + Self::WriteError { source } => Some(source), + _ => None, + } + } +} + +pub type Result = std::result::Result; + +/// Salt as defined in the Parquet spec +const SALT: [u32; 8] = [ + 0x47b6137b_u32, + 0x44974d91_u32, + 0x8824ad5b_u32, + 0xa2b7289d_u32, + 0x705495c7_u32, + 0x2df1424b_u32, + 0x9efc4947_u32, + 0x5c6bfb31_u32, +]; + +/// Each block is 256 bits, broken up into eight contiguous "words", each consisting of 32 bits. +/// Each word is thought of as an array of bits; each bit is either "set" or "not set". +#[derive(Debug, Copy, Clone)] +struct Block([u32; 8]); + +impl Block { + const ZERO: Self = Self([0; 8]); + + /// Takes as its argument a single unsigned 32-bit integer and returns a block in which each + /// word has exactly one bit set. + fn mask(x: u32) -> Self { + let mut result = [0_u32; 8]; + for i in 0..8 { + // wrapping instead of checking for overflow + let y = x.wrapping_mul(SALT[i]); + let y = y >> 27; + result[i] = 1 << y; + } + Self(result) + } + + #[inline] + #[cfg(target_endian = "little")] + fn to_le_bytes(self) -> [u8; 32] { + self.to_ne_bytes() + } + + #[inline] + #[cfg(not(target_endian = "little"))] + fn to_le_bytes(self) -> [u8; 32] { + self.swap_bytes().to_ne_bytes() + } + + #[inline] + fn to_ne_bytes(self) -> [u8; 32] { + // SAFETY: [u32; 8] and [u8; 32] have the same size and neither has invalid bit patterns. + unsafe { std::mem::transmute(self.0) } + } + + #[inline] + #[cfg(not(target_endian = "little"))] + fn swap_bytes(mut self) -> Self { + self.0.iter_mut().for_each(|x| *x = x.swap_bytes()); + self + } + + /// Setting every bit in the block that was also set in the result from mask + fn insert(&mut self, hash: u32) { + let mask = Self::mask(hash); + for i in 0..8 { + self[i] |= mask[i]; + } + } + + /// Returns true when every bit that is set in the result of mask is also set in the block. + fn check(&self, hash: u32) -> bool { + let mask = Self::mask(hash); + for i in 0..8 { + if self[i] & mask[i] == 0 { + return false; + } + } + true + } +} + +impl std::ops::Index for Block { + type Output = u32; + + #[inline] + fn index(&self, index: usize) -> &Self::Output { + self.0.index(index) + } +} + +impl std::ops::IndexMut for Block { + #[inline] + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + self.0.index_mut(index) + } +} + +// This implements the false positive probability in Putze et al.'s "Cache-, hash-and +// space-efficient bloom filters", equation 3. +#[inline] +fn false_positive_probability(ndv: u64, log_space_bytes: u8) -> f64 { + const WORD_BITS: f64 = 32.0; + const BUCKET_WORDS: f64 = 8.0; + let bytes = (1u64 << log_space_bytes) as f64; + let ndv = ndv as f64; + if ndv == 0.0 { + return 0.0; + } + // This short-cuts a slowly-converging sum for very dense filters + if ndv / (bytes * u8::BITS as f64) > 2.0 { + return 1.0; + } + let mut result: f64 = 0.0; + // lam is the usual parameter to the Poisson's PMF. Following the notation in the paper, + // lam is B/c, where B is the number of bits in a bucket and c is the number of bits per + // distinct value + let lam = BUCKET_WORDS * WORD_BITS / ((bytes * u8::BITS as f64) / ndv); + // Some of the calculations are done in log-space to increase numerical stability + let loglam = lam.ln(); + + // 750 iterations are sufficient to cause the sum to converge in all of the tests. In + // other words, setting the iterations higher than 750 will give the same result as + // leaving it at 750. + const ITERS: i32 = 750; + // We start with the highest value of i, since the values we're adding to result are + // mostly smaller at high i, and this increases accuracy to sum from the smallest + // values up. + for i in (0..ITERS).rev() { + // The PMF of the Poisson distribution is lam^i * exp(-lam) / i!. In logspace, using + // lgamma for the log of the factorial function: + let logp = i as f64 * loglam - lam - lgamma((i + 1).into()); + // The f_inner part of the equation in the paper is the probability of a single + // collision in the bucket. Since there are kBucketWords non-overlapping lanes in each + // bucket, the log of this probability is: + let logfinner = BUCKET_WORDS * (1.0 - (1.0 - 1.0 / WORD_BITS).powi(i)).ln(); + // Here we are forced out of log-space calculations + result += (logp + logfinner).exp(); + } + result.min(1.0) +} + +/// Minimum and maximum filter sizes +const BITSET_LOG2_MIN_BYTES: u8 = 5; // 32B (1 Block) +const BITSET_LOG2_MAX_BYTES: u8 = 27; // 128MiB + +#[inline] +fn min_log2_bytes(ndv: u64, fpp: f64) -> u8 { + let mut low = 0; + let mut high = 64; + while high > low + 1 { + let mid = (high + low) / 2; + let candidate = false_positive_probability(ndv, mid); + if candidate <= fpp { + high = mid; + } else { + low = mid; + } + } + high +} + +/// A Split Block Bloom Filter (SBBF) implementation +/// +/// This is a high-performance bloom filter optimized for SIMD operations, +/// compatible with the Parquet specification. +#[derive(Debug, Clone)] +pub struct Sbbf { + blocks: Vec, +} + +impl Sbbf { + /// Create a new SBBF from raw bitset data + pub fn new(bitset: &[u8]) -> Result { + if !bitset.len().is_multiple_of(32) { + return Err(SbbfError::InvalidData { + message: format!( + "Bitset length must be a multiple of 32, got {}", + bitset.len() + ), + }); + } + + let data = bitset + .chunks_exact(4 * 8) + .map(|chunk| { + let mut block = Block::ZERO; + for (i, word) in chunk.chunks_exact(4).enumerate() { + block[i] = u32::from_le_bytes(word.try_into().unwrap()); + } + block + }) + .collect::>(); + + Ok(Self { blocks: data }) + } + + /// Create a new empty SBBF with the given number of bytes + /// The actual size will be adjusted to the next power of two within bounds + pub fn with_log2_num_bytes(log2_num_bytes: u8) -> Self { + let num_bytes = + 1_usize << log2_num_bytes.clamp(BITSET_LOG2_MIN_BYTES, BITSET_LOG2_MAX_BYTES); + let bitset = vec![0_u8; num_bytes]; + // unwrap is safe because we know the size is valid + Self::new(&bitset).unwrap() + } + + /// Create a new SBBF with given number of distinct values and false positive probability + pub fn with_ndv_fpp(ndv: u64, fpp: f64) -> Result { + if !(0.0..1.0).contains(&fpp) { + return Err(SbbfError::InvalidFpp { fpp }); + } + let log2_num_bytes = min_log2_bytes(ndv, fpp); + Ok(Self::with_log2_num_bytes(log2_num_bytes)) + } + + /// Get the hash-to-block-index for a given hash + #[inline] + fn hash_to_block_index(&self, hash: u64) -> usize { + (((hash >> 32).saturating_mul(self.blocks.len() as u64)) >> 32) as usize + } + + /// Insert an AsBytes value into the filter + pub fn insert(&mut self, value: &T) { + self.insert_hash(hash_as_bytes(value)); + } + + /// Insert a hash into the filter + pub fn insert_hash(&mut self, hash: u64) { + let block_index = self.hash_to_block_index(hash); + self.blocks[block_index].insert(hash as u32) + } + + /// Check if an AsBytes value is probably present or definitely absent in the filter + pub fn check(&self, value: &T) -> bool { + self.check_hash(hash_as_bytes(value)) + } + + /// Check if a hash is in the filter. May return + /// true for values that were never inserted ("false positive") + /// but will always return false if a hash has not been inserted. + pub fn check_hash(&self, hash: u64) -> bool { + let block_index = self.hash_to_block_index(hash); + self.blocks[block_index].check(hash as u32) + } + + /// Write the bitset in serialized form to the writer + pub fn write_bitset(&self, mut writer: W) -> Result<()> { + for block in &self.blocks { + writer + .write_all(block.to_le_bytes().as_slice()) + .map_err(|source| SbbfError::WriteError { source })?; + } + Ok(()) + } + + /// Get the raw bitset as bytes + pub fn to_bytes(&self) -> Vec { + let mut result = Vec::with_capacity(self.blocks.len() * 32); + for block in &self.blocks { + result.extend_from_slice(&block.to_le_bytes()); + } + result + } + + /// Get the number of blocks in this filter + pub fn num_blocks(&self) -> usize { + self.blocks.len() + } + + /// Get the size in bytes of this filter + pub fn size_bytes(&self) -> usize { + self.blocks.len() * 32 + } + + /// Return the total in memory size of this bloom filter in bytes + pub fn estimated_memory_size(&self) -> usize { + self.blocks.capacity() * std::mem::size_of::() + } + + /// Check if this filter might intersect with another filter. + /// Returns true if there's at least one bit position where both filters have a 1. + /// This is a fast check that may return false positives but never false negatives. + /// + /// Returns an error if the filters have different sizes, as bloom filters with + /// different configurations cannot be reliably compared. + pub fn might_intersect(&self, other: &Self) -> Result { + if self.blocks.len() != other.blocks.len() { + return Err(SbbfError::InvalidData { + message: format!( + "Cannot compare bloom filters with different sizes: {} blocks vs {} blocks. \ + Both filters must use the same configuration.", + self.blocks.len(), + other.blocks.len() + ), + }); + } + for i in 0..self.blocks.len() { + for j in 0..8 { + if (self.blocks[i][j] & other.blocks[i][j]) != 0 { + return Ok(true); + } + } + } + Ok(false) + } + + /// Check if this filter might intersect with a raw bitmap. + /// The bitmap should be in the same format as produced by to_bytes(). + /// + /// Returns an error if the bitmaps have different sizes, as bloom filters with + /// different configurations cannot be reliably compared. + pub fn might_intersect_bytes(&self, other_bytes: &[u8]) -> Result { + Self::bytes_might_intersect(&self.to_bytes(), other_bytes) + } + + /// Check if two raw bloom filter bitmaps might intersect. + /// Returns true if there's at least one bit position where both filters have a 1. + /// + /// This is a fast probabilistic check: if it returns false, the filters definitely + /// have no common elements. If it returns true, they might have common elements + /// (with possible false positives). + /// + /// Returns an error if the bitmaps have different sizes, as bloom filters with + /// different configurations cannot be reliably compared. + pub fn bytes_might_intersect(a: &[u8], b: &[u8]) -> Result { + if a.len() != b.len() { + return Err(SbbfError::InvalidData { + message: format!( + "Cannot compare bloom filters with different sizes: {} bytes vs {} bytes. \ + Both filters must use the same configuration.", + a.len(), + b.len() + ), + }); + } + for i in 0..a.len() { + if (a[i] & b[i]) != 0 { + return Ok(true); + } + } + Ok(false) + } +} + +// Per spec we use xxHash with seed=0 +const SEED: u64 = 0; + +#[inline] +fn hash_as_bytes(value: &A) -> u64 { + XxHash64::oneshot(SEED, value.as_bytes().as_ref()) +} + +/// Builder for creating SBBF instances with a fluent API +pub struct SbbfBuilder { + ndv: Option, + fpp: Option, + log2_num_bytes: Option, +} + +impl SbbfBuilder { + /// Create a new SBBF builder + pub fn new() -> Self { + Self { + ndv: None, + fpp: None, + log2_num_bytes: None, + } + } + + /// Set the expected number of distinct values + pub fn expected_items(mut self, ndv: u64) -> Self { + self.ndv = Some(ndv); + self + } + + /// Set the desired false positive probability + pub fn false_positive_probability(mut self, fpp: f64) -> Self { + self.fpp = Some(fpp); + self + } + + /// Set the number of bytes directly + pub fn log2_num_bytes(mut self, log2_num_bytes: u8) -> Self { + self.log2_num_bytes = Some(log2_num_bytes); + self + } + + /// Build the SBBF + pub fn build(self) -> Result { + if let Some(log2_num_bytes) = self.log2_num_bytes { + Ok(Sbbf::with_log2_num_bytes(log2_num_bytes)) + } else if let (Some(ndv), Some(fpp)) = (self.ndv, self.fpp) { + Sbbf::with_ndv_fpp(ndv, fpp) + } else { + Err(SbbfError::InvalidData { + message: "Must specify either log2_num_bytes or both ndv and fpp".to_string(), + }) + } + } +} + +impl Default for SbbfBuilder { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hash_bytes() { + assert_eq!(hash_as_bytes(""), 17241709254077376921); + } + + #[test] + fn test_mask_set_quick_check() { + for i in 0..1_000 { + let result = Block::mask(i); + assert!(result.0.iter().all(|&x| x.is_power_of_two())); + } + } + + #[test] + fn test_block_insert_and_check() { + for i in 0..1_000 { + let mut block = Block::ZERO; + block.insert(i); + assert!(block.check(i)); + } + } + + #[test] + fn test_sbbf_insert_and_check() { + let mut sbbf = Sbbf::with_log2_num_bytes(10); + for i in 0..1_000 { + sbbf.insert(&i); + assert!(sbbf.check(&i)); + } + } + + #[test] + fn test_sbbf_builder() { + let sbbf = SbbfBuilder::new() + .expected_items(1000) + .false_positive_probability(0.01) + .build() + .unwrap(); + + assert!(sbbf.num_blocks() > 0); + } + + #[test] + fn test_sbbf_string_types() { + let mut sbbf = SbbfBuilder::new() + .expected_items(100) + .false_positive_probability(0.01) + .build() + .unwrap(); + + // Test different string types + let string_val = "hello"; + let str_val = "world"; + let bytes_val = b"bytes"; + + sbbf.insert(string_val); + sbbf.insert(str_val); + sbbf.insert(&bytes_val[..]); + + assert!(sbbf.check(string_val)); + assert!(sbbf.check(str_val)); + assert!(sbbf.check(&bytes_val[..])); + assert!(!sbbf.check("not_inserted")); + } + + #[test] + fn test_sbbf_numeric_types() { + let mut sbbf = SbbfBuilder::new() + .expected_items(100) + .false_positive_probability(0.01) + .build() + .unwrap(); + + // Test different numeric types + let i32_val = 42i32; + let i64_val = 12345i64; + let f64_val = std::f64::consts::PI; + let bool_val = true; + + sbbf.insert(&i32_val); + sbbf.insert(&i64_val); + sbbf.insert(&f64_val); + sbbf.insert(&bool_val); + + assert!(sbbf.check(&i32_val)); + assert!(sbbf.check(&i64_val)); + assert!(sbbf.check(&f64_val)); + assert!(sbbf.check(&bool_val)); + assert!(!sbbf.check(&999i32)); + } + + #[test] + fn test_num_of_bits_from_ndv_fpp() { + for (fpp, ndv, log2_num_bytes) in &[ + (0.1, 10, 3), + (0.01, 10, 4), + (0.001, 10, 5), + (0.1, 100, 7), + (0.01, 100, 8), + (0.001, 100, 8), + (0.1, 1000, 10), + (0.01, 1000, 11), + (0.001, 1000, 12), + ] { + assert_eq!(*log2_num_bytes, min_log2_bytes(*ndv, *fpp)); + } + } + + #[test] + fn test_serialization() { + let mut sbbf = SbbfBuilder::new() + .expected_items(100) + .false_positive_probability(0.01) + .build() + .unwrap(); + + // Insert some values + for i in 0..50 { + sbbf.insert(&i); + } + + // Serialize to bytes + let bytes = sbbf.to_bytes(); + assert!(!bytes.is_empty()); + assert_eq!(bytes.len(), sbbf.size_bytes()); + + // Deserialize from bytes + let sbbf2 = Sbbf::new(&bytes).unwrap(); + assert_eq!(sbbf.num_blocks(), sbbf2.num_blocks()); + + // Check that deserialized filter works + for i in 0..50 { + assert!(sbbf2.check(&i)); + } + assert!(!sbbf2.check(&999)); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/cpu.rs b/lance-artifact/rust/lance-core/src/utils/cpu.rs new file mode 100644 index 000000000..c4d5a976c --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/cpu.rs @@ -0,0 +1,357 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::fmt; +use std::sync::LazyLock; + +/// A level of SIMD support for some feature. +/// +/// `#[non_exhaustive]` so future tiers (e.g. AVX-512 BF16, AMX) can be added +/// without breaking external `match` consumers. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum SimdSupport { + None, + Neon, + Sse, + /// AVX (256-bit float ops) but no FMA and no AVX2. + /// Intel Sandy Bridge / Ivy Bridge. + Avx, + /// AVX + FMA but no AVX2. + /// AMD Piledriver / Steamroller / FX-7500. + AvxFma, + /// AVX2 + FMA. Intel Haswell / AMD Excavator and later. + /// + /// Selecting this tier asserts FMA is present: the kernels it dispatches to + /// are `#[target_feature(enable = "avx,fma")]`. + Avx2, + Avx512, + Avx512FP16, + Lsx, + Lasx, +} + +impl fmt::Display for SimdSupport { + /// Formats the tier name in lowercase, matching pyarrow's + /// `runtime_info().simd_level` convention. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let name = match self { + Self::None => "none", + Self::Neon => "neon", + Self::Sse => "sse", + Self::Avx => "avx", + Self::AvxFma => "avx_fma", + Self::Avx2 => "avx2", + Self::Avx512 => "avx512", + Self::Avx512FP16 => "avx512_fp16", + Self::Lsx => "lsx", + Self::Lasx => "lasx", + }; + f.write_str(name) + } +} + +/// Snapshot of the SIMD tier lance dispatches to on the current host, plus the +/// raw CPU features detected for diagnostic purposes. +/// +/// Mirrors the role of `pyarrow.runtime_info()`: a single, cheap call users can +/// make to verify which SIMD tier the runtime selected and what underlying +/// features the host advertises. Obtain one with [`simd_info()`]. +#[derive(Debug, Clone)] +pub struct SimdInfo { + /// The SIMD tier lance dispatches to at runtime on this host. + pub tier: SimdSupport, + /// The architecture name (e.g. "x86_64", "aarch64", "loongarch64"). + pub target_arch: &'static str, + /// Raw CPU feature flags detected on this host (x86_64 only; empty on + /// other architectures). Each entry is a feature name like "avx2", + /// "fma", "avx512f", "popcnt", etc. + pub host_features: Vec<&'static str>, +} + +/// Returns a snapshot of the SIMD tier lance is using on this host along with +/// the raw CPU feature flags that drove the decision. +/// +/// Useful for performance debugging and giving users a way to verify which +/// dispatch tier they are hitting without rebuilding lance. See [`SimdInfo`] +/// for the meaning of each field and [`SimdSupport`] for the tier values. +/// +/// # Examples +/// +/// ``` +/// use lance_core::utils::cpu::simd_info; +/// +/// let info = simd_info(); +/// println!("dispatching to {} on {}", info.tier, info.target_arch); +/// ``` +pub fn simd_info() -> SimdInfo { + SimdInfo { + tier: *SIMD_SUPPORT, + target_arch: std::env::consts::ARCH, + host_features: detect_host_features(), + } +} + +#[cfg(target_arch = "x86_64")] +fn detect_host_features() -> Vec<&'static str> { + // Each call must be inline: `is_x86_feature_detected!` does its own custom + // input parsing and rejects feature names received via a `macro_rules!` + // `:literal` metavariable on some toolchains. + let mut features = Vec::with_capacity(17); + if is_x86_feature_detected!("sse2") { + features.push("sse2"); + } + if is_x86_feature_detected!("sse3") { + features.push("sse3"); + } + if is_x86_feature_detected!("ssse3") { + features.push("ssse3"); + } + if is_x86_feature_detected!("sse4.1") { + features.push("sse4.1"); + } + if is_x86_feature_detected!("sse4.2") { + features.push("sse4.2"); + } + if is_x86_feature_detected!("popcnt") { + features.push("popcnt"); + } + if is_x86_feature_detected!("avx") { + features.push("avx"); + } + if is_x86_feature_detected!("avx2") { + features.push("avx2"); + } + if is_x86_feature_detected!("fma") { + features.push("fma"); + } + if is_x86_feature_detected!("f16c") { + features.push("f16c"); + } + if is_x86_feature_detected!("bmi1") { + features.push("bmi1"); + } + if is_x86_feature_detected!("bmi2") { + features.push("bmi2"); + } + if is_x86_feature_detected!("avx512f") { + features.push("avx512f"); + } + if is_x86_feature_detected!("avx512bw") { + features.push("avx512bw"); + } + if is_x86_feature_detected!("avx512cd") { + features.push("avx512cd"); + } + if is_x86_feature_detected!("avx512dq") { + features.push("avx512dq"); + } + if is_x86_feature_detected!("avx512vl") { + features.push("avx512vl"); + } + features +} + +#[cfg(not(target_arch = "x86_64"))] +fn detect_host_features() -> Vec<&'static str> { + Vec::new() +} + +/// Support for SIMD operations +pub static SIMD_SUPPORT: LazyLock = LazyLock::new(|| { + #[cfg(all(target_arch = "aarch64", any(target_os = "ios", target_os = "tvos")))] + { + // AArch64 iOS/tvOS has NEON; fp16 arithmetic is available on modern targets. + SimdSupport::Neon + } + #[cfg(all( + target_arch = "aarch64", + not(any(target_os = "ios", target_os = "tvos")) + ))] + { + if aarch64::has_neon_f16_support() { + SimdSupport::Neon + } else { + SimdSupport::None + } + } + #[cfg(target_arch = "x86_64")] + { + if x86::has_avx512() { + if x86::has_avx512_f16_support() { + SimdSupport::Avx512FP16 + } else { + SimdSupport::Avx512 + } + } else if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("fma") { + // FMA is checked explicitly: every kernel selected for this tier is + // `#[target_feature(enable = "avx,fma")]`, and AVX2 does not imply + // FMA in the ISA. Every shipping AVX2 part has FMA, so this only + // guards against a host that would otherwise take an FMA kernel + // without FMA. + SimdSupport::Avx2 + } else if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { + // AMD Piledriver / Steamroller / FX-7500: 256-bit float ops + FMA but no AVX2. + SimdSupport::AvxFma + } else if is_x86_feature_detected!("avx") { + // Intel Sandy Bridge / Ivy Bridge: 256-bit float ops without FMA. + SimdSupport::Avx + } else { + SimdSupport::None + } + } + #[cfg(target_arch = "loongarch64")] + { + if loongarch64::has_lasx_support() { + SimdSupport::Lasx + } else if loongarch64::has_lsx_support() { + SimdSupport::Lsx + } else { + SimdSupport::None + } + } +}); + +#[cfg(target_arch = "x86_64")] +mod x86 { + use core::arch::x86_64::__cpuid; + + #[inline] + fn check_flag(x: usize, position: u32) -> bool { + x & (1 << position) != 0 + } + + pub fn has_avx512_f16_support() -> bool { + // this macro does many OS checks/etc. to determine if allowed to use AVX512 + if !has_avx512() { + return false; + } + + // EAX=7, ECX=0: Extended Features (includes AVX512) + // More info on calling CPUID can be found here (section 1.4) + // https://www.intel.com/content/dam/develop/external/us/en/documents/architecture-instruction-set-extensions-programming-reference.pdf + // __cpuid is safe in nightly but unsafe in stable, allow both + #[allow(unused_unsafe)] + let ext_cpuid_result = unsafe { __cpuid(7) }; + check_flag(ext_cpuid_result.edx as usize, 23) + } + + pub fn has_avx512() -> bool { + is_x86_feature_detected!("avx512f") + } +} + +// Inspired by https://github.com/RustCrypto/utils/blob/master/cpufeatures/src/aarch64.rs +// aarch64 doesn't have userspace feature detection built in, so we have to call +// into OS-specific functions to check for features. + +#[cfg(all(target_arch = "aarch64", target_os = "macos"))] +mod aarch64 { + pub fn has_neon_f16_support() -> bool { + // Maybe we can assume it's there? + true + } +} + +#[cfg(all(target_arch = "aarch64", target_os = "linux"))] +mod aarch64 { + pub fn has_neon_f16_support() -> bool { + // See: https://github.com/rust-lang/libc/blob/7ce81ca7aeb56aae7ca0237ef9353d58f3d7d2f1/src/unix/linux_like/linux/gnu/b64/aarch64/mod.rs#L533 + let flags = unsafe { libc::getauxval(libc::AT_HWCAP) }; + flags & libc::HWCAP_FPHP != 0 + } +} + +#[cfg(all(target_arch = "aarch64", target_os = "windows"))] +mod aarch64 { + pub fn has_neon_f16_support() -> bool { + // https://github.com/lance-format/lance/issues/2411 + false + } +} + +#[cfg(target_arch = "loongarch64")] +mod loongarch64 { + pub fn has_lsx_support() -> bool { + // See: https://github.com/rust-lang/libc/blob/7ce81ca7aeb56aae7ca0237ef9353d58f3d7d2f1/src/unix/linux_like/linux/gnu/b64/loongarch64/mod.rs#L263 + let flags = unsafe { libc::getauxval(libc::AT_HWCAP) }; + flags & libc::HWCAP_LOONGARCH_LSX != 0 + } + pub fn has_lasx_support() -> bool { + // See: https://github.com/rust-lang/libc/blob/7ce81ca7aeb56aae7ca0237ef9353d58f3d7d2f1/src/unix/linux_like/linux/gnu/b64/loongarch64/mod.rs#L264 + let flags = unsafe { libc::getauxval(libc::AT_HWCAP) }; + flags & libc::HWCAP_LOONGARCH_LASX != 0 + } +} + +#[cfg(all(target_arch = "aarch64", target_os = "android"))] +mod aarch64 { + pub fn has_neon_f16_support() -> bool { + false + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rstest::rstest; + + #[test] + fn simd_info_exposes_tier() { + let info = simd_info(); + assert_eq!(info.target_arch, std::env::consts::ARCH); + // Tier should match the detected SIMD support. + assert_eq!(info.tier, *SIMD_SUPPORT); + } + + #[cfg(target_arch = "x86_64")] + #[test] + fn simd_info_features_include_baseline() { + let info = simd_info(); + // The x86_64 ABI mandates SSE2, so it must always be present on this + // architecture. + assert!(info.host_features.contains(&"sse2")); + } + + #[cfg(not(target_arch = "x86_64"))] + #[test] + fn simd_info_features_empty_off_x86_64() { + let info = simd_info(); + assert!(info.host_features.is_empty()); + } + + /// The `Avx2` and `AvxFma` tiers both dispatch to kernels declared + /// `#[target_feature(enable = "avx,fma")]`, so neither may be selected on a + /// host without FMA. AVX2 does not imply FMA in the ISA, so the detection + /// checks it explicitly. (`Avx512*` is excluded: its kernels declare + /// `avx512f`, which is what `has_avx512` verifies.) + #[cfg(target_arch = "x86_64")] + #[test] + fn avx_fma_tiers_are_only_selected_when_fma_is_detected() { + if matches!(*SIMD_SUPPORT, SimdSupport::Avx2 | SimdSupport::AvxFma) { + assert!( + is_x86_feature_detected!("fma"), + "tier {} dispatches to avx,fma kernels but the host has no FMA", + *SIMD_SUPPORT + ); + } + } + + #[rstest] + #[case::none(SimdSupport::None, "none")] + #[case::neon(SimdSupport::Neon, "neon")] + #[case::sse(SimdSupport::Sse, "sse")] + #[case::avx(SimdSupport::Avx, "avx")] + #[case::avx_fma(SimdSupport::AvxFma, "avx_fma")] + #[case::avx2(SimdSupport::Avx2, "avx2")] + #[case::avx512(SimdSupport::Avx512, "avx512")] + #[case::avx512_fp16(SimdSupport::Avx512FP16, "avx512_fp16")] + #[case::lsx(SimdSupport::Lsx, "lsx")] + #[case::lasx(SimdSupport::Lasx, "lasx")] + fn simd_support_display_matches_lowercase_convention( + #[case] tier: SimdSupport, + #[case] expected: &str, + ) { + assert_eq!(tier.to_string(), expected); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/deletion.rs b/lance-artifact/rust/lance-core/src/utils/deletion.rs new file mode 100644 index 000000000..c7f8b1424 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/deletion.rs @@ -0,0 +1,522 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashSet, ops::Range, sync::Arc}; + +use crate::deepsize::{Context, DeepSizeOf}; +use arrow_array::BooleanArray; +use roaring::RoaringBitmap; + +/// Threshold for when a DeletionVector::Set should be promoted to a DeletionVector::Bitmap. +const BITMAP_THRESDHOLD: usize = 5_000; +// TODO: Benchmark to find a better value. + +/// Represents a set of deleted row offsets in a single fragment. +#[derive(Debug, Clone, Default)] +pub enum DeletionVector { + #[default] + NoDeletions, + Set(HashSet), + Bitmap(RoaringBitmap), +} + +impl DeepSizeOf for DeletionVector { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + match self { + Self::NoDeletions => 0, + Self::Set(set) => set.deep_size_of_children(context), + // Inexact but probably close enough + Self::Bitmap(bitmap) => bitmap.serialized_size(), + } + } +} + +impl DeletionVector { + pub fn len(&self) -> usize { + match self { + Self::NoDeletions => 0, + Self::Set(set) => set.len(), + Self::Bitmap(bitmap) => bitmap.len() as usize, + } + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn contains(&self, i: u32) -> bool { + match self { + Self::NoDeletions => false, + Self::Set(set) => set.contains(&i), + Self::Bitmap(bitmap) => bitmap.contains(i), + } + } + + pub fn contains_range(&self, mut range: Range) -> bool { + match self { + Self::NoDeletions => range.is_empty(), + Self::Set(set) => range.all(|i| set.contains(&i)), + Self::Bitmap(bitmap) => bitmap.contains_range(range), + } + } + + fn range_cardinality(&self, range: Range) -> u64 { + match self { + Self::NoDeletions => 0, + Self::Set(set) => range.fold(0, |acc, i| acc + set.contains(&i) as u64), + Self::Bitmap(bitmap) => bitmap.range_cardinality(range), + } + } + + pub fn iter(&self) -> Box + Send + '_> { + match self { + Self::NoDeletions => Box::new(std::iter::empty()), + Self::Set(set) => Box::new(set.iter().copied()), + Self::Bitmap(bitmap) => Box::new(bitmap.iter()), + } + } + + pub fn into_sorted_iter(self) -> Box + Send + 'static> { + match self { + Self::NoDeletions => Box::new(std::iter::empty()), + Self::Set(set) => { + // If we're using a set we shouldn't have too many values + // and so this conversion should be affordable. + let mut values = Vec::from_iter(set); + values.sort(); + Box::new(values.into_iter()) + } + // Bitmaps always iterate in sorted order + Self::Bitmap(bitmap) => Box::new(bitmap.into_iter()), + } + } + + /// Create an iterator that iterates over the values in the deletion vector in sorted order. + pub fn to_sorted_iter<'a>(&'a self) -> Box + Send + 'a> { + match self { + Self::NoDeletions => Box::new(std::iter::empty()), + // We have to make a clone when we're using a set + // but sets should be relatively small. + Self::Set(_) => self.clone().into_sorted_iter(), + Self::Bitmap(bitmap) => Box::new(bitmap.iter()), + } + } + + // Note: deletion vectors are based on 32-bit offsets. However, this function works + // even when given 64-bit row addresses. That is because `id as u32` returns the lower + // 32 bits (the row offset) and the upper 32 bits are ignored. + pub fn build_predicate(&self, row_addrs: std::slice::Iter) -> Option { + match self { + Self::Bitmap(bitmap) => Some( + row_addrs + .map(|&id| !bitmap.contains(id as u32)) + .collect::>(), + ), + Self::Set(set) => Some( + row_addrs + .map(|&id| !set.contains(&(id as u32))) + .collect::>(), + ), + Self::NoDeletions => None, + } + .map(BooleanArray::from) + } +} + +/// Maps a naive offset into a fragment to the local row offset that is +/// not deleted. +/// +/// For example, if the deletion vector is [0, 1, 2], then the mapping +/// would be: +/// +/// - 0 -> 3 +/// - 1 -> 4 +/// - 2 -> 5 +/// +/// and so on. +/// +/// This expects a monotonically increasing sequence of input offsets. State +/// is re-used between calls to `map_offset` to make the mapping more efficient. +pub struct OffsetMapper { + dv: Arc, + left: u32, + last_diff: u32, +} + +impl OffsetMapper { + pub fn new(dv: Arc) -> Self { + Self { + dv, + left: 0, + last_diff: 0, + } + } + + pub fn map_offset(&mut self, offset: u32) -> u32 { + // The best initial guess is the offset + last diff. That's the right + // answer if there are no deletions in the range between the last + // offset and the current one. + let mut mid = offset + self.last_diff; + let mut right = offset + self.dv.len() as u32; + loop { + let deleted_in_range = self.dv.range_cardinality(0..(mid + 1)) as u32; + match mid.cmp(&(offset + deleted_in_range)) { + std::cmp::Ordering::Equal if !self.dv.contains(mid) => { + self.last_diff = mid - offset; + return mid; + } + std::cmp::Ordering::Less => { + assert_ne!(self.left, mid + 1); + self.left = mid + 1; + mid = self.left + (right - self.left) / 2; + } + // Binary search left when the guess overshoots. This can happen when: + // - Greater: last_diff was calibrated for a denser deletion region + // - Equal with deleted mid: the guess lands exactly on a deleted row + std::cmp::Ordering::Greater | std::cmp::Ordering::Equal => { + right = mid; + mid = self.left + (right - self.left) / 2; + } + } + } + } +} + +impl From<&DeletionVector> for RoaringBitmap { + fn from(value: &DeletionVector) -> Self { + match value { + DeletionVector::Bitmap(bitmap) => bitmap.clone(), + DeletionVector::Set(set) => Self::from_iter(set.iter()), + DeletionVector::NoDeletions => Self::new(), + } + } +} + +impl PartialEq for DeletionVector { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::NoDeletions, Self::NoDeletions) => true, + (Self::Set(set1), Self::Set(set2)) => set1 == set2, + (Self::Bitmap(bitmap1), Self::Bitmap(bitmap2)) => bitmap1 == bitmap2, + (Self::Set(set), Self::Bitmap(bitmap)) | (Self::Bitmap(bitmap), Self::Set(set)) => { + let set = set.iter().copied().collect::(); + set == *bitmap + } + _ => false, + } + } +} + +impl Extend for DeletionVector { + fn extend>(&mut self, iter: T) { + let iter = iter.into_iter(); + // The mem::replace allows changing the variant of Self when we only + // have &mut Self. + *self = match (std::mem::take(self), iter.size_hint()) { + (Self::NoDeletions, (_, Some(0))) => Self::NoDeletions, + (Self::NoDeletions, (lower, _)) if lower >= BITMAP_THRESDHOLD => { + let bitmap = iter.collect::(); + Self::Bitmap(bitmap) + } + (Self::NoDeletions, (_, Some(upper))) if upper < BITMAP_THRESDHOLD => { + let set = iter.collect::>(); + Self::Set(set) + } + (Self::NoDeletions, _) => { + // We don't know the size, so just try as a set and move to bitmap + // if it ends up being big. + let set = iter.collect::>(); + if set.len() > BITMAP_THRESDHOLD { + let bitmap = set.into_iter().collect::(); + Self::Bitmap(bitmap) + } else { + Self::Set(set) + } + } + (Self::Set(mut set), _) => { + set.extend(iter); + if set.len() > BITMAP_THRESDHOLD { + let bitmap = set.drain().collect::(); + Self::Bitmap(bitmap) + } else { + Self::Set(set) + } + } + (Self::Bitmap(mut bitmap), _) => { + bitmap.extend(iter); + Self::Bitmap(bitmap) + } + }; + } +} + +// TODO: impl methods for DeletionVector +/// impl DeletionVector { +/// pub fn get(i: u32) -> bool { ... } +/// } +/// impl BitAnd for DeletionVector { ... } +impl IntoIterator for DeletionVector { + type IntoIter = Box + Send>; + type Item = u32; + + fn into_iter(self) -> Self::IntoIter { + match self { + Self::NoDeletions => Box::new(std::iter::empty()), + Self::Set(set) => { + // In many cases, it's much better if this is sorted. It's + // guaranteed to be small, so the cost is low. + let mut sorted = set.into_iter().collect::>(); + sorted.sort(); + Box::new(sorted.into_iter()) + } + Self::Bitmap(bitmap) => Box::new(bitmap.into_iter()), + } + } +} + +impl FromIterator for DeletionVector { + fn from_iter>(iter: T) -> Self { + let mut deletion_vector = Self::default(); + deletion_vector.extend(iter); + deletion_vector + } +} + +impl From for DeletionVector { + fn from(bitmap: RoaringBitmap) -> Self { + if bitmap.is_empty() { + Self::NoDeletions + } else { + Self::Bitmap(bitmap) + } + } +} + +#[cfg(test)] +#[cfg_attr(coverage, coverage(off))] +mod test { + use super::*; + use crate::deepsize::DeepSizeOf; + use rstest::rstest; + + fn set_dv(vals: impl IntoIterator) -> DeletionVector { + DeletionVector::Set(HashSet::from_iter(vals)) + } + fn bitmap_dv(vals: impl IntoIterator) -> DeletionVector { + DeletionVector::Bitmap(RoaringBitmap::from_iter(vals)) + } + + #[test] + fn test_set_bitmap_equality() { + assert_eq!(set_dv(0..100), bitmap_dv(0..100)); + } + + #[test] + fn test_threshold_promotes_to_bitmap() { + let dv = DeletionVector::from_iter(0..(BITMAP_THRESDHOLD as u32)); + assert!(matches!(dv, DeletionVector::Bitmap(_))); + } + + #[rstest] + #[case::middle_deletions(&[3, 5], &[0, 1, 2, 4, 6, 7, 8])] + #[case::start_deletions(&[0, 1, 2], &[3, 4, 5, 6, 7, 8, 9])] + fn test_map_offsets(#[case] deleted: &[u32], #[case] expected: &[u32]) { + let dv = DeletionVector::from_iter(deleted.iter().copied()); + let mut mapper = OffsetMapper::new(Arc::new(dv)); + let output: Vec<_> = (0..expected.len() as u32) + .map(|o| mapper.map_offset(o)) + .collect(); + assert_eq!(output, expected); + } + + #[test] + fn test_deep_size_of() { + assert_eq!( + DeletionVector::NoDeletions.deep_size_of(), + std::mem::size_of::() + ); + assert!(set_dv([1, 2, 3]).deep_size_of() > std::mem::size_of::()); + assert!(bitmap_dv([1, 2, 3]).deep_size_of() > std::mem::size_of::()); + } + + #[rstest] + #[case::no_deletions(DeletionVector::NoDeletions, 0, true)] + #[case::set(set_dv([1, 2, 3]), 3, false)] + #[case::bitmap(bitmap_dv([1, 2, 3, 4, 5]), 5, false)] + fn test_len_is_empty(#[case] dv: DeletionVector, #[case] len: usize, #[case] empty: bool) { + assert_eq!(dv.len(), len); + assert_eq!(dv.is_empty(), empty); + } + + #[rstest] + #[case::no_deletions(DeletionVector::NoDeletions, 1, false)] + #[case::set_contains(set_dv([1, 2, 3]), 1, true)] + #[case::set_missing(set_dv([1, 2, 3]), 0, false)] + #[case::bitmap_contains(bitmap_dv([10, 20, 30]), 10, true)] + #[case::bitmap_missing(bitmap_dv([10, 20, 30]), 5, false)] + fn test_contains(#[case] dv: DeletionVector, #[case] val: u32, #[case] expected: bool) { + assert_eq!(dv.contains(val), expected); + } + + #[rstest] + #[case::no_del_empty_range(DeletionVector::NoDeletions, 0..0, true)] + #[case::no_del_non_empty(DeletionVector::NoDeletions, 0..1, false)] + #[case::set_full_range(set_dv([1, 2, 3]), 1..4, true)] + #[case::set_partial(set_dv([1, 2, 3]), 0..2, false)] + #[case::bitmap_full(bitmap_dv([10, 11, 12]), 10..13, true)] + #[case::bitmap_partial(bitmap_dv([10, 11, 12]), 9..11, false)] + fn test_contains_range( + #[case] dv: DeletionVector, + #[case] range: std::ops::Range, + #[case] expected: bool, + ) { + assert_eq!(dv.contains_range(range), expected); + } + + #[test] + fn test_range_cardinality() { + assert_eq!(DeletionVector::NoDeletions.range_cardinality(0..100), 0); + let bm = bitmap_dv([5, 10, 15]); + assert_eq!(bm.range_cardinality(0..20), 3); + assert_eq!(bm.range_cardinality(6..14), 1); + } + + #[rstest] + #[case::no_deletions(DeletionVector::NoDeletions, vec![])] + #[case::set(set_dv([3, 1, 2]), vec![1, 2, 3])] + #[case::bitmap(bitmap_dv([30, 10, 20]), vec![10, 20, 30])] + fn test_iterators(#[case] dv: DeletionVector, #[case] expected: Vec) { + // Test iter() + let mut items: Vec<_> = dv.iter().collect(); + items.sort(); + assert_eq!(items, expected); + + // Test to_sorted_iter() + assert_eq!(dv.to_sorted_iter().collect::>(), expected); + + // Test into_sorted_iter() and into_iter() (both consume, so clone first) + assert_eq!(dv.clone().into_sorted_iter().collect::>(), expected); + assert_eq!(dv.into_iter().collect::>(), expected); + } + + #[test] + fn test_build_predicate() { + let addrs = [0u64, 1, 2, 3, 4]; + assert!( + DeletionVector::NoDeletions + .build_predicate(addrs.iter()) + .is_none() + ); + + let pred = set_dv([1, 3]).build_predicate(addrs.iter()).unwrap(); + assert_eq!( + pred.iter().map(|v| v.unwrap()).collect::>(), + [true, false, true, false, true] + ); + + let pred = bitmap_dv([0, 2, 4]).build_predicate(addrs.iter()).unwrap(); + assert_eq!( + pred.iter().map(|v| v.unwrap()).collect::>(), + [false, true, false, true, false] + ); + } + + #[rstest] + #[case::no_deletions(DeletionVector::NoDeletions, 0)] + #[case::set(set_dv([1, 2, 3]), 3)] + #[case::bitmap(bitmap_dv([10, 20]), 2)] + fn test_to_roaring(#[case] dv: DeletionVector, #[case] len: u64) { + let bitmap: RoaringBitmap = (&dv).into(); + assert_eq!(bitmap.len(), len); + } + + #[test] + fn test_partial_eq() { + assert_eq!(DeletionVector::NoDeletions, DeletionVector::NoDeletions); + assert_eq!(set_dv([1, 2, 3]), set_dv([1, 2, 3])); + assert_eq!(bitmap_dv([1, 2, 3]), bitmap_dv([1, 2, 3])); + assert_eq!(set_dv([5, 6, 7]), bitmap_dv([5, 6, 7])); // cross-type + assert_eq!(bitmap_dv([5, 6, 7]), set_dv([5, 6, 7])); // reverse + assert_ne!(DeletionVector::NoDeletions, set_dv([1])); + assert_ne!(DeletionVector::NoDeletions, bitmap_dv([1])); + } + + #[test] + fn test_extend() { + // Empty iter -> stays NoDeletions + let mut dv = DeletionVector::NoDeletions; + dv.extend(std::iter::empty::()); + assert!(matches!(dv, DeletionVector::NoDeletions)); + + // Unknown size small -> Set + let mut dv = DeletionVector::NoDeletions; + dv.extend(std::iter::from_fn({ + let mut i = 0u32; + move || { + i += 1; + (i <= 10).then_some(i - 1) + } + })); + assert!(matches!(dv, DeletionVector::Set(_))); + + // Unknown size large -> Bitmap + let mut dv = DeletionVector::NoDeletions; + dv.extend((0u32..10_000).filter(|_| true)); + assert!(matches!(dv, DeletionVector::Bitmap(_))); + + // Set stays Set when small + let mut dv = set_dv([1, 2, 3]); + dv.extend([4, 5, 6]); + assert!(matches!(dv, DeletionVector::Set(_)) && dv.len() == 6); + + // Set promotes to Bitmap when large + let mut dv = set_dv([1, 2, 3]); + dv.extend(100..(BITMAP_THRESDHOLD as u32 + 100)); + assert!(matches!(dv, DeletionVector::Bitmap(_))); + + // Bitmap stays Bitmap + let mut dv = bitmap_dv([1, 2, 3]); + dv.extend([4, 5, 6]); + assert!(matches!(dv, DeletionVector::Bitmap(_)) && dv.len() == 6); + } + + #[test] + fn test_from_roaring() { + let dv: DeletionVector = RoaringBitmap::new().into(); + assert!(matches!(dv, DeletionVector::NoDeletions)); + + let dv: DeletionVector = RoaringBitmap::from_iter([1, 2, 3]).into(); + assert!(matches!(dv, DeletionVector::Bitmap(_)) && dv.len() == 3); + } + + #[test] + fn test_map_offset_dense_then_sparse() { + // First half densely deleted (80% deleted), second half sparse (20% deleted) + // This creates varying deletion density that might trip up the algorithm + let mut deleted = Vec::new(); + // Dense region: delete 4 out of every 5 rows (keep every 5th) + for i in 0..500u32 { + if i % 5 != 0 { + deleted.push(i); + } + } + // Sparse region: delete 1 out of every 5 rows + for i in 500..1000u32 { + if i % 5 == 0 { + deleted.push(i); + } + } + let dv = DeletionVector::Bitmap(RoaringBitmap::from_iter(deleted)); + let mut mapper = OffsetMapper::new(Arc::new(dv)); + + // In dense region: offset 0 -> row 0 (kept), offset 1 -> row 5 (kept), etc. + assert_eq!(mapper.map_offset(0), 0); + assert_eq!(mapper.map_offset(1), 5); + assert_eq!(mapper.map_offset(99), 495); + + // Transition to sparse region + // At row 500, we've had 400 deletions in dense region, plus row 500 is deleted + // offset 100 should get row 501 + assert_eq!(mapper.map_offset(100), 501); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/futures.rs b/lance-artifact/rust/lance-core/src/utils/futures.rs new file mode 100644 index 000000000..95a1c39aa --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/futures.rs @@ -0,0 +1,491 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, + task::Waker, +}; + +use futures::{Stream, StreamExt, stream::BoxStream}; +use pin_project::{pin_project, pinned_drop}; +use tokio::sync::Semaphore; +use tokio_util::sync::PollSemaphore; + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Side { + Left, + Right, +} + +/// A potentially unbounded capacity +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Capacity { + Bounded(u32), + Unbounded, +} + +struct InnerState<'a, T> { + inner: Option>, + buffer: VecDeque, + polling: Option, + waker: Option, + exhausted: bool, + left_buffered: u32, + right_buffered: u32, + available_buffer: Option, +} + +/// A stream that can be shared between two consumers. +pub struct SharedStream<'a, T: Clone> { + state: Arc>>, + side: Side, +} + +impl<'a, T: Clone> SharedStream<'a, T> { + pub fn new(inner: BoxStream<'a, T>, capacity: Capacity) -> (Self, Self) { + let available_buffer = match capacity { + Capacity::Unbounded => None, + Capacity::Bounded(capacity) => Some(PollSemaphore::new(Arc::new(Semaphore::new( + capacity as usize, + )))), + }; + let state = InnerState { + inner: Some(inner), + buffer: VecDeque::new(), + polling: None, + waker: None, + exhausted: false, + left_buffered: 0, + right_buffered: 0, + available_buffer, + }; + + let state = Arc::new(Mutex::new(state)); + + let left = Self { + state: state.clone(), + side: Side::Left, + }; + let right = Self { + state, + side: Side::Right, + }; + (left, right) + } +} + +impl Stream for SharedStream<'_, T> { + type Item = T; + + fn poll_next( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + let mut inner_state = self.state.lock().unwrap(); + let can_take_buffered = match self.side { + Side::Left => inner_state.left_buffered > 0, + Side::Right => inner_state.right_buffered > 0, + }; + if can_take_buffered { + // Easy case, there is an item in the buffer. Grab it, decrement the count, and return it. + let item = inner_state.buffer.pop_front(); + match self.side { + Side::Left => { + inner_state.left_buffered -= 1; + } + Side::Right => { + inner_state.right_buffered -= 1; + } + } + if let Some(available_buffer) = inner_state.available_buffer.as_mut() { + available_buffer.add_permits(1); + } + std::task::Poll::Ready(item) + } else { + if inner_state.exhausted { + return std::task::Poll::Ready(None); + } + // No buffered items, if we have room in the buffer, then try and poll for one + let permit = if let Some(available_buffer) = inner_state.available_buffer.as_mut() { + match available_buffer.poll_acquire(cx) { + // Can return None if the semaphore is closed but we never close the semaphore + // so its safe to unwrap here + std::task::Poll::Ready(permit) => Some(permit.unwrap()), + std::task::Poll::Pending => { + return std::task::Poll::Pending; + } + } + } else { + None + }; + if let Some(polling_side) = inner_state.polling.as_ref() + && *polling_side != self.side + { + // Another task is already polling the inner stream, so we don't need to do anything + + // Per rust docs: + // Note that on multiple calls to poll, only the Waker from the Context + // passed to the most recent call should be scheduled to receive a wakeup. + // + // So it is safe to replace a potentially stale waker here. + inner_state.waker = Some(cx.waker().clone()); + return std::task::Poll::Pending; + } + inner_state.polling = Some(self.side); + // Release the mutex here as polling the inner stream is potentially expensive + let mut to_poll = inner_state + .inner + .take() + .expect("Other half of shared stream panic'd while polling inner stream"); + drop(inner_state); + let res = to_poll.poll_next_unpin(cx); + let mut inner_state = self.state.lock().unwrap(); + + let mut should_wake = true; + match &res { + std::task::Poll::Ready(None) => { + inner_state.exhausted = true; + inner_state.polling = None; + } + std::task::Poll::Ready(Some(item)) => { + // We got an item, forget the permit to mark that we can take one fewer items + if let Some(permit) = permit { + permit.forget(); + } + inner_state.polling = None; + // Let the other side know an item is available + match self.side { + Side::Left => { + inner_state.right_buffered += 1; + } + Side::Right => { + inner_state.left_buffered += 1; + } + }; + inner_state.buffer.push_back(item.clone()); + } + std::task::Poll::Pending => { + should_wake = false; + } + }; + + inner_state.inner = Some(to_poll); + + // If the other side was waiting for us to poll, wake them up, but only after we release the mutex + let to_wake = if should_wake { + inner_state.waker.take() + } else { + // If the inner stream is pending then the inner stream will wake us up and we will wake the + // other side up then. + None + }; + drop(inner_state); + if let Some(waker) = to_wake { + waker.wake(); + } + res + } + } +} + +pub trait SharedStreamExt<'a>: Stream + Send +where + Self::Item: Clone, +{ + /// Split a stream into two shared streams + /// + /// Each shared stream will return the full set of items from the underlying stream. + /// This works by buffering the items from the underlying stream and then replaying + /// them to the other side. + /// + /// The capacity parameter controls how many items can be buffered at once. Be careful + /// with the capacity parameter as it can lead to deadlock if the two streams are not + /// polled evenly. + /// + /// If the capacity is unbounded then the stream could potentially buffer the entire + /// input stream in memory. + fn share( + self, + capacity: Capacity, + ) -> (SharedStream<'a, Self::Item>, SharedStream<'a, Self::Item>); +} + +impl<'a, T: Clone> SharedStreamExt<'a> for BoxStream<'a, T> { + fn share(self, capacity: Capacity) -> (SharedStream<'a, T>, SharedStream<'a, T>) { + SharedStream::new(self, capacity) + } +} + +#[pin_project] +pub struct FinallyStream { + #[pin] + stream: S, + f: Option, +} + +impl FinallyStream { + pub fn new(stream: S, f: F) -> Self { + Self { stream, f: Some(f) } + } +} + +impl Stream for FinallyStream { + type Item = S::Item; + + fn poll_next( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + let this = self.project(); + let res = this.stream.poll_next(cx); + if matches!(res, std::task::Poll::Ready(None)) { + // It's possible that None is polled multiple times, but we only call the function once + if let Some(f) = this.f.take() { + f(); + } + } + res + } +} + +pub trait FinallyStreamExt: Stream + Sized { + fn finally(self, f: F) -> FinallyStream { + FinallyStream { + stream: self, + f: Some(f), + } + } +} + +impl FinallyStreamExt for S { + fn finally(self, f: F) -> FinallyStream { + FinallyStream::new(self, f) + } +} + +/// A stream wrapper that calls a function when dropped. +/// +/// Unlike [`FinallyStream`], which fires when the inner stream yields `None`, +/// this fires when the wrapper is dropped — even if the stream was not fully +/// consumed. +#[pin_project(PinnedDrop)] +pub struct OnDropStream { + #[pin] + stream: S, + f: Option, +} + +impl OnDropStream { + pub fn new(stream: S, f: F) -> Self { + Self { stream, f: Some(f) } + } +} + +impl Stream for OnDropStream { + type Item = S::Item; + + fn poll_next( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + self.project().stream.poll_next(cx) + } +} + +#[pinned_drop] +impl PinnedDrop for OnDropStream { + fn drop(self: std::pin::Pin<&mut Self>) { + let this = self.project(); + if let Some(f) = this.f.take() { + f(); + } + } +} + +pub trait StreamOnDropExt: Stream + Sized { + /// Wrap this stream so that `f` is called when the stream is dropped. + fn on_drop(self, f: F) -> OnDropStream { + OnDropStream::new(self, f) + } +} + +impl StreamOnDropExt for S {} + +#[cfg(test)] +mod tests { + + use std::sync::Arc; + use std::sync::atomic::{AtomicBool, Ordering}; + + use futures::{FutureExt, StreamExt}; + use tokio_stream::wrappers::ReceiverStream; + + use crate::utils::futures::{Capacity, SharedStreamExt, StreamOnDropExt}; + + fn is_pending(fut: &mut (impl std::future::Future + Unpin)) -> bool { + let noop_waker = futures::task::noop_waker(); + let mut context = std::task::Context::from_waker(&noop_waker); + fut.poll_unpin(&mut context).is_pending() + } + + #[tokio::test] + async fn test_shared_stream() { + let (tx, rx) = tokio::sync::mpsc::channel::(10); + let inner_stream = ReceiverStream::new(rx); + + // Feed in a few items + for i in 0..3 { + tx.send(i).await.unwrap(); + } + + let (mut left, mut right) = inner_stream.boxed().share(Capacity::Bounded(2)); + + // We should be able to immediately poll 2 items + assert_eq!(left.next().await.unwrap(), 0); + assert_eq!(left.next().await.unwrap(), 1); + + // Polling again should block because the right side has fallen behind + let mut left_fut = left.next(); + + assert!(is_pending(&mut left_fut)); + + // Polling the right side should yield the first cached item and unblock the left + assert_eq!(right.next().await.unwrap(), 0); + assert_eq!(left_fut.await.unwrap(), 2); + + // Drain the rest of the stream from the right + assert_eq!(right.next().await.unwrap(), 1); + assert_eq!(right.next().await.unwrap(), 2); + + // The channel isn't closed yet so we should get pending on both sides + let mut right_fut = right.next(); + let mut left_fut = left.next(); + assert!(is_pending(&mut right_fut)); + assert!(is_pending(&mut left_fut)); + + // Send one more item + tx.send(3).await.unwrap(); + + // Should be received by both + assert_eq!(right_fut.await.unwrap(), 3); + assert_eq!(left_fut.await.unwrap(), 3); + + drop(tx); + + // Now we should be able to poll the end from either side + assert_eq!(left.next().await, None); + assert_eq!(right.next().await, None); + + // We should be self-fused + assert_eq!(left.next().await, None); + assert_eq!(right.next().await, None); + } + + #[tokio::test] + async fn test_unbounded_shared_stream() { + let (tx, rx) = tokio::sync::mpsc::channel::(10); + let inner_stream = ReceiverStream::new(rx); + + // Feed in a few items + for i in 0..10 { + tx.send(i).await.unwrap(); + } + drop(tx); + + let (mut left, mut right) = inner_stream.boxed().share(Capacity::Unbounded); + + // We should be able to completely drain one side + for i in 0..10 { + assert_eq!(left.next().await.unwrap(), i); + } + assert_eq!(left.next().await, None); + + // And still drain the other side from the buffer + for i in 0..10 { + assert_eq!(right.next().await.unwrap(), i); + } + assert_eq!(right.next().await, None); + } + + #[tokio::test(flavor = "multi_thread")] + async fn stress_shared_stream() { + for _ in 0..100 { + let (tx, rx) = tokio::sync::mpsc::channel::(10); + let inner_stream = ReceiverStream::new(rx); + let (mut left, mut right) = inner_stream.boxed().share(Capacity::Bounded(2)); + + let left_handle = tokio::spawn(async move { + let mut counter = 0; + while let Some(item) = left.next().await { + assert_eq!(item, counter); + counter += 1; + } + }); + + let right_handle = tokio::spawn(async move { + let mut counter = 0; + while let Some(item) = right.next().await { + assert_eq!(item, counter); + counter += 1; + } + }); + + for i in 0..1000 { + tx.send(i).await.unwrap(); + } + drop(tx); + left_handle.await.unwrap(); + right_handle.await.unwrap(); + } + } + + #[tokio::test] + async fn test_on_drop_fires_on_early_drop() { + let called = Arc::new(AtomicBool::new(false)); + let called_clone = called.clone(); + + let stream = futures::stream::iter(vec![1, 2, 3]); + let mut stream = stream.on_drop(move || { + called_clone.store(true, Ordering::SeqCst); + }); + + // Consume only one item, then drop + assert_eq!(stream.next().await, Some(1)); + assert!(!called.load(Ordering::SeqCst)); + drop(stream); + assert!(called.load(Ordering::SeqCst)); + } + + #[tokio::test] + async fn test_on_drop_fires_after_exhaustion() { + let called = Arc::new(AtomicBool::new(false)); + let called_clone = called.clone(); + + let stream = futures::stream::iter(vec![1]); + let mut stream = stream.on_drop(move || { + called_clone.store(true, Ordering::SeqCst); + }); + + assert_eq!(stream.next().await, Some(1)); + assert_eq!(stream.next().await, None); + assert!(!called.load(Ordering::SeqCst)); + drop(stream); + assert!(called.load(Ordering::SeqCst)); + } + + #[tokio::test] + async fn test_on_drop_fires_without_polling() { + let called = Arc::new(AtomicBool::new(false)); + let called_clone = called.clone(); + + let stream = futures::stream::iter(vec![1, 2, 3]); + let stream = stream.on_drop(move || { + called_clone.store(true, Ordering::SeqCst); + }); + + assert!(!called.load(Ordering::SeqCst)); + drop(stream); + assert!(called.load(Ordering::SeqCst)); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/hash.rs b/lance-artifact/rust/lance-core/src/utils/hash.rs new file mode 100644 index 000000000..a09e2d2c1 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/hash.rs @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::hash::Hasher; + +/// A wrapper for `&[u8]` to allow byte slices as hash keys. +/// +/// ``` +/// use lance_core::utils::hash::U8SliceKey; +/// use std::collections::HashMap; +/// +/// let mut map: HashMap = HashMap::new(); +/// map.insert(U8SliceKey(&[1, 2, 3]), 42); +/// +/// assert_eq!(map.get(&U8SliceKey(&[1, 2, 3])), Some(&42)); +/// assert_eq!(map.get(&U8SliceKey(&[1, 2, 4])), None); +/// +/// // Equality is based on slice contents +/// assert_eq!(U8SliceKey(&[1, 2, 3]), U8SliceKey(&[1, 2, 3])); +/// assert_ne!(U8SliceKey(&[1, 2, 3]), U8SliceKey(&[1, 2, 4])); +/// ``` +#[derive(Debug, Eq)] +pub struct U8SliceKey<'a>(pub &'a [u8]); + +impl PartialEq for U8SliceKey<'_> { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl std::hash::Hash for U8SliceKey<'_> { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + #[test] + fn test_u8_slice_key() { + // Test cases not in doctest: key not found, inequality + let mut map = HashMap::new(); + map.insert(U8SliceKey(&[1, 2, 3]), 42); + assert_eq!(map.get(&U8SliceKey(&[4, 5, 6])), None); + assert_ne!(U8SliceKey(&[1]), U8SliceKey(&[2])); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/io_stats.rs b/lance-artifact/rust/lance-core/src/utils/io_stats.rs new file mode 100644 index 000000000..e2169d71a --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/io_stats.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::ops::Range; + +/// A sink that records I/O requests as they are submitted to storage. +/// +/// This lives in `lance-core` so that the encoding layer (`lance-encoding`) and +/// the I/O layer (`lance-io`) can both refer to it without depending on one +/// another. It lets a caller attach a lightweight counter to a file reader and +/// measure the exact bytes/IOPS performed for a bounded scope (e.g. a single +/// query); see `lance_io::scheduler::IoStats` for the concrete implementation. +/// +/// # When to use this +/// +/// Lance also exposes two *process-wide, cumulative* I/O accounting facilities: +/// the global scheduler counters (`lance_io::scheduler::iops_counter` / +/// `bytes_read_counter`) and the object-store `IOTracker` wrapper used in tests. +/// Both aggregate every read in the process and cannot attribute I/O to a single +/// bounded scope. Prefer an `IoStatsRecorder` when you need the *exact* I/O of +/// one operation (e.g. a single query): attach it to a reader with +/// `with_io_stats`, then read the snapshot when the scope ends. It re-uses the +/// reader's cached metadata, so measuring costs no extra file opens and does not +/// disturb the global counters. +pub trait IoStatsRecorder: std::fmt::Debug + Send + Sync { + /// Record one completed request, given the byte ranges as actually + /// submitted to storage (i.e. after any coalescing/splitting), so the + /// counts reflect physical I/O. + fn record_request(&self, ranges: &[Range]); +} diff --git a/lance-artifact/rust/lance-core/src/utils/parse.rs b/lance-artifact/rust/lance-core/src/utils/parse.rs new file mode 100644 index 000000000..e9e43e393 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/parse.rs @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Parse a string into a boolean value. +pub fn str_is_truthy(val: &str) -> bool { + val.eq_ignore_ascii_case("1") + | val.eq_ignore_ascii_case("true") + | val.eq_ignore_ascii_case("on") + | val.eq_ignore_ascii_case("yes") + | val.eq_ignore_ascii_case("y") +} + +/// Parse an environment variable as a truthy-only boolean. +/// +/// Returns `default_value` if the env var is not set. +/// Returns `true` only for truthy values (1/true/on/yes/y, case-insensitive). +/// Returns `false` for all other set values. +pub fn parse_env_as_bool(env_var_name: &str, default_value: bool) -> bool { + std::env::var(env_var_name) + .ok() + .map(|value| str_is_truthy(value.trim())) + .unwrap_or(default_value) +} diff --git a/lance-artifact/rust/lance-core/src/utils/path.rs b/lance-artifact/rust/lance-core/src/utils/path.rs new file mode 100644 index 000000000..d8096dda9 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/path.rs @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use object_store::path::Path; + +pub trait LancePathExt { + fn child_path(&self, path: &Path) -> Path; +} + +impl LancePathExt for Path { + fn child_path(&self, path: &Path) -> Path { + let mut new_path = self.clone(); + for part in path.parts() { + new_path = new_path.clone().join(part); + } + new_path + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/row_addr_remap.rs b/lance-artifact/rust/lance-core/src/utils/row_addr_remap.rs new file mode 100644 index 000000000..6f5a6f2aa --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/row_addr_remap.rs @@ -0,0 +1,413 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Compact row-address remapping for compaction. +//! +//! Compaction rewrites rows into new fragments, so indices that store physical +//! row addresses need an old-address to new-address mapping without building an +//! O(total rows) `HashMap>`. +//! +//! Layout: +//! +//! * Old rows: `old_fragment_id -> (old_offsets, old_rows_before)` +//! * `old_offsets`: rewritten old row offsets in this old fragment. +//! * `old_rows_before`: rewritten row count before this old fragment. +//! * New rows: ordered new-fragment ranges +//! `(fragment_id, new_rows_before, physical_rows)` +//! * `new_rows_before`: rewritten row count before this new fragment. +//! +//! Lookup: +//! +//! * An address whose fragment was not rewritten returns `None`. +//! * For an address whose fragment was rewritten: +//! * Read `(old_offsets, old_rows_before)` from the old-row layout. +//! * If `offset` is not in `old_offsets`, return `Some(None)` because the +//! row was deleted. +//! * Otherwise, `old_offsets.rank(offset) - 1` is this row's 0-based +//! position among rewritten old rows in this old fragment. Add +//! `old_rows_before` to get `k`, the row's 0-based position among all +//! rewritten old rows. +//! * In the new-row layout, find the range +//! `(fragment_id, new_rows_before, physical_rows)` where +//! `new_rows_before <= k < new_rows_before + physical_rows`. +//! * The new address is `(fragment_id, k - new_rows_before)`. +//! +//! Ordering: +//! +//! Compact remap does not store each old-to-new row mapping. It computes `k` +//! from the old-row layout, then maps it to the k-th row written to the new +//! fragments. This requires the reader-to-writer pipeline to preserve row order. +//! +//! * `old_frag_ids` must match the order old fragments are read. Within each +//! old fragment, rewritten rows are interpreted by ascending old row offset. +//! * `new_frags` must match the order new rows are written. +//! * Current compaction satisfies this because it scans selected fragments in +//! order and writes the resulting stream without reordering rows. + +use crate::utils::address::RowAddress; +use crate::{Error, Result}; +use roaring::{RoaringBitmap, RoaringTreemap}; +use std::collections::HashMap; + +/// A queryable row-address remapping with the exact semantics of +/// `HashMap>::get(&addr).copied()`: +/// +/// * `None` — the address is not affected by this remap (keep it unchanged) +/// * `Some(None)` — the row was deleted +/// * `Some(Some(addr))` — the row moved to `addr` +#[derive(Clone)] +pub enum RowAddrRemap { + /// Compact, `O(#fragments)` remap built from per-group rewritten-row + /// bitmaps and new-fragment layouts. + Compact(CompactRowAddrRemap), + /// Full materialized old-to-new address map. Uses `O(#rows)` memory. + Direct(HashMap>), +} + +impl RowAddrRemap { + pub fn compact(groups: impl IntoIterator) -> Result { + Ok(Self::Compact(CompactRowAddrRemap::new(groups)?)) + } + + /// Build a remap from a fully materialized old-to-new address map. + pub fn direct(map: HashMap>) -> Self { + Self::Direct(map) + } + + /// An empty remap that leaves every address unchanged. + pub fn empty() -> Self { + Self::Direct(HashMap::new()) + } + + /// Look up `addr`. See [`RowAddrRemap`] for the tri-state return semantics. + #[inline] + pub fn get(&self, addr: u64) -> Option> { + match self { + Self::Compact(c) => c.get(addr), + Self::Direct(m) => m.get(&addr).copied(), + } + } + + pub fn is_empty(&self) -> bool { + match self { + Self::Compact(c) => c.is_empty(), + Self::Direct(m) => m.is_empty(), + } + } + + pub fn affected_fragments(&self) -> RoaringBitmap { + match self { + Self::Compact(c) => RoaringBitmap::from_iter(c.frag_to_group.keys().copied()), + Self::Direct(m) => RoaringBitmap::from_iter(m.keys().map(|addr| (addr >> 32) as u32)), + } + } + + pub fn fully_deleted_fragments(&self) -> Option { + match self { + Self::Compact(c) => c.fully_deleted_fragments(), + Self::Direct(m) => { + if m.values().all(|v| v.is_none()) { + Some(RoaringBitmap::from_iter( + m.keys().map(|addr| (addr >> 32) as u32), + )) + } else { + None + } + } + } + } +} + +/// Input describing one rewrite group: the old row addresses that were +/// rewritten plus the fragment layout before/after the rewrite. +pub struct GroupInput { + /// Old row addresses that were read and re-written into the new fragments. + pub rewritten_old_row_addrs: RoaringTreemap, + /// Old fragment ids covered by this group. + pub old_frag_ids: Vec, + /// New fragments produced by this group, as `(fragment_id, physical_rows)`, + pub new_frags: Vec<(u32, u32)>, +} + +#[derive(Clone)] +struct GroupRemap { + /// Old fragment id -> (rewritten old row offsets in that fragment, + /// rewritten row count before this fragment in the group). + frags: HashMap, + /// New fragment ranges as `(fragment_id, rewritten_rows_before, physical_rows)`, + /// used to map a rewritten row's group-local index to its new address via binary search. + new_frag_row_ranges: Vec<(u32, u64, u32)>, +} + +impl GroupRemap { + fn new(input: GroupInput) -> Result { + // `compute_new_addr` maps a rewritten row's group-local index to a new + // address by accumulating `physical_rows` in `new_frags` order, so that + // order must be the order rows were written. New fragment ids are + // reserved monotonically in write order (see `reserve_fragment_ids` in + // compaction), so ascending id is a proxy for write order; reject any + // input that violates it before it can silently misplace addresses. + let mut new_frag_row_ranges = Vec::with_capacity(input.new_frags.len()); + let mut rewritten_rows_before = 0u64; + let mut prev_frag_id: Option = None; + for (frag_id, physical_rows) in input.new_frags { + if physical_rows == 0 { + continue; + } + if let Some(prev) = prev_frag_id + && frag_id <= prev + { + return Err(Error::invalid_input(format!( + "compaction new fragments must be in ascending id (write) order, but fragment {frag_id} follows {prev}", + ))); + } + prev_frag_id = Some(frag_id); + new_frag_row_ranges.push((frag_id, rewritten_rows_before, physical_rows)); + rewritten_rows_before += physical_rows as u64; + } + let total_new_rows = rewritten_rows_before; + + let mut per_frag: HashMap = input + .rewritten_old_row_addrs + .bitmaps() + .map(|(frag_id, bitmap)| (frag_id, bitmap.clone())) + .collect(); + let mut frags = HashMap::new(); + let mut rewritten_rows_before = 0u64; + for &frag_id in &input.old_frag_ids { + // A fragment with no rewritten rows (fully deleted) contributes + // nothing to the rewritten row sequence. + if let Some(bitmap) = per_frag.remove(&frag_id) { + let num_rewritten_rows = bitmap.len(); + frags.insert(frag_id, (bitmap, rewritten_rows_before)); + rewritten_rows_before += num_rewritten_rows; + } + } + // Rewritten old row addresses must reference only fragments listed in `old_frag_ids`. + if !per_frag.is_empty() { + return Err(Error::invalid_input(format!( + "compaction rewritten old row addresses reference fragments {:?} not in the rewrite group's old fragments {:?}", + per_frag.keys().collect::>(), + input.old_frag_ids, + ))); + } + + // Rewritten old rows are mapped positionally onto the new rows, so the + // two counts must match exactly + let total_rewritten_old_rows = input.rewritten_old_row_addrs.len(); + if total_new_rows != total_rewritten_old_rows { + return Err(Error::invalid_input(format!( + "compaction rewrote {total_rewritten_old_rows} old rows from fragments {:?} but the new fragments hold {total_new_rows} rows", + input.old_frag_ids, + ))); + } + + Ok(Self { + frags, + new_frag_row_ranges, + }) + } + + fn compute_new_addr(&self, rewritten_row_index: u64) -> u64 { + let idx = + match self + .new_frag_row_ranges + .binary_search_by(|(_, rewritten_rows_before, _)| { + rewritten_rows_before.cmp(&rewritten_row_index) + }) { + Ok(i) => i, + Err(i) => i - 1, + }; + let (frag_id, rewritten_rows_before, _rows) = self.new_frag_row_ranges[idx]; + let offset = (rewritten_row_index - rewritten_rows_before) as u32; + u64::from(RowAddress::new_from_parts(frag_id, offset)) + } + + /// Compute the new address for an old row in this group. + /// Returns `None` if the old row was not rewritten. + #[inline] + fn get(&self, frag: u32, offset: u32) -> Option { + match self.frags.get(&frag) { + Some((bitmap, rewritten_rows_before)) if bitmap.contains(offset) => { + let rewritten_row_index = rewritten_rows_before + bitmap.rank(offset) - 1; + Some(self.compute_new_addr(rewritten_row_index)) + } + _ => None, + } + } +} + +/// Compact remap backed by per-group rewritten row bitmaps + new-fragment layouts. +#[derive(Clone)] +pub struct CompactRowAddrRemap { + groups: Vec, + /// Old fragment id -> index into `groups`. Size is O(#fragments), not rows. + frag_to_group: HashMap, +} + +impl CompactRowAddrRemap { + fn new(groups: impl IntoIterator) -> Result { + let mut frag_to_group = HashMap::new(); + let mut group_remaps = Vec::new(); + for input in groups { + let gi = group_remaps.len(); + for &frag_id in &input.old_frag_ids { + frag_to_group.insert(frag_id, gi); + } + group_remaps.push(GroupRemap::new(input)?); + } + Ok(Self { + groups: group_remaps, + frag_to_group, + }) + } + + #[inline] + pub fn get(&self, addr: u64) -> Option> { + let frag = (addr >> 32) as u32; + // Not in any rewrite group -> unaffected by this remap. + let gi = *self.frag_to_group.get(&frag)?; + Some(self.groups[gi].get(frag, addr as u32)) + } + + pub fn is_empty(&self) -> bool { + self.groups.is_empty() + } + + fn fully_deleted_fragments(&self) -> Option { + // A group with any rewritten row moved at least one row. + if self.groups.iter().any(|g| !g.frags.is_empty()) { + return None; + } + Some(RoaringBitmap::from_iter(self.frag_to_group.keys().copied())) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn addr(frag: u32, offset: u32) -> u64 { + u64::from(RowAddress::new_from_parts(frag, offset)) + } + + #[test] + fn test_compact_lookup() { + // Group A: out-of-order old frags [4, 3], split new frags (11 empty), + // some deletions. frag 4 (5 rows) keeps 0,2,4; frag 3 keeps 0,1, so the + // rewritten rows (4,0)(4,2)(4,4)(3,0)(3,1) go to new frags 10(2), 12(3). + // Group B is a fully-deleted fragment. + let group_a = GroupInput { + rewritten_old_row_addrs: RoaringTreemap::from_iter([ + addr(4, 0), + addr(4, 2), + addr(4, 4), + addr(3, 0), + addr(3, 1), + ]), + old_frag_ids: vec![4, 3], + new_frags: vec![(10, 2), (11, 0), (12, 3)], + }; + let group_b = GroupInput { + rewritten_old_row_addrs: RoaringTreemap::new(), + old_frag_ids: vec![7], + new_frags: vec![], + }; + let remap = RowAddrRemap::compact([group_a, group_b]).unwrap(); + + // Moves, in rewrite order; frag 4 comes first despite the larger id. + assert_eq!(remap.get(addr(4, 0)), Some(Some(addr(10, 0)))); + assert_eq!(remap.get(addr(4, 2)), Some(Some(addr(10, 1)))); + // Rank 2 skips the zero-row new fragment 11 and lands in fragment 12. + assert_eq!(remap.get(addr(4, 4)), Some(Some(addr(12, 0)))); + assert_eq!(remap.get(addr(3, 0)), Some(Some(addr(12, 1)))); + assert_eq!(remap.get(addr(3, 1)), Some(Some(addr(12, 2)))); + // Deleted offsets inside a rewritten fragment. + assert_eq!(remap.get(addr(4, 1)), Some(None)); + assert_eq!(remap.get(addr(4, 3)), Some(None)); + // Covered but fully-deleted fragment -> Some(None), not None. + assert_eq!(remap.get(addr(7, 0)), Some(None)); + // Fragment in no group -> unaffected. + assert_eq!(remap.get(addr(9, 0)), None); + assert!(!remap.is_empty()); + } + + #[test] + fn test_fragment_sets() { + // No rewritten rows at all: every covered fragment is fully deleted. + let dead = RowAddrRemap::compact([GroupInput { + rewritten_old_row_addrs: RoaringTreemap::new(), + old_frag_ids: vec![3, 7], + new_frags: vec![], + }]) + .unwrap(); + assert_eq!( + dead.fully_deleted_fragments(), + Some(RoaringBitmap::from_iter([3u32, 7u32])) + ); + assert_eq!( + dead.affected_fragments(), + RoaringBitmap::from_iter([3u32, 7u32]) + ); + + // At least one rewritten row -> not fully deleted, but both covered + // fragments (including the fully-deleted frag 1) are still affected. + let alive = RowAddrRemap::compact([GroupInput { + rewritten_old_row_addrs: RoaringTreemap::from_iter([addr(0, 0)]), + old_frag_ids: vec![0, 1], + new_frags: vec![(10, 1)], + }]) + .unwrap(); + assert!(alive.fully_deleted_fragments().is_none()); + assert_eq!( + alive.affected_fragments(), + RoaringBitmap::from_iter([0u32, 1u32]) + ); + } + + #[test] + fn test_compact_rejects_rewritten_addrs_outside_old_frags() { + // Rewritten addresses reference frag 5, not in old_frag_ids. The count + // still matches (2 == 2), so only the per-fragment split catches it. + let input = GroupInput { + rewritten_old_row_addrs: RoaringTreemap::from_iter([addr(0, 0), addr(5, 0)]), + old_frag_ids: vec![0], + new_frags: vec![(10, 2)], + }; + assert!(RowAddrRemap::compact([input]).is_err()); + } + + #[test] + fn test_compact_rejects_new_frags_out_of_write_order() { + // New fragments out of ascending id (write) order would make + // `compute_new_addr` accumulate rows in the wrong order, silently + // misplacing addresses. A zero-row fragment between them is ignored. + let input = GroupInput { + rewritten_old_row_addrs: RoaringTreemap::from_iter([addr(0, 0), addr(0, 1)]), + old_frag_ids: vec![0], + new_frags: vec![(12, 1), (11, 1)], + }; + assert!(RowAddrRemap::compact([input]).is_err()); + } + + #[test] + fn test_direct_and_empty() { + // Direct covers arbitrary maps the compact form can't express. + let mut map = HashMap::new(); + map.insert(addr(2, 0), Some(addr(9, 9))); + map.insert(addr(5, 1), None); + let remap = RowAddrRemap::direct(map); + assert_eq!(remap.get(addr(2, 0)), Some(Some(addr(9, 9)))); + assert_eq!(remap.get(addr(5, 1)), Some(None)); + assert_eq!(remap.get(addr(2, 1)), None); + // affected_fragments over an explicit map: the fragment of every key. + assert_eq!( + remap.affected_fragments(), + RoaringBitmap::from_iter([2u32, 5u32]) + ); + + let empty = RowAddrRemap::empty(); + assert!(empty.is_empty()); + assert_eq!(empty.get(addr(0, 0)), None); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/tempfile.rs b/lance-artifact/rust/lance-core/src/utils/tempfile.rs new file mode 100644 index 000000000..a5a13ba26 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/tempfile.rs @@ -0,0 +1,343 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utility functions for creating temporary files and directories. +//! +//! Most of these types wrap around the `tempfile` crate. We add two +//! additional features: +//! +//! * There are wrappers around temporary directories and files that expose +//! the dir/file as an object store path, std path, or string, which can save +//! some boilerplate. +//! * We work around a current bug in the `url` crate which fails to parse +//! Windows paths like `C:\` correctly. We do so by replacing all `\` with +//! `/` in the path. This is not safe in general (e.g. paths may use `\` as +//! an escape) but it should be safe for temporary paths. + +use object_store::path::Path as ObjPath; +use std::{ + ops::Deref, + path::{Path as StdPath, PathBuf}, +}; +use tempfile::NamedTempFile; + +use crate::Result; + +/// A temporary directory +/// +/// This create a temporary directory using [`tempfile::tempdir`]. It will +/// generally be cleaned up when the object is dropped. +/// +/// This type is primarily useful when you need multiple representations (string, +/// path, object store path) of the same temporary directory. If you only need +/// a single representation you can use the [`TempStdDir`], [`TempStrDir`], or +/// [`TempObjDir`] types. +#[derive(Debug)] +pub struct TempDir { + tempdir: tempfile::TempDir, +} + +impl TempDir { + fn new() -> Self { + let tempdir = tempfile::tempdir().unwrap(); + Self { tempdir } + } + + /// Create a temporary directory, exposing any potential errors. + /// + /// For most test cases you should use the [`Default`] implementation instead. + /// However, when we use this type in production code, we may want to return + /// errors gracefully instead of panicking. + pub fn try_new() -> Result { + let tempdir = tempfile::tempdir()?; + Ok(Self { tempdir }) + } + + /// Get the path as a string + /// + /// This path will be safe to use as a URI on Windows + pub fn path_str(&self) -> String { + if cfg!(windows) { + self.tempdir.path().to_str().unwrap().replace("\\", "/") + } else { + self.tempdir.path().to_str().unwrap().to_owned() + } + } + + /// Get the path as a standard library path + /// + /// If you convert this to a string, it will NOT be safe to use as a URI on Windows. + /// Use [`TempDir::path_str`] instead. + /// + /// It is safe to use this as a standard path on Windows. + pub fn std_path(&self) -> &StdPath { + self.tempdir.path() + } + + /// Get the path as an object store path + /// + /// This path will be safe to use as a URI on Windows + pub fn obj_path(&self) -> ObjPath { + ObjPath::parse(self.path_str()).unwrap() + } +} + +impl Default for TempDir { + fn default() -> Self { + Self::new() + } +} + +/// A temporary directory that is exposed as an object store path +/// +/// This is a wrapper around [`TempDir`] that exposes the path as an object store path. +/// It is useful when you need to create a temporary directory that is only +/// used as an object store path. +pub struct TempObjDir { + _tempdir: TempDir, + path: ObjPath, +} + +impl Deref for TempObjDir { + type Target = ObjPath; + + fn deref(&self) -> &Self::Target { + &self.path + } +} + +impl AsRef for TempObjDir { + fn as_ref(&self) -> &ObjPath { + &self.path + } +} + +impl Default for TempObjDir { + fn default() -> Self { + let tempdir = TempDir::default(); + let path = tempdir.obj_path(); + Self { + _tempdir: tempdir, + path, + } + } +} + +/// A temporary directory that is exposed as a string +/// +/// This is a wrapper around [`TempDir`] that exposes the path as a string. +/// It is useful when you need to create a temporary directory that is only +/// used as a string. +pub struct TempStrDir { + _tempdir: TempDir, + string: String, +} + +impl std::fmt::Display for TempStrDir { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.string.fmt(f) + } +} + +impl TempStrDir { + /// Create a cloned copy of the string that can be used if `Into` is needed + pub fn as_into_string(&self) -> impl Into { + self.string.clone() + } +} + +impl Default for TempStrDir { + fn default() -> Self { + let tempdir = TempDir::default(); + let string = tempdir.path_str(); + Self { + _tempdir: tempdir, + string, + } + } +} + +impl Deref for TempStrDir { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.string + } +} + +impl AsRef for TempStrDir { + fn as_ref(&self) -> &str { + self.string.as_ref() + } +} + +/// A temporary directory that is exposed as a standard library path +/// +/// This is a wrapper around [`TempDir`] that exposes the path as a standard library path. +/// It is useful when you need to create a temporary directory that is only +/// used as a standard library path. +#[derive(Default)] +pub struct TempStdDir { + tempdir: TempDir, +} + +impl AsRef for TempStdDir { + fn as_ref(&self) -> &StdPath { + self.tempdir.std_path() + } +} + +impl Deref for TempStdDir { + type Target = StdPath; + + fn deref(&self) -> &Self::Target { + self.tempdir.std_path() + } +} + +/// A temporary file +/// +/// This is a wrapper around [`tempfile::NamedTempFile`]. The file will normally be cleaned +/// up when the object is dropped. +/// +/// Note: this function may create an empty file when the object is created. If you are checking +/// that the path does not exist, you should use [`TempStdPath`] instead. +pub struct TempFile { + temppath: NamedTempFile, +} + +impl TempFile { + fn new() -> Self { + let temppath = tempfile::NamedTempFile::new().unwrap(); + Self { temppath } + } + + /// Get the path as a string safe to use as a URI on Windows. + pub fn path_str(&self) -> String { + if cfg!(windows) { + self.temppath.path().to_str().unwrap().replace("\\", "/") + } else { + self.temppath.path().to_str().unwrap().to_owned() + } + } + + /// Get the path as a standard library path + /// + /// If you convert this to a string, it will NOT be safe to use as a URI on Windows. + /// Use [`TempFile::path_str`] instead. + /// + /// It is safe to use this as a standard path on Windows. + pub fn std_path(&self) -> &StdPath { + self.temppath.path() + } + + /// Get the path as an object store path + /// + /// This path will be safe to use as a URI on Windows + pub fn obj_path(&self) -> ObjPath { + ObjPath::parse(self.path_str()).unwrap() + } +} + +impl Default for TempFile { + fn default() -> Self { + Self::new() + } +} + +/// A temporary file that is exposed as a standard library path +/// +/// This is a wrapper around [`TempFile`] that exposes the path as a standard library path. +/// It is useful when you need to create a temporary file that is only used as a standard library path. +#[derive(Default)] +pub struct TempStdFile { + tempfile: TempFile, +} + +impl AsRef for TempStdFile { + fn as_ref(&self) -> &StdPath { + self.tempfile.std_path() + } +} + +impl Deref for TempStdFile { + type Target = StdPath; + + fn deref(&self) -> &Self::Target { + self.tempfile.std_path() + } +} + +/// A unique path to a temporary file, exposed as an object store path +/// +/// Unlike [`TempFile`], this does not create an empty file. We create a +/// temporary directory and then construct a path inside it, following the +/// same pattern as [`TempStdPath`]. This avoids holding an open file handle, +/// which on Windows would prevent atomic renames to the same path. +pub struct TempObjFile { + _tempdir: TempDir, + path: ObjPath, +} + +impl AsRef for TempObjFile { + fn as_ref(&self) -> &ObjPath { + &self.path + } +} + +impl std::ops::Deref for TempObjFile { + type Target = ObjPath; + + fn deref(&self) -> &Self::Target { + &self.path + } +} + +impl Default for TempObjFile { + fn default() -> Self { + let tempdir = TempDir::default(); + let path = ObjPath::parse(format!("{}/some_file", tempdir.path_str())).unwrap(); + Self { + _tempdir: tempdir, + path, + } + } +} + +/// Get a unique path to a temporary file +/// +/// Unlike [`TempFile`], this function will not create an empty file. We create +/// a temporary directory and then create a path inside of it. Since the temporary +/// directory is created first, we can be confident that the path is unique. +/// +/// This path will be safe to use as a URI on Windows +pub struct TempStdPath { + _tempdir: TempDir, + path: PathBuf, +} + +impl Default for TempStdPath { + fn default() -> Self { + let tempdir = TempDir::default(); + let path = format!("{}/some_file", tempdir.path_str()); + let path = PathBuf::from(path); + Self { + _tempdir: tempdir, + path, + } + } +} + +impl Deref for TempStdPath { + type Target = PathBuf; + + fn deref(&self) -> &Self::Target { + &self.path + } +} + +impl AsRef for TempStdPath { + fn as_ref(&self) -> &StdPath { + self.path.as_path() + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/testing.rs b/lance-artifact/rust/lance-core/src/utils/testing.rs new file mode 100644 index 000000000..96f6dd9ca --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/testing.rs @@ -0,0 +1,300 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Testing utilities + +use crate::Result; +use async_trait::async_trait; +use bytes::Bytes; +use futures::stream::BoxStream; +use futures::{StreamExt, TryStreamExt}; +use object_store::path::Path; +use object_store::{ + CopyOptions, Error as OSError, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, + ObjectStore, PutMultipartOptions, PutOptions, PutPayload, PutResult, RenameOptions, + Result as OSResult, +}; +use std::collections::HashMap; +use std::fmt::Debug; +use std::future; +use std::ops::Range; +use std::pin::Pin; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicUsize, Ordering}, +}; + +// A policy function takes in the name of the operation (e.g. "put") and the location +// that is being accessed / modified and returns an optional error. +pub trait PolicyFnT: Fn(&str, &Path) -> Result<()> + Send + Sync {} +impl PolicyFnT for F where F: Fn(&str, &Path) -> Result<()> + Send + Sync {} +impl Debug for dyn PolicyFnT { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "PolicyFn") + } +} +type PolicyFn = Arc; + +// These policy functions receive (and optionally transform) an ObjectMeta +// They apply to functions that list file info +pub trait ObjectMetaPolicyFnT: Fn(&str, ObjectMeta) -> Result + Send + Sync {} +impl ObjectMetaPolicyFnT for F where F: Fn(&str, ObjectMeta) -> Result + Send + Sync {} +impl Debug for dyn ObjectMetaPolicyFnT { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "PolicyFn") + } +} +type ObjectMetaPolicyFn = Arc; + +/// A policy container, meant to be shared between test code and the proxy object store. +/// +/// This container allows you to configure policies that should apply to the proxied calls. +/// +/// Typically, you would use this to simulate I/O errors or mock out data. +/// +/// Currently, for simplicity, we only proxy calls that involve some kind of path. Calls +/// to copy functions, which have a src and dst, will provide the source to the policy +#[derive(Debug, Default)] +pub struct ProxyObjectStorePolicy { + /// Policies which run before a method is invoked. If the policy returns + /// an error then the target method will not be invoked and the error will + /// be returned instead. + before_policies: HashMap, + /// Policies which run after calls that return ObjectMeta. The policy can + /// transform the returned ObjectMeta to mock out file listing results. + object_meta_policies: HashMap, +} + +impl ProxyObjectStorePolicy { + pub fn new() -> Self { + Default::default() + } + + /// Set a new policy with the given name + /// + /// The name can be used to later remove this policy + pub fn set_before_policy(&mut self, name: &str, policy: PolicyFn) { + self.before_policies.insert(name.to_string(), policy); + } + + pub fn clear_before_policy(&mut self, name: &str) { + self.before_policies.remove(name); + } + + pub fn set_obj_meta_policy(&mut self, name: &str, policy: ObjectMetaPolicyFn) { + self.object_meta_policies.insert(name.to_string(), policy); + } +} + +/// A proxy object store +/// +/// This store wraps another object store and applies the given policy to all calls +/// made to the underlying store. This can be used to simulate failures or, perhaps +/// in the future, to mock out results or provide other fine-grained control. +#[derive(Debug)] +pub struct ProxyObjectStore { + target: Arc, + policy: Arc>, +} + +impl ProxyObjectStore { + pub fn new(target: Arc, policy: Arc>) -> Self { + Self { target, policy } + } + + fn before_method(&self, method: &str, location: &Path) -> OSResult<()> { + let policy = self.policy.lock().unwrap(); + for policy in policy.before_policies.values() { + policy(method, location).map_err(OSError::from)?; + } + Ok(()) + } + + fn transform_meta(&self, method: &str, meta: ObjectMeta) -> OSResult { + let policy = self.policy.lock().unwrap(); + let mut meta = meta; + for policy in policy.object_meta_policies.values() { + meta = policy(method, meta).map_err(OSError::from)?; + } + Ok(meta) + } +} + +impl std::fmt::Display for ProxyObjectStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ProxyObjectStore({})", self.target) + } +} + +/// An object store wrapper that counts listing operations. +/// +/// This increments the shared counter for both `list` and `list_with_delimiter` +/// so tests can observe all listing-based directory and version discovery calls. +#[derive(Debug)] +pub struct CountingObjectStore { + target: Arc, + listing_count: Arc, +} + +impl CountingObjectStore { + pub fn new(target: Arc, listing_count: Arc) -> Self { + Self { + target, + listing_count, + } + } + + fn record_listing(&self) { + self.listing_count.fetch_add(1, Ordering::SeqCst); + } + + fn delegate_list( + &self, + prefix: Option<&Path>, + ) -> Pin> + Send>> { + self.target.list(prefix) + } +} + +impl std::fmt::Display for CountingObjectStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "CountingObjectStore({})", self.target) + } +} + +#[async_trait] +impl ObjectStore for ProxyObjectStore { + async fn put_opts( + &self, + location: &Path, + bytes: PutPayload, + opts: PutOptions, + ) -> OSResult { + self.before_method("put", location)?; + self.target.put_opts(location, bytes, opts).await + } + + async fn put_multipart_opts( + &self, + location: &Path, + opts: PutMultipartOptions, + ) -> OSResult> { + self.before_method("put_multipart", location)?; + self.target.put_multipart_opts(location, opts).await + } + + async fn get_opts(&self, location: &Path, options: GetOptions) -> OSResult { + self.before_method("get_opts", location)?; + let is_head = options.head; + let mut result = self.target.get_opts(location, options).await?; + if is_head { + result.meta = self.transform_meta("head", result.meta)?; + } + Ok(result) + } + + async fn get_ranges(&self, location: &Path, ranges: &[Range]) -> OSResult> { + self.before_method("get_ranges", location)?; + self.target.get_ranges(location, ranges).await + } + + fn delete_stream( + &self, + locations: BoxStream<'static, OSResult>, + ) -> BoxStream<'static, OSResult> { + let policy = Arc::clone(&self.policy); + let checked = locations + .and_then(move |location| { + let result = { + let policy = policy.lock().unwrap(); + policy + .before_policies + .values() + .try_for_each(|policy| policy("delete", &location).map_err(OSError::from)) + }; + future::ready(result.map(|_| location)) + }) + .boxed(); + self.target.delete_stream(checked) + } + + fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, OSResult> { + let target = self.target.clone(); + let policy = Arc::clone(&self.policy); + + target + .list(prefix) + .and_then(move |meta| { + let policy = policy.lock().unwrap(); + let mut meta = meta; + for p in policy.object_meta_policies.values() { + meta = p("list", meta).map_err(OSError::from).unwrap(); + } + future::ready(Ok(meta)) + }) + .boxed() + } + + async fn list_with_delimiter(&self, prefix: Option<&Path>) -> OSResult { + self.target.list_with_delimiter(prefix).await + } + + async fn copy_opts(&self, from: &Path, to: &Path, opts: CopyOptions) -> OSResult<()> { + self.before_method("copy", from)?; + self.target.copy_opts(from, to, opts).await + } + + async fn rename_opts(&self, from: &Path, to: &Path, opts: RenameOptions) -> OSResult<()> { + self.before_method("rename", from)?; + self.target.rename_opts(from, to, opts).await + } +} + +#[async_trait] +impl ObjectStore for CountingObjectStore { + async fn put_opts( + &self, + location: &Path, + bytes: PutPayload, + opts: PutOptions, + ) -> OSResult { + self.target.put_opts(location, bytes, opts).await + } + + async fn put_multipart_opts( + &self, + location: &Path, + opts: PutMultipartOptions, + ) -> OSResult> { + self.target.put_multipart_opts(location, opts).await + } + + async fn get_opts(&self, location: &Path, options: GetOptions) -> OSResult { + self.target.get_opts(location, options).await + } + + async fn get_ranges(&self, location: &Path, ranges: &[Range]) -> OSResult> { + self.target.get_ranges(location, ranges).await + } + + fn delete_stream( + &self, + locations: BoxStream<'static, OSResult>, + ) -> BoxStream<'static, OSResult> { + self.target.delete_stream(locations) + } + + fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, OSResult> { + self.record_listing(); + self.delegate_list(prefix).boxed() + } + + async fn list_with_delimiter(&self, prefix: Option<&Path>) -> OSResult { + self.record_listing(); + self.target.list_with_delimiter(prefix).await + } + + async fn copy_opts(&self, from: &Path, to: &Path, opts: CopyOptions) -> OSResult<()> { + self.target.copy_opts(from, to, opts).await + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/tokio.rs b/lance-artifact/rust/lance-core/src/utils/tokio.rs new file mode 100644 index 000000000..aacce4ad2 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/tokio.rs @@ -0,0 +1,232 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::atomic::Ordering; +use std::sync::{LazyLock, atomic}; +use std::time::Duration; + +use futures::{Future, FutureExt}; +use tokio::runtime::{Builder, Runtime}; +use tracing::Span; + +/// We cache the call to num_cpus::get() because: +/// +/// 1. It shouldn't change during the lifetime of the program +/// 2. It's a relatively expensive call (requires opening several files and examining them) +static NUM_COMPUTE_INTENSIVE_CPUS: LazyLock = + LazyLock::new(calculate_num_compute_intensive_cpus); + +pub fn get_num_compute_intensive_cpus() -> usize { + *NUM_COMPUTE_INTENSIVE_CPUS +} + +fn calculate_num_compute_intensive_cpus() -> usize { + if let Ok(raw) = std::env::var("LANCE_CPU_THREADS") { + return parse_env_usize("LANCE_CPU_THREADS", &raw, 1).unwrap_or_else(|e| panic!("{e}")); + } + + let cpus = num_cpus::get(); + + if cpus <= *IO_CORE_RESERVATION { + // If the user is not setting a custom value for LANCE_IO_CORE_RESERVATION then we don't emit + // a warning because they're just on a small machine and there isn't much they can do about it. + if cpus > 2 { + log::warn!( + "Number of CPUs is less than or equal to the number of IO core reservations. \ + This is not a supported configuration. using 1 CPU for compute intensive tasks." + ); + } + return 1; + } + + num_cpus::get() - *IO_CORE_RESERVATION +} + +/// Parse an integer environment variable, rejecting values below `min`. +/// +/// The error names the variable, so a bad value is diagnosable instead of +/// surfacing as a bare `ParseIntError` or, for `LANCE_CPU_THREADS=0`, a panic +/// deep inside tokio's `max_blocking_threads`. +fn parse_env_usize(name: &str, raw: &str, min: usize) -> Result { + let value: usize = raw + .trim() + .parse() + .map_err(|e| format!("environment variable {name} must be an integer, got {raw:?}: {e}"))?; + if value < min { + return Err(format!( + "environment variable {name} must be at least {min}, got {value}" + )); + } + Ok(value) +} + +/// Number of CPU cores held back for I/O and control tasks. +/// +/// Overridable via the `LANCE_IO_CORE_RESERVATION` environment variable; +/// defaults to `2` when unset. `0` is allowed (reserve nothing); +/// [`get_num_compute_intensive_cpus`] subtracts this from the core count to +/// size the compute pool. A non-integer value panics on first access with an +/// error naming the variable. +pub static IO_CORE_RESERVATION: LazyLock = + LazyLock::new(|| match std::env::var("LANCE_IO_CORE_RESERVATION") { + Ok(raw) => { + parse_env_usize("LANCE_IO_CORE_RESERVATION", &raw, 0).unwrap_or_else(|e| panic!("{e}")) + } + Err(_) => 2, + }); + +fn create_runtime() -> Runtime { + Builder::new_multi_thread() + .thread_name("lance-cpu") + .max_blocking_threads(get_num_compute_intensive_cpus()) + .worker_threads(1) + // keep the thread alive "forever" + .thread_keep_alive(Duration::from_secs(u64::MAX)) + .build() + .unwrap() +} + +static CPU_RUNTIME: atomic::AtomicPtr = atomic::AtomicPtr::new(std::ptr::null_mut()); + +static RUNTIME_INSTALLED: atomic::AtomicBool = atomic::AtomicBool::new(false); + +static ATFORK_INSTALLED: atomic::AtomicBool = atomic::AtomicBool::new(false); + +fn global_cpu_runtime() -> &'static Runtime { + loop { + let ptr = CPU_RUNTIME.load(Ordering::SeqCst); + if !ptr.is_null() { + // SAFETY: `ptr` was produced by `Box::into_raw` below and is only ever + // reset to null by `atfork_tokio_child` in the forked child (single- + // threaded, async-signal context). The `Box` is never reclaimed, so the + // `Runtime` lives for the rest of the process. + return unsafe { &*ptr }; + } + if !RUNTIME_INSTALLED.fetch_or(true, Ordering::SeqCst) { + break; + } + std::thread::yield_now(); + } + if !ATFORK_INSTALLED.fetch_or(true, Ordering::SeqCst) { + install_atfork(); + } + let new_ptr = Box::into_raw(Box::new(create_runtime())); + CPU_RUNTIME.store(new_ptr, Ordering::SeqCst); + // SAFETY: `new_ptr` was just obtained from `Box::into_raw`, so it is non-null, + // aligned, and points to a live `Runtime` that is never reclaimed. + unsafe { &*new_ptr } +} + +/// After a fork() operation, force re-creation of the BackgroundExecutor. Note: this function +/// runs in "async-signal context" which means that we can't (safely) do much here. +extern "C" fn atfork_tokio_child() { + CPU_RUNTIME.store(std::ptr::null_mut(), Ordering::SeqCst); + RUNTIME_INSTALLED.store(false, Ordering::SeqCst); +} + +#[cfg(not(windows))] +fn install_atfork() { + unsafe { libc::pthread_atfork(None, None, Some(atfork_tokio_child)) }; +} + +#[cfg(windows)] +fn install_atfork() {} + +/// Spawn a CPU intensive task +/// +/// This task will be put onto a thread pool dedicated for CPU-intensive work +/// This keeps the tokio thread pool free so that we can always be ready to service +/// cheap I/O & control requests. +/// +/// This can also be used to convert a big chunk of synchronous work into a future +/// so that it can be run in parallel with something like StreamExt::buffered() +/// +/// # Only hand over substantial CPU work +/// +/// Dispatching to the pool has real overhead (a `spawn_blocking` hop plus a oneshot +/// channel round trip). As a rule of thumb the closure should be expected to do at +/// least ~100µs of CPU work; below that the thread-pool overhead is likely to +/// outweigh any parallelism benefit, and the work is better left inline. +/// +/// # The task must never wait on anything +/// +/// The CPU pool is sized to [`get_num_compute_intensive_cpus`], which is +/// `max(1, num_cpus - LANCE_IO_CORE_RESERVATION)`. On a big host that is plenty of +/// workers (e.g. 62 on a 64-core box), but in resource-constrained environments it can +/// collapse to a **single blocking thread** — on machines with `<= 3` visible CPUs +/// (1-vCPU VMs, CI runners, CPU-limited Kubernetes pods) the pool has exactly one +/// worker. A closure passed to `spawn_cpu` occupies one of these threads for its entire +/// lifetime, including any time it spends *parked*. So the closure must only consume +/// CPU and return; it must +/// **never** block, wait, or park. Concretely, the closure must not, directly or +/// transitively: +/// +/// * **No channels** — no blocking send/recv (`send_blocking`, blocking `recv`, etc.). +/// A full/empty channel parks the thread, and whatever would drain/fill the channel +/// may need the same pool to run. +/// * **No I/O** — no file, network, or object-store reads/writes, and no disk spills. +/// I/O parks the thread while making no progress on CPU work. +/// * **No locks** — no acquiring a contended lock (or any lock that is held across an +/// `.await` elsewhere). Waiting for the lock parks the thread. +/// * **No `block_on` / `.blocking_*`** — never drive or wait on another async task +/// from inside the closure. +/// +/// If any of these hold, the parked thread can starve the exact work that would +/// unblock it, deadlocking the whole pool with no timeout and no error — a silent +/// hang at 0% CPU. (See .) When work +/// needs to wait on a channel/lock/I/O, keep the waiting in an async task and only +/// hand the pure-CPU portion to `spawn_cpu`, e.g. build each batch with `spawn_cpu` +/// and dispatch it with `tx.send(batch).await` in the surrounding async code. +pub fn spawn_cpu< + E: std::error::Error + Send + 'static, + F: FnOnce() -> std::result::Result + Send + 'static, + R: Send + 'static, +>( + func: F, +) -> impl Future> { + let (send, recv) = tokio::sync::oneshot::channel(); + // Propagate the current span into the task + let span = Span::current(); + global_cpu_runtime().spawn_blocking(move || { + let _span_guard = span.enter(); + let result = func(); + let _ = send.send(result); + }); + recv.map(|res| res.unwrap()) +} + +#[cfg(test)] +mod tests { + use super::*; + + // The env vars feed process-global `LazyLock`s that read once and are read + // in parallel by other tests, so the pure parser is tested directly rather + // than by mutating the environment. + + #[test] + fn parses_valid_value_and_trims_surrounding_whitespace() { + assert_eq!(parse_env_usize("VAR", "8", 1).unwrap(), 8); + assert_eq!(parse_env_usize("VAR", " 8 ", 1).unwrap(), 8); + } + + #[test] + fn rejects_non_integer_naming_the_variable() { + let err = parse_env_usize("LANCE_CPU_THREADS", "abc", 1).unwrap_err(); + assert!(err.contains("LANCE_CPU_THREADS"), "{err}"); + assert!(err.contains("must be an integer"), "{err}"); + } + + #[test] + fn rejects_value_below_minimum() { + // LANCE_CPU_THREADS=0 parses fine but would panic in tokio's + // max_blocking_threads(0); the minimum stops it at the boundary. + let err = parse_env_usize("LANCE_CPU_THREADS", "0", 1).unwrap_err(); + assert!(err.contains("at least 1"), "{err}"); + } + + #[test] + fn allows_zero_when_minimum_is_zero() { + // LANCE_IO_CORE_RESERVATION=0 is valid: no cores reserved for IO. + assert_eq!(parse_env_usize("VAR", "0", 0).unwrap(), 0); + } +} diff --git a/lance-artifact/rust/lance-core/src/utils/tracing.rs b/lance-artifact/rust/lance-core/src/utils/tracing.rs new file mode 100644 index 000000000..e1f19e3c6 --- /dev/null +++ b/lance-artifact/rust/lance-core/src/utils/tracing.rs @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use futures::Stream; +use pin_project::pin_project; +use tracing::Span; + +#[pin_project] +pub struct InstrumentedStream { + #[pin] + stream: I, + span: Span, +} + +impl Stream for InstrumentedStream { + type Item = I::Item; + + fn poll_next( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + let this = self.project(); + let _guard = this.span.enter(); + this.stream.poll_next(cx) + } +} + +// It would be nice to call the method in_current_span but sadly the Instrumented trait in +// the tracing crate already stole the name for all Sized types +pub trait StreamTracingExt { + /// All calls to poll the stream will be done in the context of the current span (when this method is called) + fn stream_in_current_span(self) -> InstrumentedStream + where + Self: Stream, + Self: Sized; + + fn stream_in_span(self, span: Span) -> InstrumentedStream + where + Self: Stream, + Self: Sized; +} + +impl StreamTracingExt for S { + fn stream_in_current_span(self) -> InstrumentedStream + where + Self: Stream, + Self: Sized, + { + self.stream_in_span(Span::current()) + } + + fn stream_in_span(self, span: Span) -> InstrumentedStream + where + Self: Stream, + Self: Sized, + { + InstrumentedStream { stream: self, span } + } +} + +pub const TRACE_FILE_AUDIT: &str = "lance::file_audit"; +pub const AUDIT_MODE_CREATE: &str = "create"; +pub const AUDIT_MODE_DELETE: &str = "delete"; +pub const AUDIT_MODE_DELETE_UNVERIFIED: &str = "delete_unverified"; +pub const AUDIT_TYPE_DELETION: &str = "deletion"; +pub const AUDIT_TYPE_MANIFEST: &str = "manifest"; +pub const AUDIT_TYPE_INDEX: &str = "index"; +pub const AUDIT_TYPE_DATA: &str = "data"; +pub const AUDIT_TYPE_TRANSACTION: &str = "transaction"; +pub const TRACE_FILE_CREATE: &str = "create"; +pub const TRACE_IO_EVENTS: &str = "lance::io_events"; +pub const IO_TYPE_OPEN_SCALAR: &str = "open_scalar_index"; +pub const IO_TYPE_OPEN_VECTOR: &str = "open_vector_index"; +pub const IO_TYPE_OPEN_FRAG_REUSE: &str = "open_frag_reuse_index"; +pub const IO_TYPE_OPEN_MEM_WAL: &str = "open_mem_wal_index"; +pub const IO_TYPE_LOAD_VECTOR_PART: &str = "load_vector_part"; +pub const IO_TYPE_LOAD_SCALAR_PART: &str = "load_scalar_part"; +pub const TRACE_EXECUTION: &str = "lance::execution"; +pub const EXECUTION_PLAN_RUN: &str = "plan_run"; +pub const TRACE_DATASET_EVENTS: &str = "lance::dataset_events"; +pub const DATASET_WRITING_EVENT: &str = "writing"; +pub const DATASET_COMMITTED_EVENT: &str = "committed"; +pub const DATASET_DROPPING_COLUMN_EVENT: &str = "dropping_column"; +pub const DATASET_DELETING_EVENT: &str = "deleting"; +pub const DATASET_COMPACTING_EVENT: &str = "compacting"; +pub const DATASET_CLEANING_EVENT: &str = "cleaning"; +pub const DATASET_LOADING_EVENT: &str = "loading"; +pub const TRACE_OBJECT_STORE_THROTTLE: &str = "lance::object_store::throttle"; diff --git a/lance-artifact/rust/lance-core/tests/cache_key_allocations.rs b/lance-artifact/rust/lance-core/tests/cache_key_allocations.rs new file mode 100644 index 000000000..ef73d485e --- /dev/null +++ b/lance-artifact/rust/lance-core/tests/cache_key_allocations.rs @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::borrow::Cow; +use std::cell::Cell; +use std::hint::black_box; +use std::sync::atomic::{AtomicUsize, Ordering}; + +use lance_core::cache::{CacheKey, CacheKeySchema, CacheNamespace, InternalCacheKey, KeyBuilder}; + +struct TrackingAllocator; + +thread_local! { + static TRACK_ALLOCATIONS: Cell = const { Cell::new(false) }; +} + +static ALLOCATION_COUNT: AtomicUsize = AtomicUsize::new(0); + +fn record_allocation() { + if TRACK_ALLOCATIONS.try_with(Cell::get).unwrap_or(false) { + ALLOCATION_COUNT.fetch_add(1, Ordering::Relaxed); + } +} + +unsafe impl GlobalAlloc for TrackingAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + record_allocation(); + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + record_allocation(); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + record_allocation(); + unsafe { System.realloc(ptr, layout, new_size) } + } +} + +#[global_allocator] +static ALLOCATOR: TrackingAllocator = TrackingAllocator; + +fn measured_allocations(operation: impl FnOnce()) -> usize { + TRACK_ALLOCATIONS.with(|tracking| tracking.set(false)); + ALLOCATION_COUNT.store(0, Ordering::Relaxed); + TRACK_ALLOCATIONS.with(|tracking| tracking.set(true)); + operation(); + TRACK_ALLOCATIONS.with(|tracking| tracking.set(false)); + ALLOCATION_COUNT.load(Ordering::Relaxed) +} + +fn prepare_key(namespace: CacheNamespace, key: &K) -> InternalCacheKey { + let mut builder = KeyBuilder::new(namespace, K::stable_type_id(), K::schema()); + key.write_key(&mut builder); + builder.finish() +} + +struct PageKey { + path: &'static str, + column_index: u32, + page_index: u64, +} + +impl CacheKey for PageKey { + type ValueType = u64; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed("unused") + } + + fn type_name() -> &'static str { + "allocation-test-page" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("allocation-test-page", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_str(self.path); + builder.write_u32(self.column_index); + builder.write_u64(self.page_index); + } +} + +struct OptionalUuidKey { + generation: u64, + uuid: Option<[u8; 16]>, +} + +impl CacheKey for OptionalUuidKey { + type ValueType = u64; + + fn key(&self) -> Cow<'_, str> { + Cow::Borrowed("unused") + } + + fn type_name() -> &'static str { + "allocation-test-optional-uuid" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("allocation-test-optional-uuid", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u64(self.generation); + match self.uuid { + Some(uuid) => { + builder.write_some(); + builder.write_fixed_bytes(&uuid); + } + None => builder.write_none(), + } + } +} + +#[test] +fn production_shaped_typed_keys_allocate_nothing_after_warmup() { + let namespace = CacheNamespace::root() + .child("tenant-with-a-long-stable-identifier") + .child("index-with-a-long-stable-identifier"); + let page = PageKey { + path: "indices/01999f62-c3c2-7d6f-820d-22e7db948f31/pages/000000000042.lance", + column_index: 17, + page_index: 42, + }; + let uuid = OptionalUuidKey { + generation: 9, + uuid: Some(*b"0123456789abcdef"), + }; + let no_uuid = OptionalUuidKey { + generation: 10, + uuid: None, + }; + + black_box(prepare_key(namespace, &page)); + black_box(prepare_key(namespace, &uuid)); + black_box(prepare_key(namespace, &no_uuid)); + + assert_eq!( + measured_allocations(|| { + black_box(prepare_key(namespace, &page)); + }), + 0 + ); + assert_eq!( + measured_allocations(|| { + black_box(prepare_key(namespace, &uuid)); + }), + 0 + ); + assert_eq!( + measured_allocations(|| { + black_box(prepare_key(namespace, &no_uuid)); + }), + 0 + ); +} diff --git a/lance-artifact/rust/lance-datafusion/Cargo.toml b/lance-artifact/rust/lance-datafusion/Cargo.toml new file mode 100644 index 000000000..7f93ab619 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/Cargo.toml @@ -0,0 +1,52 @@ +[package] +categories.workspace = true +description = "Internal utilities used by other lance modules to simplify working with datafusion" +edition.workspace = true +keywords.workspace = true +license.workspace = true +name = "lance-datafusion" +readme.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +arrow = {workspace = true, features = ["ffi"]} +arrow-array = {workspace = true, features = ["ffi"]} +arrow-buffer.workspace = true +arrow-cast.workspace = true +arrow-ord.workspace = true +arrow-schema.workspace = true +arrow-select.workspace = true +async-trait.workspace = true +datafusion-common.workspace = true +datafusion-functions.workspace = true +datafusion-physical-expr.workspace = true +datafusion-substrait = {workspace = true, optional = true} +datafusion.workspace = true +futures.workspace = true +jsonb = {workspace = true} +lance-arrow.workspace = true +lance-core = {workspace = true, features = ["datafusion"]} +lance-datagen.workspace = true +lance-geo = {workspace = true, optional = true} +chrono.workspace = true +log.workspace = true +pin-project.workspace = true +prost.workspace = true +tokio.workspace = true +tracing.workspace = true + +[build-dependencies] +prost-build.workspace = true +protobuf-src = {version = "2.1", optional = true} + +[dev-dependencies] +lance-datagen.workspace = true + +[features] +geo = ["dep:lance-geo"] +substrait = ["dep:datafusion-substrait"] +protoc = ["dep:protobuf-src"] + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-datafusion/build.rs b/lance-artifact/rust/lance-datafusion/build.rs new file mode 100644 index 000000000..59f63c4b8 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/build.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::io::Result; + +fn main() -> Result<()> { + println!("cargo:rerun-if-changed=protos"); + + #[cfg(feature = "protoc")] + // Use vendored protobuf compiler if requested. + unsafe { + std::env::set_var("PROTOC", protobuf_src::protoc()); + } + + let mut prost_build = prost_build::Config::new(); + prost_build.protoc_arg("--experimental_allow_proto3_optional"); + prost_build.enable_type_names(); + prost_build.compile_protos( + &[ + "./protos/table_identifier.proto", + "./protos/filtered_read.proto", + ], + &["./protos"], + )?; + + Ok(()) +} diff --git a/lance-artifact/rust/lance-datafusion/protos b/lance-artifact/rust/lance-datafusion/protos new file mode 120000 index 000000000..69d0d0d54 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/protos @@ -0,0 +1 @@ +../../protos \ No newline at end of file diff --git a/lance-artifact/rust/lance-datafusion/src/aggregate.rs b/lance-artifact/rust/lance-datafusion/src/aggregate.rs new file mode 100644 index 000000000..3b4ee96b7 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/aggregate.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Aggregate specification for DataFusion aggregates. + +use datafusion::logical_expr::Expr; + +use crate::planner::Planner; + +/// Aggregate specification with group by and aggregate expressions. +#[derive(Debug, Clone)] +pub struct Aggregate { + /// Expressions to group by (e.g., column references). + pub group_by: Vec, + /// Aggregate function expressions (e.g., SUM, COUNT, AVG). + /// Use `.alias()` on the expression to set output column names. + pub aggregates: Vec, +} + +impl Aggregate { + /// Create a new Aggregate. + pub fn new(group_by: Vec, aggregates: Vec) -> Self { + Self { + group_by, + aggregates, + } + } + + /// Compute column names required by this aggregate. + /// + /// For COUNT(*), this returns empty. For SUM(x), GROUP BY y, this returns [x, y]. + pub fn required_columns(&self) -> Vec { + let mut required_columns = Vec::new(); + for expr in self.group_by.iter().chain(self.aggregates.iter()) { + required_columns.extend(Planner::column_names_in_expr(expr)); + } + required_columns.sort(); + required_columns.dedup(); + required_columns + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/chunker.rs b/lance-artifact/rust/lance-datafusion/src/chunker.rs new file mode 100644 index 000000000..f30e215e7 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/chunker.rs @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::pin::Pin; +use std::task::Poll; +use std::{collections::VecDeque, task::Context}; + +use arrow::compute::kernels; +use arrow_array::RecordBatch; +use datafusion::physical_plan::{SendableRecordBatchStream, stream::RecordBatchStreamAdapter}; +use datafusion_common::DataFusionError; +use futures::{Stream, StreamExt, TryStreamExt, ready}; + +use lance_core::Result; +use lance_core::error::DataFusionResult; + +/// Wraps a [`SendableRecordBatchStream`] into a stream of RecordBatch chunks of +/// a given size. This slices but does not copy any buffers. +struct BatchReaderChunker { + /// The inner stream + inner: SendableRecordBatchStream, + /// The batches that have been read from the inner stream but not yet fully yielded + buffered: VecDeque, + /// The number of rows to yield in each chunk + output_size: usize, + /// The position within the first batch in the buffer to start yielding from + i: usize, +} + +impl BatchReaderChunker { + fn new(inner: SendableRecordBatchStream, output_size: usize) -> Self { + Self { + inner, + buffered: VecDeque::new(), + output_size, + i: 0, + } + } + + fn buffered_len(&self) -> usize { + let buffer_total: usize = self.buffered.iter().map(|batch| batch.num_rows()).sum(); + buffer_total - self.i + } + + async fn fill_buffer(&mut self) -> Result<()> { + while self.buffered_len() < self.output_size { + match self.inner.next().await { + Some(Ok(batch)) => self.buffered.push_back(batch), + Some(Err(e)) => return Err(e.into()), + None => break, + } + } + Ok(()) + } + + async fn next(&mut self) -> Option>> { + match self.fill_buffer().await { + Ok(_) => {} + Err(e) => return Some(Err(e)), + }; + + let mut batches = Vec::new(); + + let mut rows_collected = 0; + + while rows_collected < self.output_size { + if let Some(batch) = self.buffered.pop_front() { + // Skip empty batch + if batch.num_rows() == 0 { + continue; + } + + let rows_remaining_in_batch = batch.num_rows() - self.i; + let rows_to_take = + std::cmp::min(rows_remaining_in_batch, self.output_size - rows_collected); + + if rows_to_take == rows_remaining_in_batch { + // We're taking the whole batch, so we can just move it + let batch = if self.i == 0 { + batch + } else { + // We are taking the remainder of the batch, so we need to slice it + batch.slice(self.i, rows_to_take) + }; + batches.push(batch); + self.i = 0; + } else { + // We're taking a slice of the batch, so we need to copy it + batches.push(batch.slice(self.i, rows_to_take)); + // And then we need to push the remainder back onto the front of the queue + self.i += rows_to_take; + self.buffered.push_front(batch); + } + + rows_collected += rows_to_take; + } else { + break; + } + } + + if batches.is_empty() { + None + } else { + Some(Ok(batches)) + } + } +} + +struct BreakStreamState { + max_rows: usize, + rows_seen: usize, + rows_remaining: usize, + batch: Option, +} + +impl BreakStreamState { + fn next(mut self) -> Option<(Result, Self)> { + if self.rows_remaining == 0 { + return None; + } + if self.rows_remaining + self.rows_seen <= self.max_rows { + self.rows_seen = (self.rows_seen + self.rows_remaining) % self.max_rows; + self.rows_remaining = 0; + let next = self.batch.take().unwrap(); + Some((Ok(next), self)) + } else { + let rows_to_emit = self.max_rows - self.rows_seen; + self.rows_seen = 0; + self.rows_remaining -= rows_to_emit; + let batch = self.batch.as_mut().unwrap(); + let next = batch.slice(0, rows_to_emit); + *batch = batch.slice(rows_to_emit, batch.num_rows() - rows_to_emit); + Some((Ok(next), self)) + } + } +} + +// Given a stream of record batches, and a desired break point, this will +// make sure that a new record batch is emitted every time `break_point` rows +// have passed. +// +// This method will not combine record batches in any way. For example, if +// the input lengths are [3, 5, 8, 3, 5], and the break point is 10 then the +// output batches will be [3, 5, 2 (break inserted) 6, 3, 1 (break inserted) 4] +pub fn break_stream( + stream: SendableRecordBatchStream, + max_chunk_size: usize, +) -> Pin> + Send>> { + let mut rows_already_seen = 0; + stream + .map_ok(move |batch| { + let state = BreakStreamState { + rows_remaining: batch.num_rows(), + max_rows: max_chunk_size, + rows_seen: rows_already_seen, + batch: Some(batch), + }; + rows_already_seen = (state.rows_seen + state.rows_remaining) % state.max_rows; + + futures::stream::unfold(state, move |state| std::future::ready(state.next())) + .fuse() + .boxed() + }) + .try_flatten() + .boxed() +} + +/// Given a stream of record batches, this will yield batches of a fixed size. +/// +/// In order to avoid copying data the batches will be converted into a stream of +/// `Vec` where each item is a `Vec` of batches whose total size is +/// `chunk_size`. +pub fn chunk_stream( + stream: SendableRecordBatchStream, + chunk_size: usize, +) -> Pin>> + Send>> { + let chunker = BatchReaderChunker::new(stream, chunk_size); + futures::stream::unfold(chunker, |mut chunker| async move { + match chunker.next().await { + Some(Ok(batches)) => Some((Ok(batches), chunker)), + Some(Err(e)) => Some((Err(e), chunker)), + None => None, + } + }) + .fuse() + .boxed() +} + +/// Given a stream of record batches, this will yield batches of a fixed size. +/// +/// This stream _will_ combine record batches and so it can be fairly expensive as it will +/// likely force a copy of incoming data. However, it can be useful when users require +/// precise batch sizing. +pub fn chunk_concat_stream( + stream: SendableRecordBatchStream, + chunk_size: usize, +) -> SendableRecordBatchStream { + let schema = stream.schema(); + let schema_copy = schema.clone(); + let chunked = chunk_stream(stream, chunk_size); + let chunk_concat = chunked + .and_then(move |batches| { + std::future::ready( + // chunk_stream is zero-copy and so it gives us pieces of batches. However, the btree + // index needs 1 batch-per-page and so we concatenate here. + kernels::concat::concat_batches(&schema, batches.iter()).map_err(|e| e.into()), + ) + }) + .map_err(DataFusionError::from) + .boxed(); + Box::pin(RecordBatchStreamAdapter::new(schema_copy, chunk_concat)) +} + +/// Given a stream of record batches, this will yield batches of a fixed size. +/// +/// This stream _will_ combine record batches and so it can be fairly expensive as it will +/// likely force a copy of all incoming data. However, it can be useful when users require +/// precise batch sizing. +pub struct StrictBatchSizeStream { + inner: S, + batch_size: usize, + residual: Option, +} + +impl> + Unpin> StrictBatchSizeStream { + pub fn new(inner: S, batch_size: usize) -> Self { + Self { + inner, + batch_size, + residual: None, + } + } +} + +/// Internal polling method for strict batch size enforcement. +/// +/// # Use Case +/// When precise batch sizing is required (e.g., ML batch processing), this method guarantees +/// output batches exactly match batch_size until final partial batch. Maintains data integrity +/// across splits using row-aware splitting. +/// +/// # Example +/// With batch_size=5 and input sequence: +/// - Fragment 1: 7 rows → splits into `[5,2]` +/// (queues 5, carries 2) +/// - Fragment 2: 4 rows → combines carried 2 + 4 = 6 +/// splits into `[5,1]` +/// +/// - Output batches: `[5]`, `[5]`, `[1]` +impl Stream for StrictBatchSizeStream +where + S: Stream> + Unpin, +{ + type Item = DataFusionResult; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + loop { + // Process residual first if present + if let Some(residual) = self.residual.take() { + if residual.num_rows() >= self.batch_size { + let split_at = self.batch_size; + let chunk = residual.slice(0, split_at); + let new_residual = residual.slice(split_at, residual.num_rows() - split_at); + self.residual = Some(new_residual); + return Poll::Ready(Some(Ok(chunk))); + } else { + // Keep residual and proceed to get more data + self.residual = Some(residual); + } + } + + // Poll the inner stream for next batch + match ready!(Pin::new(&mut self.inner).poll_next(cx)) { + Some(Ok(batch)) => { + // Combine with residual if any + let current_batch = if let Some(residual) = self.residual.take() { + arrow::compute::concat_batches(&residual.schema(), &[residual, batch]) + .map_err(|e| DataFusionError::External(Box::new(e)))? + } else { + batch + }; + + if current_batch.num_rows() >= self.batch_size { + let split_at = self.batch_size; + let chunk = current_batch.slice(0, split_at); + let new_residual = + current_batch.slice(split_at, current_batch.num_rows() - split_at); + if new_residual.num_rows() > 0 { + self.residual = Some(new_residual); + } + return Poll::Ready(Some(Ok(chunk))); + } else { + // Not enough rows, store as residual + self.residual = Some(current_batch); + continue; + } + } + Some(Err(e)) => return Poll::Ready(Some(Err(e))), + None => { + return Poll::Ready( + self.residual + .take() + .filter(|r| r.num_rows() > 0) + .map(Ok::<_, DataFusionError>), + ); + } + } + } + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow::datatypes::{Int32Type, Int64Type}; + use datafusion::physical_plan::stream::RecordBatchStreamAdapter; + use futures::{StreamExt, TryStreamExt}; + use lance_datagen::{BatchCount, RowCount, array}; + + use crate::datagen::DatafusionDatagenExt; + + #[tokio::test] + async fn test_chunkers() { + let schema = Arc::new(arrow::datatypes::Schema::new(vec![ + arrow::datatypes::Field::new("", arrow::datatypes::DataType::Int32, false), + ])); + + let make_batch = |num_rows: u32| { + lance_datagen::gen_batch() + .anon_col(lance_datagen::array::step::()) + .into_batch_rows(RowCount::from(num_rows as u64)) + .unwrap() + }; + + let batches = vec![make_batch(10), make_batch(5), make_batch(13), make_batch(0)]; + + let make_stream = || { + let stream = futures::stream::iter( + batches + .clone() + .into_iter() + .map(datafusion_common::Result::Ok), + ) + .boxed(); + Box::pin(RecordBatchStreamAdapter::new(schema.clone(), stream)) + }; + + let chunked = super::chunk_stream(make_stream(), 10) + .try_collect::>() + .await + .unwrap(); + + assert_eq!(chunked.len(), 3); + assert_eq!(chunked[0].len(), 1); + assert_eq!(chunked[0][0].num_rows(), 10); + assert_eq!(chunked[1].len(), 2); + assert_eq!(chunked[1][0].num_rows(), 5); + assert_eq!(chunked[1][1].num_rows(), 5); + assert_eq!(chunked[2].len(), 1); + assert_eq!(chunked[2][0].num_rows(), 8); + + let chunked = super::chunk_concat_stream(make_stream(), 10) + .try_collect::>() + .await + .unwrap(); + + assert_eq!(chunked.len(), 3); + assert_eq!(chunked[0].num_rows(), 10); + assert_eq!(chunked[1].num_rows(), 10); + assert_eq!(chunked[2].num_rows(), 8); + + let chunked = super::break_stream(make_stream(), 10) + .try_collect::>() + .await + .unwrap(); + + assert_eq!(chunked.len(), 4); + assert_eq!(chunked[0].num_rows(), 10); + assert_eq!(chunked[1].num_rows(), 5); + assert_eq!(chunked[2].num_rows(), 5); + assert_eq!(chunked[3].num_rows(), 8); + } + + #[tokio::test] + async fn test_strict_batch_size_stream() { + let batches = lance_datagen::gen_batch() + .anon_col(array::step::()) + .anon_col(array::step::()) + .into_df_stream(RowCount::from(7), BatchCount::from(10)); + + let stream = super::StrictBatchSizeStream::new(batches, 10); + + let batches = stream.try_collect::>().await.unwrap(); + assert_eq!(batches.len(), 7); + + for batch in batches { + assert_eq!(batch.num_rows(), 10); + } + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/dataframe.rs b/lance-artifact/rust/lance-datafusion/src/dataframe.rs new file mode 100644 index 000000000..2dc0950c8 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/dataframe.rs @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Lance extensions for [DataFrame]. + +use std::ops::Range; +use std::pin::Pin; +use std::sync::Arc; +use std::task::{Context, Poll}; + +use arrow_array::RecordBatch; +use arrow_ord::partition::partition; +use arrow_schema::Schema; +use datafusion::dataframe::DataFrame; +use datafusion::error::Result as DFResult; +use datafusion::physical_plan::SendableRecordBatchStream; +use datafusion::scalar::ScalarValue; +use futures::{Stream, StreamExt}; +use lance_arrow::RecordBatchExt; + +#[async_trait::async_trait] +pub trait DataFrameExt { + /// Execute the query and return as a grouped stream. + /// + /// The data is assumed to have already been sorted by the partition columns. + async fn group_by_stream(self, partition_columns: &[&str]) -> DFResult; +} + +#[async_trait::async_trait] +impl DataFrameExt for DataFrame { + async fn group_by_stream(self, partition_columns: &[&str]) -> DFResult { + if partition_columns.is_empty() { + return Err(datafusion::error::DataFusionError::Execution( + "No partition columns specified".into(), + )); + } + if partition_columns.len() > 1 { + return Err(datafusion::error::DataFusionError::NotImplemented( + "Only one partition column supported".into(), + )); + } + for col in partition_columns { + if self.schema().field_with_name(None, col).is_err() { + return Err(datafusion::error::DataFusionError::Execution(format!( + "Partition column '{}' not found", + col + ))); + } + } + + Ok(BatchStreamGrouper::new( + self.execute_stream().await?, + partition_columns[0].into(), + )) + } +} + +type GroupRange = (ScalarValue, Range); + +/// A stream of record batch groups. +/// +/// The stream works by pulling batches from the input stream and buffering them +/// into `buffer`. Once a new partition value is pulled from the input stream, +/// the buffered batches are grouped by the partition value and returned. +/// +/// The partition columns are removed from the schema as they are pulled from +/// `input`. +pub struct BatchStreamGrouper { + /// The input stream. + input: SendableRecordBatchStream, + /// The partition columns. + partition_column: String, // TODO: support multiple + /// The output schema. This is computed as the input schema minus the + /// partition columns. + schema: Arc, + /// The buffer containing the batches to be grouped for the current partition. + buffer: Vec, + current_partition: Option, + /// Data that has been pulled from the input stream but not yet processed + /// into a group. + unprocessed: Option<(Vec, RecordBatch)>, +} + +impl std::fmt::Debug for BatchStreamGrouper { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("BatchStreamGrouper") + .field("input", &"...") + .field("partition_column", &self.partition_column) + .field("schema", &self.schema) + .field("buffer", &self.buffer) + .field("current_partition", &self.current_partition) + .field("unprocessed", &self.unprocessed) + .finish() + } +} + +impl BatchStreamGrouper { + pub fn new(input: SendableRecordBatchStream, partition_column: String) -> Self { + let schema = Arc::new(Schema::new( + input + .schema() + .fields() + .iter() + .filter(|f| f.name() != &partition_column) + .cloned() + .collect::>(), + )); + Self { + input, + partition_column, + schema, + buffer: vec![], + current_partition: None, + unprocessed: None, + } + } + + /// Get the output schema of the stream. + pub fn schema(&self) -> &Arc { + &self.schema + } + + /// Given a record batch, find the distinct ranges of partition values. + /// + /// Returns the values in reverse order, so that we can pop them off the + /// end of the vector one-by-one. + fn compute_ranges(&self, batch: &RecordBatch) -> DFResult)>> { + let column = batch.column_by_name(&self.partition_column).ok_or( + datafusion::error::DataFusionError::Execution("Partition column not found".into()), + )?; + let ranges = partition(std::slice::from_ref(column))?.ranges(); + ranges + .into_iter() + .rev() + .map(|r| Ok((ScalarValue::try_from_array(column, r.start)?, r))) + .collect::>>() + } + + /// Fill the buffer with data from `unprocessed`. + /// + /// If we encounter data from a new partition, returns the current batch. + /// + /// If we exhaust the unprocessed data, returns None. + fn fill_buffer(&mut self) -> Option<(Vec, Vec)> { + // If there is data in the unprocessed buffer that matches, bring it + // into the buffer + if self.unprocessed.is_some() { + let unprocessed_value = self.peek_unprocessed_value(); + match (&mut self.current_partition, unprocessed_value) { + (Some(current), Some(next)) if current == &next => { + if let Some(batch) = self.pop_next_unprocessed() { + self.buffer.push(batch); + } + } + (None, Some(next)) => { + self.current_partition = Some(next); + if let Some(batch) = self.pop_next_unprocessed() { + self.buffer.push(batch); + } + } + _ => {} + } + } + + if self.unprocessed.is_some() && self.current_partition.is_some() { + // If there is remaining data in the unprocessed buffer, we have reached + // end of group, so we should return the current. + Some(( + vec![self.current_partition.take().unwrap()], + self.buffer.drain(..).collect(), + )) + } else { + // If there is no data in the unprocessed buffer, return None as we aren't finished. + None + } + } + + /// Peek at the next partition value in the unprocessed buffer. + fn peek_unprocessed_value(&self) -> Option { + self.unprocessed + .as_ref() + .map(|data| data.0.last().unwrap().0.clone()) + } + + /// Get the next unprocessed slice of data with constant partition value. + fn pop_next_unprocessed(&mut self) -> Option { + if let Some(data) = &mut self.unprocessed { + if data.0.is_empty() { + self.unprocessed = None; + return None; + } + let (_part, range) = data.0.pop().unwrap(); + let batch = data.1.slice(range.start, range.end - range.start); + let batch = batch.drop_column(&self.partition_column).unwrap(); + if data.0.is_empty() { + self.unprocessed = None; + } + Some(batch) + } else { + None + } + } +} + +impl Stream for BatchStreamGrouper { + type Item = DFResult<(Vec, Vec)>; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + loop { + if let Some(ready_data) = self.fill_buffer() { + return Poll::Ready(Some(Ok(ready_data))); + } + debug_assert!( + self.unprocessed.is_none(), + "Something went wrong with state: {:?}", + self + ); + + match self.input.poll_next_unpin(cx) { + Poll::Ready(Some(Ok(batch))) => { + self.unprocessed = Some((self.compute_ranges(&batch)?, batch)); + } + Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))), + Poll::Ready(None) => { + if self.current_partition.is_some() { + let batches = std::mem::take(&mut self.buffer); + let partition = vec![self.current_partition.take().unwrap()]; + return Poll::Ready(Some(Ok((partition, batches)))); + } else { + return Poll::Ready(None); + } + } + Poll::Pending => return Poll::Pending, + } + } + } +} + +#[cfg(test)] +mod tests { + use arrow_array::Int32Array; + use arrow_schema::{DataType, Field}; + use datafusion::{datasource::MemTable, execution::context::SessionContext}; + use futures::TryStreamExt; + + use super::*; + + #[tokio::test] + async fn test_group_by_stream() { + let schema = Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int32, false), + Field::new("b", DataType::Int32, false), + ])); + let batch = RecordBatch::try_new( + schema.clone(), + vec![ + Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8])), + Arc::new(Int32Array::from(vec![1, 1, 2, 2, 2, 3, 3, 4])), + ], + ) + .unwrap(); + let batches = vec![ + batch.slice(0, 3), // a = [1, 2, 3], b = [1, 1, 2] + batch.slice(3, 2), // a = [4, 5], b = [2, 2] + batch.slice(5, 3), // a = [6, 7, 8], b = [3, 3, 4] + ]; + + let table = MemTable::try_new(schema, vec![batches]).unwrap(); + let ctx = SessionContext::new(); + let df = ctx.read_table(Arc::new(table)).unwrap(); + let actual = df + .group_by_stream(&["b"]) + .await + .unwrap() + .try_collect::>() + .await + .unwrap(); + + let expected_batch = RecordBatch::try_new( + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])), + vec![batch["a"].clone()], + ) + .unwrap(); + let expected = vec![ + ( + vec![ScalarValue::Int32(Some(1))], + vec![expected_batch.slice(0, 2)], + ), + ( + vec![ScalarValue::Int32(Some(2))], + vec![expected_batch.slice(2, 1), expected_batch.slice(3, 2)], + ), + ( + vec![ScalarValue::Int32(Some(3))], + vec![expected_batch.slice(5, 2)], + ), + ( + vec![ScalarValue::Int32(Some(4))], + vec![expected_batch.slice(7, 1)], + ), + ]; + + assert_eq!(expected, actual); + } + + // TODO: test the stream more. +} diff --git a/lance-artifact/rust/lance-datafusion/src/datagen.rs b/lance-artifact/rust/lance-datafusion/src/datagen.rs new file mode 100644 index 000000000..c9d039c4d --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/datagen.rs @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_array::RecordBatchReader; +use datafusion::{ + execution::SendableRecordBatchStream, + physical_plan::{ExecutionPlan, stream::RecordBatchStreamAdapter}, +}; +use datafusion_common::DataFusionError; +use futures::TryStreamExt; +use lance_core::Error; +use lance_datagen::{BatchCount, BatchGeneratorBuilder, ByteCount, RoundingBehavior, RowCount}; + +use crate::exec::OneShotExec; + +pub trait DatafusionDatagenExt { + fn into_df_stream( + self, + batch_size: RowCount, + num_batches: BatchCount, + ) -> SendableRecordBatchStream; + + fn into_df_stream_bytes( + self, + batch_size: ByteCount, + num_batches: BatchCount, + rounding_behavior: RoundingBehavior, + ) -> Result; + + fn into_df_exec(self, batch_size: RowCount, num_batches: BatchCount) -> Arc; +} + +impl DatafusionDatagenExt for BatchGeneratorBuilder { + fn into_df_stream( + self, + batch_size: RowCount, + num_batches: BatchCount, + ) -> SendableRecordBatchStream { + let (stream, schema) = self.into_reader_stream(batch_size, num_batches); + let stream = stream.map_err(DataFusionError::from); + Box::pin(RecordBatchStreamAdapter::new(schema, stream)) + } + + fn into_df_stream_bytes( + self, + batch_size: ByteCount, + num_batches: BatchCount, + rounding_behavior: RoundingBehavior, + ) -> Result { + let stream = self.into_reader_bytes(batch_size, num_batches, rounding_behavior)?; + let schema = stream.schema(); + let stream = futures::stream::iter(stream).map_err(DataFusionError::from); + Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream))) + } + + fn into_df_exec(self, batch_size: RowCount, num_batches: BatchCount) -> Arc { + let stream = self.into_df_stream(batch_size, num_batches); + Arc::new(OneShotExec::new(stream)) + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/exec.rs b/lance-artifact/rust/lance-datafusion/src/exec.rs new file mode 100644 index 000000000..994e16b78 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/exec.rs @@ -0,0 +1,1346 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for working with datafusion execution plans + +use std::{ + collections::HashMap, + fmt::{self, Formatter}, + num::NonZero, + sync::{Arc, Mutex, OnceLock}, + time::Duration, +}; + +use chrono::{DateTime, Utc}; + +use arrow_array::RecordBatch; +use arrow_schema::Schema as ArrowSchema; +use datafusion::{ + catalog::{TableProvider, streaming::StreamingTable}, + dataframe::DataFrame, + execution::{ + TaskContext, + context::{SessionConfig, SessionContext}, + disk_manager::DiskManagerBuilder, + memory_pool::FairSpillPool, + runtime_env::RuntimeEnvBuilder, + }, + physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, ExecutionPlanProperties, PlanProperties, + SendableRecordBatchStream, + analyze::AnalyzeExec, + coalesce_partitions::CoalescePartitionsExec, + display::DisplayableExecutionPlan, + execution_plan::{Boundedness, CardinalityEffect, EmissionType}, + metrics::MetricValue, + sorts::sort_preserving_merge::SortPreservingMergeExec, + stream::RecordBatchStreamAdapter, + streaming::PartitionStream, + }, +}; +use datafusion::{execution::memory_pool::TrackConsumersPool, physical_plan::metrics::MetricType}; +use datafusion_common::{DataFusionError, Statistics}; +use datafusion_physical_expr::{EquivalenceProperties, Partitioning}; + +use futures::{StreamExt, stream}; +use lance_arrow::SchemaExt; +use lance_core::{ + Error, Result, + utils::{ + futures::FinallyStreamExt, + tracing::{EXECUTION_PLAN_RUN, StreamTracingExt, TRACE_EXECUTION}, + }, +}; +use log::{debug, info, warn}; +use tracing::Span; + +use crate::udf::register_functions; +use crate::{ + chunker::StrictBatchSizeStream, + utils::{ + BYTES_READ_METRIC, INDEX_CACHE_HITS_METRIC, INDEX_CACHE_MISSES_METRIC, + INDEX_COMPARISONS_METRIC, INDICES_LOADED_METRIC, IOPS_METRIC, MetricsExt, + PARTS_LOADED_METRIC, REQUESTS_METRIC, + }, +}; + +/// An source execution node created from an existing stream +/// +/// It can only be used once, and will return the stream. After that the node +/// is exhausted. +/// +/// Note: the stream should be finite, otherwise we will report datafusion properties +/// incorrectly. +pub struct OneShotExec { + stream: Mutex>, + // We save off a copy of the schema to speed up formatting and so ExecutionPlan::schema & display_as + // can still function after exhausted + schema: Arc, + properties: Arc, +} + +impl OneShotExec { + /// Create a new instance from a given stream + pub fn new(stream: SendableRecordBatchStream) -> Self { + let schema = stream.schema(); + Self { + stream: Mutex::new(Some(stream)), + schema: schema.clone(), + properties: Arc::new(PlanProperties::new( + EquivalenceProperties::new(schema), + Partitioning::RoundRobinBatch(1), + EmissionType::Incremental, + Boundedness::Bounded, + )), + } + } + + pub fn from_batch(batch: RecordBatch) -> Self { + let schema = batch.schema(); + let stream = Box::pin(RecordBatchStreamAdapter::new( + schema, + stream::iter(vec![Ok(batch)]), + )); + Self::new(stream) + } +} + +impl std::fmt::Debug for OneShotExec { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let stream = self.stream.lock().unwrap(); + f.debug_struct("OneShotExec") + .field("exhausted", &stream.is_none()) + .field("schema", self.schema.as_ref()) + .finish() + } +} + +impl DisplayAs for OneShotExec { + fn fmt_as( + &self, + t: datafusion::physical_plan::DisplayFormatType, + f: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + let stream = self.stream.lock().unwrap(); + let exhausted = if stream.is_some() { "" } else { "EXHAUSTED" }; + let columns = self + .schema + .field_names() + .iter() + .cloned() + .cloned() + .collect::>(); + match t { + DisplayFormatType::Default | DisplayFormatType::Verbose => { + write!( + f, + "OneShotStream: {}columns=[{}]", + exhausted, + columns.join(",") + ) + } + DisplayFormatType::TreeRender => { + write!( + f, + "OneShotStream\nexhausted={}\ncolumns=[{}]", + exhausted, + columns.join(",") + ) + } + } + } +} + +impl ExecutionPlan for OneShotExec { + fn name(&self) -> &str { + "OneShotExec" + } + + fn schema(&self) -> arrow_schema::SchemaRef { + self.schema.clone() + } + + fn children(&self) -> Vec<&Arc> { + vec![] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> datafusion_common::Result> { + // OneShotExec has no children, so this should only be called with an empty vector + if !children.is_empty() { + return Err(datafusion_common::DataFusionError::Internal( + "OneShotExec does not support children".to_string(), + )); + } + Ok(self) + } + + fn execute( + &self, + _partition: usize, + _context: Arc, + ) -> datafusion_common::Result { + let stream = self + .stream + .lock() + .map_err(|err| DataFusionError::Execution(err.to_string()))? + .take(); + if let Some(stream) = stream { + Ok(stream) + } else { + Err(DataFusionError::Execution( + "OneShotExec has already been executed".to_string(), + )) + } + } + + fn properties(&self) -> &Arc { + &self.properties + } +} + +struct TracedExec { + input: Arc, + properties: Arc, + span: Span, +} + +impl TracedExec { + pub fn new(input: Arc, span: Span) -> Self { + Self { + properties: input.properties().clone(), + input, + span, + } + } +} + +impl DisplayAs for TracedExec { + fn fmt_as( + &self, + t: datafusion::physical_plan::DisplayFormatType, + f: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + match t { + DisplayFormatType::Default + | DisplayFormatType::Verbose + | DisplayFormatType::TreeRender => { + write!(f, "TracedExec") + } + } + } +} + +impl std::fmt::Debug for TracedExec { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "TracedExec") + } +} +impl ExecutionPlan for TracedExec { + fn name(&self) -> &str { + "TracedExec" + } + + fn properties(&self) -> &Arc { + &self.properties + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.input] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> datafusion_common::Result> { + Ok(Arc::new(Self { + input: children[0].clone(), + properties: self.properties.clone(), + span: self.span.clone(), + })) + } + + fn execute( + &self, + partition: usize, + context: Arc, + ) -> datafusion_common::Result { + let _guard = self.span.enter(); + let stream = self.input.execute(partition, context)?; + let schema = stream.schema(); + let stream = stream.stream_in_span(self.span.clone()); + Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream))) + } +} + +/// Callback for reporting statistics after a scan +pub type ExecutionStatsCallback = Arc; + +#[derive(Default, Clone)] +pub struct LanceExecutionOptions { + pub use_spilling: bool, + pub mem_pool_size: Option, + pub max_temp_directory_size: Option, + pub batch_size: Option, + pub target_partition: Option, + pub execution_stats_callback: Option, + pub skip_logging: bool, +} + +impl std::fmt::Debug for LanceExecutionOptions { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("LanceExecutionOptions") + .field("use_spilling", &self.use_spilling) + .field("mem_pool_size", &self.mem_pool_size) + .field("max_temp_directory_size", &self.max_temp_directory_size) + .field("batch_size", &self.batch_size) + .field("target_partition", &self.target_partition) + .field("skip_logging", &self.skip_logging) + .field( + "execution_stats_callback", + &self.execution_stats_callback.is_some(), + ) + .finish() + } +} + +const DEFAULT_LANCE_MEM_POOL_SIZE_PER_PARTITION: u64 = 150 * 1024 * 1024; +const DEFAULT_LANCE_MAX_TEMP_DIRECTORY_SIZE: u64 = 100 * 1024 * 1024 * 1024; // 100GB + +impl LanceExecutionOptions { + pub fn mem_pool_size(&self) -> u64 { + let num_partitions = self.target_partition.unwrap_or(1) as u64; + self.mem_pool_size.unwrap_or_else(|| { + std::env::var("LANCE_MEM_POOL_SIZE") + .map(|s| match s.parse::() { + Ok(v) => v, + Err(e) => { + warn!("Failed to parse LANCE_MEM_POOL_SIZE: {}, using default", e); + DEFAULT_LANCE_MEM_POOL_SIZE_PER_PARTITION * num_partitions + } + }) + .unwrap_or(DEFAULT_LANCE_MEM_POOL_SIZE_PER_PARTITION * num_partitions) + }) + } + + pub fn max_temp_directory_size(&self) -> u64 { + self.max_temp_directory_size.unwrap_or_else(|| { + std::env::var("LANCE_MAX_TEMP_DIRECTORY_SIZE") + .map(|s| match s.parse::() { + Ok(v) => v, + Err(e) => { + warn!( + "Failed to parse LANCE_MAX_TEMP_DIRECTORY_SIZE: {}, using default", + e + ); + DEFAULT_LANCE_MAX_TEMP_DIRECTORY_SIZE + } + }) + .unwrap_or(DEFAULT_LANCE_MAX_TEMP_DIRECTORY_SIZE) + }) + } + + pub fn use_spilling(&self) -> bool { + if !self.use_spilling { + return false; + } + std::env::var("LANCE_BYPASS_SPILLING") + .map(|_| { + info!("Bypassing spilling because LANCE_BYPASS_SPILLING is set"); + false + }) + .unwrap_or(true) + } +} + +pub fn new_session_context(options: &LanceExecutionOptions) -> SessionContext { + let mut session_config = SessionConfig::new(); + let mut runtime_env_builder = RuntimeEnvBuilder::new(); + if let Some(target_partition) = options.target_partition { + session_config = session_config.with_target_partitions(target_partition); + } + if options.use_spilling() { + // The default 10MB sort spill reservation seems to be too small for many common cases. + // + // There currently is no reasonable guidance provided by DataFusion for setting this value. + // We bump this to 40MB but try a smaller value if the mem pool is small. + let sort_spill_reservation_bytes = + (options.mem_pool_size() / 3).min(40 * 1024 * 1024) as usize; + session_config = + session_config.with_sort_spill_reservation_bytes(sort_spill_reservation_bytes); + let disk_manager_builder = DiskManagerBuilder::default() + .with_max_temp_directory_size(options.max_temp_directory_size()); + runtime_env_builder = runtime_env_builder + .with_disk_manager_builder(disk_manager_builder) + .with_memory_pool(Arc::new(TrackConsumersPool::new( + FairSpillPool::new(options.mem_pool_size() as usize), + NonZero::try_from(16).unwrap(), + ))); + } + let runtime_env = runtime_env_builder.build_arc().unwrap(); + + let ctx = SessionContext::new_with_config_rt(session_config, runtime_env); + register_functions(&ctx); + + ctx +} + +/// Cache key for session contexts based on resolved configuration values. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +struct SessionContextCacheKey { + mem_pool_size: u64, + max_temp_directory_size: u64, + target_partition: Option, + use_spilling: bool, +} + +impl SessionContextCacheKey { + fn from_options(options: &LanceExecutionOptions) -> Self { + Self { + mem_pool_size: options.mem_pool_size(), + max_temp_directory_size: options.max_temp_directory_size(), + target_partition: options.target_partition, + use_spilling: options.use_spilling(), + } + } +} + +struct CachedSessionContext { + context: SessionContext, + last_access: std::time::Instant, +} + +fn get_session_cache() -> &'static Mutex> { + static SESSION_CACHE: OnceLock>> = + OnceLock::new(); + SESSION_CACHE.get_or_init(|| Mutex::new(HashMap::new())) +} + +fn get_max_cache_size() -> usize { + const DEFAULT_CACHE_SIZE: usize = 4; + static MAX_CACHE_SIZE: OnceLock = OnceLock::new(); + *MAX_CACHE_SIZE.get_or_init(|| { + std::env::var("LANCE_SESSION_CACHE_SIZE") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(DEFAULT_CACHE_SIZE) + }) +} + +pub fn get_session_context(options: &LanceExecutionOptions) -> SessionContext { + let key = SessionContextCacheKey::from_options(options); + let mut cache = get_session_cache() + .lock() + .unwrap_or_else(|e| e.into_inner()); + + // If key exists, update access time and return + if let Some(entry) = cache.get_mut(&key) { + entry.last_access = std::time::Instant::now(); + return entry.context.clone(); + } + + // Evict least recently used entry if cache is full + if cache.len() >= get_max_cache_size() + && let Some(lru_key) = cache + .iter() + .min_by_key(|(_, v)| v.last_access) + .map(|(k, _)| k.clone()) + { + cache.remove(&lru_key); + } + + let context = new_session_context(options); + cache.insert( + key, + CachedSessionContext { + context: context.clone(), + last_access: std::time::Instant::now(), + }, + ); + context +} + +fn get_task_context( + session_ctx: &SessionContext, + options: &LanceExecutionOptions, +) -> Arc { + let mut state = session_ctx.state(); + if let Some(batch_size) = options.batch_size.as_ref() { + state.config_mut().options_mut().execution.batch_size = *batch_size; + } + + state.task_ctx() +} + +#[derive(Default, Clone, Debug, PartialEq, Eq)] +pub struct ExecutionSummaryCounts { + /// The number of I/O operations performed + pub iops: usize, + /// The number of requests made to the storage layer (may be larger or smaller than iops + /// depending on coalescing configuration) + pub requests: usize, + /// The number of bytes read during the execution of the plan + pub bytes_read: usize, + /// The number of top-level indices loaded + pub indices_loaded: usize, + /// The number of index partitions loaded + pub parts_loaded: usize, + /// The number of index comparisons performed (the exact meaning depends on the index type) + pub index_comparisons: usize, + /// Additional metrics for more detailed statistics. These are subject to change in the future + /// and should only be used for debugging purposes. + /// + /// Newer metrics (e.g. [`INDEX_CACHE_HITS_METRIC`], [`INDEX_CACHE_MISSES_METRIC`]) are added + /// here rather than as `pub` fields, so this struct stays backwards compatible for callers + /// that construct or destructure it. Prefer the typed accessors below. + pub all_counts: HashMap, + /// Additional time metrics for more detailed statistics, stored in nanoseconds. + /// These are subject to change in the future and should only be used for debugging purposes. + pub all_times: HashMap, +} + +impl ExecutionSummaryCounts { + /// Number of index cache page lookups where the loader was not executed + /// (per-page granularity). + /// + /// A "hit" is any page-level lookup at an instrumented cache boundary that + /// did not run the loader on this call. That covers both a true cache hit + /// on an already-populated entry and a coalesced concurrent load where an + /// in-flight loader started by a different caller produced the value. + /// + /// Instrumented boundaries in this release: + /// BTree page, IVF partition (v2, `write_cache=true` scan path), inverted + /// posting list (grouped and per-token), inverted per-token metadata + /// (`PostingMetadataKey`), inverted phrase positions (`PositionKey`), + /// bitmap posting (Equals / Range / IsIn), ngram posting, and rtree page + /// / null slot. + /// + /// Caveats: + /// * IVF v2 streaming scans and legacy v1 IVF partitions run + /// `load_partition` with `write_cache=false`. Those loads always execute + /// the loader and never write the result back, so they are reported as a + /// miss on every call. See [`Self::index_cache_hit_ratio`]. + /// * A cold posting-list lookup on the grouped inverted layout can record + /// up to two misses (posting-list group + per-token metadata) for a + /// single term. + /// + /// Other index cache boundaries such as HNSW graph pages and quantizer + /// codebooks are not yet instrumented; a scan that only touches those + /// paths returns `0` here. + pub fn index_cache_hits(&self) -> usize { + self.all_counts + .get(INDEX_CACHE_HITS_METRIC) + .copied() + .unwrap_or(0) + } + + /// Number of index cache page lookups that had to execute the loader + /// (per-page granularity). + /// + /// A "miss" is any page-level lookup at an instrumented cache boundary + /// where the loader ran, i.e. the page was not resident and had to be + /// materialised (typically from storage). See + /// [`Self::index_cache_hits`] for the paired counter and the list of + /// instrumented boundaries. + pub fn index_cache_misses(&self) -> usize { + self.all_counts + .get(INDEX_CACHE_MISSES_METRIC) + .copied() + .unwrap_or(0) + } + + /// Ratio of index cache hits to total lookups. Returns `0.0` when no lookups + /// were recorded in this scan. + /// + /// This ratio only reflects paths that write their result back to the + /// index cache. Streaming scans (IVF v2 `write_cache=false` and legacy v1 + /// IVF `load_partition_stream`) intentionally bypass the cache and are + /// counted as misses on every call, so a workload dominated by streaming + /// vector scans will report a hit ratio near `0.0` regardless of cache + /// size. + pub fn index_cache_hit_ratio(&self) -> f32 { + // Widen to u128 before summing so a pathological (hits + misses) + // overflow can't panic in debug builds nor wrap in release builds. + let hits = self.index_cache_hits() as u128; + let total = hits + self.index_cache_misses() as u128; + if total == 0 { + 0.0 + } else { + hits as f32 / total as f32 + } + } +} + +pub fn collect_execution_metrics(node: &dyn ExecutionPlan, counts: &mut ExecutionSummaryCounts) { + if let Some(metrics) = node.metrics() { + for (metric_name, count) in metrics.iter_counts() { + match metric_name.as_ref() { + IOPS_METRIC => counts.iops += count.value(), + REQUESTS_METRIC => counts.requests += count.value(), + BYTES_READ_METRIC => counts.bytes_read += count.value(), + INDICES_LOADED_METRIC => counts.indices_loaded += count.value(), + PARTS_LOADED_METRIC => counts.parts_loaded += count.value(), + INDEX_COMPARISONS_METRIC => counts.index_comparisons += count.value(), + _ => { + let existing = counts + .all_counts + .entry(metric_name.as_ref().to_string()) + .or_insert(0); + *existing += count.value(); + } + } + } + for (metric_name, time) in metrics.iter_times() { + let existing = counts + .all_times + .entry(metric_name.as_ref().to_string()) + .or_insert(0); + *existing += time.value(); + } + // Include gauge-based I/O metrics (some nodes record I/O as gauges) + for (metric_name, gauge) in metrics.iter_gauges() { + match metric_name.as_ref() { + IOPS_METRIC => counts.iops += gauge.value(), + REQUESTS_METRIC => counts.requests += gauge.value(), + BYTES_READ_METRIC => counts.bytes_read += gauge.value(), + _ => {} + } + } + } + for child in node.children() { + collect_execution_metrics(child.as_ref(), counts); + } +} + +fn report_plan_summary_metrics(plan: &dyn ExecutionPlan, options: &LanceExecutionOptions) { + let output_rows = plan + .metrics() + .map(|m| m.output_rows().unwrap_or(0)) + .unwrap_or(0); + let mut counts = ExecutionSummaryCounts::default(); + collect_execution_metrics(plan, &mut counts); + if !options.skip_logging { + tracing::info!( + target: TRACE_EXECUTION, + r#type = EXECUTION_PLAN_RUN, + plan_summary = display_plan_one_liner(plan), + output_rows, + iops = counts.iops, + requests = counts.requests, + bytes_read = counts.bytes_read, + indices_loaded = counts.indices_loaded, + parts_loaded = counts.parts_loaded, + index_comparisons = counts.index_comparisons, + index_cache_hits = counts.index_cache_hits(), + index_cache_misses = counts.index_cache_misses(), + ); + } + if let Some(callback) = options.execution_stats_callback.as_ref() { + callback(&counts); + } +} + +/// Create a one-line rough summary of the given execution plan. +/// +/// The summary just shows the name of the operators in the plan. It omits any +/// details such as parameters or schema information. +/// +/// Example: `Projection(Take(CoalesceBatches(Filter(LanceScan))))` +fn display_plan_one_liner(plan: &dyn ExecutionPlan) -> String { + let mut output = String::new(); + + display_plan_one_liner_impl(plan, &mut output); + + output +} + +fn display_plan_one_liner_impl(plan: &dyn ExecutionPlan, output: &mut String) { + // Remove the "Exec" suffix from the plan name if present for brevity + let name = plan.name().trim_end_matches("Exec"); + output.push_str(name); + + let children = plan.children(); + if !children.is_empty() { + output.push('('); + for (i, child) in children.iter().enumerate() { + if i > 0 { + output.push(','); + } + display_plan_one_liner_impl(child.as_ref(), output); + } + output.push(')'); + } +} + +/// Executes a plan using default session & runtime configuration +/// +/// Only executes a single partition. Panics if the plan has more than one partition. +pub fn execute_plan( + plan: Arc, + options: LanceExecutionOptions, +) -> Result { + if !options.skip_logging { + debug!( + "Executing plan:\n{}", + DisplayableExecutionPlan::new(plan.as_ref()).indent(true) + ); + } + + let session_ctx = get_session_context(&options); + + // Coalesce to a single partition if the optimizer left more than one. + // EnforceDistribution may remove RepartitionExec(1) nodes when the parent + // declares UnspecifiedDistribution, leaving multi-partition plans here. + // + // If the plan carries an output ordering (e.g. a top-k `SortExec` whose + // result was later repartitioned to parallelize downstream operators), + // a plain `CoalescePartitionsExec` would scramble that order because it + // merges partitions in scheduling-dependent order. Use an order-preserving + // merge in that case instead, mirroring what `EnforceDistribution` itself + // does when it needs to merge an ordered, multi-partition plan. + let plan: Arc = if plan.properties().partitioning.partition_count() == 1 { + plan + } else if let Some(ordering) = plan.output_ordering() { + Arc::new(SortPreservingMergeExec::new(ordering.clone(), plan)) + } else { + Arc::new(CoalescePartitionsExec::new(plan)) + }; + + let stream = plan.execute(0, get_task_context(&session_ctx, &options))?; + + let schema = stream.schema(); + let stream = stream.finally(move || { + if !options.skip_logging || options.execution_stats_callback.is_some() { + report_plan_summary_metrics(plan.as_ref(), &options); + } + }); + Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream))) +} + +pub async fn analyze_plan( + plan: Arc, + options: LanceExecutionOptions, +) -> Result { + // This is needed as AnalyzeExec launches a thread task per + // partition, and we want these to be connected to the parent span + let plan = Arc::new(TracedExec::new(plan, Span::current())); + + let schema = plan.schema(); + // TODO(tsaucer) I chose SUMMARY here but do we also want DEV? + let analyze = Arc::new(AnalyzeExec::new( + true, + true, + vec![MetricType::Summary], + None, + plan, + schema, + )); + + let session_ctx = get_session_context(&options); + assert_eq!(analyze.properties().partitioning.partition_count(), 1); + let mut stream = analyze + .execute(0, get_task_context(&session_ctx, &options)) + .map_err(|err| Error::io(format!("Failed to execute analyze plan: {}", err)))?; + + // fully execute the plan + while (stream.next().await).is_some() {} + + let result = format_plan(analyze); + Ok(result) +} + +pub fn format_plan(plan: Arc) -> String { + /// A visitor which calculates additional metrics for all the plans. + struct CalculateVisitor { + highest_index: usize, + index_to_elapsed: HashMap, + } + + /// Result of calculating metrics for a subtree + struct SubtreeMetrics { + min_start: Option>, + max_end: Option>, + } + + impl CalculateVisitor { + fn calculate_metrics(&mut self, plan: &Arc) -> SubtreeMetrics { + self.highest_index += 1; + let plan_index = self.highest_index; + + // Get timestamps for this node + let (mut min_start, mut max_end) = Self::node_timerange(plan); + + // Accumulate from children + for child in plan.children() { + let child_metrics = self.calculate_metrics(child); + min_start = Self::min_option(min_start, child_metrics.min_start); + max_end = Self::max_option(max_end, child_metrics.max_end); + } + + // Calculate wall clock duration for this subtree (only if we have timestamps) + let elapsed = match (min_start, max_end) { + (Some(start), Some(end)) => Some((end - start).to_std().unwrap_or_default()), + _ => None, + }; + + if let Some(e) = elapsed { + self.index_to_elapsed.insert(plan_index, e); + } + + SubtreeMetrics { min_start, max_end } + } + + fn node_timerange( + plan: &Arc, + ) -> (Option>, Option>) { + let Some(metrics) = plan.metrics() else { + return (None, None); + }; + let min_start = metrics + .iter() + .filter_map(|m| match m.value() { + MetricValue::StartTimestamp(ts) => ts.value(), + _ => None, + }) + .min(); + let max_end = metrics + .iter() + .filter_map(|m| match m.value() { + MetricValue::EndTimestamp(ts) => ts.value(), + _ => None, + }) + .max(); + (min_start, max_end) + } + + fn min_option(a: Option>, b: Option>) -> Option> { + [a, b].into_iter().flatten().min() + } + + fn max_option(a: Option>, b: Option>) -> Option> { + [a, b].into_iter().flatten().max() + } + } + + /// A visitor which prints out all the plans. + struct PrintVisitor { + highest_index: usize, + indent: usize, + } + impl PrintVisitor { + fn write_output( + &mut self, + plan: &Arc, + f: &mut Formatter, + calcs: &CalculateVisitor, + ) -> std::fmt::Result { + self.highest_index += 1; + write!(f, "{:indent$}", "", indent = self.indent * 2)?; + + // Format the plan description + let displayable = + datafusion::physical_plan::display::DisplayableExecutionPlan::new(plan.as_ref()); + let plan_str = displayable.one_line().to_string(); + let plan_str = plan_str.trim(); + + // Write operator with elapsed time inserted after the name + match calcs.index_to_elapsed.get(&self.highest_index) { + Some(elapsed) => match plan_str.find(": ") { + Some(i) => write!( + f, + "{}: elapsed={elapsed:?}, {}", + &plan_str[..i], + &plan_str[i + 2..] + )?, + None => write!(f, "{plan_str}, elapsed={elapsed:?}")?, + }, + None => write!(f, "{plan_str}")?, + } + + if let Some(metrics) = plan.metrics() { + let metrics = metrics + .aggregate_by_name() + .sorted_for_display() + .timestamps_removed(); + + write!(f, ", metrics=[{metrics}]")?; + } else { + write!(f, ", metrics=[]")?; + } + writeln!(f)?; + self.indent += 1; + for child in plan.children() { + self.write_output(child, f, calcs)?; + } + self.indent -= 1; + std::fmt::Result::Ok(()) + } + } + // A wrapper which prints out a plan. + struct PrintWrapper { + plan: Arc, + } + impl fmt::Display for PrintWrapper { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + let mut calcs = CalculateVisitor { + highest_index: 0, + index_to_elapsed: HashMap::new(), + }; + calcs.calculate_metrics(&self.plan); + let mut prints = PrintVisitor { + highest_index: 0, + indent: 0, + }; + prints.write_output(&self.plan, f, &calcs) + } + } + let wrapper = PrintWrapper { plan }; + format!("{}", wrapper) +} + +pub trait SessionContextExt { + /// Creates a DataFrame for reading a stream of data + /// + /// This dataframe may only be queried once, future queries will fail + fn read_one_shot( + &self, + data: SendableRecordBatchStream, + ) -> datafusion::common::Result; +} + +pub struct OneShotPartitionStream { + data: Arc>>, + schema: Arc, +} + +impl std::fmt::Debug for OneShotPartitionStream { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let data = self.data.lock().unwrap(); + f.debug_struct("OneShotPartitionStream") + .field("exhausted", &data.is_none()) + .field("schema", self.schema.as_ref()) + .finish() + } +} + +impl OneShotPartitionStream { + pub fn new(data: SendableRecordBatchStream) -> Self { + let schema = data.schema(); + Self { + data: Arc::new(Mutex::new(Some(data))), + schema, + } + } +} + +impl PartitionStream for OneShotPartitionStream { + fn schema(&self) -> &arrow_schema::SchemaRef { + &self.schema + } + + fn execute(&self, _ctx: Arc) -> SendableRecordBatchStream { + let mut stream = self.data.lock().unwrap(); + stream + .take() + .expect("Attempt to consume a one shot dataframe multiple times") + } +} + +impl SessionContextExt for SessionContext { + fn read_one_shot( + &self, + data: SendableRecordBatchStream, + ) -> datafusion::common::Result { + let schema = data.schema(); + let part_stream = Arc::new(OneShotPartitionStream::new(data)); + let provider = StreamingTable::try_new(schema, vec![part_stream])?; + self.read_table(Arc::new(provider)) + } +} + +/// Scan a [`TableProvider`] into a single-partition [`SendableRecordBatchStream`]. +/// +/// Multi-partition providers are coalesced into a single partition. This adapts a +/// re-scannable provider back into the one stream the writer pipeline consumes; +/// re-scanning the same provider (e.g. on a write retry) yields a fresh stream. +/// +/// # Examples +/// +/// ``` +/// # use std::sync::Arc; +/// # use arrow_array::{Int32Array, RecordBatch}; +/// # use arrow_schema::{DataType, Field, Schema}; +/// # use datafusion::catalog::TableProvider; +/// # use datafusion::datasource::MemTable; +/// # use futures::TryStreamExt; +/// # use lance_datafusion::exec::provider_to_stream; +/// # #[tokio::main] +/// # async fn main() -> Result<(), Box> { +/// let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); +/// let batch = +/// RecordBatch::try_new(schema.clone(), vec![Arc::new(Int32Array::from(vec![1, 2, 3]))])?; +/// let provider: Arc = Arc::new(MemTable::try_new(schema, vec![vec![batch]])?); +/// +/// // A re-scannable provider yields a fresh stream on each call. +/// let batches: Vec = provider_to_stream(provider).await?.try_collect().await?; +/// assert_eq!(batches.iter().map(|b| b.num_rows()).sum::(), 3); +/// # Ok(()) +/// # } +/// ``` +pub async fn provider_to_stream( + provider: Arc, +) -> Result { + let ctx = SessionContext::new(); + let plan = provider.scan(&ctx.state(), None, &[], None).await?; + let plan: Arc = + if plan.properties().output_partitioning().partition_count() > 1 { + Arc::new(CoalescePartitionsExec::new(plan)) + } else { + plan + }; + Ok(plan.execute(0, ctx.task_ctx())?) +} + +#[derive(Clone, Debug)] +pub struct StrictBatchSizeExec { + input: Arc, + batch_size: usize, +} + +impl StrictBatchSizeExec { + pub fn new(input: Arc, batch_size: usize) -> Self { + Self { input, batch_size } + } +} + +impl DisplayAs for StrictBatchSizeExec { + fn fmt_as( + &self, + _t: datafusion::physical_plan::DisplayFormatType, + f: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + write!(f, "StrictBatchSizeExec") + } +} + +impl ExecutionPlan for StrictBatchSizeExec { + fn name(&self) -> &str { + "StrictBatchSizeExec" + } + + fn properties(&self) -> &Arc { + self.input.properties() + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.input] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> datafusion_common::Result> { + Ok(Arc::new(Self { + input: children[0].clone(), + batch_size: self.batch_size, + })) + } + + fn execute( + &self, + partition: usize, + context: Arc, + ) -> datafusion_common::Result { + let stream = self.input.execute(partition, context)?; + let schema = stream.schema(); + let stream = StrictBatchSizeStream::new(stream, self.batch_size); + Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream))) + } + + fn maintains_input_order(&self) -> Vec { + vec![true] + } + + fn benefits_from_input_partitioning(&self) -> Vec { + vec![false] + } + + fn partition_statistics( + &self, + partition: Option, + ) -> datafusion_common::Result> { + self.input.partition_statistics(partition) + } + + fn cardinality_effect(&self) -> CardinalityEffect { + CardinalityEffect::Equal + } + + fn supports_limit_pushdown(&self) -> bool { + true + } +} + +/// Exec node that rechunks batches so no output batch exceeds `max_bytes`. +/// +/// # Why this exists +/// +/// DataFusion's sort operator cannot handle batches larger than the memory +/// pool size. When upstream operators produce very large batches this can +/// cause the sort to fail. This node caps batch sizes +/// *before* the sort so the operation succeeds. The trade-off is a +/// potentially expensive deep copy of the batch data — see below — but that +/// is preferable to failing the operation entirely. This workaround may +/// become unnecessary if a fix is upstreamed to DataFusion. +/// +/// # Deep copy +/// +/// After slicing a RecordBatch, `get_array_memory_size` still reports the +/// size of the *original* backing buffers, not the slice. To get accurate +/// sizes the slices must be deep-copied. This is a last resort and can be +/// expensive for large batches, but the deep copy is only performed when a +/// batch actually needs to be sliced — batches that are already within the +/// target range pass through at zero cost. +/// +/// If a single row exceeds `max_bytes`, execution fails with an error. +#[derive(Clone, Debug)] +pub struct HardCapBatchSizeExec { + input: Arc, + max_bytes: usize, +} + +impl HardCapBatchSizeExec { + pub fn new(input: Arc, max_bytes: usize) -> Self { + Self { input, max_bytes } + } +} + +impl DisplayAs for HardCapBatchSizeExec { + fn fmt_as( + &self, + _t: datafusion::physical_plan::DisplayFormatType, + f: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + write!(f, "HardCapBatchSizeExec(max_bytes={})", self.max_bytes) + } +} + +impl ExecutionPlan for HardCapBatchSizeExec { + fn name(&self) -> &str { + "HardCapBatchSizeExec" + } + + fn properties(&self) -> &Arc { + self.input.properties() + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.input] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> datafusion_common::Result> { + Ok(Arc::new(Self { + input: children[0].clone(), + max_bytes: self.max_bytes, + })) + } + + fn execute( + &self, + partition: usize, + context: Arc, + ) -> datafusion_common::Result { + let stream = self.input.execute(partition, context)?; + let schema = stream.schema(); + let max_bytes = self.max_bytes; + let rechunked = lance_arrow::stream::rechunk_stream_by_size_deep_copy( + stream, + schema.clone(), + 0, + max_bytes, + ); + // Check that no single-row batch exceeds the limit. + let validated = rechunked.map(move |result| { + let batch = result?; + if batch.num_rows() == 1 && batch.get_array_memory_size() > max_bytes { + return Err(DataFusionError::External(Box::new(Error::invalid_input( + format!( + "a single row is {} bytes which exceeds the maximum allowed batch \ + size of {} bytes", + batch.get_array_memory_size(), + max_bytes, + ), + )))); + } + Ok(batch) + }); + Ok(Box::pin(RecordBatchStreamAdapter::new(schema, validated))) + } + + fn maintains_input_order(&self) -> Vec { + vec![true] + } + + fn benefits_from_input_partitioning(&self) -> Vec { + vec![false] + } + + fn partition_statistics( + &self, + partition: Option, + ) -> datafusion_common::Result> { + self.input.partition_statistics(partition) + } + + fn cardinality_effect(&self) -> CardinalityEffect { + CardinalityEffect::Equal + } + + fn supports_limit_pushdown(&self) -> bool { + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // Serialize cache tests since they share global state + static CACHE_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + #[test] + fn test_session_context_cache() { + let _lock = CACHE_TEST_LOCK.lock().unwrap(); + let cache = get_session_cache(); + + // Clear any existing entries from other tests + cache.lock().unwrap().clear(); + + // Create first session with default options + let opts1 = LanceExecutionOptions::default(); + let _ctx1 = get_session_context(&opts1); + + { + let cache_guard = cache.lock().unwrap(); + assert_eq!(cache_guard.len(), 1); + } + + // Same options should reuse cached session (no new entry) + let _ctx1_again = get_session_context(&opts1); + { + let cache_guard = cache.lock().unwrap(); + assert_eq!(cache_guard.len(), 1); + } + + // Different options should create new entry + let opts2 = LanceExecutionOptions { + use_spilling: true, + ..Default::default() + }; + let _ctx2 = get_session_context(&opts2); + { + let cache_guard = cache.lock().unwrap(); + assert_eq!(cache_guard.len(), 2); + } + } + + #[test] + fn test_session_context_cache_lru_eviction() { + let _lock = CACHE_TEST_LOCK.lock().unwrap(); + let cache = get_session_cache(); + + // Clear any existing entries from other tests + cache.lock().unwrap().clear(); + + // Create 4 different configurations to fill the cache + let configs: Vec = (0..4) + .map(|i| LanceExecutionOptions { + mem_pool_size: Some((i + 1) as u64 * 1024 * 1024), + ..Default::default() + }) + .collect(); + + for config in &configs { + let _ctx = get_session_context(config); + } + + { + let cache_guard = cache.lock().unwrap(); + assert_eq!(cache_guard.len(), 4); + } + + // Access config[0] to make it more recently used than config[1] + // (config[0] was inserted first, so without this access it would be evicted) + std::thread::sleep(std::time::Duration::from_millis(1)); + let _ctx = get_session_context(&configs[0]); + + // Add a 5th configuration - should evict config[1] (now least recently used) + let opts5 = LanceExecutionOptions { + mem_pool_size: Some(5 * 1024 * 1024), + ..Default::default() + }; + let _ctx5 = get_session_context(&opts5); + + { + let cache_guard = cache.lock().unwrap(); + assert_eq!(cache_guard.len(), 4); + + // config[0] should still be present (was accessed recently) + let key0 = SessionContextCacheKey::from_options(&configs[0]); + assert!( + cache_guard.contains_key(&key0), + "config[0] should still be cached after recent access" + ); + + // config[1] should be evicted (was least recently used) + let key1 = SessionContextCacheKey::from_options(&configs[1]); + assert!( + !cache_guard.contains_key(&key1), + "config[1] should have been evicted" + ); + + // New config should be present + let key5 = SessionContextCacheKey::from_options(&opts5); + assert!( + cache_guard.contains_key(&key5), + "new config should be cached" + ); + } + } + + #[test] + fn test_mem_pool_size_scales_with_partitions() { + let default_per_partition = DEFAULT_LANCE_MEM_POOL_SIZE_PER_PARTITION; + + // No partitions specified → defaults to 1 partition + let opts = LanceExecutionOptions::default(); + assert_eq!(opts.mem_pool_size(), default_per_partition); + + // 4 partitions → 4x the per-partition size + let opts = LanceExecutionOptions { + target_partition: Some(4), + ..Default::default() + }; + assert_eq!(opts.mem_pool_size(), default_per_partition * 4); + + // 8 partitions → 8x the per-partition size + let opts = LanceExecutionOptions { + target_partition: Some(8), + ..Default::default() + }; + assert_eq!(opts.mem_pool_size(), default_per_partition * 8); + + // Explicit mem_pool_size is not scaled + let opts = LanceExecutionOptions { + mem_pool_size: Some(50 * 1024 * 1024), + target_partition: Some(8), + ..Default::default() + }; + assert_eq!(opts.mem_pool_size(), 50 * 1024 * 1024); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/expr.rs b/lance-artifact/rust/lance-datafusion/src/expr.rs new file mode 100644 index 000000000..a0da34ba2 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/expr.rs @@ -0,0 +1,886 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for working with datafusion expressions + +use std::sync::Arc; + +use arrow::compute::cast; +use arrow_array::{ArrayRef, cast::AsArray}; +use arrow_schema::{DataType, TimeUnit}; +use datafusion_common::ScalarValue; + +const MS_PER_DAY: i64 = 86400000; + +// This is slightly tedious but when we convert expressions from SQL strings to logical +// datafusion expressions there is no type coercion that happens. In other words "x = 7" +// will always yield "x = 7_u64" regardless of the type of the column "x". As a result, we +// need to do that literal coercion ourselves. +pub fn safe_coerce_scalar(value: &ScalarValue, ty: &DataType) -> Option { + // A dictionary target coerces the value to the dictionary's value type and + // re-wraps it as a dictionary literal. Only an untyped `ScalarValue::Null` + // keeps its untyped form, matching the behavior for all other targets; a + // *typed* null (e.g. `Utf8(None)`) is coerced and wrapped like any other + // value so it produces a `Dictionary(..)` literal that matches the column. + if let DataType::Dictionary(key_type, value_type) = ty { + if matches!(value, ScalarValue::Null) { + return Some(value.clone()); + } + let inner = safe_coerce_scalar(value, value_type)?; + return Some(ScalarValue::Dictionary(key_type.clone(), Box::new(inner))); + } + match value { + ScalarValue::Int8(val) => match ty { + DataType::Int8 => Some(value.clone()), + DataType::Int16 => val.map(|v| ScalarValue::Int16(Some(i16::from(v)))), + DataType::Int32 => val.map(|v| ScalarValue::Int32(Some(i32::from(v)))), + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(i64::from(v)))), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => { + val.and_then(|v| u32::try_from(v).map(|v| ScalarValue::UInt32(Some(v))).ok()) + } + DataType::UInt64 => { + val.and_then(|v| u64::try_from(v).map(|v| ScalarValue::UInt64(Some(v))).ok()) + } + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(f32::from(v)))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(f64::from(v)))), + _ => None, + }, + ScalarValue::Int16(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => Some(value.clone()), + DataType::Int32 => val.map(|v| ScalarValue::Int32(Some(i32::from(v)))), + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(i64::from(v)))), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => { + val.and_then(|v| u32::try_from(v).map(|v| ScalarValue::UInt32(Some(v))).ok()) + } + DataType::UInt64 => { + val.and_then(|v| u64::try_from(v).map(|v| ScalarValue::UInt64(Some(v))).ok()) + } + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(f32::from(v)))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(f64::from(v)))), + _ => None, + }, + ScalarValue::Int32(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => { + val.and_then(|v| i16::try_from(v).map(|v| ScalarValue::Int16(Some(v))).ok()) + } + DataType::Int32 => Some(value.clone()), + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(i64::from(v)))), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => { + val.and_then(|v| u32::try_from(v).map(|v| ScalarValue::UInt32(Some(v))).ok()) + } + DataType::UInt64 => { + val.and_then(|v| u64::try_from(v).map(|v| ScalarValue::UInt64(Some(v))).ok()) + } + // These conversions are inherently lossy as the full range of i32 cannot + // be represented in f32. However, there is no f32::TryFrom(i32) and its not + // clear users would want that anyways + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(v as f32))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(v as f64))), + _ => None, + }, + ScalarValue::Int64(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => { + val.and_then(|v| i16::try_from(v).map(|v| ScalarValue::Int16(Some(v))).ok()) + } + DataType::Int32 => { + val.and_then(|v| i32::try_from(v).map(|v| ScalarValue::Int32(Some(v))).ok()) + } + DataType::Int64 => Some(value.clone()), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => { + val.and_then(|v| u32::try_from(v).map(|v| ScalarValue::UInt32(Some(v))).ok()) + } + DataType::UInt64 => { + val.and_then(|v| u64::try_from(v).map(|v| ScalarValue::UInt64(Some(v))).ok()) + } + // See above warning about lossy float conversion + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(v as f32))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(v as f64))), + DataType::Decimal128(_, _) | DataType::Decimal256(_, _) => value.cast_to(ty).ok(), + _ => None, + }, + ScalarValue::UInt8(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => val.map(|v| ScalarValue::Int16(Some(v.into()))), + DataType::Int32 => val.map(|v| ScalarValue::Int32(Some(v.into()))), + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(v.into()))), + DataType::UInt8 => Some(value.clone()), + DataType::UInt16 => val.map(|v| ScalarValue::UInt16(Some(u16::from(v)))), + DataType::UInt32 => val.map(|v| ScalarValue::UInt32(Some(u32::from(v)))), + DataType::UInt64 => val.map(|v| ScalarValue::UInt64(Some(u64::from(v)))), + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(f32::from(v)))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(f64::from(v)))), + _ => None, + }, + ScalarValue::UInt16(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => { + val.and_then(|v| i16::try_from(v).map(|v| ScalarValue::Int16(Some(v))).ok()) + } + DataType::Int32 => val.map(|v| ScalarValue::Int32(Some(v.into()))), + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(v.into()))), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => Some(value.clone()), + DataType::UInt32 => val.map(|v| ScalarValue::UInt32(Some(u32::from(v)))), + DataType::UInt64 => val.map(|v| ScalarValue::UInt64(Some(u64::from(v)))), + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(f32::from(v)))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(f64::from(v)))), + _ => None, + }, + ScalarValue::UInt32(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => { + val.and_then(|v| i16::try_from(v).map(|v| ScalarValue::Int16(Some(v))).ok()) + } + DataType::Int32 => { + val.and_then(|v| i32::try_from(v).map(|v| ScalarValue::Int32(Some(v))).ok()) + } + DataType::Int64 => val.map(|v| ScalarValue::Int64(Some(v.into()))), + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => Some(value.clone()), + DataType::UInt64 => val.map(|v| ScalarValue::UInt64(Some(u64::from(v)))), + // See above warning about lossy float conversion + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(v as f32))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(v as f64))), + _ => None, + }, + ScalarValue::UInt64(val) => match ty { + DataType::Int8 => { + val.and_then(|v| i8::try_from(v).map(|v| ScalarValue::Int8(Some(v))).ok()) + } + DataType::Int16 => { + val.and_then(|v| i16::try_from(v).map(|v| ScalarValue::Int16(Some(v))).ok()) + } + DataType::Int32 => { + val.and_then(|v| i32::try_from(v).map(|v| ScalarValue::Int32(Some(v))).ok()) + } + DataType::Int64 => { + val.and_then(|v| i64::try_from(v).map(|v| ScalarValue::Int64(Some(v))).ok()) + } + DataType::UInt8 => { + val.and_then(|v| u8::try_from(v).map(|v| ScalarValue::UInt8(Some(v))).ok()) + } + DataType::UInt16 => { + val.and_then(|v| u16::try_from(v).map(|v| ScalarValue::UInt16(Some(v))).ok()) + } + DataType::UInt32 => { + val.and_then(|v| u32::try_from(v).map(|v| ScalarValue::UInt32(Some(v))).ok()) + } + DataType::UInt64 => Some(value.clone()), + // See above warning about lossy float conversion + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(v as f32))), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(v as f64))), + _ => None, + }, + ScalarValue::Float32(val) => match ty { + DataType::Float32 => Some(value.clone()), + DataType::Float64 => val.map(|v| ScalarValue::Float64(Some(f64::from(v)))), + _ => None, + }, + ScalarValue::Float64(val) => match ty { + DataType::Float32 => val.map(|v| ScalarValue::Float32(Some(v as f32))), + DataType::Float64 => Some(value.clone()), + _ => None, + }, + ScalarValue::Utf8(val) => match ty { + DataType::Utf8 => Some(value.clone()), + DataType::LargeUtf8 => Some(ScalarValue::LargeUtf8(val.clone())), + DataType::Utf8View => Some(ScalarValue::Utf8View(val.clone())), + _ => None, + }, + ScalarValue::LargeUtf8(val) => match ty { + DataType::Utf8 => Some(ScalarValue::Utf8(val.clone())), + DataType::LargeUtf8 => Some(value.clone()), + DataType::Utf8View => Some(ScalarValue::Utf8View(val.clone())), + _ => None, + }, + ScalarValue::Utf8View(val) => match ty { + DataType::Utf8 => Some(ScalarValue::Utf8(val.clone())), + DataType::LargeUtf8 => Some(ScalarValue::LargeUtf8(val.clone())), + DataType::Utf8View => Some(value.clone()), + _ => None, + }, + ScalarValue::Boolean(_) => match ty { + DataType::Boolean => Some(value.clone()), + _ => None, + }, + ScalarValue::Null => Some(value.clone()), + ScalarValue::List(values) => { + let values = values.clone() as ArrayRef; + let new_values = cast(&values, ty).ok()?; + match ty { + DataType::List(_) => { + Some(ScalarValue::List(Arc::new(new_values.as_list().clone()))) + } + DataType::LargeList(_) => Some(ScalarValue::LargeList(Arc::new( + new_values.as_list().clone(), + ))), + DataType::FixedSizeList(_, _) => Some(ScalarValue::FixedSizeList(Arc::new( + new_values.as_fixed_size_list().clone(), + ))), + _ => None, + } + } + ScalarValue::TimestampSecond(seconds, _) => match ty { + DataType::Timestamp(TimeUnit::Second, _) => Some(value.clone()), + DataType::Timestamp(TimeUnit::Millisecond, tz) => seconds + .and_then(|v| v.checked_mul(1000)) + .map(|val| ScalarValue::TimestampMillisecond(Some(val), tz.clone())), + DataType::Timestamp(TimeUnit::Microsecond, tz) => seconds + .and_then(|v| v.checked_mul(1000000)) + .map(|val| ScalarValue::TimestampMicrosecond(Some(val), tz.clone())), + DataType::Timestamp(TimeUnit::Nanosecond, tz) => seconds + .and_then(|v| v.checked_mul(1000000000)) + .map(|val| ScalarValue::TimestampNanosecond(Some(val), tz.clone())), + _ => None, + }, + ScalarValue::TimestampMillisecond(millis, _) => match ty { + DataType::Timestamp(TimeUnit::Second, tz) => { + millis.map(|val| ScalarValue::TimestampSecond(Some(val / 1000), tz.clone())) + } + DataType::Timestamp(TimeUnit::Millisecond, _) => Some(value.clone()), + DataType::Timestamp(TimeUnit::Microsecond, tz) => millis + .and_then(|v| v.checked_mul(1000)) + .map(|val| ScalarValue::TimestampMicrosecond(Some(val), tz.clone())), + DataType::Timestamp(TimeUnit::Nanosecond, tz) => millis + .and_then(|v| v.checked_mul(1000000)) + .map(|val| ScalarValue::TimestampNanosecond(Some(val), tz.clone())), + _ => None, + }, + ScalarValue::TimestampMicrosecond(micros, _) => match ty { + DataType::Timestamp(TimeUnit::Second, tz) => { + micros.map(|val| ScalarValue::TimestampSecond(Some(val / 1000000), tz.clone())) + } + DataType::Timestamp(TimeUnit::Millisecond, tz) => { + micros.map(|val| ScalarValue::TimestampMillisecond(Some(val / 1000), tz.clone())) + } + DataType::Timestamp(TimeUnit::Microsecond, _) => Some(value.clone()), + DataType::Timestamp(TimeUnit::Nanosecond, tz) => micros + .and_then(|v| v.checked_mul(1000)) + .map(|val| ScalarValue::TimestampNanosecond(Some(val), tz.clone())), + _ => None, + }, + ScalarValue::TimestampNanosecond(nanos, _) => { + match ty { + DataType::Timestamp(TimeUnit::Second, tz) => nanos + .map(|val| ScalarValue::TimestampSecond(Some(val / 1000000000), tz.clone())), + DataType::Timestamp(TimeUnit::Millisecond, tz) => nanos + .map(|val| ScalarValue::TimestampMillisecond(Some(val / 1000000), tz.clone())), + DataType::Timestamp(TimeUnit::Microsecond, tz) => { + nanos.map(|val| ScalarValue::TimestampMicrosecond(Some(val / 1000), tz.clone())) + } + DataType::Timestamp(TimeUnit::Nanosecond, _) => Some(value.clone()), + _ => None, + } + } + ScalarValue::Date32(ticks) => match ty { + DataType::Date32 => Some(value.clone()), + DataType::Date64 => Some(ScalarValue::Date64( + ticks.map(|v| i64::from(v) * MS_PER_DAY), + )), + _ => None, + }, + ScalarValue::Date64(ticks) => match ty { + DataType::Date32 => Some(ScalarValue::Date32(ticks.map(|v| (v / MS_PER_DAY) as i32))), + DataType::Date64 => Some(value.clone()), + _ => None, + }, + ScalarValue::Time32Second(seconds) => { + match ty { + DataType::Time32(TimeUnit::Second) => Some(value.clone()), + DataType::Time32(TimeUnit::Millisecond) => { + seconds.map(|val| ScalarValue::Time32Millisecond(Some(val * 1000))) + } + DataType::Time64(TimeUnit::Microsecond) => seconds + .map(|val| ScalarValue::Time64Microsecond(Some(i64::from(val) * 1000000))), + DataType::Time64(TimeUnit::Nanosecond) => seconds + .map(|val| ScalarValue::Time64Nanosecond(Some(i64::from(val) * 1000000000))), + _ => None, + } + } + ScalarValue::Time32Millisecond(millis) => match ty { + DataType::Time32(TimeUnit::Second) => { + millis.map(|val| ScalarValue::Time32Second(Some(val / 1000))) + } + DataType::Time32(TimeUnit::Millisecond) => Some(value.clone()), + DataType::Time64(TimeUnit::Microsecond) => { + millis.map(|val| ScalarValue::Time64Microsecond(Some(i64::from(val) * 1000))) + } + DataType::Time64(TimeUnit::Nanosecond) => { + millis.map(|val| ScalarValue::Time64Nanosecond(Some(i64::from(val) * 1000000))) + } + _ => None, + }, + ScalarValue::Time64Microsecond(micros) => match ty { + DataType::Time32(TimeUnit::Second) => { + micros.map(|val| ScalarValue::Time32Second(Some((val / 1000000) as i32))) + } + DataType::Time32(TimeUnit::Millisecond) => { + micros.map(|val| ScalarValue::Time32Millisecond(Some((val / 1000) as i32))) + } + DataType::Time64(TimeUnit::Microsecond) => Some(value.clone()), + DataType::Time64(TimeUnit::Nanosecond) => { + micros.map(|val| ScalarValue::Time64Nanosecond(Some(val * 1000))) + } + _ => None, + }, + ScalarValue::Time64Nanosecond(nanos) => match ty { + DataType::Time32(TimeUnit::Second) => { + nanos.map(|val| ScalarValue::Time32Second(Some((val / 1000000000) as i32))) + } + DataType::Time32(TimeUnit::Millisecond) => { + nanos.map(|val| ScalarValue::Time32Millisecond(Some((val / 1000000) as i32))) + } + DataType::Time64(TimeUnit::Microsecond) => { + nanos.map(|val| ScalarValue::Time64Microsecond(Some(val / 1000))) + } + DataType::Time64(TimeUnit::Nanosecond) => Some(value.clone()), + _ => None, + }, + ScalarValue::LargeList(values) => { + let values = values.clone() as ArrayRef; + let new_values = cast(&values, ty).ok()?; + match ty { + DataType::List(_) => { + Some(ScalarValue::List(Arc::new(new_values.as_list().clone()))) + } + DataType::LargeList(_) => Some(ScalarValue::LargeList(Arc::new( + new_values.as_list().clone(), + ))), + DataType::FixedSizeList(_, _) => Some(ScalarValue::FixedSizeList(Arc::new( + new_values.as_fixed_size_list().clone(), + ))), + _ => None, + } + } + ScalarValue::FixedSizeList(values) => { + let values = values.clone() as ArrayRef; + let new_values = cast(&values, ty).ok()?; + match ty { + DataType::List(_) => { + Some(ScalarValue::List(Arc::new(new_values.as_list().clone()))) + } + DataType::LargeList(_) => Some(ScalarValue::LargeList(Arc::new( + new_values.as_list().clone(), + ))), + DataType::FixedSizeList(_, _) => Some(ScalarValue::FixedSizeList(Arc::new( + new_values.as_fixed_size_list().clone(), + ))), + _ => None, + } + } + ScalarValue::FixedSizeBinary(len, value) => match ty { + DataType::FixedSizeBinary(len2) => { + if len == len2 { + Some(ScalarValue::FixedSizeBinary(*len, value.clone())) + } else { + None + } + } + DataType::Binary => Some(ScalarValue::Binary(value.clone())), + _ => None, + }, + ScalarValue::Binary(value) => match ty { + DataType::Binary => Some(ScalarValue::Binary(value.clone())), + DataType::LargeBinary => Some(ScalarValue::LargeBinary(value.clone())), + DataType::BinaryView => Some(ScalarValue::BinaryView(value.clone())), + DataType::FixedSizeBinary(len) => { + if let Some(value) = value { + if value.len() == *len as usize { + Some(ScalarValue::FixedSizeBinary(*len, Some(value.clone()))) + } else { + None + } + } else { + None + } + } + _ => None, + }, + ScalarValue::BinaryView(val) => match ty { + DataType::Binary => Some(ScalarValue::Binary(val.clone())), + DataType::LargeBinary => Some(ScalarValue::LargeBinary(val.clone())), + DataType::BinaryView => Some(value.clone()), + _ => None, + }, + // A dictionary-encoded literal (e.g. produced by DataFusion's dictionary + // cast in the scalar-index path) coerces by unwrapping its underlying value. + ScalarValue::Dictionary(_, inner) => safe_coerce_scalar(inner, ty), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_temporal_coerce() { + // Conversion from timestamps in one resolution to timestamps in another resolution is allowed + // s->s + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampSecond(Some(5), None), + &DataType::Timestamp(TimeUnit::Second, None), + ), + Some(ScalarValue::TimestampSecond(Some(5), None)) + ); + // s->ms + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampSecond(Some(5), None), + &DataType::Timestamp(TimeUnit::Millisecond, None), + ), + Some(ScalarValue::TimestampMillisecond(Some(5000), None)) + ); + // s->us + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampSecond(Some(5), None), + &DataType::Timestamp(TimeUnit::Microsecond, None), + ), + Some(ScalarValue::TimestampMicrosecond(Some(5000000), None)) + ); + // s->ns + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampSecond(Some(5), None), + &DataType::Timestamp(TimeUnit::Nanosecond, None), + ), + Some(ScalarValue::TimestampNanosecond(Some(5000000000), None)) + ); + // ms->s + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMillisecond(Some(5000), None), + &DataType::Timestamp(TimeUnit::Second, None), + ), + Some(ScalarValue::TimestampSecond(Some(5), None)) + ); + // ms->ms + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMillisecond(Some(5000), None), + &DataType::Timestamp(TimeUnit::Millisecond, None), + ), + Some(ScalarValue::TimestampMillisecond(Some(5000), None)) + ); + // ms->us + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMillisecond(Some(5000), None), + &DataType::Timestamp(TimeUnit::Microsecond, None), + ), + Some(ScalarValue::TimestampMicrosecond(Some(5000000), None)) + ); + // ms->ns + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMillisecond(Some(5000), None), + &DataType::Timestamp(TimeUnit::Nanosecond, None), + ), + Some(ScalarValue::TimestampNanosecond(Some(5000000000), None)) + ); + // us->s + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMicrosecond(Some(5000000), None), + &DataType::Timestamp(TimeUnit::Second, None), + ), + Some(ScalarValue::TimestampSecond(Some(5), None)) + ); + // us->ms + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMicrosecond(Some(5000000), None), + &DataType::Timestamp(TimeUnit::Millisecond, None), + ), + Some(ScalarValue::TimestampMillisecond(Some(5000), None)) + ); + // us->us + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMicrosecond(Some(5000000), None), + &DataType::Timestamp(TimeUnit::Microsecond, None), + ), + Some(ScalarValue::TimestampMicrosecond(Some(5000000), None)) + ); + // us->ns + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampMicrosecond(Some(5000000), None), + &DataType::Timestamp(TimeUnit::Nanosecond, None), + ), + Some(ScalarValue::TimestampNanosecond(Some(5000000000), None)) + ); + // ns->s + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampNanosecond(Some(5000000000), None), + &DataType::Timestamp(TimeUnit::Second, None), + ), + Some(ScalarValue::TimestampSecond(Some(5), None)) + ); + // ns->ms + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampNanosecond(Some(5000000000), None), + &DataType::Timestamp(TimeUnit::Millisecond, None), + ), + Some(ScalarValue::TimestampMillisecond(Some(5000), None)) + ); + // ns->us + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampNanosecond(Some(5000000000), None), + &DataType::Timestamp(TimeUnit::Microsecond, None), + ), + Some(ScalarValue::TimestampMicrosecond(Some(5000000), None)) + ); + // ns->ns + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampNanosecond(Some(5000000000), None), + &DataType::Timestamp(TimeUnit::Nanosecond, None), + ), + Some(ScalarValue::TimestampNanosecond(Some(5000000000), None)) + ); + // Precision loss on coercion is allowed (truncation) + // ns->s + assert_eq!( + safe_coerce_scalar( + &ScalarValue::TimestampNanosecond(Some(5987654321), None), + &DataType::Timestamp(TimeUnit::Second, None), + ), + Some(ScalarValue::TimestampSecond(Some(5), None)) + ); + // Conversions from date-32 to date-64 is allowed + assert_eq!( + safe_coerce_scalar(&ScalarValue::Date32(Some(5)), &DataType::Date32,), + Some(ScalarValue::Date32(Some(5))) + ); + assert_eq!( + safe_coerce_scalar(&ScalarValue::Date32(Some(5)), &DataType::Date64,), + Some(ScalarValue::Date64(Some(5 * MS_PER_DAY))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Date64(Some(5 * MS_PER_DAY)), + &DataType::Date32, + ), + Some(ScalarValue::Date32(Some(5))) + ); + assert_eq!( + safe_coerce_scalar(&ScalarValue::Date64(Some(5)), &DataType::Date64,), + Some(ScalarValue::Date64(Some(5))) + ); + // Time-32 to time-64 (and within time-32 and time-64) is allowed + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Second(Some(5)), + &DataType::Time32(TimeUnit::Second), + ), + Some(ScalarValue::Time32Second(Some(5))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Second(Some(5)), + &DataType::Time32(TimeUnit::Millisecond), + ), + Some(ScalarValue::Time32Millisecond(Some(5000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Second(Some(5)), + &DataType::Time64(TimeUnit::Microsecond), + ), + Some(ScalarValue::Time64Microsecond(Some(5000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Second(Some(5)), + &DataType::Time64(TimeUnit::Nanosecond), + ), + Some(ScalarValue::Time64Nanosecond(Some(5000000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Millisecond(Some(5000)), + &DataType::Time32(TimeUnit::Second), + ), + Some(ScalarValue::Time32Second(Some(5))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Millisecond(Some(5000)), + &DataType::Time32(TimeUnit::Millisecond), + ), + Some(ScalarValue::Time32Millisecond(Some(5000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Millisecond(Some(5000)), + &DataType::Time64(TimeUnit::Microsecond), + ), + Some(ScalarValue::Time64Microsecond(Some(5000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time32Millisecond(Some(5000)), + &DataType::Time64(TimeUnit::Nanosecond), + ), + Some(ScalarValue::Time64Nanosecond(Some(5000000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Microsecond(Some(5000000)), + &DataType::Time32(TimeUnit::Second), + ), + Some(ScalarValue::Time32Second(Some(5))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Microsecond(Some(5000000)), + &DataType::Time32(TimeUnit::Millisecond), + ), + Some(ScalarValue::Time32Millisecond(Some(5000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Microsecond(Some(5000000)), + &DataType::Time64(TimeUnit::Microsecond), + ), + Some(ScalarValue::Time64Microsecond(Some(5000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Microsecond(Some(5000000)), + &DataType::Time64(TimeUnit::Nanosecond), + ), + Some(ScalarValue::Time64Nanosecond(Some(5000000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Nanosecond(Some(5000000000)), + &DataType::Time32(TimeUnit::Second), + ), + Some(ScalarValue::Time32Second(Some(5))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Nanosecond(Some(5000000000)), + &DataType::Time32(TimeUnit::Millisecond), + ), + Some(ScalarValue::Time32Millisecond(Some(5000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Nanosecond(Some(5000000000)), + &DataType::Time64(TimeUnit::Microsecond), + ), + Some(ScalarValue::Time64Microsecond(Some(5000000))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Time64Nanosecond(Some(5000000000)), + &DataType::Time64(TimeUnit::Nanosecond), + ), + Some(ScalarValue::Time64Nanosecond(Some(5000000000))) + ); + } + + #[test] + fn test_string_view_coerce() { + // Utf8 <-> Utf8View + assert_eq!( + safe_coerce_scalar(&ScalarValue::Utf8(Some("hi".into())), &DataType::Utf8View), + Some(ScalarValue::Utf8View(Some("hi".into()))) + ); + assert_eq!( + safe_coerce_scalar(&ScalarValue::Utf8View(Some("hi".into())), &DataType::Utf8), + Some(ScalarValue::Utf8(Some("hi".into()))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Utf8View(Some("hi".into())), + &DataType::LargeUtf8 + ), + Some(ScalarValue::LargeUtf8(Some("hi".into()))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::LargeUtf8(Some("hi".into())), + &DataType::Utf8View + ), + Some(ScalarValue::Utf8View(Some("hi".into()))) + ); + // identity + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Utf8View(Some("hi".into())), + &DataType::Utf8View + ), + Some(ScalarValue::Utf8View(Some("hi".into()))) + ); + // Binary <-> BinaryView + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Binary(Some(vec![1, 2, 3])), + &DataType::BinaryView + ), + Some(ScalarValue::BinaryView(Some(vec![1, 2, 3]))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::BinaryView(Some(vec![1, 2, 3])), + &DataType::Binary + ), + Some(ScalarValue::Binary(Some(vec![1, 2, 3]))) + ); + assert_eq!( + safe_coerce_scalar( + &ScalarValue::BinaryView(Some(vec![1, 2, 3])), + &DataType::BinaryView + ), + Some(ScalarValue::BinaryView(Some(vec![1, 2, 3]))) + ); + } + + #[test] + fn test_dictionary_coerce() { + let dict_ty = DataType::Dictionary(Box::new(DataType::Int16), Box::new(DataType::Utf8)); + + // A string literal coerces to a dictionary target by wrapping the + // coerced value in a dictionary scalar. + assert_eq!( + safe_coerce_scalar(&ScalarValue::Utf8(Some("com".to_string())), &dict_ty), + Some(ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(Some("com".to_string()))), + )) + ); + + // The inner value is coerced through to the dictionary value type, so a + // LargeUtf8 literal lands as a Utf8 value inside the dictionary. + assert_eq!( + safe_coerce_scalar(&ScalarValue::LargeUtf8(Some("com".to_string())), &dict_ty), + Some(ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(Some("com".to_string()))), + )) + ); + + // A dictionary literal round-trips back to its value type. + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(Some("com".to_string()))), + ), + &DataType::Utf8, + ), + Some(ScalarValue::Utf8(Some("com".to_string()))) + ); + + // A dictionary literal coerces to a dictionary target, adopting the + // target's key type. + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Dictionary( + Box::new(DataType::Int32), + Box::new(ScalarValue::Utf8(Some("com".to_string()))), + ), + &dict_ty, + ), + Some(ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(Some("com".to_string()))), + )) + ); + + // An untyped null keeps its untyped form for a dictionary target, just + // like for every other target type. + assert_eq!( + safe_coerce_scalar(&ScalarValue::Null, &dict_ty), + Some(ScalarValue::Null) + ); + + // A *typed* null (e.g. an API-built `Utf8(None)` literal, or an IN value + // already typed as Utf8) is still wrapped in the dictionary type so it + // matches the dictionary column. Returning a bare `Utf8(None)` here would + // leave `resolve_value` with a literal whose type does not line up with + // the column, breaking planning/evaluation the same way non-null strings + // used to break. + assert_eq!( + safe_coerce_scalar(&ScalarValue::Utf8(None), &dict_ty), + Some(ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(None)), + )) + ); + + // The inner null is coerced through to the dictionary value type as well, + // so a LargeUtf8 typed null lands as a Utf8 null inside the dictionary. + assert_eq!( + safe_coerce_scalar(&ScalarValue::LargeUtf8(None), &dict_ty), + Some(ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(None)), + )) + ); + + // A value that cannot be coerced to the dictionary value type fails. + assert_eq!( + safe_coerce_scalar( + &ScalarValue::Utf8(Some("com".to_string())), + &DataType::Dictionary(Box::new(DataType::Int16), Box::new(DataType::Int32)), + ), + None + ); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/lib.rs b/lance-artifact/rust/lance-datafusion/src/lib.rs new file mode 100644 index 000000000..ecc786729 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/lib.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod aggregate; +pub mod chunker; +pub mod dataframe; +pub mod datagen; +pub mod exec; +pub mod expr; +pub mod logical_expr; +pub mod planner; +pub mod projection; +pub mod pb { + #![allow(clippy::all)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + #![allow(clippy::upper_case_acronyms)] + #![allow(clippy::use_self)] + include!(concat!(env!("OUT_DIR"), "/lance.datafusion.rs")); +} +pub mod spill; +pub mod sql; +#[cfg(feature = "substrait")] +pub mod substrait; +pub mod udf; +pub mod utils; diff --git a/lance-artifact/rust/lance-datafusion/src/logical_expr.rs b/lance-artifact/rust/lance-datafusion/src/logical_expr.rs new file mode 100644 index 000000000..db9abd7e2 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/logical_expr.rs @@ -0,0 +1,536 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Extends logical expression. + +use std::sync::Arc; + +use arrow_schema::DataType; + +use crate::expr::safe_coerce_scalar; +use datafusion::logical_expr::{Between, ScalarUDF, ScalarUDFImpl}; +use datafusion::logical_expr::{BinaryExpr, Operator, expr::ScalarFunction}; +use datafusion::prelude::*; +use datafusion::scalar::ScalarValue; +use datafusion_functions::core::getfield::GetFieldFunc; +use lance_arrow::DataTypeExt; + +use lance_core::datatypes::Schema; +use lance_core::{Error, Result}; +/// Resolve a Value +fn resolve_value(expr: &Expr, data_type: &DataType) -> Result { + match expr { + Expr::Literal(scalar_value, metadata) => { + Ok(Expr::Literal(safe_coerce_scalar(scalar_value, data_type).ok_or_else(|| Error::invalid_input(format!("Received literal {expr} and could not convert to literal of type '{data_type:?}'")))?, metadata.clone())) + } + _ => Err(Error::invalid_input(format!("Expected a literal of type '{data_type:?}' but received: {expr}"))), + } +} + +/// A simple helper function that interprets an Expr as a string scalar +/// or returns None if it is not. +pub fn get_as_string_scalar_opt(expr: &Expr) -> Option<&str> { + match expr { + Expr::Literal(ScalarValue::Utf8(Some(s)), _) => Some(s), + _ => None, + } +} + +/// Given a Expr::Column or Expr::GetIndexedField, get the data type of referenced +/// field in the schema. +/// +/// If the column is not found in the schema, return None. If the expression is +/// not a field reference, also returns None. +pub fn resolve_column_type(expr: &Expr, schema: &Schema) -> Option { + let mut field_path = Vec::new(); + let mut current_expr = expr; + // We are looping from outer-most reference to inner-most. + loop { + match current_expr { + Expr::Column(c) => { + field_path.push(c.name.as_str()); + break; + } + Expr::ScalarFunction(udf) if udf.name() == GetFieldFunc::default().name() => { + let name = get_as_string_scalar_opt(&udf.args[1])?; + field_path.push(name); + current_expr = &udf.args[0]; + } + _ => return None, + } + } + + let mut path_iter = field_path.iter().rev(); + let mut field = schema.field(path_iter.next()?)?; + for name in path_iter { + if field.data_type().is_struct() { + field = field.children.iter().find(|f| &f.name == name)?; + } else { + return None; + } + } + Some(field.data_type()) +} + +/// Resolve logical expression `expr`. +/// +/// Parameters +/// +/// - *expr*: a datafusion logical expression +/// - *schema*: lance schema. +pub fn resolve_expr(expr: &Expr, schema: &Schema) -> Result { + match expr { + Expr::Between(Between { + expr: inner_expr, + low, + high, + negated, + }) => { + if let Some(inner_expr_type) = resolve_column_type(inner_expr.as_ref(), schema) { + Ok(Expr::Between(Between { + expr: inner_expr.clone(), + low: Box::new(coerce_expr(low.as_ref(), &inner_expr_type)?), + high: Box::new(coerce_expr(high.as_ref(), &inner_expr_type)?), + negated: *negated, + })) + } else { + Ok(expr.clone()) + } + } + Expr::BinaryExpr(BinaryExpr { left, op, right }) => { + if matches!(op, Operator::And | Operator::Or) { + Ok(Expr::BinaryExpr(BinaryExpr { + left: Box::new(resolve_expr(left.as_ref(), schema)?), + op: *op, + right: Box::new(resolve_expr(right.as_ref(), schema)?), + })) + } else if let Some(left_type) = resolve_column_type(left.as_ref(), schema) { + match right.as_ref() { + Expr::Literal(..) => Ok(Expr::BinaryExpr(BinaryExpr { + left: left.clone(), + op: *op, + right: Box::new(resolve_value(right.as_ref(), &left_type)?), + })), + // For cases complex expressions (not just literals) on right hand side like x = 1 + 1 + -2*2 + Expr::BinaryExpr(r) => Ok(Expr::BinaryExpr(BinaryExpr { + left: left.clone(), + op: *op, + right: Box::new(Expr::BinaryExpr(BinaryExpr { + left: coerce_expr(&r.left, &left_type).map(Box::new)?, + op: r.op, + right: coerce_expr(&r.right, &left_type).map(Box::new)?, + })), + })), + _ => Ok(expr.clone()), + } + } else if let Some(right_type) = resolve_column_type(right.as_ref(), schema) { + match left.as_ref() { + Expr::Literal(..) => Ok(Expr::BinaryExpr(BinaryExpr { + left: Box::new(resolve_value(left.as_ref(), &right_type)?), + op: *op, + right: right.clone(), + })), + _ => Ok(expr.clone()), + } + } else { + Ok(expr.clone()) + } + } + Expr::InList(in_list) => { + if matches!(in_list.expr.as_ref(), Expr::Column(_)) { + if let Some(resolved_type) = resolve_column_type(in_list.expr.as_ref(), schema) { + let resolved_values = in_list + .list + .iter() + .map(|val| coerce_expr(val, &resolved_type)) + .collect::>>()?; + Ok(Expr::in_list( + in_list.expr.as_ref().clone(), + resolved_values, + in_list.negated, + )) + } else { + Ok(expr.clone()) + } + } else { + Ok(expr.clone()) + } + } + _ => { + // Passthrough + Ok(expr.clone()) + } + } +} + +/// Coerce expression of literals to column type. +/// +/// Parameters +/// +/// - *expr*: a datafusion logical expression +/// - *dtype*: a lance data type +pub fn coerce_expr(expr: &Expr, dtype: &DataType) -> Result { + match expr { + Expr::BinaryExpr(BinaryExpr { left, op, right }) => Ok(Expr::BinaryExpr(BinaryExpr { + left: Box::new(coerce_expr(left, dtype)?), + op: *op, + right: Box::new(coerce_expr(right, dtype)?), + })), + literal_expr @ Expr::Literal(..) => Ok(resolve_value(literal_expr, dtype)?), + _ => Ok(expr.clone()), + } +} + +/// Coerce logical expression for filters to boolean. +/// +/// Parameters +/// +/// - *expr*: a datafusion logical expression +pub fn coerce_filter_type_to_boolean(expr: Expr) -> Expr { + match expr { + // Coerce regexp_match to boolean by checking for non-null + Expr::ScalarFunction(sf) if sf.func.name() == "regexp_match" => { + log::warn!( + "regexp_match now is coerced to boolean, this may be changed in the future, please use `regexp_like` instead" + ); + Expr::IsNotNull(Box::new(Expr::ScalarFunction(sf))) + } + + // Recurse into boolean contexts so nested regexp_match terms are also coerced + Expr::BinaryExpr(BinaryExpr { left, op, right }) => Expr::BinaryExpr(BinaryExpr { + left: Box::new(coerce_filter_type_to_boolean(*left)), + op, + right: Box::new(coerce_filter_type_to_boolean(*right)), + }), + Expr::Not(inner) => Expr::Not(Box::new(coerce_filter_type_to_boolean(*inner))), + Expr::IsNull(inner) => Expr::IsNull(Box::new(coerce_filter_type_to_boolean(*inner))), + Expr::IsNotNull(inner) => Expr::IsNotNull(Box::new(coerce_filter_type_to_boolean(*inner))), + + // Pass-through for all other nodes + other => other, + } +} + +// As part of the DF 37 release there are now two different ways to +// represent a nested field access in `Expr`. The old way is to use +// `Expr::field` which returns a `GetStructField` and the new way is +// to use `Expr::ScalarFunction` with a `GetFieldFunc` UDF. +// +// Currently, the old path leads to bugs in DF. This is probably a +// bug and will probably be fixed in a future version. In the meantime +// we need to make sure we are always using the new way to avoid this +// bug. This trait adds field_newstyle which lets us easily create +// logical `Expr` that use the new style. +pub trait ExprExt { + // Helper function to replace Expr::field in DF 37 since DF + // confuses itself with the GetStructField returned by Expr::field + fn field_newstyle(&self, name: &str) -> Expr; +} + +impl ExprExt for Expr { + fn field_newstyle(&self, name: &str) -> Expr { + Self::ScalarFunction(ScalarFunction { + func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())), + args: vec![ + self.clone(), + Self::Literal(ScalarValue::Utf8(Some(name.to_string())), None), + ], + }) + } +} + +/// Convert a field path string into a DataFusion expression. +/// +/// This function handles: +/// - Simple column names: "column" +/// - Nested paths: "parent.child" or "parent.child.grandchild" +/// - Backtick-escaped field names: "parent.`field.with.dots`" +/// +/// # Arguments +/// +/// * `field_path` - The field path to convert. Supports simple columns, nested paths, +/// and backtick-escaped field names. +/// +/// # Returns +/// +/// Returns `Result` - Ok with the DataFusion expression, or Err if the path +/// could not be parsed. +/// +/// # Example +/// +/// ``` +/// use lance_datafusion::logical_expr::field_path_to_expr; +/// +/// // Simple column +/// let expr = field_path_to_expr("column_name").unwrap(); +/// +/// // Nested field +/// let expr = field_path_to_expr("parent.child").unwrap(); +/// +/// // Backtick-escaped field with dots +/// let expr = field_path_to_expr("parent.`field.with.dots`").unwrap(); +/// ``` +pub fn field_path_to_expr(field_path: &str) -> Result { + // Parse the field path to handle nested fields and backtick-escaped names + let parts = lance_core::datatypes::parse_field_path(field_path)?; + + if parts.is_empty() { + return Err(Error::invalid_input(format!( + "Invalid empty field path: {}", + field_path + ))); + } + + // Build the column expression, handling nested fields. + let mut expr = Expr::Column(datafusion::common::Column::new_unqualified( + parts[0].clone(), + )); + for part in &parts[1..] { + expr = expr.field_newstyle(part); + } + + Ok(expr) +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use super::*; + + use arrow_schema::{Field, Schema as ArrowSchema}; + use datafusion::common::Column; + use datafusion_functions::core::expr_ext::FieldAccessor; + + #[test] + fn test_field_path_to_expr_preserves_case_sensitive_root_column() { + let expr = field_path_to_expr("VECTOR").unwrap(); + + assert_eq!(expr, Expr::Column(Column::new_unqualified("VECTOR"))); + } + + #[test] + fn test_field_path_to_expr_preserves_case_sensitive_escaped_nested_path() { + let expr = field_path_to_expr("Parent.`Child.With.Dot`").unwrap(); + + assert_eq!( + expr, + Expr::Column(Column::new_unqualified("Parent")).field_newstyle("Child.With.Dot") + ); + } + + #[test] + fn test_resolve_large_utf8() { + let arrow_schema = ArrowSchema::new(vec![Field::new("a", DataType::LargeUtf8, false)]); + let expr = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column("a".to_string().into())), + op: Operator::Eq, + right: Box::new(Expr::Literal( + ScalarValue::Utf8(Some("a".to_string())), + None, + )), + }); + + let resolved = resolve_expr(&expr, &Schema::try_from(&arrow_schema).unwrap()).unwrap(); + match resolved { + Expr::BinaryExpr(be) => { + assert_eq!( + be.right.as_ref(), + &Expr::Literal(ScalarValue::LargeUtf8(Some("a".to_string())), None) + ) + } + _ => unreachable!("Expected BinaryExpr"), + }; + } + + #[test] + fn test_resolve_binary_expr_on_right() { + let arrow_schema = ArrowSchema::new(vec![Field::new("a", DataType::Float64, false)]); + let expr = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column("a".to_string().into())), + op: Operator::Eq, + right: Box::new(Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Literal(ScalarValue::Int64(Some(2)), None)), + op: Operator::Minus, + right: Box::new(Expr::Literal(ScalarValue::Int64(Some(-1)), None)), + })), + }); + let resolved = resolve_expr(&expr, &Schema::try_from(&arrow_schema).unwrap()).unwrap(); + + match resolved { + Expr::BinaryExpr(be) => match be.right.as_ref() { + Expr::BinaryExpr(r_be) => { + assert_eq!( + r_be.left.as_ref(), + &Expr::Literal(ScalarValue::Float64(Some(2.0)), None) + ); + assert_eq!( + r_be.right.as_ref(), + &Expr::Literal(ScalarValue::Float64(Some(-1.0)), None) + ); + } + _ => panic!("Expected BinaryExpr"), + }, + _ => panic!("Expected BinaryExpr"), + } + } + + #[test] + fn test_resolve_in_expr() { + // Type coercion should apply for `A IN (0)` or `A NOT IN (0)` + let arrow_schema = ArrowSchema::new(vec![Field::new("a", DataType::Float32, false)]); + let expr = Expr::in_list( + Expr::Column("a".to_string().into()), + vec![Expr::Literal(ScalarValue::Float64(Some(0.0)), None)], + false, + ); + let resolved = resolve_expr(&expr, &Schema::try_from(&arrow_schema).unwrap()).unwrap(); + let expected = Expr::in_list( + Expr::Column("a".to_string().into()), + vec![Expr::Literal(ScalarValue::Float32(Some(0.0)), None)], + false, + ); + assert_eq!(resolved, expected); + + let expr = Expr::in_list( + Expr::Column("a".to_string().into()), + vec![Expr::Literal(ScalarValue::Float64(Some(0.0)), None)], + true, + ); + let resolved = resolve_expr(&expr, &Schema::try_from(&arrow_schema).unwrap()).unwrap(); + let expected = Expr::in_list( + Expr::Column("a".to_string().into()), + vec![Expr::Literal(ScalarValue::Float32(Some(0.0)), None)], + true, + ); + assert_eq!(resolved, expected); + } + + #[test] + fn test_resolve_column_type() { + let schema = Arc::new(ArrowSchema::new(vec![ + Field::new("int", DataType::Int32, true), + Field::new( + "st", + DataType::Struct( + vec![ + Field::new("str", DataType::Utf8, true), + Field::new( + "st", + DataType::Struct( + vec![Field::new("float", DataType::Float64, true)].into(), + ), + true, + ), + ] + .into(), + ), + true, + ), + ])); + let schema = Schema::try_from(schema.as_ref()).unwrap(); + + assert_eq!( + resolve_column_type(&col("int"), &schema), + Some(DataType::Int32) + ); + assert_eq!( + resolve_column_type(&col("st").field("str"), &schema), + Some(DataType::Utf8) + ); + assert_eq!( + resolve_column_type(&col("st").field("st").field("float"), &schema), + Some(DataType::Float64) + ); + + assert_eq!(resolve_column_type(&col("x"), &schema), None); + assert_eq!(resolve_column_type(&col("str"), &schema), None); + assert_eq!(resolve_column_type(&col("float"), &schema), None); + assert_eq!( + resolve_column_type(&col("st").field("str").eq(lit("x")), &schema), + None + ); + } + + #[test] + fn test_resolve_utf8view_literal_against_utf8_column() { + // Simulates DataFusion 43+ producing a Utf8View literal (e.g. from md5()) + // being compared against a Utf8 column stored in Lance. + let arrow_schema = ArrowSchema::new(vec![Field::new("hash", DataType::Utf8, false)]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let expr = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column("hash".to_string().into())), + op: Operator::Eq, + right: Box::new(Expr::Literal( + ScalarValue::Utf8View(Some("abc".to_string())), + None, + )), + }); + + let resolved = resolve_expr(&expr, &schema).unwrap(); + match resolved { + Expr::BinaryExpr(be) => { + assert_eq!( + be.right.as_ref(), + &Expr::Literal(ScalarValue::Utf8(Some("abc".to_string())), None) + ) + } + _ => unreachable!("Expected BinaryExpr"), + } + } + + #[test] + fn test_resolve_typed_null_against_dictionary_column() { + // A dictionary-encoded string column, e.g. a categorical field. + let dict_ty = DataType::Dictionary(Box::new(DataType::Int16), Box::new(DataType::Utf8)); + let arrow_schema = ArrowSchema::new(vec![Field::new("etld", dict_ty, true)]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + // A typed null must be wrapped in the dictionary type, not left as a bare + // `Utf8(None)` literal sitting next to a `Dictionary(...)` column. + let expected_null = Expr::Literal( + ScalarValue::Dictionary(Box::new(DataType::Int16), Box::new(ScalarValue::Utf8(None))), + None, + ); + + // `etld = ` built directly via the API, as opposed to coming + // through SQL parsing. + let expr = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column("etld".to_string().into())), + op: Operator::Eq, + right: Box::new(Expr::Literal(ScalarValue::Utf8(None), None)), + }); + match resolve_expr(&expr, &schema).unwrap() { + Expr::BinaryExpr(be) => assert_eq!(be.right.as_ref(), &expected_null), + other => unreachable!("Expected BinaryExpr, got {other:?}"), + } + + // `etld IN ('a', )` — a typed value mixed with a typed null, + // both already typed as Utf8. Every list element is wrapped in the + // dictionary type. + let expr = Expr::in_list( + Expr::Column("etld".to_string().into()), + vec![ + Expr::Literal(ScalarValue::Utf8(Some("a".to_string())), None), + Expr::Literal(ScalarValue::Utf8(None), None), + ], + false, + ); + let expected = Expr::in_list( + Expr::Column("etld".to_string().into()), + vec![ + Expr::Literal( + ScalarValue::Dictionary( + Box::new(DataType::Int16), + Box::new(ScalarValue::Utf8(Some("a".to_string()))), + ), + None, + ), + expected_null, + ], + false, + ); + assert_eq!(resolve_expr(&expr, &schema).unwrap(), expected); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/planner.rs b/lance-artifact/rust/lance-datafusion/src/planner.rs new file mode 100644 index 000000000..5ee19ee2f --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/planner.rs @@ -0,0 +1,2096 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Exec plan planner + +use std::borrow::Cow; +use std::collections::{BTreeSet, VecDeque}; +use std::sync::Arc; + +use crate::exec::{LanceExecutionOptions, get_session_context}; +use crate::expr::safe_coerce_scalar; +use crate::logical_expr::{coerce_filter_type_to_boolean, get_as_string_scalar_opt, resolve_expr}; +use crate::sql::{parse_sql_expr, parse_sql_filter}; +use arrow::compute::CastOptions; +use arrow_array::ListArray; +use arrow_buffer::OffsetBuffer; +use arrow_cast::cast_with_options; +use arrow_schema::{DataType as ArrowDataType, Field, SchemaRef, TimeUnit}; +use arrow_select::concat::concat; +use datafusion::common::DFSchema; +use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion, TreeNodeVisitor}; +use datafusion::config::ConfigOptions; +use datafusion::error::Result as DFResult; +use datafusion::execution::context::SessionState; +use datafusion::logical_expr::expr::ScalarFunction; +use datafusion::logical_expr::planner::{ExprPlanner, PlannerResult, RawFieldAccessExpr}; +use datafusion::logical_expr::{ + AggregateUDF, ColumnarValue, GetFieldAccess, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, + Signature, Volatility, WindowUDF, +}; +use datafusion::optimizer::simplify_expressions::SimplifyContext; +use datafusion::sql::planner::{ + ContextProvider, NullOrdering, ParserOptions, PlannerContext, SqlToRel, +}; +use datafusion::sql::sqlparser::ast::{ + AccessExpr, Array as SQLArray, BinaryOperator, DataType as SQLDataType, ExactNumberInfo, + Expr as SQLExpr, Function, FunctionArg, FunctionArgExpr, FunctionArguments, Ident, + ObjectNamePart, Subscript, TimezoneInfo, TypedString, UnaryOperator, Value, ValueWithSpan, +}; +use datafusion::{ + common::Column, + logical_expr::{Between, BinaryExpr, Like, Operator}, + physical_plan::PhysicalExpr, + prelude::Expr, + scalar::ScalarValue, +}; +use datafusion_functions::core::getfield::GetFieldFunc; +use lance_core::datatypes::Schema; +use lance_core::error::LanceOptionExt; + +use chrono::Utc; +use lance_core::{Error, Result}; + +/// Encode a JSON string into a JSONB `LargeBinary` literal expression. +fn encode_jsonb(json_str: &str) -> Result { + let bytes = lance_arrow::json::encode_json(json_str) + .map_err(|e| Error::invalid_input(format!("Failed to encode JSONB: {e}")))?; + Ok(Expr::Literal(ScalarValue::LargeBinary(Some(bytes)), None)) +} + +// The escape in `LIKE/ILIKE ... ESCAPE ''` must be exactly one character. +// Reject empty or multi-character escape strings rather than silently treating +// them as "no escape" or truncating to the first character. +fn parse_like_escape_char(escape_char: &Option) -> Result> { + let Some(value) = escape_char else { + return Ok(None); + }; + let ValueWithSpan { + value: Value::SingleQuotedString(escape), + .. + } = value + else { + return Err(Error::invalid_input(format!( + "Invalid escape character in LIKE expression. Expected a single character wrapped with single quotes, got {value}" + ))); + }; + let mut chars = escape.chars(); + match (chars.next(), chars.next()) { + (Some(c), None) => Ok(Some(c)), + _ => Err(Error::invalid_input(format!( + "Invalid escape character in LIKE expression. Expected a single character, got '{escape}'" + ))), + } +} + +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +struct CastListF16Udf { + signature: Signature, +} + +impl CastListF16Udf { + pub fn new() -> Self { + Self { + signature: Signature::any(1, Volatility::Immutable), + } + } +} + +impl ScalarUDFImpl for CastListF16Udf { + fn name(&self) -> &str { + "_cast_list_f16" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[ArrowDataType]) -> DFResult { + let input = &arg_types[0]; + match input { + ArrowDataType::FixedSizeList(field, size) => { + if field.data_type() != &ArrowDataType::Float32 + && field.data_type() != &ArrowDataType::Float16 + { + return Err(datafusion::error::DataFusionError::Execution( + "cast_list_f16 only supports list of float32 or float16".to_string(), + )); + } + Ok(ArrowDataType::FixedSizeList( + Arc::new(Field::new( + field.name(), + ArrowDataType::Float16, + field.is_nullable(), + )), + *size, + )) + } + ArrowDataType::List(field) => { + if field.data_type() != &ArrowDataType::Float32 + && field.data_type() != &ArrowDataType::Float16 + { + return Err(datafusion::error::DataFusionError::Execution( + "cast_list_f16 only supports list of float32 or float16".to_string(), + )); + } + Ok(ArrowDataType::List(Arc::new(Field::new( + field.name(), + ArrowDataType::Float16, + field.is_nullable(), + )))) + } + _ => Err(datafusion::error::DataFusionError::Execution( + "cast_list_f16 only supports FixedSizeList/List arguments".to_string(), + )), + } + } + + fn invoke_with_args(&self, func_args: ScalarFunctionArgs) -> DFResult { + let ColumnarValue::Array(arr) = &func_args.args[0] else { + return Err(datafusion::error::DataFusionError::Execution( + "cast_list_f16 only supports array arguments".to_string(), + )); + }; + + let to_type = match arr.data_type() { + ArrowDataType::FixedSizeList(field, size) => ArrowDataType::FixedSizeList( + Arc::new(Field::new( + field.name(), + ArrowDataType::Float16, + field.is_nullable(), + )), + *size, + ), + ArrowDataType::List(field) => ArrowDataType::List(Arc::new(Field::new( + field.name(), + ArrowDataType::Float16, + field.is_nullable(), + ))), + _ => { + return Err(datafusion::error::DataFusionError::Execution( + "cast_list_f16 only supports array arguments".to_string(), + )); + } + }; + + let res = cast_with_options(arr.as_ref(), &to_type, &CastOptions::default())?; + Ok(ColumnarValue::Array(res)) + } +} + +// Adapter that instructs datafusion how lance expects expressions to be interpreted +struct LanceContextProvider { + options: datafusion::config::ConfigOptions, + state: SessionState, + expr_planners: Vec>, +} + +impl Default for LanceContextProvider { + fn default() -> Self { + let ctx = get_session_context(&LanceExecutionOptions::default()); + let state = ctx.state(); + let expr_planners = state.expr_planners().to_vec(); + + Self { + options: ConfigOptions::default(), + state, + expr_planners, + } + } +} + +impl ContextProvider for LanceContextProvider { + fn get_table_source( + &self, + name: datafusion::sql::TableReference, + ) -> DFResult> { + Err(datafusion::error::DataFusionError::NotImplemented(format!( + "Attempt to reference inner table {} not supported", + name + ))) + } + + fn get_aggregate_meta(&self, name: &str) -> Option> { + self.state.aggregate_functions().get(name).cloned() + } + + fn get_window_meta(&self, name: &str) -> Option> { + self.state.window_functions().get(name).cloned() + } + + fn get_higher_order_meta( + &self, + name: &str, + ) -> Option> { + self.state.higher_order_functions().get(name).cloned() + } + + fn get_function_meta(&self, f: &str) -> Option> { + match f { + // TODO: cast should go thru CAST syntax instead of UDF + // Going thru UDF makes it hard for the optimizer to find no-ops + "_cast_list_f16" => Some(Arc::new(ScalarUDF::new_from_impl(CastListF16Udf::new()))), + _ => self.state.scalar_functions().get(f).cloned(), + } + } + + fn get_variable_type(&self, _: &[String]) -> Option { + // Variables (things like @@LANGUAGE) not supported + None + } + + fn options(&self) -> &datafusion::config::ConfigOptions { + &self.options + } + + fn udf_names(&self) -> Vec { + self.state.scalar_functions().keys().cloned().collect() + } + + fn udaf_names(&self) -> Vec { + self.state.aggregate_functions().keys().cloned().collect() + } + + fn udwf_names(&self) -> Vec { + self.state.window_functions().keys().cloned().collect() + } + + fn higher_order_function_names(&self) -> Vec { + self.state + .higher_order_functions() + .keys() + .cloned() + .collect() + } + + fn get_expr_planners(&self) -> &[Arc] { + &self.expr_planners + } +} + +pub struct Planner { + schema: SchemaRef, + context_provider: LanceContextProvider, + enable_relations: bool, +} + +impl Planner { + pub fn new(schema: SchemaRef) -> Self { + Self { + schema, + context_provider: LanceContextProvider::default(), + enable_relations: false, + } + } + + /// If passed with `true`, then the first identifier in column reference + /// is parsed as the relation. For example, `table.field.inner` will be + /// read as the nested field `field.inner` (`inner` on struct field `field`) + /// on the `table` relation. If `false` (the default), then no relations + /// are used and all identifiers are assumed to be a nested column path. + pub fn with_enable_relations(mut self, enable_relations: bool) -> Self { + self.enable_relations = enable_relations; + self + } + + /// Resolve a column name using case-insensitive matching against the schema. + /// Returns the actual field name if found, otherwise returns the original name. + fn resolve_column_name(&self, name: &str) -> String { + // Try exact match first + if self.schema.field_with_name(name).is_ok() { + return name.to_string(); + } + // Fall back to case-insensitive match + for field in self.schema.fields() { + if field.name().eq_ignore_ascii_case(name) { + return field.name().clone(); + } + } + // Not found in schema - return original (might be computed column, system column, etc.) + name.to_string() + } + + fn column(&self, idents: &[Ident]) -> Expr { + fn handle_remaining_idents(expr: &mut Expr, idents: &[Ident]) { + for ident in idents { + *expr = Expr::ScalarFunction(ScalarFunction { + args: vec![ + std::mem::take(expr), + Expr::Literal(ScalarValue::Utf8(Some(ident.value.clone())), None), + ], + func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())), + }); + } + } + + if self.enable_relations && idents.len() > 1 { + // Create qualified column reference (relation.column) + let relation = &idents[0].value; + let column_name = self.resolve_column_name(&idents[1].value); + let column = Expr::Column(Column::new(Some(relation.clone()), column_name)); + let mut result = column; + handle_remaining_idents(&mut result, &idents[2..]); + result + } else { + // Default behavior - treat as struct field access + // Use resolved column name to handle case-insensitive matching + let resolved_name = self.resolve_column_name(&idents[0].value); + let mut column = Expr::Column(Column::from_name(resolved_name)); + handle_remaining_idents(&mut column, &idents[1..]); + column + } + } + + fn binary_op(&self, op: &BinaryOperator) -> Result { + Ok(match op { + BinaryOperator::Plus => Operator::Plus, + BinaryOperator::Minus => Operator::Minus, + BinaryOperator::Multiply => Operator::Multiply, + BinaryOperator::Divide => Operator::Divide, + BinaryOperator::Modulo => Operator::Modulo, + BinaryOperator::StringConcat => Operator::StringConcat, + BinaryOperator::Gt => Operator::Gt, + BinaryOperator::Lt => Operator::Lt, + BinaryOperator::GtEq => Operator::GtEq, + BinaryOperator::LtEq => Operator::LtEq, + BinaryOperator::Eq => Operator::Eq, + BinaryOperator::NotEq => Operator::NotEq, + BinaryOperator::And => Operator::And, + BinaryOperator::Or => Operator::Or, + _ => { + return Err(Error::invalid_input(format!( + "Operator {op} is not supported" + ))); + } + }) + } + + fn is_logical_binary_op(op: &BinaryOperator) -> bool { + matches!(op, BinaryOperator::And | BinaryOperator::Or) + } + + fn is_same_logical_binary_op(left: &BinaryOperator, right: &BinaryOperator) -> bool { + matches!( + (left, right), + (BinaryOperator::And, BinaryOperator::And) | (BinaryOperator::Or, BinaryOperator::Or) + ) + } + + fn flatten_logical_binary_exprs<'a>( + left: &'a SQLExpr, + op: &BinaryOperator, + right: &'a SQLExpr, + ) -> Vec<&'a SQLExpr> { + let mut leaves = Vec::new(); + let mut stack = vec![right, left]; + + while let Some(expr) = stack.pop() { + match expr { + SQLExpr::BinaryOp { + left, + op: child_op, + right, + } if Self::is_same_logical_binary_op(op, child_op) => { + stack.push(right.as_ref()); + stack.push(left.as_ref()); + } + _ => leaves.push(expr), + } + } + + leaves + } + + fn balanced_binary_expr(mut exprs: VecDeque, op: Operator) -> Result { + if exprs.is_empty() { + return Err(Error::invalid_input("Binary expression has no operands")); + } + + while exprs.len() > 1 { + let mut next = VecDeque::with_capacity(exprs.len().div_ceil(2)); + while let Some(left) = exprs.pop_front() { + if let Some(right) = exprs.pop_front() { + next.push_back(Expr::BinaryExpr(BinaryExpr::new( + Box::new(left), + op, + Box::new(right), + ))); + } else { + next.push_back(left); + } + } + exprs = next; + } + + exprs + .pop_front() + .ok_or_else(|| Error::invalid_input("Binary expression has no operands")) + } + + fn binary_expr(&self, left: &SQLExpr, op: &BinaryOperator, right: &SQLExpr) -> Result { + let df_op = self.binary_op(op)?; + if Self::is_logical_binary_op(op) { + let leaves = Self::flatten_logical_binary_exprs(left, op, right); + let mut exprs = VecDeque::with_capacity(leaves.len()); + for leaf in leaves { + exprs.push_back(self.parse_sql_expr(leaf)?); + } + return Self::balanced_binary_expr(exprs, df_op); + } + + Ok(Expr::BinaryExpr(BinaryExpr::new( + Box::new(self.parse_sql_expr(left)?), + df_op, + Box::new(self.parse_sql_expr(right)?), + ))) + } + + fn unary_expr(&self, op: &UnaryOperator, expr: &SQLExpr) -> Result { + Ok(match op { + UnaryOperator::Not | UnaryOperator::BitwiseNot => { + Expr::Not(Box::new(self.parse_sql_expr(expr)?)) + } + + UnaryOperator::Minus => { + use datafusion::logical_expr::lit; + match expr { + SQLExpr::Value(ValueWithSpan { value: Value::Number(n, _), ..}) => match n.parse::() { + Ok(n) => lit(-n), + Err(_) => lit(-n + .parse::() + .map_err(|_e| { + Error::invalid_input(format!("negative operator can be only applied to integer and float operands, got: {n}")) + })?), + }, + _ => { + Expr::Negative(Box::new(self.parse_sql_expr(expr)?)) + } + } + } + + _ => { + return Err(Error::invalid_input(format!( + "Unary operator '{:?}' is not supported", + op + ))); + } + }) + } + + // See datafusion `sqlToRel::parse_sql_number()` + fn number(&self, value: &str, negative: bool) -> Result { + use datafusion::logical_expr::lit; + let value: Cow = if negative { + Cow::Owned(format!("-{}", value)) + } else { + Cow::Borrowed(value) + }; + if let Ok(n) = value.parse::() { + Ok(lit(n)) + } else if let Ok(n) = value.parse::() { + Ok(lit(n)) + } else { + value.parse::().map(lit).map_err(|_| { + Error::invalid_input(format!("'{value}' is not supported number value.")) + }) + } + } + + fn value(&self, value: &Value) -> Result { + Ok(match value { + Value::Number(v, _) => self.number(v.as_str(), false)?, + Value::SingleQuotedString(s) => Expr::Literal(ScalarValue::Utf8(Some(s.clone())), None), + Value::HexStringLiteral(hsl) => { + Expr::Literal(ScalarValue::Binary(Self::try_decode_hex_literal(hsl)), None) + } + Value::DoubleQuotedString(s) => Expr::Literal(ScalarValue::Utf8(Some(s.clone())), None), + Value::Boolean(v) => Expr::Literal(ScalarValue::Boolean(Some(*v)), None), + Value::Null => Expr::Literal(ScalarValue::Null, None), + _ => todo!(), + }) + } + + fn parse_function_args(&self, func_args: &FunctionArg) -> Result { + match func_args { + FunctionArg::Unnamed(FunctionArgExpr::Expr(expr)) => self.parse_sql_expr(expr), + _ => Err(Error::invalid_input(format!( + "Unsupported function args: {:?}", + func_args + ))), + } + } + + // We now use datafusion to parse functions. This allows us to use datafusion's + // entire collection of functions (previously we had just hard-coded support for two functions). + // + // Unfortunately, one of those two functions was is_valid and the reason we needed it was because + // this is a function that comes from duckdb. Datafusion does not consider is_valid to be a function + // but rather an AST node (Expr::IsNotNull) and so we need to handle this case specially. + fn legacy_parse_function(&self, func: &Function) -> Result { + match &func.args { + FunctionArguments::List(args) => { + if func.name.0.len() != 1 { + return Err(Error::invalid_input(format!( + "Function name must have 1 part, got: {:?}", + func.name.0 + ))); + } + Ok(Expr::IsNotNull(Box::new( + self.parse_function_args(&args.args[0])?, + ))) + } + _ => Err(Error::invalid_input(format!( + "Unsupported function args: {:?}", + func.args + ))), + } + } + + fn parse_function(&self, function: SQLExpr) -> Result { + if let SQLExpr::Function(function) = &function + && let Some(ObjectNamePart::Identifier(name)) = &function.name.0.first() + && &name.value == "is_valid" + { + return self.legacy_parse_function(function); + } + let sql_to_rel = SqlToRel::new_with_options( + &self.context_provider, + ParserOptions { + parse_float_as_decimal: false, + enable_ident_normalization: false, + support_varchar_with_length: false, + enable_options_value_normalization: false, + collect_spans: false, + map_string_types_to_utf8view: false, + default_null_ordering: NullOrdering::NullsMax, + }, + ); + + let mut planner_context = PlannerContext::default(); + let schema = DFSchema::try_from(self.schema.as_ref().clone())?; + sql_to_rel + .sql_to_expr(function, &schema, &mut planner_context) + .map_err(|e| Error::invalid_input(format!("Error parsing function: {e}"))) + } + + fn parse_type(&self, data_type: &SQLDataType) -> Result { + const SUPPORTED_TYPES: [&str; 13] = [ + "int [unsigned]", + "tinyint [unsigned]", + "smallint [unsigned]", + "bigint [unsigned]", + "float", + "double", + "string", + "binary", + "date", + "timestamp(precision)", + "datetime(precision)", + "decimal(precision,scale)", + "boolean", + ]; + match data_type { + SQLDataType::String(_) => Ok(ArrowDataType::Utf8), + SQLDataType::Binary(_) => Ok(ArrowDataType::Binary), + SQLDataType::Float(_) => Ok(ArrowDataType::Float32), + SQLDataType::Double(_) => Ok(ArrowDataType::Float64), + SQLDataType::Boolean => Ok(ArrowDataType::Boolean), + SQLDataType::TinyInt(_) => Ok(ArrowDataType::Int8), + SQLDataType::SmallInt(_) => Ok(ArrowDataType::Int16), + SQLDataType::Int(_) | SQLDataType::Integer(_) => Ok(ArrowDataType::Int32), + SQLDataType::BigInt(_) => Ok(ArrowDataType::Int64), + SQLDataType::TinyIntUnsigned(_) => Ok(ArrowDataType::UInt8), + SQLDataType::SmallIntUnsigned(_) => Ok(ArrowDataType::UInt16), + SQLDataType::IntUnsigned(_) | SQLDataType::IntegerUnsigned(_) => { + Ok(ArrowDataType::UInt32) + } + SQLDataType::BigIntUnsigned(_) => Ok(ArrowDataType::UInt64), + SQLDataType::Date => Ok(ArrowDataType::Date32), + SQLDataType::Timestamp(resolution, tz) => { + match tz { + TimezoneInfo::None => {} + _ => { + return Err(Error::invalid_input( + "Timezone not supported in timestamp".to_string(), + )); + } + }; + let time_unit = match resolution { + // Default to microsecond to match PyArrow + None => TimeUnit::Microsecond, + Some(0) => TimeUnit::Second, + Some(3) => TimeUnit::Millisecond, + Some(6) => TimeUnit::Microsecond, + Some(9) => TimeUnit::Nanosecond, + _ => { + return Err(Error::invalid_input(format!( + "Unsupported datetime resolution: {:?}", + resolution + ))); + } + }; + Ok(ArrowDataType::Timestamp(time_unit, None)) + } + SQLDataType::Datetime(resolution) => { + let time_unit = match resolution { + None => TimeUnit::Microsecond, + Some(0) => TimeUnit::Second, + Some(3) => TimeUnit::Millisecond, + Some(6) => TimeUnit::Microsecond, + Some(9) => TimeUnit::Nanosecond, + _ => { + return Err(Error::invalid_input(format!( + "Unsupported datetime resolution: {:?}", + resolution + ))); + } + }; + Ok(ArrowDataType::Timestamp(time_unit, None)) + } + SQLDataType::Decimal(number_info) => match number_info { + ExactNumberInfo::PrecisionAndScale(precision, scale) => { + Ok(ArrowDataType::Decimal128(*precision as u8, *scale as i8)) + } + _ => Err(Error::invalid_input(format!( + "Must provide precision and scale for decimal: {:?}", + number_info + ))), + }, + _ => Err(Error::invalid_input(format!( + "Unsupported data type: {:?}. Supported types: {:?}", + data_type, SUPPORTED_TYPES + ))), + } + } + + fn plan_field_access(&self, mut field_access_expr: RawFieldAccessExpr) -> Result { + let df_schema = DFSchema::try_from(self.schema.as_ref().clone())?; + for planner in self.context_provider.get_expr_planners() { + match planner.plan_field_access(field_access_expr, &df_schema)? { + PlannerResult::Planned(expr) => return Ok(expr), + PlannerResult::Original(expr) => { + field_access_expr = expr; + } + } + } + Err(Error::invalid_input("Field access could not be planned")) + } + + fn parse_sql_expr(&self, expr: &SQLExpr) -> Result { + match expr { + SQLExpr::Identifier(id) => { + // Users can pass string literals wrapped in `"`. + // (Normally SQL only allows single quotes.) + if id.quote_style == Some('"') { + Ok(Expr::Literal( + ScalarValue::Utf8(Some(id.value.clone())), + None, + )) + // Users can wrap identifiers with ` to reference non-standard + // names, such as uppercase or spaces. + } else if id.quote_style == Some('`') { + Ok(Expr::Column(Column::from_name(id.value.clone()))) + } else { + Ok(self.column(vec![id.clone()].as_slice())) + } + } + SQLExpr::CompoundIdentifier(ids) => Ok(self.column(ids.as_slice())), + SQLExpr::BinaryOp { left, op, right } => self.binary_expr(left, op, right), + SQLExpr::UnaryOp { op, expr } => self.unary_expr(op, expr), + SQLExpr::Value(value) => self.value(&value.value), + SQLExpr::Array(SQLArray { elem, .. }) => { + let mut values = vec![]; + + let array_literal_error = |pos: usize, value: &_| { + Err(Error::invalid_input(format!( + "Expected a literal value in array, instead got {} at position {}", + value, pos + ))) + }; + + for (pos, expr) in elem.iter().enumerate() { + match expr { + SQLExpr::Value(value) => { + if let Expr::Literal(value, _) = self.value(&value.value)? { + values.push(value); + } else { + return array_literal_error(pos, expr); + } + } + SQLExpr::UnaryOp { + op: UnaryOperator::Minus, + expr, + } => { + if let SQLExpr::Value(ValueWithSpan { + value: Value::Number(number, _), + .. + }) = expr.as_ref() + { + if let Expr::Literal(value, _) = self.number(number, true)? { + values.push(value); + } else { + return array_literal_error(pos, expr); + } + } else { + return array_literal_error(pos, expr); + } + } + _ => { + return array_literal_error(pos, expr); + } + } + } + + let field = if !values.is_empty() { + let data_type = values[0].data_type(); + + for value in &mut values { + if value.data_type() != data_type { + *value = safe_coerce_scalar(value, &data_type).ok_or_else(|| Error::invalid_input(format!("Array expressions must have a consistent datatype. Expected: {}, got: {}", data_type, value.data_type())))?; + } + } + Field::new("item", data_type, true) + } else { + Field::new("item", ArrowDataType::Null, true) + }; + + let values = values + .into_iter() + .map(|v| v.to_array().map_err(Error::from)) + .collect::>>()?; + let array_refs = values.iter().map(|v| v.as_ref()).collect::>(); + let values = concat(&array_refs)?; + let values = ListArray::try_new( + field.into(), + OffsetBuffer::from_lengths([values.len()]), + values, + None, + )?; + + Ok(Expr::Literal(ScalarValue::List(Arc::new(values)), None)) + } + // JSONB literal: jsonb '{"key": "value"}' + SQLExpr::TypedString(TypedString { + data_type: SQLDataType::JSONB, + value, + .. + }) => match &value.value { + Value::SingleQuotedString(s) | Value::DoubleQuotedString(s) => encode_jsonb(s), + _ => Err(Error::invalid_input( + "Expected a string value for JSONB literal", + )), + }, + // For example, DATE '2020-01-01' + SQLExpr::TypedString(TypedString { + data_type, value, .. + }) => { + let value = value.clone().into_string().expect_ok()?; + Ok(Expr::Cast(datafusion::logical_expr::Cast::new( + Box::new(Expr::Literal(ScalarValue::Utf8(Some(value)), None)), + self.parse_type(data_type)?, + ))) + } + SQLExpr::IsFalse(expr) => Ok(Expr::IsFalse(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::IsNotFalse(expr) => Ok(Expr::IsNotFalse(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::IsTrue(expr) => Ok(Expr::IsTrue(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::IsNotTrue(expr) => Ok(Expr::IsNotTrue(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::IsNull(expr) => Ok(Expr::IsNull(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::IsNotNull(expr) => Ok(Expr::IsNotNull(Box::new(self.parse_sql_expr(expr)?))), + SQLExpr::InList { + expr, + list, + negated, + } => { + let value_expr = self.parse_sql_expr(expr)?; + let list_exprs = list + .iter() + .map(|e| self.parse_sql_expr(e)) + .collect::>>()?; + Ok(value_expr.in_list(list_exprs, *negated)) + } + SQLExpr::Nested(inner) => self.parse_sql_expr(inner.as_ref()), + SQLExpr::Function(_) => self.parse_function(expr.clone()), + SQLExpr::ILike { + negated, + expr, + pattern, + escape_char, + any: _, + } => Ok(Expr::Like(Like::new( + *negated, + Box::new(self.parse_sql_expr(expr)?), + Box::new(self.parse_sql_expr(pattern)?), + parse_like_escape_char(escape_char)?, + true, + ))), + SQLExpr::Like { + negated, + expr, + pattern, + escape_char, + any: _, + } => Ok(Expr::Like(Like::new( + *negated, + Box::new(self.parse_sql_expr(expr)?), + Box::new(self.parse_sql_expr(pattern)?), + parse_like_escape_char(escape_char)?, + false, + ))), + // JSONB cast: CAST('...' AS JSONB) or '...'::jsonb + SQLExpr::Cast { + data_type: SQLDataType::JSONB, + expr: inner, + .. + } => match inner.as_ref() { + SQLExpr::Value(ValueWithSpan { + value: Value::SingleQuotedString(s) | Value::DoubleQuotedString(s), + .. + }) => encode_jsonb(s), + _ => Err(Error::invalid_input( + "CAST to JSONB only supports string literals", + )), + }, + SQLExpr::Cast { + expr, + data_type, + kind, + .. + } => match kind { + datafusion::sql::sqlparser::ast::CastKind::TryCast + | datafusion::sql::sqlparser::ast::CastKind::SafeCast => { + Ok(Expr::TryCast(datafusion::logical_expr::TryCast::new( + Box::new(self.parse_sql_expr(expr)?), + self.parse_type(data_type)?, + ))) + } + _ => Ok(Expr::Cast(datafusion::logical_expr::Cast::new( + Box::new(self.parse_sql_expr(expr)?), + self.parse_type(data_type)?, + ))), + }, + SQLExpr::JsonAccess { .. } => Err(Error::invalid_input("JSON access is not supported")), + SQLExpr::CompoundFieldAccess { root, access_chain } => { + let mut expr = self.parse_sql_expr(root)?; + + for access in access_chain { + let field_access = match access { + // x.y or x['y'] + AccessExpr::Dot(SQLExpr::Identifier(Ident { value: s, .. })) + | AccessExpr::Subscript(Subscript::Index { + index: + SQLExpr::Value(ValueWithSpan { + value: + Value::SingleQuotedString(s) | Value::DoubleQuotedString(s), + .. + }), + }) => GetFieldAccess::NamedStructField { + name: ScalarValue::from(s.as_str()), + }, + AccessExpr::Subscript(Subscript::Index { index }) => { + let key = Box::new(self.parse_sql_expr(index)?); + GetFieldAccess::ListIndex { key } + } + AccessExpr::Subscript(Subscript::Slice { .. }) => { + return Err(Error::invalid_input("Slice subscript is not supported")); + } + _ => { + // Handle other cases like JSON access + // Note: JSON access is not supported in lance + return Err(Error::invalid_input( + "Only dot notation or index access is supported for field access", + )); + } + }; + + let field_access_expr = RawFieldAccessExpr { expr, field_access }; + expr = self.plan_field_access(field_access_expr)?; + } + + Ok(expr) + } + SQLExpr::Between { + expr, + negated, + low, + high, + } => { + // Parse the main expression and bounds + let expr = self.parse_sql_expr(expr)?; + let low = self.parse_sql_expr(low)?; + let high = self.parse_sql_expr(high)?; + + let between = Expr::Between(Between::new( + Box::new(expr), + *negated, + Box::new(low), + Box::new(high), + )); + Ok(between) + } + _ => Err(Error::invalid_input(format!( + "Expression '{expr}' is not supported SQL in lance" + ))), + } + } + + /// Create Logical [Expr] from a SQL filter clause. + /// + /// Note: the returned expression must be passed through `optimize_expr()` + /// before being passed to `create_physical_expr()`. + pub fn parse_filter(&self, filter: &str) -> Result { + // Allow sqlparser to parse filter as part of ONE SQL statement. + let ast_expr = parse_sql_filter(filter)?; + let expr = self.parse_sql_expr(&ast_expr)?; + let schema = Schema::try_from(self.schema.as_ref())?; + let resolved = resolve_expr(&expr, &schema).map_err(|e| { + Error::invalid_input(format!("Error resolving filter expression {filter}: {e}")) + })?; + + Ok(coerce_filter_type_to_boolean(resolved)) + } + + /// Create Logical [Expr] from a SQL expression. + /// + /// Note: the returned expression must be passed through `optimize_filter()` + /// before being passed to `create_physical_expr()`. + pub fn parse_expr(&self, expr: &str) -> Result { + // First check if it's a simple column reference (no operators, functions, etc.) + // resolve_column_name tries exact match first, then falls back to case-insensitive + let resolved_name = self.resolve_column_name(expr); + if self.schema.field_with_name(&resolved_name).is_ok() { + return Ok(Expr::Column(Column::from_name(resolved_name))); + } + + // Parse as SQL expression + let ast_expr = parse_sql_expr(expr)?; + let expr = self.parse_sql_expr(&ast_expr)?; + let schema = Schema::try_from(self.schema.as_ref())?; + let resolved = resolve_expr(&expr, &schema)?; + Ok(resolved) + } + + /// Try to decode bytes from hex literal string. + /// + /// Copied from datafusion because this is not public. + /// + /// TODO: use SqlToRel from Datafusion directly? + fn try_decode_hex_literal(s: &str) -> Option> { + let hex_bytes = s.as_bytes(); + let mut decoded_bytes = Vec::with_capacity(hex_bytes.len().div_ceil(2)); + + let start_idx = hex_bytes.len() % 2; + if start_idx > 0 { + // The first byte is formed of only one char. + decoded_bytes.push(Self::try_decode_hex_char(hex_bytes[0])?); + } + + for i in (start_idx..hex_bytes.len()).step_by(2) { + let high = Self::try_decode_hex_char(hex_bytes[i])?; + let low = Self::try_decode_hex_char(hex_bytes[i + 1])?; + decoded_bytes.push((high << 4) | low); + } + + Some(decoded_bytes) + } + + /// Try to decode a byte from a hex char. + /// + /// None will be returned if the input char is hex-invalid. + const fn try_decode_hex_char(c: u8) -> Option { + match c { + b'A'..=b'F' => Some(c - b'A' + 10), + b'a'..=b'f' => Some(c - b'a' + 10), + b'0'..=b'9' => Some(c - b'0'), + _ => None, + } + } + + /// Optimize the filter expression and coerce data types. + pub fn optimize_expr(&self, expr: Expr) -> Result { + let df_schema = Arc::new(DFSchema::try_from(self.schema.as_ref().clone())?); + + // DataFusion needs the coerce and simplify passes to be applied before + // expressions can be handled by the physical planner. + let simplify_context = SimplifyContext::builder() + .with_schema(df_schema.clone()) + .with_query_execution_start_time(Some(Utc::now())) + .build(); + let simplifier = + datafusion::optimizer::simplify_expressions::ExprSimplifier::new(simplify_context); + + // Coerce before simplify to match DataFusion's analyzer-before-optimizer pipeline. + let expr = simplifier.coerce(expr, &df_schema)?; + let expr = simplifier.simplify(expr)?; + + Ok(expr) + } + + /// Create the [`PhysicalExpr`] from a logical [`Expr`] + pub fn create_physical_expr(&self, expr: &Expr) -> Result> { + let df_schema = Arc::new(DFSchema::try_from(self.schema.as_ref().clone())?); + Ok(datafusion::physical_expr::create_physical_expr( + expr, + df_schema.as_ref(), + &Default::default(), + )?) + } + + /// Collect the columns in the expression. + /// + /// The columns are returned in sorted order. + /// + /// If the expr refers to nested columns these will be returned + /// as dotted paths (x.y.z) + pub fn column_names_in_expr(expr: &Expr) -> Vec { + let mut visitor = ColumnCapturingVisitor { + current_path: VecDeque::new(), + columns: BTreeSet::new(), + }; + expr.visit(&mut visitor).unwrap(); + visitor.columns.into_iter().collect() + } +} + +struct ColumnCapturingVisitor { + // Current column path. If this is empty, we are not in a column expression. + current_path: VecDeque, + columns: BTreeSet, +} + +impl TreeNodeVisitor<'_> for ColumnCapturingVisitor { + type Node = Expr; + + fn f_down(&mut self, node: &Self::Node) -> DFResult { + match node { + Expr::Column(Column { name, .. }) => { + // Build the field path from the column name and any nested fields + // The nested field names from get_field already come as literal strings, + // so we just need to concatenate them properly + let mut path = name.clone(); + for part in self.current_path.drain(..) { + path.push('.'); + // Check if the part needs quoting (contains dots) + if part.contains('.') || part.contains('`') { + // Quote the field name with backticks and escape any existing backticks + let escaped = part.replace('`', "``"); + path.push('`'); + path.push_str(&escaped); + path.push('`'); + } else { + path.push_str(&part); + } + } + self.columns.insert(path); + self.current_path.clear(); + } + Expr::ScalarFunction(udf) if udf.name() == GetFieldFunc::default().name() => { + if let Some(name) = get_as_string_scalar_opt(&udf.args[1]) { + self.current_path.push_front(name.to_string()) + } else { + self.current_path.clear(); + } + } + _ => { + self.current_path.clear(); + } + } + + Ok(TreeNodeRecursion::Continue) + } +} + +#[cfg(test)] +mod tests { + + use crate::logical_expr::ExprExt; + + use super::*; + + use arrow::datatypes::Float64Type; + use arrow_array::{ + ArrayRef, BooleanArray, Float32Array, Int32Array, Int64Array, RecordBatch, StringArray, + StructArray, TimestampMicrosecondArray, TimestampMillisecondArray, + TimestampNanosecondArray, TimestampSecondArray, + }; + use arrow_schema::{DataType, Fields, Schema}; + use datafusion::{ + logical_expr::{Cast, col, lit}, + prelude::{array_element, get_field}, + }; + use datafusion_functions::core::expr_ext::FieldAccessor; + + #[test] + fn test_parse_filter_simple() { + let schema = Arc::new(Schema::new(vec![ + Field::new("i", DataType::Int32, false), + Field::new("s", DataType::Utf8, true), + Field::new( + "st", + DataType::Struct(Fields::from(vec![ + Field::new("x", DataType::Float32, false), + Field::new("y", DataType::Float32, false), + ])), + true, + ), + ])); + + let planner = Planner::new(schema.clone()); + + let expected = col("i") + .gt(lit(3_i32)) + .and(col("st").field_newstyle("x").lt_eq(lit(5.0_f32))) + .and( + col("s") + .eq(lit("str-4")) + .or(col("s").in_list(vec![lit("str-4"), lit("str-5")], false)), + ); + + // double quotes + let expr = planner + .parse_filter("i > 3 AND st.x <= 5.0 AND (s == 'str-4' OR s in ('str-4', 'str-5'))") + .unwrap(); + assert_eq!(expr, expected); + + // single quote + let expr = planner + .parse_filter("i > 3 AND st.x <= 5.0 AND (s = 'str-4' OR s in ('str-4', 'str-5'))") + .unwrap(); + + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(Int32Array::from_iter_values(0..10)) as ArrayRef, + Arc::new(StringArray::from_iter_values( + (0..10).map(|v| format!("str-{}", v)), + )), + Arc::new(StructArray::from(vec![ + ( + Arc::new(Field::new("x", DataType::Float32, false)), + Arc::new(Float32Array::from_iter_values((0..10).map(|v| v as f32))) + as ArrayRef, + ), + ( + Arc::new(Field::new("y", DataType::Float32, false)), + Arc::new(Float32Array::from_iter_values( + (0..10).map(|v| (v * 10) as f32), + )), + ), + ])), + ], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, false, true, true, false, false, false, false + ]) + ); + } + + #[test] + fn test_parse_filter_uint64_literal_above_i64_max() { + let value = u64::MAX - 1; + let batch = arrow_array::record_batch!(("id", UInt64, [1, value])).unwrap(); + let planner = Planner::new(batch.schema()); + + let expr = planner.parse_filter(&format!("id = {value}")).unwrap(); + assert_eq!(expr, col("id").eq(lit(value))); + + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![false, true]) + ); + } + + #[test] + fn test_parse_deep_logical_filter() { + let planner = Planner::new(Arc::new(Schema::empty())); + + for op in ["AND", "OR"] { + let filter = std::iter::repeat_n("true", 1000) + .collect::>() + .join(&format!(" {op} ")); + + let expr = planner.parse_filter(&filter).unwrap(); + let optimized = planner.optimize_expr(expr).unwrap(); + + assert_eq!(optimized, lit(true)); + } + } + + #[derive(Debug, Eq, PartialEq, Hash)] + struct StrictFloat64Udf { + signature: Signature, + } + + impl StrictFloat64Udf { + fn new() -> Self { + Self { + signature: Signature::exact(vec![DataType::Float64], Volatility::Immutable), + } + } + } + + impl ScalarUDFImpl for StrictFloat64Udf { + fn name(&self) -> &str { + "strict_float64" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> DFResult { + Ok(DataType::Float64) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DFResult { + let data_type = args.args[0].data_type(); + assert_eq!( + data_type, + DataType::Float64, + "strict_float64 expected Float64, got {data_type}" + ); + Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(0.0)))) + } + } + + #[test] + fn test_coerce_before_simplify() { + let planner = Planner::new(Arc::new(Schema::empty())); + let strict_float64 = Arc::new(ScalarUDF::new_from_impl(StrictFloat64Udf::new())); + let expr = Expr::ScalarFunction(ScalarFunction::new_udf(strict_float64, vec![lit(0_i64)])) + .eq(lit(0.0_f64)); + + let optimized = planner.optimize_expr(expr).unwrap(); + + planner.create_physical_expr(&optimized).unwrap(); + } + + #[test] + fn test_nested_col_refs() { + let schema = Arc::new(Schema::new(vec![ + Field::new("s0", DataType::Utf8, true), + Field::new( + "st", + DataType::Struct(Fields::from(vec![ + Field::new("s1", DataType::Utf8, true), + Field::new( + "st", + DataType::Struct(Fields::from(vec![Field::new( + "s2", + DataType::Utf8, + true, + )])), + true, + ), + ])), + true, + ), + ])); + + let planner = Planner::new(schema); + + fn assert_column_eq(planner: &Planner, expr: &str, expected: &Expr) { + let expr = planner.parse_filter(&format!("{expr} = 'val'")).unwrap(); + assert!(matches!( + expr, + Expr::BinaryExpr(BinaryExpr { + left: _, + op: Operator::Eq, + right: _ + }) + )); + if let Expr::BinaryExpr(BinaryExpr { left, .. }) = expr { + assert_eq!(left.as_ref(), expected); + } + } + + let expected = Expr::Column(Column::new_unqualified("s0")); + assert_column_eq(&planner, "s0", &expected); + assert_column_eq(&planner, "`s0`", &expected); + + let expected = Expr::ScalarFunction(ScalarFunction { + func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())), + args: vec![ + Expr::Column(Column::new_unqualified("st")), + Expr::Literal(ScalarValue::Utf8(Some("s1".to_string())), None), + ], + }); + assert_column_eq(&planner, "st.s1", &expected); + assert_column_eq(&planner, "`st`.`s1`", &expected); + assert_column_eq(&planner, "st.`s1`", &expected); + + let expected = Expr::ScalarFunction(ScalarFunction { + func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())), + args: vec![ + Expr::ScalarFunction(ScalarFunction { + func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())), + args: vec![ + Expr::Column(Column::new_unqualified("st")), + Expr::Literal(ScalarValue::Utf8(Some("st".to_string())), None), + ], + }), + Expr::Literal(ScalarValue::Utf8(Some("s2".to_string())), None), + ], + }); + + assert_column_eq(&planner, "st.st.s2", &expected); + assert_column_eq(&planner, "`st`.`st`.`s2`", &expected); + assert_column_eq(&planner, "st.st.`s2`", &expected); + assert_column_eq(&planner, "st['st'][\"s2\"]", &expected); + } + + #[test] + fn test_nested_list_refs() { + let schema = Arc::new(Schema::new(vec![Field::new( + "l", + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![Field::new("f1", DataType::Utf8, true)])), + true, + ))), + true, + )])); + + let planner = Planner::new(schema); + + let expected = array_element(col("l"), lit(0_i64)); + let expr = planner.parse_expr("l[0]").unwrap(); + assert_eq!(expr, expected); + + let expected = get_field(array_element(col("l"), lit(0_i64)), "f1"); + let expr = planner.parse_expr("l[0]['f1']").unwrap(); + assert_eq!(expr, expected); + + // FIXME: This should work, but sqlparser doesn't recognize anything + // after the period for some reason. + // let expr = planner.parse_expr("l[0].f1").unwrap(); + // assert_eq!(expr, expected); + } + + #[test] + fn test_negative_expressions() { + let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int64, false)])); + + let planner = Planner::new(schema.clone()); + + let expected = col("x") + .gt(lit(-3_i64)) + .and(col("x").lt(-(lit(-5_i64) + lit(3_i64)))); + + let expr = planner.parse_filter("x > -3 AND x < -(-5 + 3)").unwrap(); + + assert_eq!(expr, expected); + + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(Int64Array::from_iter_values(-5..5)) as ArrayRef], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, true, true, true, true, false, false, false + ]) + ); + } + + #[test] + fn test_negative_array_expressions() { + let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int64, false)])); + + let planner = Planner::new(schema); + + let expected = Expr::Literal( + ScalarValue::List(Arc::new( + ListArray::from_iter_primitive::(vec![Some( + [-1_f64, -2.0, -3.0, -4.0, -5.0].map(Some), + )]), + )), + None, + ); + + let expr = planner + .parse_expr("[-1.0, -2.0, -3.0, -4.0, -5.0]") + .unwrap(); + + assert_eq!(expr, expected); + } + + #[test] + fn test_sql_like() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + + let planner = Planner::new(schema.clone()); + + let expected = col("s").like(lit("str-4")); + // single quote + let expr = planner.parse_filter("s LIKE 'str-4'").unwrap(); + assert_eq!(expr, expected); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(StringArray::from_iter_values( + (0..10).map(|v| format!("str-{}", v)), + ))], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, false, true, false, false, false, false, false + ]) + ); + } + + #[test] + fn test_not_like() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + + let planner = Planner::new(schema.clone()); + + let expected = col("s").not_like(lit("str-4")); + // single quote + let expr = planner.parse_filter("s NOT LIKE 'str-4'").unwrap(); + assert_eq!(expr, expected); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(StringArray::from_iter_values( + (0..10).map(|v| format!("str-{}", v)), + ))], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + true, true, true, true, false, true, true, true, true, true + ]) + ); + } + + #[test] + fn test_like_escape_char() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + let planner = Planner::new(schema); + + // A valid single-character escape is captured for both LIKE and ILIKE. + for filter in ["s LIKE 'a!%' ESCAPE '!'", "s ILIKE 'a!%' ESCAPE '!'"] { + match planner.parse_filter(filter).unwrap() { + Expr::Like(like) => assert_eq!(like.escape_char, Some('!'), "{filter}"), + other => panic!("expected a LIKE expression for `{filter}`, got {other:?}"), + } + } + + // Empty and multi-character escapes are rejected rather than silently + // dropped or truncated to the first character. + for filter in [ + "s LIKE 'x' ESCAPE ''", + "s LIKE 'x' ESCAPE 'ab'", + "s ILIKE 'x' ESCAPE ''", + "s ILIKE 'x' ESCAPE 'ab'", + ] { + let err = planner.parse_filter(filter).unwrap_err(); + assert!( + err.to_string() + .contains("Invalid escape character in LIKE expression"), + "unexpected error for `{filter}`: {err}" + ); + } + } + + #[test] + fn test_sql_is_in() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + + let planner = Planner::new(schema.clone()); + + let expected = col("s").in_list(vec![lit("str-4"), lit("str-5")], false); + // single quote + let expr = planner.parse_filter("s IN ('str-4', 'str-5')").unwrap(); + assert_eq!(expr, expected); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(StringArray::from_iter_values( + (0..10).map(|v| format!("str-{}", v)), + ))], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, false, true, true, false, false, false, false + ]) + ); + } + + #[test] + fn test_sql_is_null() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)])); + + let planner = Planner::new(schema.clone()); + + let expected = col("s").is_null(); + let expr = planner.parse_filter("s IS NULL").unwrap(); + assert_eq!(expr, expected); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(StringArray::from_iter((0..10).map(|v| { + if v % 3 == 0 { + Some(format!("str-{}", v)) + } else { + None + } + })))], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, true, true, false, true, true, false, true, true, false + ]) + ); + + let expr = planner.parse_filter("s IS NOT NULL").unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + true, false, false, true, false, false, true, false, false, true, + ]) + ); + } + + #[test] + fn test_sql_invert() { + let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Boolean, true)])); + + let planner = Planner::new(schema.clone()); + + let expr = planner.parse_filter("NOT s").unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![Arc::new(BooleanArray::from_iter( + (0..10).map(|v| Some(v % 3 == 0)), + ))], + ) + .unwrap(); + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, true, true, false, true, true, false, true, true, false + ]) + ); + } + + #[test] + fn test_sql_cast() { + let cases = &[ + ( + "x = cast('2021-01-01 00:00:00' as timestamp)", + ArrowDataType::Timestamp(TimeUnit::Microsecond, None), + ), + ( + "x = cast('2021-01-01 00:00:00' as timestamp(0))", + ArrowDataType::Timestamp(TimeUnit::Second, None), + ), + ( + "x = cast('2021-01-01 00:00:00.123' as timestamp(9))", + ArrowDataType::Timestamp(TimeUnit::Nanosecond, None), + ), + ( + "x = cast('2021-01-01 00:00:00.123' as datetime(9))", + ArrowDataType::Timestamp(TimeUnit::Nanosecond, None), + ), + ("x = cast('2021-01-01' as date)", ArrowDataType::Date32), + ( + "x = cast('1.238' as decimal(9,3))", + ArrowDataType::Decimal128(9, 3), + ), + ("x = cast(1 as float)", ArrowDataType::Float32), + ("x = cast(1 as double)", ArrowDataType::Float64), + ("x = cast(1 as tinyint)", ArrowDataType::Int8), + ("x = cast(1 as smallint)", ArrowDataType::Int16), + ("x = cast(1 as int)", ArrowDataType::Int32), + ("x = cast(1 as integer)", ArrowDataType::Int32), + ("x = cast(1 as bigint)", ArrowDataType::Int64), + ("x = cast(1 as tinyint unsigned)", ArrowDataType::UInt8), + ("x = cast(1 as smallint unsigned)", ArrowDataType::UInt16), + ("x = cast(1 as int unsigned)", ArrowDataType::UInt32), + ("x = cast(1 as integer unsigned)", ArrowDataType::UInt32), + ("x = cast(1 as bigint unsigned)", ArrowDataType::UInt64), + ("x = cast(1 as boolean)", ArrowDataType::Boolean), + ("x = cast(1 as string)", ArrowDataType::Utf8), + ]; + + for (sql, expected_data_type) in cases { + let schema = Arc::new(Schema::new(vec![Field::new( + "x", + expected_data_type.clone(), + true, + )])); + let planner = Planner::new(schema.clone()); + let expr = planner.parse_filter(sql).unwrap(); + + // Get the thing after 'cast(` but before ' as'. + let expected_value_str = sql + .split("cast(") + .nth(1) + .unwrap() + .split(" as") + .next() + .unwrap(); + // Remove any quote marks + let expected_value_str = expected_value_str.trim_matches('\''); + + match expr { + Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() { + Expr::Cast(Cast { expr, field }) => { + match expr.as_ref() { + Expr::Literal(ScalarValue::Utf8(Some(value_str)), _) => { + assert_eq!(value_str, expected_value_str); + } + Expr::Literal(ScalarValue::Int64(Some(value)), _) => { + assert_eq!(*value, 1); + } + _ => panic!("Expected cast to be applied to literal"), + } + assert_eq!(field.data_type(), expected_data_type); + } + _ => panic!("Expected right to be a cast"), + }, + _ => panic!("Expected binary expression"), + } + } + } + + #[test] + fn test_sql_literals() { + let cases = &[ + ( + "x = timestamp '2021-01-01 00:00:00'", + ArrowDataType::Timestamp(TimeUnit::Microsecond, None), + ), + ( + "x = timestamp(0) '2021-01-01 00:00:00'", + ArrowDataType::Timestamp(TimeUnit::Second, None), + ), + ( + "x = timestamp(9) '2021-01-01 00:00:00.123'", + ArrowDataType::Timestamp(TimeUnit::Nanosecond, None), + ), + ("x = date '2021-01-01'", ArrowDataType::Date32), + ("x = decimal(9,3) '1.238'", ArrowDataType::Decimal128(9, 3)), + ]; + + for (sql, expected_data_type) in cases { + let schema = Arc::new(Schema::new(vec![Field::new( + "x", + expected_data_type.clone(), + true, + )])); + let planner = Planner::new(schema.clone()); + let expr = planner.parse_filter(sql).unwrap(); + + let expected_value_str = sql.split('\'').nth(1).unwrap(); + + match expr { + Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() { + Expr::Cast(Cast { expr, field }) => { + match expr.as_ref() { + Expr::Literal(ScalarValue::Utf8(Some(value_str)), _) => { + assert_eq!(value_str, expected_value_str); + } + _ => panic!("Expected cast to be applied to literal"), + } + assert_eq!(field.data_type(), expected_data_type); + } + _ => panic!("Expected right to be a cast"), + }, + _ => panic!("Expected binary expression"), + } + } + } + + #[test] + fn test_sql_array_literals() { + let cases = [ + ( + "x = [1, 2, 3]", + ArrowDataType::List(Arc::new(Field::new("item", ArrowDataType::Int64, true))), + ), + ( + "x = [1, 2, 3]", + ArrowDataType::FixedSizeList( + Arc::new(Field::new("item", ArrowDataType::Int64, true)), + 3, + ), + ), + ]; + + for (sql, expected_data_type) in cases { + let schema = Arc::new(Schema::new(vec![Field::new( + "x", + expected_data_type.clone(), + true, + )])); + let planner = Planner::new(schema.clone()); + let expr = planner.parse_filter(sql).unwrap(); + let expr = planner.optimize_expr(expr).unwrap(); + + match expr { + Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() { + Expr::Literal(value, _) => { + assert_eq!(&value.data_type(), &expected_data_type); + } + _ => panic!("Expected right to be a literal"), + }, + _ => panic!("Expected binary expression"), + } + } + } + + #[test] + fn test_sql_between() { + use arrow_array::{Float64Array, Int32Array, TimestampMicrosecondArray}; + use arrow_schema::{DataType, Field, Schema, TimeUnit}; + use std::sync::Arc; + + let schema = Arc::new(Schema::new(vec![ + Field::new("x", DataType::Int32, false), + Field::new("y", DataType::Float64, false), + Field::new( + "ts", + DataType::Timestamp(TimeUnit::Microsecond, None), + false, + ), + ])); + + let planner = Planner::new(schema.clone()); + + // Test integer BETWEEN + let expr = planner + .parse_filter("x BETWEEN CAST(3 AS INT) AND CAST(7 AS INT)") + .unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + // Create timestamp array with values representing: + // 2024-01-01 00:00:00 to 2024-01-01 00:00:09 (in microseconds) + let base_ts = 1704067200000000_i64; // 2024-01-01 00:00:00 + let ts_array = TimestampMicrosecondArray::from_iter_values( + (0..10).map(|i| base_ts + i * 1_000_000), // Each value is 1 second apart + ); + + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(Int32Array::from_iter_values(0..10)) as ArrayRef, + Arc::new(Float64Array::from_iter_values((0..10).map(|v| v as f64))), + Arc::new(ts_array), + ], + ) + .unwrap(); + + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, true, true, true, true, true, false, false + ]) + ); + + // Test NOT BETWEEN + let expr = planner + .parse_filter("x NOT BETWEEN CAST(3 AS INT) AND CAST(7 AS INT)") + .unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + true, true, true, false, false, false, false, false, true, true + ]) + ); + + // Test floating point BETWEEN + let expr = planner.parse_filter("y BETWEEN 2.5 AND 6.5").unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, true, true, true, true, false, false, false + ]) + ); + + // Test timestamp BETWEEN + let expr = planner + .parse_filter( + "ts BETWEEN timestamp '2024-01-01 00:00:03' AND timestamp '2024-01-01 00:00:07'", + ) + .unwrap(); + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let predicates = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + predicates.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![ + false, false, false, true, true, true, true, true, false, false + ]) + ); + } + + #[test] + fn test_sql_comparison() { + // Create a batch with all data types + let batch: Vec<(&str, ArrayRef)> = vec![ + ( + "timestamp_s", + Arc::new(TimestampSecondArray::from_iter_values(0..10)), + ), + ( + "timestamp_ms", + Arc::new(TimestampMillisecondArray::from_iter_values(0..10)), + ), + ( + "timestamp_us", + Arc::new(TimestampMicrosecondArray::from_iter_values(0..10)), + ), + ( + "timestamp_ns", + Arc::new(TimestampNanosecondArray::from_iter_values(4995..5005)), + ), + ]; + let batch = RecordBatch::try_from_iter(batch).unwrap(); + + let planner = Planner::new(batch.schema()); + + // Each expression is meant to select the final 5 rows + let expressions = &[ + "timestamp_s >= TIMESTAMP '1970-01-01 00:00:05'", + "timestamp_ms >= TIMESTAMP '1970-01-01 00:00:00.005'", + "timestamp_us >= TIMESTAMP '1970-01-01 00:00:00.000005'", + "timestamp_ns >= TIMESTAMP '1970-01-01 00:00:00.000005'", + ]; + + let expected: ArrayRef = Arc::new(BooleanArray::from_iter( + std::iter::repeat_n(Some(false), 5).chain(std::iter::repeat_n(Some(true), 5)), + )); + for expression in expressions { + // convert to physical expression + let logical_expr = planner.parse_filter(expression).unwrap(); + let logical_expr = planner.optimize_expr(logical_expr).unwrap(); + let physical_expr = planner.create_physical_expr(&logical_expr).unwrap(); + + // Evaluate and assert they have correct results + let result = physical_expr.evaluate(&batch).unwrap(); + let result = result.into_array(batch.num_rows()).unwrap(); + assert_eq!(&expected, &result, "unexpected result for {}", expression); + } + } + + #[test] + fn test_columns_in_expr() { + let expr = col("s0").gt(lit("value")).and( + col("st") + .field("st") + .field("s2") + .eq(lit("value")) + .or(col("st") + .field("s1") + .in_list(vec![lit("value 1"), lit("value 2")], false)), + ); + + let columns = Planner::column_names_in_expr(&expr); + assert_eq!(columns, vec!["s0", "st.s1", "st.st.s2"]); + } + + #[test] + fn test_parse_binary_expr() { + let bin_str = "x'616263'"; + + let schema = Arc::new(Schema::new(vec![Field::new( + "binary", + DataType::Binary, + true, + )])); + let planner = Planner::new(schema); + let expr = planner.parse_expr(bin_str).unwrap(); + assert_eq!( + expr, + Expr::Literal(ScalarValue::Binary(Some(vec![b'a', b'b', b'c'])), None) + ); + } + + #[test] + fn test_lance_context_provider_expr_planners() { + let ctx_provider = LanceContextProvider::default(); + assert!(!ctx_provider.get_expr_planners().is_empty()); + } + + #[test] + fn test_regexp_match_and_non_empty_captions() { + // Repro for a bug where regexp_match inside an AND chain wasn't coerced to boolean, + // causing planning/evaluation failures. This should evaluate successfully. + let schema = Arc::new(Schema::new(vec![ + Field::new("keywords", DataType::Utf8, true), + Field::new("natural_caption", DataType::Utf8, true), + Field::new("poetic_caption", DataType::Utf8, true), + ])); + + let planner = Planner::new(schema.clone()); + + let expr = planner + .parse_filter( + "regexp_match(keywords, 'Liberty|revolution') AND \ + (natural_caption IS NOT NULL AND natural_caption <> '' AND \ + poetic_caption IS NOT NULL AND poetic_caption <> '')", + ) + .unwrap(); + + let physical_expr = planner.create_physical_expr(&expr).unwrap(); + + let batch = RecordBatch::try_new( + schema, + vec![ + Arc::new(StringArray::from(vec![ + Some("Liberty for all"), + Some("peace"), + Some("revolution now"), + Some("Liberty"), + Some("revolutionary"), + Some("none"), + ])) as ArrayRef, + Arc::new(StringArray::from(vec![ + Some("a"), + Some("b"), + None, + Some(""), + Some("c"), + Some("d"), + ])) as ArrayRef, + Arc::new(StringArray::from(vec![ + Some("x"), + Some(""), + Some("y"), + Some("z"), + None, + Some("w"), + ])) as ArrayRef, + ], + ) + .unwrap(); + + let result = physical_expr.evaluate(&batch).unwrap(); + assert_eq!( + result.into_array(0).unwrap().as_ref(), + &BooleanArray::from(vec![true, false, false, false, false, false]) + ); + } + + #[test] + fn test_regexp_match_infer_error_without_boolean_coercion() { + // With the fix applied, using parse_filter should coerce regexp_match to boolean + // even when nested in a larger AND expression, so this should plan successfully. + let schema = Arc::new(Schema::new(vec![ + Field::new("keywords", DataType::Utf8, true), + Field::new("natural_caption", DataType::Utf8, true), + Field::new("poetic_caption", DataType::Utf8, true), + ])); + + let planner = Planner::new(schema); + + let expr = planner + .parse_filter( + "regexp_match(keywords, 'Liberty|revolution') AND \ + (natural_caption IS NOT NULL AND natural_caption <> '' AND \ + poetic_caption IS NOT NULL AND poetic_caption <> '')", + ) + .unwrap(); + + // Should not panic + let _physical = planner.create_physical_expr(&expr).unwrap(); + } + + #[test] + fn test_jsonb_literals() { + let schema = Arc::new(Schema::new(vec![Field::new( + "j", + DataType::LargeBinary, + true, + )])); + let planner = Planner::new(schema); + + let cases = [ + ("jsonb '{\"key\": \"value\"}'", r#"{"key":"value"}"#), + ("cast('{\"a\": 1}' as jsonb)", r#"{"a":1}"#), + ("'{\"a\": 1}'::jsonb", r#"{"a":1}"#), + ]; + for (sql, expected) in cases { + let ast = parse_sql_expr(sql).unwrap(); + let expr = planner.parse_sql_expr(&ast).unwrap(); + match expr { + Expr::Literal(ScalarValue::LargeBinary(Some(bytes)), _) => { + assert_eq!( + lance_arrow::json::decode_json(&bytes), + expected, + "failed for: {sql}" + ); + } + other => panic!("Expected LargeBinary literal for '{sql}', got: {other:?}"), + } + } + } + + #[test] + fn test_jsonb_literal_errors() { + let schema = Arc::new(Schema::new(vec![Field::new( + "j", + DataType::LargeBinary, + true, + )])); + let planner = Planner::new(schema); + + // Invalid JSON content + let ast = parse_sql_expr("jsonb 'not valid json'").unwrap(); + let err = planner.parse_sql_expr(&ast).unwrap_err(); + assert!( + err.to_string().contains("Failed to encode JSONB"), + "expected JSONB encoding error, got: {err}" + ); + + // CAST with non-literal expression + let ast = parse_sql_expr("cast(j as jsonb)").unwrap(); + let err = planner.parse_sql_expr(&ast).unwrap_err(); + assert!( + err.to_string() + .contains("CAST to JSONB only supports string literals"), + "got: {err}" + ); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/projection.rs b/lance-artifact/rust/lance-datafusion/src/projection.rs new file mode 100644 index 000000000..463e83a30 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/projection.rs @@ -0,0 +1,733 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_array::RecordBatch; +use arrow_schema::{DataType, Field as ArrowField, Schema as ArrowSchema}; +use datafusion::{logical_expr::Expr, physical_plan::projection::ProjectionExec}; +use datafusion_common::{Column, DFSchema}; +use datafusion_physical_expr::PhysicalExpr; +use futures::TryStreamExt; +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, +}; +use tracing::instrument; + +use lance_core::{ + Error, ROW_ADDR, ROW_CREATED_AT_VERSION, ROW_ID, ROW_LAST_UPDATED_AT_VERSION, ROW_OFFSET, + Result, WILDCARD, + datatypes::{OnMissing, Projectable, Projection, Schema}, +}; + +use crate::{ + exec::{LanceExecutionOptions, OneShotExec, execute_plan}, + planner::Planner, +}; + +const SCORING_COLUMNS: [&str; 2] = ["_distance", "_score"]; + +fn canonical_scoring_column(name: &str) -> Option<&'static str> { + SCORING_COLUMNS + .into_iter() + .find(|scoring_column| name.eq_ignore_ascii_case(scoring_column)) +} + +struct ProjectionBuilder { + base: Arc, + planner: Planner, + output: HashMap, + output_cols: Vec, + scoring_exprs: HashMap, + physical_cols_set: HashSet, + physical_cols: Vec, + needs_row_id: bool, + needs_row_addr: bool, + needs_row_last_updated_at: bool, + needs_row_created_at: bool, + must_add_row_offset: bool, + has_wildcard: bool, +} + +impl ProjectionBuilder { + fn new(base: Arc) -> Self { + let full_schema = Arc::new(Projection::full(base.clone()).to_arrow_schema()); + let full_schema = Arc::new(ProjectionPlan::add_system_columns(&full_schema)); + let planner = Planner::new(full_schema); + + Self { + base, + planner, + output: HashMap::default(), + output_cols: Vec::default(), + scoring_exprs: HashMap::default(), + physical_cols_set: HashSet::default(), + physical_cols: Vec::default(), + needs_row_id: false, + needs_row_addr: false, + needs_row_created_at: false, + needs_row_last_updated_at: false, + must_add_row_offset: false, + has_wildcard: false, + } + } + + fn check_duplicate_column(&self, name: &str) -> Result<()> { + if self.output.contains_key(name) { + return Err(Error::invalid_input(format!( + "Duplicate column name: {}", + name + ))); + } + Ok(()) + } + + fn add_column(&mut self, output_name: &str, raw_expr: &str) -> Result<()> { + self.check_duplicate_column(output_name)?; + + let expr = self.planner.parse_expr(raw_expr)?; + let expr = if Self::references_scoring_column(&expr) { + // A scoring name can refer to either a stored column or a search-generated + // Float32 column. Reparse and coerce once the physical input schema disambiguates it. + self.scoring_exprs + .insert(output_name.to_string(), raw_expr.to_string()); + expr + } else { + // Run simplification + coercion so that expressions like `coalesce(...)` + // (which DataFusion's physical evaluator expects to have been rewritten + // into a `CASE` expression by the simplifier) work correctly. + self.planner.optimize_expr(expr)? + }; + + // If the expression is a bare column reference to a system column, mark that we need it + if let Expr::Column(Column { + name, + relation: None, + .. + }) = &expr + { + if name == ROW_ID { + self.needs_row_id = true; + } else if name == ROW_ADDR { + self.needs_row_addr = true; + } else if name == ROW_OFFSET { + self.must_add_row_offset = true; + } else if name == ROW_LAST_UPDATED_AT_VERSION { + self.needs_row_last_updated_at = true; + } else if name == ROW_CREATED_AT_VERSION { + self.needs_row_created_at = true; + } + } + + for col in Planner::column_names_in_expr(&expr) { + // Discovery can bind an exact provisional scoring field beside a mixed-case stored + // field. Load the stored field too so final-schema replanning can select the stored + // or search-generated field from the physical input. + let physical_col = if canonical_scoring_column(&col).is_some() { + self.base + .schema() + .field_case_insensitive(&col) + .map(|field| field.name.clone()) + .unwrap_or(col) + } else { + col + }; + if self.physical_cols_set.contains(&physical_col) { + continue; + } + self.physical_cols.push(physical_col.clone()); + self.physical_cols_set.insert(physical_col); + } + self.output.insert(output_name.to_string(), expr.clone()); + + self.output_cols.push(OutputColumn { + expr, + name: output_name.to_string(), + }); + + Ok(()) + } + + fn references_scoring_column(expr: &Expr) -> bool { + Planner::column_names_in_expr(expr) + .iter() + .any(|name| canonical_scoring_column(name).is_some()) + } + + fn add_columns(&mut self, columns: &[(impl AsRef, impl AsRef)]) -> Result<()> { + for (output_name, raw_expr) in columns { + if raw_expr.as_ref() == WILDCARD { + self.has_wildcard = true; + for col in self.base.schema().fields.iter().map(|f| f.name.as_str()) { + self.check_duplicate_column(col)?; + self.output_cols.push(OutputColumn { + expr: Expr::Column(Column::from_name(col)), + name: col.to_string(), + }); + // Throw placeholder expr in self.output, this will trigger error on duplicates + self.output.insert(col.to_string(), Expr::default()); + } + } else { + self.add_column(output_name.as_ref(), raw_expr.as_ref())?; + } + } + Ok(()) + } + + fn build(self) -> Result { + // Now, calculate the physical projection from the columns referenced by the expressions + // + // If a column is missing it might be a system column (_rowid, _distance, etc.) and so + // we ignore it. We don't need to load that column from disk at least, which is all we are + // trying to calculate here. + let mut physical_projection = if self.has_wildcard { + Projection::full(self.base.clone()) + } else { + Projection::empty(self.base.clone()) + .union_columns(&self.physical_cols, OnMissing::Ignore)? + }; + + physical_projection.with_row_id = self.needs_row_id; + physical_projection.with_row_addr = self.needs_row_addr || self.must_add_row_offset; + physical_projection.with_row_last_updated_at_version = self.needs_row_last_updated_at; + physical_projection.with_row_created_at_version = self.needs_row_created_at; + + Ok(ProjectionPlan { + physical_projection, + must_add_row_offset: self.must_add_row_offset, + requested_output_expr: self.output_cols, + scoring_exprs: self.scoring_exprs, + }) + } +} + +#[derive(Clone, Debug)] +pub struct OutputColumn { + /// The expression that represents the output column + pub expr: Expr, + /// The name of the output column + pub name: String, +} + +#[derive(Clone, Debug)] +pub struct ProjectionPlan { + /// The physical schema that must be loaded from the dataset + pub physical_projection: Projection, + + /// Needs the row address converted into a row offset + pub must_add_row_offset: bool, + + /// The desired output columns + pub requested_output_expr: Vec, + + /// Original SQL for scoring expressions that must be replanned against the physical schema. + scoring_exprs: HashMap, +} + +impl ProjectionPlan { + fn add_system_columns(schema: &ArrowSchema) -> ArrowSchema { + let mut fields = Vec::from_iter(schema.fields.iter().cloned()); + fields.push(Arc::new(ArrowField::new(ROW_ID, DataType::UInt64, true))); + fields.push(Arc::new(ArrowField::new(ROW_ADDR, DataType::UInt64, true))); + fields.push(Arc::new(ArrowField::new( + ROW_OFFSET, + DataType::UInt64, + true, + ))); + fields.push(Arc::new( + (*lance_core::ROW_LAST_UPDATED_AT_VERSION_FIELD).clone(), + )); + fields.push(Arc::new( + (*lance_core::ROW_CREATED_AT_VERSION_FIELD).clone(), + )); + // Exact scoring fields are needed for initial parsing of schema-dependent functions, even + // beside a mixed-case stored field. The stored field is carried into the physical + // projection separately, and scoring expressions are replanned against the final schema. + for name in SCORING_COLUMNS { + if schema.field_with_name(name).is_err() { + fields.push(Arc::new(ArrowField::new(name, DataType::Float32, true))); + } + } + ArrowSchema::new(fields) + } + + /// Set the projection from SQL expressions + pub fn from_expressions( + base: Arc, + columns: &[(impl AsRef, impl AsRef)], + ) -> Result { + let mut builder = ProjectionBuilder::new(base); + builder.add_columns(columns)?; + builder.build() + } + + /// Set the projection from a schema + /// + /// This plan will have no complex expressions, the schema must be a subset of the dataset schema. + /// + /// With this approach it is possible to refer to portions of nested fields. + /// + /// For example, if the schema is: + /// + /// ```ignore + /// { + /// "metadata": { + /// "location": { + /// "x": f32, + /// "y": f32, + /// }, + /// "age": i32, + /// } + /// } + /// ``` + /// + /// It is possible to project a partial schema that drops `y` like: + /// + /// ```ignore + /// { + /// "metadata": { + /// "location": { + /// "x": f32, + /// }, + /// "age": i32, + /// } + /// } + /// ``` + /// + /// This is something that cannot be done easily using expressions. + pub fn from_schema(base: Arc, projection: &Schema) -> Result { + // Separate data columns from system columns + // System columns (_rowid, _rowaddr, etc.) are handled via flags in Projection, + // not as fields in the Schema + let mut data_fields = Vec::new(); + let mut with_row_id = false; + let mut with_row_addr = false; + let mut must_add_row_offset = false; + let mut with_row_last_updated_at_version = false; + let mut with_row_created_at_version = false; + + for field in projection.fields.iter() { + if lance_core::is_system_column(&field.name) { + // Handle known system columns that can be included in projections + if field.name == ROW_ID { + with_row_id = true; + must_add_row_offset = true; + } else if field.name == ROW_ADDR { + with_row_addr = true; + } else if field.name == ROW_OFFSET { + with_row_addr = true; + must_add_row_offset = true; + } else if field.name == ROW_LAST_UPDATED_AT_VERSION { + with_row_last_updated_at_version = true; + } else if field.name == ROW_CREATED_AT_VERSION { + with_row_created_at_version = true; + } + } else { + // Regular data column - validate it exists in base schema + if base.schema().field(&field.name).is_none() { + return Err(Error::invalid_input(format!( + "Column '{}' not found in schema", + field.name + ))); + } + data_fields.push(field.clone()); + } + } + + // Create a schema with only data columns for the physical projection + let data_schema = Schema { + fields: data_fields, + metadata: projection.metadata.clone(), + }; + + // Calculate the physical projection from data columns only + let mut physical_projection = Projection::empty(base).union_schema(&data_schema); + physical_projection.with_row_id = with_row_id; + physical_projection.with_row_addr = with_row_addr; + physical_projection.with_row_last_updated_at_version = with_row_last_updated_at_version; + physical_projection.with_row_created_at_version = with_row_created_at_version; + + // Build output expressions preserving the original order (including system columns) + let exprs = projection + .fields + .iter() + .map(|f| OutputColumn { + expr: Expr::Column(Column::from_name(&f.name)), + name: f.name.clone(), + }) + .collect::>(); + + Ok(Self { + physical_projection, + requested_output_expr: exprs, + must_add_row_offset, + scoring_exprs: HashMap::default(), + }) + } + + pub fn full(base: Arc) -> Result { + let physical_cols: Vec<&str> = base + .schema() + .fields + .iter() + .map(|f| f.name.as_ref()) + .collect::>(); + + let physical_projection = + Projection::empty(base.clone()).union_columns(&physical_cols, OnMissing::Ignore)?; + + let requested_output_expr = physical_cols + .into_iter() + .map(|col_name| OutputColumn { + expr: Expr::Column(Column::from_name(col_name)), + name: col_name.to_string(), + }) + .collect(); + + Ok(Self { + physical_projection, + must_add_row_offset: false, + requested_output_expr, + scoring_exprs: HashMap::default(), + }) + } + + /// Convert the projection to a list of physical expressions + /// + /// This is used to apply the final projection (including dynamic expressions) to the data. + pub fn to_physical_exprs( + &self, + current_schema: &ArrowSchema, + ) -> Result, String)>> { + let physical_df_schema = Arc::new(DFSchema::try_from(current_schema.clone())?); + self.requested_output_expr + .iter() + .map(|output_column| { + let expr = if let Some(raw_expr) = self.scoring_exprs.get(&output_column.name) { + let planner = Planner::new(Arc::new(current_schema.clone())); + let expr = planner.parse_expr(raw_expr)?; + planner.optimize_expr(expr)? + } else { + output_column.expr.clone() + }; + Ok(( + datafusion::physical_expr::create_physical_expr( + &expr, + physical_df_schema.as_ref(), + &Default::default(), + )?, + output_column.name.clone(), + )) + }) + .collect::>>() + } + + /// Include the row id in the output + pub fn include_row_id(&mut self) { + self.physical_projection.with_row_id = true; + if !self + .requested_output_expr + .iter() + .any(|OutputColumn { name, .. }| name == ROW_ID) + { + self.requested_output_expr.push(OutputColumn { + expr: Expr::Column(Column::from_name(ROW_ID)), + name: ROW_ID.to_string(), + }); + } + } + + /// Include the row address in the output + pub fn include_row_addr(&mut self) { + self.physical_projection.with_row_addr = true; + if !self + .requested_output_expr + .iter() + .any(|OutputColumn { name, .. }| name == ROW_ADDR) + { + self.requested_output_expr.push(OutputColumn { + expr: Expr::Column(Column::from_name(ROW_ADDR)), + name: ROW_ADDR.to_string(), + }); + } + } + + /// Check if the projection has any output columns + /// + /// This doesn't mean there is a physical projection. For example, we may someday support + /// something like `SELECT 1 AS foo` which would have an output column (foo) but no physical projection + pub fn has_output_cols(&self) -> bool { + !self.requested_output_expr.is_empty() + } + + pub fn output_schema(&self) -> Result { + let physical_schema = self.physical_projection.to_arrow_schema(); + let exprs = self.to_physical_exprs(&physical_schema)?; + let fields = exprs + .iter() + .map(|(expr, name)| { + let metadata = expr.return_field(&physical_schema)?.metadata().clone(); + Ok(ArrowField::new( + name, + expr.data_type(&physical_schema)?, + expr.nullable(&physical_schema)?, + ) + .with_metadata(metadata)) + }) + .collect::>>()?; + Ok(ArrowSchema::new_with_metadata( + fields, + physical_schema.metadata().clone(), + )) + } + + #[instrument(skip_all, level = "debug")] + pub async fn project_batch(&self, batch: RecordBatch) -> Result { + let src = Arc::new(OneShotExec::from_batch(batch)); + + // Need to add ROW_OFFSET to get filterable schema + let extra_columns = vec![ + ArrowField::new(ROW_ADDR, DataType::UInt64, true), + ArrowField::new(ROW_OFFSET, DataType::UInt64, true), + ]; + let mut filterable_schema = self.physical_projection.to_schema(); + filterable_schema = filterable_schema.merge(&ArrowSchema::new(extra_columns))?; + + let physical_exprs = self.to_physical_exprs(&(&filterable_schema).into())?; + let projection = Arc::new(ProjectionExec::try_new(physical_exprs, src)?); + + // Run dummy plan to execute projection, do not log the plan run + let stream = execute_plan( + projection, + LanceExecutionOptions { + skip_logging: true, + ..Default::default() + }, + )?; + let batches = stream.try_collect::>().await?; + if batches.len() != 1 { + Err(Error::internal("Expected exactly one batch".to_string())) + } else { + Ok(batches.into_iter().next().unwrap()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use arrow_array::{ArrayRef, Float32Array, Int64Array}; + use lance_arrow::json::{is_json_field, json_field}; + + #[test] + fn test_scoring_column_expression() { + for scoring_column in ["_distance", "_score"] { + for has_stored_column in [false, true] { + let base = if has_stored_column { + Arc::new( + Schema::try_from(&ArrowSchema::new(vec![ArrowField::new( + scoring_column, + DataType::Float64, + true, + )])) + .unwrap(), + ) + } else { + Arc::new(Schema::default()) + }; + let expression = format!("1 - {scoring_column}"); + let plan = + ProjectionPlan::from_expressions(base, &[("inverted", expression.as_str())]) + .unwrap(); + + if has_stored_column { + let stored_output = plan.output_schema().unwrap(); + assert_eq!(stored_output.field(0).data_type(), &DataType::Float64); + } + + let batch = RecordBatch::try_from_iter([( + scoring_column, + Arc::new(Float32Array::from(vec![0.25, 0.75])) as ArrayRef, + )]) + .unwrap(); + + let physical_exprs = plan.to_physical_exprs(batch.schema().as_ref()).unwrap(); + let values = physical_exprs[0] + .0 + .evaluate(&batch) + .unwrap() + .into_array(batch.num_rows()) + .unwrap(); + + assert_eq!( + values.as_ref(), + &Float32Array::from(vec![0.75, 0.25]), + "unexpected result for {scoring_column}", + ); + } + } + } + + #[test] + fn test_stored_scoring_column_does_not_break_other_expressions() { + for scoring_column in ["_distance", "_score"] { + let base = Arc::new( + Schema::try_from(&ArrowSchema::new(vec![ + ArrowField::new("id", DataType::Int64, false), + ArrowField::new(scoring_column, DataType::Float64, true), + ])) + .unwrap(), + ); + + ProjectionPlan::from_expressions(base, &[("incremented", "id + 1")]).unwrap(); + } + } + + #[test] + fn test_stored_scoring_column_is_case_insensitive() { + for (stored_name, requested_name) in [("_Distance", "_distance"), ("_Score", "_score")] { + let base = Arc::new( + Schema::try_from(&ArrowSchema::new(vec![ArrowField::new( + stored_name, + DataType::Float64, + true, + )])) + .unwrap(), + ); + let plan = + ProjectionPlan::from_expressions(base, &[("stored", requested_name)]).unwrap(); + + assert_eq!( + plan.output_schema().unwrap().field(0).data_type(), + &DataType::Float64, + ); + + let batch = RecordBatch::try_from_iter([( + requested_name, + Arc::new(Float32Array::from(vec![0.25, 0.75])) as ArrayRef, + )]) + .unwrap(); + let physical_exprs = plan.to_physical_exprs(batch.schema().as_ref()).unwrap(); + assert_eq!( + physical_exprs[0] + .0 + .data_type(batch.schema().as_ref()) + .unwrap(), + DataType::Float32, + ); + } + } + + #[test] + fn test_generated_scoring_function_with_mixed_case_stored_column() { + for (stored_name, generated_name) in [("_Distance", "_distance"), ("_Score", "_score")] { + let base = Arc::new( + Schema::try_from(&ArrowSchema::new(vec![ArrowField::new( + stored_name, + DataType::Float64, + true, + )])) + .unwrap(), + ); + let expression = format!("coalesce(1 - {generated_name}, 0)"); + let plan = + ProjectionPlan::from_expressions(base, &[("normalized", expression.as_str())]) + .unwrap(); + let batch = RecordBatch::try_from_iter([( + generated_name, + Arc::new(Float32Array::from(vec![Some(0.25), None])) as ArrayRef, + )]) + .unwrap(); + + let physical_exprs = plan.to_physical_exprs(batch.schema().as_ref()).unwrap(); + let values = physical_exprs[0] + .0 + .evaluate(&batch) + .unwrap() + .into_array(batch.num_rows()) + .unwrap(); + assert_eq!(values.as_ref(), &Float32Array::from(vec![0.75, 0.0])); + } + } + + #[test] + fn test_scoring_column_function_expression() { + for scoring_column in ["_distance", "_score"] { + let expression = format!("coalesce(1 - {scoring_column}, 0)"); + let plan = ProjectionPlan::from_expressions( + Arc::new(Schema::default()), + &[("normalized", expression.as_str())], + ) + .unwrap(); + let batch = RecordBatch::try_from_iter([( + scoring_column, + Arc::new(Float32Array::from(vec![Some(0.25), None])) as ArrayRef, + )]) + .unwrap(); + + let physical_exprs = plan.to_physical_exprs(batch.schema().as_ref()).unwrap(); + let values = physical_exprs[0] + .0 + .evaluate(&batch) + .unwrap() + .into_array(batch.num_rows()) + .unwrap(); + assert_eq!(values.as_ref(), &Float32Array::from(vec![0.75, 0.0])); + } + } + + #[tokio::test] + async fn test_coalesce_in_column_map() { + // Regression test: `coalesce` in a column-map expression used to fail with + // "coalesce should have been simplified to case" because the parsed expression + // was passed straight to `create_physical_expr` without running the simplifier. + let arrow_schema = Arc::new(ArrowSchema::new(vec![ + ArrowField::new("col_a", DataType::Int64, true), + ArrowField::new("col_b", DataType::Int64, true), + ])); + let base_schema = Schema::try_from(arrow_schema.as_ref()).unwrap(); + let base = Arc::new(base_schema); + + let plan = + ProjectionPlan::from_expressions(base, &[("foo", "coalesce(col_a, col_b)")]).unwrap(); + + let batch = RecordBatch::try_new( + arrow_schema, + vec![ + Arc::new(Int64Array::from(vec![Some(1), None, Some(3), None])), + Arc::new(Int64Array::from(vec![Some(10), Some(20), None, None])), + ], + ) + .unwrap(); + + let projected = plan.project_batch(batch).await.unwrap(); + let foo = projected + .column(0) + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!( + foo.iter().collect::>(), + vec![Some(1), Some(20), Some(3), None], + ); + } + + #[test] + fn test_output_schema_preserves_json_extension_metadata() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("id", DataType::Int32, false), + json_field("meta", true), + ]); + let base_schema = Schema::try_from(&arrow_schema).unwrap(); + let base = Arc::new(base_schema.clone()); + + let plan = ProjectionPlan::from_schema(base, &base_schema).unwrap(); + + let physical = plan.physical_projection.to_arrow_schema(); + assert!(is_json_field(physical.field_with_name("meta").unwrap())); + + let output = plan.output_schema().unwrap(); + let output_field = output.field_with_name("meta").unwrap(); + assert!(is_json_field(output_field)); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/spill.rs b/lance-artifact/rust/lance-datafusion/src/spill.rs new file mode 100644 index 000000000..7f4c45efa --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/spill.rs @@ -0,0 +1,897 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + io::{BufReader, BufWriter}, + path::PathBuf, + sync::{Arc, Mutex}, +}; + +use arrow::ipc::{reader::StreamReader, writer::StreamWriter}; +use arrow_array::RecordBatch; +use arrow_schema::{ArrowError, Schema, SchemaRef}; +use datafusion::{ + catalog::{TableProvider, streaming::StreamingTable}, + execution::{SendableRecordBatchStream, TaskContext}, + physical_plan::{stream::RecordBatchStreamAdapter, streaming::PartitionStream}, +}; +use datafusion_common::DataFusionError; +use futures::StreamExt; +use lance_arrow::memory::MemoryAccumulator; +use lance_core::error::LanceOptionExt; +use lance_core::utils::tempfile::TempDir; + +/// Start a spill of Arrow data to a file that can be read later multiple times. +/// +/// Up to `memory_limit` bytes of data can be buffered in memory before a spill +/// is created. If the memory limit is never reached before [`SpillSender::finish()`] +/// is called, then the data will simply be kept in memory and no spill will be +/// created. +/// +/// `path` is the path to the file that may be created. It should not already +/// exist. It is the responsibility of the caller to delete the file after it is +/// no longer needed. +/// +/// The [`SpillSender`] allows you to write batches to the spill. +/// +/// The [`SpillReceiver`] can open a [`SendableRecordBatchStream`] that reads +/// batches from the spill. This can be opened before, during, or after batches +/// have been written to the spill. +/// +/// Once [`SpillSender`] is dropped, the temporary file is deleted. This will +/// cause the [`SpillReceiver`] to return an error if it is still open. +pub fn create_replay_spill( + path: std::path::PathBuf, + schema: Arc, + memory_limit: usize, +) -> (SpillSender, SpillReceiver) { + let initial_status = WriteStatus::default(); + let (status_sender, status_receiver) = tokio::sync::watch::channel(initial_status); + let sender = SpillSender { + memory_limit, + path: path.clone(), + schema: schema.clone(), + state: SpillState::default(), + status_sender, + }; + + let receiver = SpillReceiver { + status_receiver, + path, + schema, + }; + + (sender, receiver) +} + +/// Wrap a one-shot [`SendableRecordBatchStream`] in a re-scannable [`TableProvider`]. +/// +/// The source is drained in the background into a replayable spill. Two properties +/// keep this cheap for the common case: +/// +/// - **Memory-first.** Up to `memory_limit` bytes are buffered in memory; the spill +/// only touches disk once that budget is exceeded. A source that fits under the +/// limit never hits the filesystem. +/// - **Streaming replay.** A scan can start consuming batches as soon as they land, +/// before the source has finished draining — the first reader is not blocked +/// waiting for the whole source to buffer. +/// +/// Each scan of the returned provider replays the full source, which is what makes a +/// one-shot stream usable in the write retry loop. +/// +/// The provider reports no statistics — the source size is not known until it has +/// been fully drained — so callers that need source statistics (e.g. to drive join +/// ordering) should prefer a materialized or file-backed provider instead. +/// +/// # Examples +/// +/// ``` +/// # use std::sync::Arc; +/// # use arrow_array::{Int32Array, RecordBatch}; +/// # use arrow_schema::{DataType, Field, Schema}; +/// # use datafusion::execution::SendableRecordBatchStream; +/// # use datafusion::physical_plan::stream::RecordBatchStreamAdapter; +/// # use futures::TryStreamExt; +/// # use lance_datafusion::exec::provider_to_stream; +/// # use lance_datafusion::spill::spilling_table_provider; +/// # #[tokio::main] +/// # async fn main() -> Result<(), Box> { +/// let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); +/// let batch = +/// RecordBatch::try_new(schema.clone(), vec![Arc::new(Int32Array::from(vec![1, 2, 3]))])?; +/// // A one-shot stream can only be consumed once. +/// let source: SendableRecordBatchStream = Box::pin(RecordBatchStreamAdapter::new( +/// schema.clone(), +/// futures::stream::iter(vec![Ok(batch)]), +/// )); +/// +/// // Wrapping it makes it re-scannable: each scan replays the full source. +/// let provider = spilling_table_provider(source, 100 * 1024 * 1024).await?; +/// let first: Vec = provider_to_stream(provider.clone()).await?.try_collect().await?; +/// let second: Vec = provider_to_stream(provider).await?.try_collect().await?; +/// assert_eq!(first.iter().map(|b| b.num_rows()).sum::(), 3); +/// assert_eq!(second.iter().map(|b| b.num_rows()).sum::(), 3); +/// # Ok(()) +/// # } +/// ``` +pub async fn spilling_table_provider( + mut source: SendableRecordBatchStream, + memory_limit: usize, +) -> Result, DataFusionError> { + let schema = source.schema(); + let tmp_dir = tokio::task::spawn_blocking(TempDir::try_new) + .await + .map_err(|e| DataFusionError::Execution(format!("Failed to spawn temp dir task: {e}")))? + .map_err(|e| DataFusionError::Execution(format!("Failed to create temp dir: {e}")))?; + let tmp_path = tmp_dir.std_path().join("spill.arrows"); + let (mut sender, receiver) = create_replay_spill(tmp_path, schema.clone(), memory_limit); + + // Drain the one-shot source into the spill once, in the background. The spill + // tees to memory/disk so the first reader can consume batches as they arrive + // while later readers replay the complete source. + let drain_handle = tokio::task::spawn(async move { + let mut errored = false; + while let Some(res) = source.next().await { + match res { + Ok(batch) => { + if let Err(e) = sender.write(batch).await { + sender.send_error(e); + errored = true; + break; + } + } + Err(e) => { + sender.send_error(e); + errored = true; + break; + } + } + } + // Only finish on a clean drain. Calling finish() after an error would + // overwrite the original (replayable) error with a generic one, losing + // the source error's type (e.g. an external error from user code). + if !errored && let Err(err) = sender.finish().await { + sender.send_error(err); + } + sender + }); + + let partition = Arc::new(SpillPartition { + schema: schema.clone(), + receiver, + _tmp_dir: Arc::new(tmp_dir), + _drain_handle: Arc::new(drain_handle), + }); + Ok(Arc::new(StreamingTable::try_new(schema, vec![partition])?)) +} + +/// A [`PartitionStream`] backed by a replayable spill. +/// +/// Each call to [`PartitionStream::execute`] opens a fresh stream over the spill, +/// so the partition can be scanned repeatedly. The spill file and the background +/// task draining the source are kept alive for as long as this partition exists. +struct SpillPartition { + schema: SchemaRef, + receiver: SpillReceiver, + // The spilled data lives in this temp dir; dropping it deletes the spill file. + _tmp_dir: Arc, + // Keeps the background drain task (which owns the `SpillSender`) alive. The + // `SpillSender` must outlive the readers or they error out, so we hold the + // handle rather than detaching it. + _drain_handle: Arc>, +} + +impl std::fmt::Debug for SpillPartition { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SpillPartition") + .field("schema", &self.schema) + .finish() + } +} + +impl PartitionStream for SpillPartition { + fn schema(&self) -> &SchemaRef { + &self.schema + } + + fn execute(&self, _ctx: Arc) -> SendableRecordBatchStream { + self.receiver.read() + } +} + +#[derive(Clone)] +pub struct SpillReceiver { + status_receiver: tokio::sync::watch::Receiver, + path: PathBuf, + schema: Arc, +} + +impl SpillReceiver { + /// Returns a stream of batches from the spill. The stream will emit + /// batches as they are written to the spill. If the spill has already + /// been finished, the stream will emit all batches in the spill. + /// + /// The stream will not complete until [`SpillSender::finish()`] is called. + /// + /// If the spill has been dropped, an error will be returned. + pub fn read(&self) -> SendableRecordBatchStream { + let rx = self.status_receiver.clone(); + let reader = SpillReader::new(rx, self.path.clone()); + + let stream = futures::stream::try_unfold(reader, move |mut reader| async move { + match reader.read().await { + Ok(None) => Ok(None), + Ok(Some(batch)) => Ok(Some((batch, reader))), + Err(err) => Err(err), + } + }); + + Box::pin(RecordBatchStreamAdapter::new(self.schema.clone(), stream)) + } +} + +struct SpillReader { + pub batches_read: usize, + receiver: tokio::sync::watch::Receiver, + state: SpillReaderState, +} + +enum SpillReaderState { + Buffered { spill_path: PathBuf }, + Reader { reader: AsyncStreamReader }, +} + +impl SpillReader { + fn new(receiver: tokio::sync::watch::Receiver, spill_path: PathBuf) -> Self { + Self { + batches_read: 0, + receiver, + state: SpillReaderState::Buffered { spill_path }, + } + } + + async fn wait_for_more_data(&mut self) -> Result>, DataFusionError> { + let status = self + .receiver + .wait_for(|status| { + status.error.is_some() + || status.finished + || status.batches_written() > self.batches_read + }) + .await + .map_err(|_| { + DataFusionError::Execution( + "Spill has been dropped before reader has finish.".into(), + ) + })?; + + if let Some(error) = &status.error { + let mut guard = error.lock().ok().expect_ok()?; + return Err(DataFusionError::from(&mut (*guard))); + } + + if let DataLocation::Buffered { batches } = &status.data_location { + Ok(Some(batches.clone())) + } else { + Ok(None) + } + } + + async fn get_reader(&mut self) -> Result<&AsyncStreamReader, ArrowError> { + if let SpillReaderState::Buffered { spill_path } = &self.state { + let reader = AsyncStreamReader::open(spill_path.clone()).await?; + // Skip batches we've already read before the writer started spilling. + // The read batches were spilled to the file for the benefit of + // future readers, as the spill is replay-able. + for _ in 0..self.batches_read { + reader.read().await?; + } + self.state = SpillReaderState::Reader { reader }; + } + + if let SpillReaderState::Reader { reader } = &mut self.state { + Ok(reader) + } else { + unreachable!() + } + } + + async fn read(&mut self) -> Result, DataFusionError> { + let maybe_data = self.wait_for_more_data().await?; + + if let Some(batches) = maybe_data { + if self.batches_read < batches.len() { + let batch = batches[self.batches_read].clone(); + self.batches_read += 1; + Ok(Some(batch)) + } else { + Ok(None) + } + } else { + let reader = self.get_reader().await?; + let batch = reader.read().await?; + if batch.is_some() { + self.batches_read += 1; + } + Ok(batch) + } + } +} + +/// The sender side of the spill. This is used to write batches to the spill. +/// +/// Note: this must be kept alive until after the readers are done reading the +/// spill. Otherwise, they will return an error. +pub struct SpillSender { + memory_limit: usize, + schema: Arc, + path: PathBuf, + state: SpillState, + status_sender: tokio::sync::watch::Sender, +} + +enum SpillState { + Buffering { + batches: Vec, + memory_accumulator: MemoryAccumulator, + }, + Spilling { + writer: AsyncStreamWriter, + batches_written: usize, + }, + Finished { + batches: Option>, + batches_written: usize, + }, + Errored { + error: Arc>, + }, +} + +impl Default for SpillState { + fn default() -> Self { + Self::Buffering { + batches: Vec::new(), + memory_accumulator: MemoryAccumulator::default(), + } + } +} + +#[derive(Clone, Debug, Default)] +struct WriteStatus { + error: Option>>, + finished: bool, + data_location: DataLocation, +} + +impl WriteStatus { + fn batches_written(&self) -> usize { + match &self.data_location { + DataLocation::Buffered { batches } => batches.len(), + DataLocation::Spilled { + batches_written, .. + } => *batches_written, + } + } +} + +#[derive(Clone, Debug)] +enum DataLocation { + Buffered { batches: Arc<[RecordBatch]> }, + Spilled { batches_written: usize }, +} + +impl Default for DataLocation { + fn default() -> Self { + Self::Buffered { + batches: Arc::new([]), + } + } +} + +/// A DataFusion error that be be emitted multiple times. We provide the +/// Original error first, and subsequent conversions provide a copy with a +/// string representation of the original error. +#[derive(Debug)] +enum SpillError { + Original(DataFusionError), + Copy(DataFusionError), +} + +impl From for SpillError { + fn from(err: DataFusionError) -> Self { + Self::Original(err) + } +} + +impl From<&mut SpillError> for DataFusionError { + fn from(err: &mut SpillError) -> Self { + match err { + SpillError::Original(inner) => { + let copy = Self::Execution(inner.to_string()); + let original = std::mem::replace(err, SpillError::Copy(copy)); + if let SpillError::Original(inner) = original { + inner + } else { + unreachable!() + } + } + SpillError::Copy(Self::Execution(message)) => Self::Execution(message.clone()), + _ => unreachable!(), + } + } +} + +impl From<&SpillState> for WriteStatus { + fn from(state: &SpillState) -> Self { + match state { + SpillState::Buffering { batches, .. } => Self { + finished: false, + data_location: DataLocation::Buffered { + batches: batches.clone().into(), + }, + error: None, + }, + SpillState::Spilling { + batches_written, .. + } => Self { + finished: false, + data_location: DataLocation::Spilled { + batches_written: *batches_written, + }, + error: None, + }, + SpillState::Finished { + batches_written, + batches, + } => { + let data_location = if let Some(batches) = batches { + DataLocation::Buffered { + batches: batches.clone(), + } + } else { + DataLocation::Spilled { + batches_written: *batches_written, + } + }; + Self { + finished: true, + data_location, + error: None, + } + } + SpillState::Errored { error } => Self { + finished: true, + data_location: DataLocation::default(), // Doesn't matter. + error: Some(error.clone()), + }, + } + } +} + +impl SpillSender { + /// Write a batch to the spill. + /// + /// If there is room in the `memory_limit` then the batch is queued. + /// If `memory_limit` is first encountered then all queued batches, and this one, + /// will be written to disk as part of this call. + /// If we are already spilling then the batch will be written to disk as part of this + /// call. + pub async fn write(&mut self, batch: RecordBatch) -> Result<(), DataFusionError> { + if let SpillState::Finished { .. } = self.state { + return Err(DataFusionError::Execution( + "Spill has already been finished".to_string(), + )); + } + + if let SpillState::Errored { .. } = &self.state { + return Err(DataFusionError::Execution( + "Spill has sent an error".to_string(), + )); + } + + let (writer, batches_written) = match &mut self.state { + SpillState::Buffering { + batches, + memory_accumulator, + } => { + memory_accumulator.record_batch(&batch); + + if memory_accumulator.total() > self.memory_limit { + let writer = + AsyncStreamWriter::open(self.path.clone(), self.schema.clone()).await?; + let batches_written = batches.len(); + for batch in batches.drain(..) { + writer.write(batch).await?; + } + self.state = SpillState::Spilling { + writer, + batches_written, + }; + if let SpillState::Spilling { + writer, + batches_written, + } = &mut self.state + { + (writer, batches_written) + } else { + unreachable!() + } + } else { + batches.push(batch); + self.status_sender + .send_replace(WriteStatus::from(&self.state)); + return Ok(()); + } + } + SpillState::Spilling { + writer, + batches_written, + } => (writer, batches_written), + _ => unreachable!(), + }; + + writer.write(batch).await?; + *batches_written += 1; + self.status_sender + .send_replace(WriteStatus::from(&self.state)); + + Ok(()) + } + + /// Send an error to the spill. This will be sent to all readers of the + /// spill. + pub fn send_error(&mut self, err: DataFusionError) { + let error = Arc::new(Mutex::new(err.into())); + self.state = SpillState::Errored { error }; + self.status_sender + .send_replace(WriteStatus::from(&self.state)); + } + + /// Complete the spill write. This will finalize the Arrow IPC stream file. + /// The file will remain available for reading until the spill is dropped. + pub async fn finish(&mut self) -> Result<(), DataFusionError> { + // We create a temporary state to get an owned copy of current state. + // Since we hold an exclusive reference to `self`, no one should be + // able to see this temporary state. + let tmp_state = SpillState::Finished { + batches_written: 0, + batches: None, + }; + match std::mem::replace(&mut self.state, tmp_state) { + SpillState::Buffering { batches, .. } => { + let batches_written = batches.len(); + self.state = SpillState::Finished { + batches_written, + batches: Some(batches.into()), + }; + self.status_sender + .send_replace(WriteStatus::from(&self.state)); + } + SpillState::Spilling { + writer, + batches_written, + } => { + writer.finish().await?; + self.state = SpillState::Finished { + batches_written, + batches: None, + }; + self.status_sender + .send_replace(WriteStatus::from(&self.state)); + } + SpillState::Finished { .. } => { + return Err(DataFusionError::Execution( + "Spill has already been finished".to_string(), + )); + } + SpillState::Errored { .. } => { + return Err(DataFusionError::Execution( + "Spill has sent an error".to_string(), + )); + } + }; + + Ok(()) + } +} + +/// An async wrapper around [`StreamWriter`]. Each call uses [`tokio::task::spawn_blocking`] +/// to spawn a blocking task to write the batch. +struct AsyncStreamWriter { + writer: Arc>>>, +} + +impl AsyncStreamWriter { + pub async fn open(path: PathBuf, schema: Arc) -> Result { + let writer = tokio::task::spawn_blocking(move || { + let file = std::fs::File::create(&path).map_err(ArrowError::from)?; + let writer = BufWriter::new(file); + StreamWriter::try_new(writer, &schema) + }) + .await + .unwrap()?; + let writer = Arc::new(Mutex::new(writer)); + Ok(Self { writer }) + } + + pub async fn write(&self, batch: RecordBatch) -> Result<(), ArrowError> { + let writer = self.writer.clone(); + tokio::task::spawn_blocking(move || { + let mut writer = writer.lock().unwrap(); + writer.write(&batch)?; + writer.flush() + }) + .await + .unwrap() + } + + pub async fn finish(self) -> Result<(), ArrowError> { + let writer = self.writer.clone(); + tokio::task::spawn_blocking(move || { + let mut writer = writer.lock().unwrap(); + writer.finish() + }) + .await + .unwrap() + } +} + +struct AsyncStreamReader { + reader: Arc>>>, +} + +impl AsyncStreamReader { + pub async fn open(path: PathBuf) -> Result { + let reader = tokio::task::spawn_blocking(move || { + let file = std::fs::File::open(&path).map_err(ArrowError::from)?; + let reader = BufReader::new(file); + StreamReader::try_new(reader, None) + }) + .await + .unwrap()?; + let reader = Arc::new(Mutex::new(reader)); + Ok(Self { reader }) + } + + pub async fn read(&self) -> Result, ArrowError> { + let reader = self.reader.clone(); + tokio::task::spawn_blocking(move || { + let mut reader = reader.lock().unwrap(); + reader.next() + }) + .await + .unwrap() + .transpose() + } +} + +#[cfg(test)] +mod tests { + use arrow_array::Int32Array; + use arrow_schema::{DataType, Field}; + use futures::{StreamExt, TryStreamExt, poll}; + use lance_core::utils::tempfile::{TempStdFile, TempStdPath}; + + use super::*; + + #[tokio::test] + async fn test_spill() { + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + let batches = [ + RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], + ) + .unwrap(), + RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![4, 5, 6]))], + ) + .unwrap(), + ]; + + // Create a stream + let path = TempStdFile::default(); + let (mut spill, receiver) = create_replay_spill(path.to_owned(), schema.clone(), 0); + + // We can open a reader prior to writing any data. No batches will be ready. + let mut stream_before = receiver.read(); + let mut stream_before_next = stream_before.next(); + let poll_res = poll!(&mut stream_before_next); + assert!(poll_res.is_pending()); + + // If we write a batch, the existing reader can now receive it. + spill.write(batches[0].clone()).await.unwrap(); + let stream_before_batch1 = stream_before_next + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_before_batch1, &batches[0]); + let mut stream_before_next = stream_before.next(); + let poll_res = poll!(&mut stream_before_next); + assert!(poll_res.is_pending()); + + // We can also open a ready while the spill is being written to. We can + // retrieve batches written so far immediately. + let mut stream_during = receiver.read(); + let stream_during_batch1 = stream_during + .next() + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_during_batch1, &batches[0]); + let mut stream_during_next = stream_during.next(); + let poll_res = poll!(&mut stream_during_next); + assert!(poll_res.is_pending()); + + // Once we finish the spill, readers can get remaining batches and will + // reach the end of the stream. + spill.write(batches[1].clone()).await.unwrap(); + spill.finish().await.unwrap(); + + let stream_before_batch2 = stream_before_next + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_before_batch2, &batches[1]); + assert!(stream_before.next().await.is_none()); + + let stream_during_batch2 = stream_during_next + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_during_batch2, &batches[1]); + assert!(stream_during.next().await.is_none()); + + // Can also start a reader after finishing. + let stream_after = receiver.read(); + let stream_after_batches = stream_after.try_collect::>().await.unwrap(); + assert_eq!(&stream_after_batches, &batches); + + std::fs::remove_file(path).unwrap(); + } + + #[tokio::test] + async fn test_spill_error() { + // Create a spill + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + let path = TempStdFile::default(); + let (mut spill, receiver) = + create_replay_spill(path.as_ref().to_owned(), schema.clone(), 0); + let batch = RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![1, 2, 3]))], + ) + .unwrap(); + + spill.write(batch.clone()).await.unwrap(); + + let mut stream = receiver.read(); + let stream_batch = stream + .next() + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_batch, &batch); + + spill.send_error(DataFusionError::ResourcesExhausted("🥱".into())); + let stream_error = stream + .next() + .await + .expect("Expected an error") + .expect_err("Expected an error"); + assert!(matches!( + stream_error, + DataFusionError::ResourcesExhausted(message) if message == "🥱" + )); + + // If we try to write after sending an error, it should return an error. + let err = spill.write(batch).await; + assert!(matches!( + err, + Err(DataFusionError::Execution(message)) if message == "Spill has sent an error" + )); + + // If we try to finish after sending an error, it should return an error. + let err = spill.finish().await; + assert!(matches!( + err, + Err(DataFusionError::Execution(message)) if message == "Spill has sent an error" + )); + + // If we try to read after sending an error, it should return an error. + let mut stream = receiver.read(); + let stream_error = stream + .next() + .await + .expect("Expected an error") + .expect_err("Expected an error"); + assert!(matches!( + stream_error, + DataFusionError::Execution(message) if message.contains("🥱") + )); + + std::fs::remove_file(path).unwrap(); + } + + #[tokio::test] + async fn test_spill_buffered() { + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + let path = TempStdPath::default(); + let memory_limit = 1024 * 1024; // 1 MiB + let (mut spill, receiver) = create_replay_spill(path.clone(), schema.clone(), memory_limit); + + // 0.5 MB batch + let batch = RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![1; (512 * 1024) / 4]))], + ) + .unwrap(); + spill.write(batch.clone()).await.unwrap(); + assert!(!std::fs::exists(&path).unwrap()); + + spill.finish().await.unwrap(); + assert!(!std::fs::exists(&path).unwrap()); + + let mut stream = receiver.read(); + let stream_batch = stream + .next() + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_batch, &batch); + + assert!(!std::fs::exists(&path).unwrap()); + } + + #[tokio::test] + async fn test_spill_buffered_transition() { + // Starts as buffered, then spills, then finished. + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + let path = TempStdPath::default(); + let memory_limit = 1024 * 1024; // 1 MiB + let (mut spill, receiver) = create_replay_spill(path.clone(), schema.clone(), memory_limit); + + // 0.7 MB batch + let batch = RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![1; (768 * 1024) / 4]))], + ) + .unwrap(); + spill.write(batch.clone()).await.unwrap(); + assert!(!std::fs::exists(&path).unwrap()); + + let mut stream = receiver.read(); + let stream_batch = stream + .next() + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_batch, &batch); + assert!(!std::fs::exists(&path).unwrap()); + + // 0.5 MB batch + let batch = RecordBatch::try_new( + schema.clone(), + vec![Arc::new(Int32Array::from(vec![1; (512 * 1024) / 4]))], + ) + .unwrap(); + spill.write(batch.clone()).await.unwrap(); + assert!(std::fs::exists(&path).unwrap()); + + let stream_batch = stream + .next() + .await + .expect("Expected a batch") + .expect("Expected no error"); + assert_eq!(&stream_batch, &batch); + assert!(std::fs::exists(&path).unwrap()); + + spill.finish().await.unwrap(); + + assert!(stream.next().await.is_none()); + + std::fs::remove_file(path).unwrap(); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/sql.rs b/lance-artifact/rust/lance-datafusion/src/sql.rs new file mode 100644 index 000000000..67ce2ea24 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/sql.rs @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! SQL Parser utility + +use std::any::TypeId; + +use datafusion::sql::sqlparser::{ + ast::{Expr, SelectItem, SetExpr, Statement}, + dialect::{Dialect, GenericDialect}, + parser::Parser, + tokenizer::{Token, Tokenizer}, +}; + +use lance_core::{Error, Result}; +#[derive(Debug, Default)] +struct LanceDialect(GenericDialect); + +impl LanceDialect { + fn new() -> Self { + Self(GenericDialect {}) + } +} + +impl Dialect for LanceDialect { + fn dialect(&self) -> TypeId { + self.0.dialect() + } + + fn is_identifier_start(&self, ch: char) -> bool { + self.0.is_identifier_start(ch) + } + + fn is_identifier_part(&self, ch: char) -> bool { + self.0.is_identifier_part(ch) + } + + fn is_delimited_identifier_start(&self, ch: char) -> bool { + ch == '`' + } +} + +/// Parse sql filter to Expression. +pub(crate) fn parse_sql_filter(filter: &str) -> Result { + let sql = format!("SELECT 1 FROM t WHERE {filter}"); + let statement = parse_statement(&sql)?; + + if let Statement::Query(query) = statement + && let SetExpr::Select(select) = *query.body + && let Some(expr) = select.selection + { + Ok(expr) + } else { + Err(Error::invalid_input(format!( + "Filter is not valid: {filter}" + ))) + } +} + +/// Parse a SQL expression to Expression. This is more lenient than parse_sql_filter +/// as it can be used for projection expressions as well. +pub(crate) fn parse_sql_expr(expr: &str) -> Result { + let sql = format!("SELECT {expr} FROM t"); + let statement = parse_statement(&sql)?; + + if let Statement::Query(query) = statement + && let SetExpr::Select(select) = *query.body + && let Some(SelectItem::UnnamedExpr(expr)) = select.projection.into_iter().next() + { + Ok(expr) + } else { + Err(Error::invalid_input(format!( + "Expression is not valid: {expr}" + ))) + } +} + +fn parse_statement(statement: &str) -> Result { + let dialect = LanceDialect::new(); + + // Hack to allow == as equals + // This is used to parse PyArrow expressions from strings. + // See: https://github.com/sqlparser-rs/sqlparser-rs/pull/815#issuecomment-1450714278 + let mut tokenizer = Tokenizer::new(&dialect, statement); + let mut tokens = Vec::new(); + let mut token_iter = tokenizer + .tokenize() + .map_err(|e| { + Error::invalid_input(format!("Error tokenizing statement: {statement} ({e})")) + })? + .into_iter(); + let mut prev_token = token_iter.next().unwrap(); + for next_token in token_iter { + if let (Token::Eq, Token::Eq) = (&prev_token, &next_token) { + continue; // skip second equals + } + let token = std::mem::replace(&mut prev_token, next_token); + tokens.push(token); + } + tokens.push(prev_token); + + Parser::new(&dialect) + .with_tokens(tokens) + .parse_statement() + .map_err(|e| Error::invalid_input(format!("Error parsing statement: {statement} ({e})"))) +} + +#[cfg(test)] +mod tests { + use super::*; + + use datafusion::sql::sqlparser::{ + ast::{BinaryOperator, Ident, Value, ValueWithSpan}, + tokenizer::Span, + }; + + #[test] + fn test_double_equal() { + let expr = parse_sql_filter("a == b").unwrap(); + assert_eq!( + Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::Eq, + right: Box::new(Expr::Identifier(Ident::new("b"))) + }, + expr + ); + } + + #[test] + fn test_like() { + let expr = parse_sql_filter("a LIKE 'abc%'").unwrap(); + assert_eq!( + Expr::Like { + negated: false, + expr: Box::new(Expr::Identifier(Ident::new("a"))), + pattern: Box::new(Expr::Value(ValueWithSpan { + value: Value::SingleQuotedString("abc%".to_string()), + span: Span::empty(), + })), + escape_char: None, + any: false, + }, + expr + ); + } + + #[test] + fn test_quoted_ident() { + // CUBE is a SQL keyword, so it must be quoted. + let expr = parse_sql_filter("`a:Test_Something` == `CUBE`").unwrap(); + assert_eq!( + Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::with_quote('`', "a:Test_Something"))), + op: BinaryOperator::Eq, + right: Box::new(Expr::Identifier(Ident::with_quote('`', "CUBE"))) + }, + expr + ); + + let expr = parse_sql_filter("`outer field`.`inner field` == 1").unwrap(); + assert_eq!( + Expr::BinaryOp { + left: Box::new(Expr::CompoundIdentifier(vec![ + Ident::with_quote('`', "outer field"), + Ident::with_quote('`', "inner field") + ])), + op: BinaryOperator::Eq, + right: Box::new(Expr::Value(ValueWithSpan { + value: Value::Number("1".to_string(), false), + span: Span::empty(), + })), + }, + expr + ); + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/substrait.rs b/lance-artifact/rust/lance-datafusion/src/substrait.rs new file mode 100644 index 000000000..f38e6b79c --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/substrait.rs @@ -0,0 +1,1156 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_schema::{DataType, Schema as ArrowSchema}; +use datafusion::{execution::SessionState, logical_expr::Expr}; + +use crate::aggregate::Aggregate; +use datafusion_common::DFSchema; +use datafusion_substrait::extensions::Extensions; +use datafusion_substrait::logical_plan::consumer::{ + DefaultSubstraitConsumer, from_substrait_agg_func, from_substrait_rex, from_substrait_sorts, +}; +use datafusion_substrait::substrait::proto::{ + AggregateRel, Expression, ExpressionReference, ExtendedExpression, NamedStruct, Plan, Type, + expression::{ + RexType, + field_reference::{ReferenceType, RootType}, + reference_segment, + }, + expression_reference::ExprType, + function_argument::ArgType, + rel::RelType, + r#type::{Kind, Struct}, +}; +use lance_core::{Error, Result}; +use prost::Message; +use std::collections::HashMap; +use std::sync::Arc; + +/// FixedSizeList has no Substrait producer support in datafusion-substrait. +/// Other unsupported types (Null, Float16) are encoded as UserDefined and +/// handled by `remove_extension_types` on the decode side. +fn is_substrait_compatible(data_type: &DataType) -> bool { + match data_type { + DataType::FixedSizeList(_, _) => false, + DataType::List(inner) => is_substrait_compatible(inner.data_type()), + DataType::Struct(fields) => fields + .iter() + .all(|f| is_substrait_compatible(f.data_type())), + _ => true, + } +} + +/// Removes top-level fields that contain data types that the Substrait +/// producer cannot encode (currently only FixedSizeList). +pub fn prune_schema_for_substrait(schema: &ArrowSchema) -> ArrowSchema { + ArrowSchema::new( + schema + .fields() + .iter() + .filter(|f| is_substrait_compatible(f.data_type())) + .cloned() + .collect::>(), + ) +} + +/// Convert a DF Expr into a Substrait ExtendedExpressions message +/// +/// The schema needs to contain all of the fields that are referenced in the expression. +/// It is ok if the schema has more fields than are required. However, we cannot currently +/// convert all field types (e.g. extension types, FSL) and if these fields are present then +/// the conversion will fail. +/// +/// As a result, it may be a good idea for now to remove those types from the schema before +/// calling this function. +pub fn encode_substrait( + expr: Expr, + schema: Arc, + state: &SessionState, +) -> Result> { + use arrow_schema::Field; + use datafusion::logical_expr::ExprSchemable; + use datafusion_common::DFSchema; + + let df_schema = Arc::new(DFSchema::try_from(schema)?); + let output_type = expr.get_type(&df_schema)?; + // Nullability doesn't matter + let output_field = Field::new("output", output_type, /*nullable=*/ true); + let extended_expr = datafusion_substrait::logical_plan::producer::to_substrait_extended_expr( + &[(&expr, &output_field)], + &df_schema, + state, + )?; + + Ok(extended_expr.encode_to_vec()) +} + +fn count_fields(dtype: &Type) -> usize { + match dtype.kind.as_ref().unwrap() { + Kind::Struct(struct_type) => struct_type.types.iter().map(count_fields).sum::() + 1, + Kind::List(list_type) => { + // Recursively count fields in the list's child type + // This is critical for schemas with List patterns + count_fields(list_type.r#type.as_ref().unwrap()) + } + _ => 1, + } +} + +fn remove_extension_types( + substrait_schema: &NamedStruct, + arrow_schema: Arc, +) -> Result<(NamedStruct, Arc, HashMap)> { + let fields = substrait_schema.r#struct.as_ref().unwrap(); + if fields.types.len() != arrow_schema.fields.len() { + return Err(Error::invalid_input_source("the number of fields in the provided substrait schema did not match the number of fields in the input schema.".into())); + } + let mut kept_substrait_fields = Vec::with_capacity(fields.types.len()); + let mut kept_arrow_fields = Vec::with_capacity(arrow_schema.fields.len()); + let mut index_mapping = HashMap::with_capacity(arrow_schema.fields.len()); + let mut field_counter = 0; + let mut field_index = 0; + // TODO: this logic doesn't catch user defined fields inside of struct fields + for (substrait_field, arrow_field) in fields.types.iter().zip(arrow_schema.fields.iter()) { + let num_fields = count_fields(substrait_field); + + let kind = substrait_field.kind.as_ref().unwrap(); + let is_user_defined = match kind { + Kind::UserDefined(_) => true, + // Keep compatibility with older Substrait plans. + #[allow(deprecated)] + Kind::UserDefinedTypeReference(_) => true, + _ => false, + }; + + if !substrait_schema.names[field_index].starts_with("__unlikely_name_placeholder") + && !is_user_defined + { + kept_substrait_fields.push(substrait_field.clone()); + kept_arrow_fields.push(arrow_field.clone()); + for i in 0..num_fields { + index_mapping.insert(field_index + i, field_counter + i); + } + field_counter += num_fields; + } + field_index += num_fields; + } + let mut names = vec![String::new(); index_mapping.len()]; + for (old_idx, old_name) in substrait_schema.names.iter().enumerate() { + if let Some(new_idx) = index_mapping.get(&old_idx) { + names[*new_idx] = old_name.clone(); + } + } + let new_arrow_schema = Arc::new(ArrowSchema::new(kept_arrow_fields)); + let new_substrait_schema = NamedStruct { + names, + r#struct: Some(Struct { + nullability: fields.nullability, + type_variation_reference: fields.type_variation_reference, + types: kept_substrait_fields, + }), + }; + Ok((new_substrait_schema, new_arrow_schema, index_mapping)) +} + +fn remap_expr_references(expr: &mut Expression, mapping: &HashMap) -> Result<()> { + match expr.rex_type.as_mut().unwrap() { + // Simple, no field references possible + RexType::Literal(_) | RexType::Nested(_) | RexType::DynamicParameter(_) => Ok(()), + // Enum literals are deprecated in Substrait and should only appear in older plans. + #[allow(deprecated)] + RexType::Enum(_) => Ok(()), + // Complex operators not supported in filters + RexType::WindowFunction(_) | RexType::Subquery(_) => Err(Error::invalid_input( + "Window functions or subqueries not allowed in filter expression", + )), + RexType::Lambda(_) | RexType::LambdaInvocation(_) => Err(Error::invalid_input( + "Lambda expressions not allowed in filter expression", + )), + // Pass through operators, nested children may have field references + RexType::ScalarFunction(func) => { + #[allow(deprecated)] + for arg in &mut func.args { + remap_expr_references(arg, mapping)?; + } + for arg in &mut func.arguments { + match arg.arg_type.as_mut().unwrap() { + ArgType::Value(expr) => remap_expr_references(expr, mapping)?, + ArgType::Enum(_) | ArgType::Type(_) => {} + } + } + Ok(()) + } + RexType::IfThen(ifthen) => { + for clause in ifthen.ifs.iter_mut() { + remap_expr_references(clause.r#if.as_mut().unwrap(), mapping)?; + remap_expr_references(clause.then.as_mut().unwrap(), mapping)?; + } + remap_expr_references(ifthen.r#else.as_mut().unwrap(), mapping)?; + Ok(()) + } + RexType::SwitchExpression(switch) => { + for clause in switch.ifs.iter_mut() { + remap_expr_references(clause.then.as_mut().unwrap(), mapping)?; + } + remap_expr_references(switch.r#else.as_mut().unwrap(), mapping)?; + Ok(()) + } + RexType::SingularOrList(orlist) => { + for opt in orlist.options.iter_mut() { + remap_expr_references(opt, mapping)?; + } + remap_expr_references(orlist.value.as_mut().unwrap(), mapping)?; + Ok(()) + } + RexType::MultiOrList(orlist) => { + for opt in orlist.options.iter_mut() { + for field in opt.fields.iter_mut() { + remap_expr_references(field, mapping)?; + } + } + for val in orlist.value.iter_mut() { + remap_expr_references(val, mapping)?; + } + Ok(()) + } + RexType::Cast(cast) => { + remap_expr_references(cast.input.as_mut().unwrap(), mapping)?; + Ok(()) + } + RexType::Selection(sel) => { + // Finally, the selection, which might actually have field references + let root_type = sel.root_type.as_mut().unwrap(); + // These types of references do not reference input fields so no remap needed + if matches!( + root_type, + RootType::Expression(_) | RootType::OuterReference(_) + ) { + return Ok(()); + } + match sel.reference_type.as_mut().unwrap() { + ReferenceType::DirectReference(direct) => { + match direct.reference_type.as_mut().unwrap() { + reference_segment::ReferenceType::ListElement(_) + | reference_segment::ReferenceType::MapKey(_) => Err(Error::invalid_input( + "map/list nested references not supported in pushdown filters", + )), + reference_segment::ReferenceType::StructField(field) => { + if field.child.is_some() { + Err(Error::invalid_input( + "nested references in pushdown filters not yet supported", + )) + } else { + if let Some(new_index) = mapping.get(&(field.field as usize)) { + field.field = *new_index as i32; + } else { + return Err(Error::invalid_input( + "pushdown filter referenced a field that is not yet supported by Substrait conversion", + )); + } + Ok(()) + } + } + } + } + ReferenceType::MaskedReference(_) => Err(Error::invalid_input( + "masked references not yet supported in filter expressions", + )), + } + } + } +} + +/// Convert a Substrait ExtendedExpressions message into a DF Expr +/// +/// The ExtendedExpressions message must contain a single scalar expression +pub async fn parse_substrait( + expr: &[u8], + input_schema: Arc, + state: &SessionState, +) -> Result { + let envelope = ExtendedExpression::decode(expr)?; + if envelope.referred_expr.is_empty() { + return Err(Error::invalid_input_source( + "the provided substrait expression is empty (contains no expressions)".into(), + )); + } + if envelope.referred_expr.len() > 1 { + return Err(Error::invalid_input_source( + format!( + "the provided substrait expression had {} expressions when only 1 was expected", + envelope.referred_expr.len() + ) + .into(), + )); + } + let mut expr = match &envelope.referred_expr[0].expr_type { + None => Err(Error::invalid_input_source( + "the provided substrait had an expression but was missing an expr_type".into(), + )), + Some(ExprType::Expression(expr)) => Ok(expr.clone()), + _ => Err(Error::invalid_input_source( + "the provided substrait was not a scalar expression".into(), + )), + }?; + + // The Substrait may have come from a producer that uses extension types that DF doesn't support (e.g. + // from pyarrow) so we need to remove them and remap expr references (since they are indexes into the + // schema and we may have removed some fields) + let substrait_schema = if envelope.base_schema.as_ref().unwrap().r#struct.is_some() { + let (substrait_schema, _, index_mapping) = + remove_extension_types(envelope.base_schema.as_ref().unwrap(), input_schema.clone())?; + + if substrait_schema.r#struct.as_ref().unwrap().types.len() + != envelope + .base_schema + .as_ref() + .unwrap() + .r#struct + .as_ref() + .unwrap() + .types + .len() + { + remap_expr_references(&mut expr, &index_mapping)?; + } + + substrait_schema + } else { + envelope.base_schema.as_ref().unwrap().clone() + }; + + let extended_expr = ExtendedExpression { + base_schema: Some(substrait_schema), + referred_expr: vec![ExpressionReference { + output_names: envelope.referred_expr[0].output_names.clone(), + expr_type: Some(ExprType::Expression(expr)), + }], + ..envelope + }; + + let mut expr_container = + datafusion_substrait::logical_plan::consumer::from_substrait_extended_expr( + state, + &extended_expr, + ) + .await?; + + if expr_container.exprs.is_empty() { + return Err(Error::invalid_input( + "Substrait expression did not contain any expressions", + )); + } + + if expr_container.exprs.len() > 1 { + return Err(Error::invalid_input( + "Substrait expression contained multiple expressions", + )); + } + + Ok(expr_container.exprs.pop().unwrap().0) +} + +/// Parse Substrait Plan bytes containing an AggregateRel. +pub async fn parse_substrait_aggregate( + bytes: &[u8], + input_schema: Arc, + state: &SessionState, +) -> Result { + let plan = Plan::decode(bytes)?; + let (aggregate_rel, output_names) = extract_aggregate_from_plan(&plan)?; + let extensions = Extensions::try_from(&plan.extensions)?; + + let mut agg = + parse_aggregate_rel_with_extensions(&aggregate_rel, input_schema, state, &extensions) + .await?; + + // Apply aliases from RelRoot.names to expressions + if !output_names.is_empty() { + let num_groups = agg.group_by.len(); + for (i, expr) in agg.group_by.iter_mut().enumerate() { + if i < output_names.len() { + *expr = expr.clone().alias(&output_names[i]); + } + } + for (i, expr) in agg.aggregates.iter_mut().enumerate() { + let name_idx = num_groups + i; + if name_idx < output_names.len() { + *expr = expr.clone().alias(&output_names[name_idx]); + } + } + } + + Ok(agg) +} + +fn extract_aggregate_from_plan(plan: &Plan) -> Result<(Box, Vec)> { + if plan.relations.is_empty() { + return Err(Error::invalid_input("Substrait Plan has no relations")); + } + + let plan_rel = &plan.relations[0]; + let (rel, output_names) = match &plan_rel.rel_type { + Some(datafusion_substrait::substrait::proto::plan_rel::RelType::Root(root)) => { + (root.input.as_ref(), root.names.clone()) + } + Some(datafusion_substrait::substrait::proto::plan_rel::RelType::Rel(rel)) => { + (Some(rel), vec![]) + } + None => (None, vec![]), + }; + + let rel = rel.ok_or_else(|| Error::invalid_input("Plan relation has no input"))?; + + match &rel.rel_type { + Some(RelType::Aggregate(agg)) => Ok((agg.clone(), output_names)), + Some(other) => Err(Error::invalid_input(format!( + "Expected Substrait AggregateRel, got {:?}", + std::mem::discriminant(other) + ))), + None => Err(Error::invalid_input("Substrait Rel has no rel_type")), + } +} + +/// Parse an AggregateRel proto with provided extensions. +pub async fn parse_aggregate_rel_with_extensions( + aggregate_rel: &AggregateRel, + input_schema: Arc, + state: &SessionState, + extensions: &Extensions, +) -> Result { + let df_schema = DFSchema::try_from(input_schema.as_ref().clone())?; + let consumer = DefaultSubstraitConsumer::new(extensions, state); + let group_by = parse_groupings(aggregate_rel, &df_schema, &consumer).await?; + let aggregates = parse_measures(aggregate_rel, &df_schema, &consumer).await?; + + Ok(Aggregate::new(group_by, aggregates)) +} + +/// Parse an AggregateRel proto with default extensions. +pub async fn parse_aggregate_rel( + aggregate_rel: &AggregateRel, + input_schema: Arc, + state: &SessionState, +) -> Result { + let extensions = Extensions::default(); + parse_aggregate_rel_with_extensions(aggregate_rel, input_schema, state, &extensions).await +} + +async fn parse_groupings( + agg_rel: &AggregateRel, + schema: &DFSchema, + consumer: &DefaultSubstraitConsumer<'_>, +) -> Result> { + let mut group_exprs = Vec::new(); + + // First, handle the new-style grouping_expressions + expression_references + if !agg_rel.grouping_expressions.is_empty() { + for grouping in &agg_rel.groupings { + for expr_ref in &grouping.expression_references { + let idx = *expr_ref as usize; + if idx >= agg_rel.grouping_expressions.len() { + return Err(Error::invalid_input(format!( + "Grouping expression reference {} out of bounds (max: {})", + idx, + agg_rel.grouping_expressions.len() + ))); + } + let expr = &agg_rel.grouping_expressions[idx]; + let df_expr = from_substrait_rex(consumer, expr, schema) + .await + .map_err(|e| { + Error::invalid_input(format!("Failed to parse grouping expression: {}", e)) + })?; + group_exprs.push(df_expr); + } + } + } else { + // Fallback to deprecated inline grouping_expressions within each Grouping + #[allow(deprecated)] + for grouping in &agg_rel.groupings { + for expr in &grouping.grouping_expressions { + let df_expr = from_substrait_rex(consumer, expr, schema) + .await + .map_err(|e| { + Error::invalid_input(format!("Failed to parse grouping expression: {}", e)) + })?; + group_exprs.push(df_expr); + } + } + } + + Ok(group_exprs) +} + +async fn parse_measures( + agg_rel: &AggregateRel, + schema: &DFSchema, + consumer: &DefaultSubstraitConsumer<'_>, +) -> Result> { + let mut aggregates = Vec::new(); + + for measure in &agg_rel.measures { + if let Some(agg_func) = &measure.measure { + // Parse optional filter + let filter = if let Some(filter_expr) = &measure.filter { + let df_filter = from_substrait_rex(consumer, filter_expr, schema) + .await + .map_err(|e| { + Error::invalid_input(format!("Failed to parse measure filter: {}", e)) + })?; + Some(Box::new(df_filter)) + } else { + None + }; + + // Parse ordering (for ordered aggregates like ARRAY_AGG) + let order_by = from_substrait_sorts(consumer, &agg_func.sorts, schema) + .await + .map_err(|e| { + Error::invalid_input(format!("Failed to parse aggregate sorts: {}", e)) + })?; + + // Check for DISTINCT invocation + let distinct = matches!( + agg_func.invocation, + i if i == datafusion_substrait::substrait::proto::aggregate_function::AggregationInvocation::Distinct as i32 + ); + + // Convert Substrait AggregateFunction to DataFusion Expr + let df_expr = + from_substrait_agg_func(consumer, agg_func, schema, filter, order_by, distinct) + .await + .map_err(|e| { + Error::invalid_input(format!("Failed to parse aggregate function: {}", e)) + })?; + + aggregates.push(df_expr.as_ref().clone()); + } + } + + Ok(aggregates) +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_schema::{DataType, Field, Schema}; + use datafusion::{ + execution::SessionState, + logical_expr::{BinaryExpr, Operator}, + prelude::{Expr, SessionContext}, + }; + use datafusion_common::{Column, ScalarValue}; + use datafusion_substrait::substrait::proto::{ + Expression, ExpressionReference, ExtendedExpression, FunctionArgument, NamedStruct, Type, + Version, + expression::{ + FieldReference, Literal, ReferenceSegment, RexType, ScalarFunction, + field_reference::{ReferenceType, RootReference, RootType}, + literal::LiteralType, + reference_segment::{self, StructField}, + }, + expression_reference::ExprType, + extensions::{ + SimpleExtensionDeclaration, SimpleExtensionUrn, + simple_extension_declaration::{ExtensionFunction, MappingType}, + }, + function_argument::ArgType, + r#type::{Boolean, I32, Kind, Nullability, Struct}, + }; + use prost::Message; + + use crate::substrait::{encode_substrait, parse_substrait}; + + fn session_state() -> SessionState { + let ctx = SessionContext::new(); + ctx.state() + } + + #[tokio::test] + async fn test_substrait_conversion() { + let expr = ExtendedExpression { + version: Some(Version { + major_number: 0, + minor_number: 63, + patch_number: 1, + git_hash: "".to_string(), + producer: "unit-test".to_string(), + }), + extension_urns: vec![ + SimpleExtensionUrn { + extension_urn_anchor: 1, + urn: "https://github.com/substrait-io/substrait/blob/main/extensions/functions_comparison.yaml".to_string(), + } + ], + extensions: vec![ + SimpleExtensionDeclaration { + mapping_type: Some(MappingType::ExtensionFunction(ExtensionFunction { + extension_urn_reference: 1, + function_anchor: 1, + name: "lt".to_string(), + })), + } + ], + referred_expr: vec![ExpressionReference { + output_names: vec!["filter_mask".to_string()], + expr_type: Some(ExprType::Expression(Expression { + rex_type: Some(RexType::ScalarFunction(ScalarFunction { + function_reference: 1, + arguments: vec![ + FunctionArgument { + arg_type: Some(ArgType::Value(Expression { + rex_type: Some(RexType::Selection(Box::new(FieldReference { + reference_type: Some(ReferenceType::DirectReference(ReferenceSegment { + reference_type: Some(reference_segment::ReferenceType::StructField(Box::new(StructField { field: 0, child: None }))) + })), + root_type: Some(RootType::RootReference(RootReference {})) + }))) + })) + }, + FunctionArgument { + arg_type: Some(ArgType::Value(Expression { + rex_type: Some(RexType::Literal(Literal { + nullable: false, + type_variation_reference: 0, + literal_type: Some(LiteralType::I32(0)) + })) + })) + } + ], + options: vec![], + output_type: Some(Type { + kind: Some(Kind::Bool(Boolean { + type_variation_reference: 0, + nullability: Nullability::Required as i32, + })), + }), + #[allow(deprecated)] + args: vec![], + })) + })), + }], + base_schema: Some(NamedStruct { + names: vec!["x".to_string()], + r#struct: Some(Struct { + types: vec![Type { + kind: Some(Kind::I32(I32 { + type_variation_reference: 0, + nullability: Nullability::Nullable as i32, + })), + }], + type_variation_reference: 0, + nullability: Nullability::Required as i32, + }), + }), + advanced_extensions: None, + expected_type_urls: vec![], + }; + let expr_bytes = expr.encode_to_vec(); + + let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int32, true)])); + + let df_expr = parse_substrait(expr_bytes.as_slice(), schema, &session_state()) + .await + .unwrap(); + + let expected = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column(Column::new_unqualified("x"))), + op: Operator::Lt, + right: Box::new(Expr::Literal(ScalarValue::Int32(Some(0)), None)), + }); + assert_eq!(df_expr, expected); + } + + #[tokio::test] + async fn test_expr_substrait_roundtrip() { + let schema = arrow_schema::Schema::new(vec![Field::new("x", DataType::Int32, true)]); + let expr = Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column(Column::new_unqualified("x"))), + op: Operator::Lt, + right: Box::new(Expr::Literal(ScalarValue::Int32(Some(0)), None)), + }); + + let bytes = + encode_substrait(expr.clone(), Arc::new(schema.clone()), &session_state()).unwrap(); + + let decoded = parse_substrait(bytes.as_slice(), Arc::new(schema.clone()), &session_state()) + .await + .unwrap(); + assert_eq!(decoded, expr); + } + + /// Helper to create a simple equality filter on the "id" field + fn id_filter(value: &str) -> Expr { + Expr::BinaryExpr(BinaryExpr { + left: Box::new(Expr::Column(Column::new_unqualified("id"))), + op: Operator::Eq, + right: Box::new(Expr::Literal( + ScalarValue::Utf8(Some(value.to_string())), + None, + )), + }) + } + + /// Helper to test substrait roundtrip encode/decode + async fn assert_substrait_roundtrip(schema: Schema, expr: Expr) { + let schema = Arc::new(schema); + let bytes = encode_substrait(expr.clone(), schema.clone(), &session_state()).unwrap(); + let decoded = parse_substrait(bytes.as_slice(), schema, &session_state()) + .await + .unwrap(); + assert_eq!(decoded, expr); + } + + /// Helper to create List field + fn list_of_struct(name: &str, fields: Vec) -> Field { + Field::new( + name, + DataType::List(Arc::new(Field::new( + "item", + DataType::Struct(fields.into()), + true, + ))), + true, + ) + } + + #[tokio::test] + async fn test_substrait_roundtrip_with_list_of_struct() { + let schema = Schema::new(vec![ + Field::new("id", DataType::Utf8, false), + list_of_struct( + "top_previous_companies", + vec![ + Field::new("company_id", DataType::Int64, true), + Field::new("company_name", DataType::Utf8, true), + ], + ), + Field::new("name", DataType::Utf8, true), + ]); + + assert_substrait_roundtrip(schema, id_filter("test-id")).await; + } + + #[tokio::test] + async fn test_substrait_roundtrip_with_list_struct_struct() { + let schema = Schema::new(vec![ + Field::new("id", DataType::Utf8, false), + list_of_struct( + "employees_count_breakdown_by_month", + vec![ + Field::new("date", DataType::Utf8, true), + Field::new( + "breakdown", + DataType::Struct( + vec![ + Field::new("employees_count_owner", DataType::Int64, true), + Field::new("employees_count_founder", DataType::Int64, true), + Field::new("employees_count_clevel", DataType::Int64, true), + ] + .into(), + ), + true, + ), + ], + ), + Field::new("name", DataType::Utf8, true), + ]); + + assert_substrait_roundtrip(schema, id_filter("test-id")).await; + } + + #[tokio::test] + async fn test_substrait_roundtrip_with_many_nested_columns() { + let schema = Schema::new(vec![ + Field::new("id", DataType::Utf8, false), + Field::new( + "location", + DataType::Struct( + vec![ + Field::new("city", DataType::Utf8, true), + Field::new("country", DataType::Utf8, true), + ] + .into(), + ), + true, + ), + list_of_struct( + "top_previous_companies", + vec![ + Field::new("company_id", DataType::Int64, true), + Field::new("company_name", DataType::Utf8, true), + ], + ), + list_of_struct( + "employees_by_month", + vec![ + Field::new("date", DataType::Utf8, true), + Field::new( + "breakdown", + DataType::Struct( + vec![ + Field::new("count_owner", DataType::Int64, true), + Field::new("count_founder", DataType::Int64, true), + ] + .into(), + ), + true, + ), + ], + ), + Field::new("name", DataType::Utf8, true), + ]); + + assert_substrait_roundtrip(schema, id_filter("test-id")).await; + } + + #[tokio::test] + async fn test_substrait_roundtrip_with_null_and_float16_columns() { + // Float16 and Null are encoded as UserDefined types in Substrait. + // The decode side (remove_extension_types) strips them and remaps + // field references, so filters on other columns still work. + let schema = Schema::new(vec![ + Field::new("id", DataType::Utf8, false), + Field::new("embedding", DataType::Float16, true), + Field::new("empty", DataType::Null, true), + Field::new("name", DataType::Utf8, true), + ]); + + assert_substrait_roundtrip(schema, id_filter("test-id")).await; + } + + #[tokio::test] + async fn test_substrait_roundtrip_with_fixed_size_list_column() { + // FixedSizeList has no Substrait producer support, so it must be + // pruned from the schema before encoding. Verify that a schema with + // FSL columns works when the filter references a different column. + use crate::substrait::prune_schema_for_substrait; + + let schema = Schema::new(vec![ + Field::new("id", DataType::Utf8, false), + Field::new( + "vector", + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float32, true)), 128), + true, + ), + Field::new("name", DataType::Utf8, true), + ]); + + // Encoding with the full schema would fail, but pruning removes the FSL column + let pruned = prune_schema_for_substrait(&schema); + assert_eq!(pruned.fields().len(), 2); // id and name only + assert_substrait_roundtrip(pruned, id_filter("test-id")).await; + } + + // ==================== Aggregate parsing tests ==================== + + use datafusion_substrait::substrait::proto::{ + AggregateFunction, AggregateRel, Plan, PlanRel, Rel, RelRoot, + aggregate_function::AggregationInvocation, + aggregate_rel::{Grouping, Measure}, + rel::RelType, + }; + + /// Helper to create a field reference expression for a column index + fn agg_field_ref(field_index: i32) -> Expression { + Expression { + rex_type: Some(RexType::Selection(Box::new(FieldReference { + reference_type: Some(ReferenceType::DirectReference(ReferenceSegment { + reference_type: Some(reference_segment::ReferenceType::StructField(Box::new( + StructField { + field: field_index, + child: None, + }, + ))), + })), + root_type: Some(RootType::RootReference(RootReference {})), + }))), + } + } + + /// Create extension declaration for an aggregate function + fn agg_extension(anchor: u32, name: &str) -> SimpleExtensionDeclaration { + SimpleExtensionDeclaration { + mapping_type: Some(MappingType::ExtensionFunction(ExtensionFunction { + extension_urn_reference: 1, + function_anchor: anchor, + name: name.to_string(), + })), + } + } + + /// Helper to create a Substrait Plan with AggregateRel + fn create_aggregate_plan( + measures: Vec, + grouping_expressions: Vec, + groupings: Vec, + extensions: Vec, + ) -> Vec { + let aggregate_rel = AggregateRel { + common: None, + input: None, // Input is ignored for pushdown + groupings, + measures, + grouping_expressions, + advanced_extension: None, + }; + + let rel = Rel { + rel_type: Some(RelType::Aggregate(Box::new(aggregate_rel))), + }; + + // Wrap in a Plan to include extensions + let plan = Plan { + version: Some(Version { + major_number: 0, + minor_number: 63, + patch_number: 0, + git_hash: String::new(), + producer: "lance-test".to_string(), + }), + extension_urns: vec![SimpleExtensionUrn { + extension_urn_anchor: 1, + urn: "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml".to_string(), + }], + extensions, + relations: vec![PlanRel { + rel_type: Some( + datafusion_substrait::substrait::proto::plan_rel::RelType::Root(RelRoot { + input: Some(rel), + names: vec![], + }), + ), + }], + advanced_extensions: None, + expected_type_urls: vec![], + parameter_bindings: vec![], + type_aliases: vec![], + }; + + plan.encode_to_vec() + } + + /// Create a COUNT(*) measure + fn count_star_measure(function_ref: u32) -> Measure { + Measure { + measure: Some(AggregateFunction { + function_reference: function_ref, + arguments: vec![], + options: vec![], + output_type: None, + phase: 0, + sorts: vec![], + invocation: AggregationInvocation::All as i32, + #[allow(deprecated)] + args: vec![], + }), + filter: None, + } + } + + /// Create a SUM/AVG/MIN/MAX measure on a column + fn simple_agg_measure(function_ref: u32, column_index: i32) -> Measure { + Measure { + measure: Some(AggregateFunction { + function_reference: function_ref, + arguments: vec![FunctionArgument { + arg_type: Some(ArgType::Value(agg_field_ref(column_index))), + }], + options: vec![], + output_type: None, + phase: 0, + sorts: vec![], + invocation: AggregationInvocation::All as i32, + #[allow(deprecated)] + args: vec![], + }), + filter: None, + } + } + + #[tokio::test] + async fn test_parse_substrait_aggregate_count_star() { + let bytes = create_aggregate_plan( + vec![count_star_measure(0)], + vec![], + vec![], + vec![agg_extension(0, "count")], + ); + + let schema = Arc::new(Schema::new(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int64, true), + ])); + + let result = + crate::substrait::parse_substrait_aggregate(&bytes, schema, &session_state()).await; + + let agg = result.expect("Failed to parse COUNT(*) aggregate"); + assert!(agg.group_by.is_empty(), "COUNT(*) should have no group by"); + assert_eq!(agg.aggregates.len(), 1, "Should have exactly one aggregate"); + + // Verify it's a COUNT aggregate + let agg_expr = &agg.aggregates[0]; + assert!( + agg_expr.schema_name().to_string().contains("count"), + "Expected COUNT aggregate, got: {}", + agg_expr.schema_name() + ); + } + + #[tokio::test] + async fn test_parse_substrait_aggregate_sum() { + let bytes = create_aggregate_plan( + vec![simple_agg_measure(0, 1)], // SUM on column index 1 (y) + vec![], + vec![], + vec![agg_extension(0, "sum")], + ); + + let schema = Arc::new(Schema::new(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int64, true), + ])); + + let result = + crate::substrait::parse_substrait_aggregate(&bytes, schema, &session_state()).await; + + let agg = result.expect("Failed to parse SUM aggregate"); + assert!(agg.group_by.is_empty(), "SUM should have no group by"); + assert_eq!(agg.aggregates.len(), 1, "Should have exactly one aggregate"); + + // Verify it's a SUM aggregate + let agg_expr = &agg.aggregates[0]; + assert!( + agg_expr.schema_name().to_string().contains("sum"), + "Expected SUM aggregate, got: {}", + agg_expr.schema_name() + ); + } + + #[tokio::test] + async fn test_parse_substrait_aggregate_sum_with_group_by() { + // SUM(y) GROUP BY x + let bytes = create_aggregate_plan( + vec![simple_agg_measure(0, 1)], // SUM on column index 1 (y) + vec![agg_field_ref(0)], // Group by column index 0 (x) + vec![Grouping { + #[allow(deprecated)] + grouping_expressions: vec![], + expression_references: vec![0], // Reference to first grouping_expression + }], + vec![agg_extension(0, "sum")], + ); + + let schema = Arc::new(Schema::new(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int64, true), + ])); + + let result = + crate::substrait::parse_substrait_aggregate(&bytes, schema, &session_state()).await; + + let agg = result.expect("Failed to parse SUM with GROUP BY"); + assert_eq!( + agg.group_by.len(), + 1, + "Should have exactly one group by expression" + ); + assert_eq!(agg.aggregates.len(), 1, "Should have exactly one aggregate"); + + // Verify group by is column x + let group_expr = &agg.group_by[0]; + assert!( + group_expr.schema_name().to_string().contains('x'), + "Expected group by on column x, got: {}", + group_expr.schema_name() + ); + + // Verify it's a SUM aggregate + let agg_expr = &agg.aggregates[0]; + assert!( + agg_expr.schema_name().to_string().contains("sum"), + "Expected SUM aggregate, got: {}", + agg_expr.schema_name() + ); + } + + #[tokio::test] + async fn test_parse_substrait_aggregate_multiple_aggregates() { + // COUNT(*) and SUM(y) + let bytes = create_aggregate_plan( + vec![count_star_measure(0), simple_agg_measure(1, 1)], + vec![], + vec![], + vec![agg_extension(0, "count"), agg_extension(1, "sum")], + ); + + let schema = Arc::new(Schema::new(vec![ + Field::new("x", DataType::Int32, true), + Field::new("y", DataType::Int64, true), + ])); + + let result = + crate::substrait::parse_substrait_aggregate(&bytes, schema, &session_state()).await; + + let agg = result.expect("Failed to parse multiple aggregates"); + assert!(agg.group_by.is_empty(), "Should have no group by"); + assert_eq!(agg.aggregates.len(), 2, "Should have two aggregates"); + + // Verify COUNT + assert!( + agg.aggregates[0] + .schema_name() + .to_string() + .contains("count"), + "Expected COUNT aggregate, got: {}", + agg.aggregates[0].schema_name() + ); + + // Verify SUM + assert!( + agg.aggregates[1].schema_name().to_string().contains("sum"), + "Expected SUM aggregate, got: {}", + agg.aggregates[1].schema_name() + ); + } + + // ==================== LIKE and starts_with tests ==================== + + #[tokio::test] + async fn test_substrait_roundtrip_like() { + use datafusion::logical_expr::Like; + + let schema = Schema::new(vec![Field::new("name", DataType::Utf8, true)]); + + let like_expr = Expr::Like(Like { + negated: false, + expr: Box::new(Expr::Column(Column::new_unqualified("name"))), + pattern: Box::new(Expr::Literal( + ScalarValue::Utf8(Some("test%".to_string())), + None, + )), + escape_char: None, + case_insensitive: false, + }); + + assert_substrait_roundtrip(schema, like_expr).await; + } + + #[tokio::test] + async fn test_substrait_roundtrip_starts_with() { + use datafusion::functions::string::starts_with; + + let schema = Schema::new(vec![Field::new("name", DataType::Utf8, true)]); + + let starts_with_expr = starts_with().call(vec![ + Expr::Column(Column::new_unqualified("name")), + Expr::Literal(ScalarValue::Utf8(Some("prefix".to_string())), None), + ]); + + assert_substrait_roundtrip(schema, starts_with_expr).await; + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/udf.rs b/lance-artifact/rust/lance-datafusion/src/udf.rs new file mode 100644 index 000000000..fc43de4a2 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/udf.rs @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Datafusion user defined functions + +use arrow_array::{Array, ArrayRef, BooleanArray, StringArray}; +use arrow_schema::DataType; +use datafusion::logical_expr::{ScalarUDF, Volatility, create_udf}; +use datafusion::prelude::SessionContext; +use datafusion_functions::utils::make_scalar_function; +use std::sync::{Arc, LazyLock}; + +pub mod json; + +/// Register UDF functions to datafusion context. +pub fn register_functions(ctx: &SessionContext) { + ctx.register_udf(CONTAINS_TOKENS_UDF.clone()); + // JSON functions + ctx.register_udf(json::json_extract_udf()); + ctx.register_udf(json::json_extract_with_type_udf()); + ctx.register_udf(json::json_exists_udf()); + ctx.register_udf(json::json_get_udf()); + ctx.register_udf(json::json_get_string_udf()); + ctx.register_udf(json::json_get_int_udf()); + ctx.register_udf(json::json_get_float_udf()); + ctx.register_udf(json::json_get_bool_udf()); + ctx.register_udf(json::json_array_contains_udf()); + ctx.register_udf(json::json_array_length_udf()); + // GEO functions + #[cfg(feature = "geo")] + lance_geo::register_functions(ctx); + #[cfg(not(feature = "geo"))] + register_geo_stub_functions(ctx); +} + +/// When the `geo` feature is disabled, register stub UDFs for spatial SQL functions +/// so that users get a clear error mentioning the feature flag instead of +/// DataFusion's generic "Unknown function" error. +#[cfg(not(feature = "geo"))] +fn register_geo_stub_functions(ctx: &SessionContext) { + let geo_funcs = [ + "st_intersects", + "st_contains", + "st_within", + "st_touches", + "st_crosses", + "st_overlaps", + "st_covers", + "st_coveredby", + "st_distance", + "st_area", + "st_length", + ]; + + for name in geo_funcs { + let func_name = name.to_string(); + let stub = Arc::new(make_scalar_function( + move |_args: &[ArrayRef]| { + Err(datafusion::error::DataFusionError::Plan(format!( + "Function '{}' requires the `geo` feature. \ + Rebuild with `--features geo` to enable geospatial functions.", + func_name + ))) + }, + vec![], + )); + + ctx.register_udf(create_udf( + name, + vec![DataType::Binary, DataType::Binary], + DataType::Boolean, + Volatility::Immutable, + stub, + )); + } +} + +/// This method checks whether a string contains all specified tokens. The tokens are separated by +/// punctuations and white spaces. +/// +/// The functionality is equivalent to FTS MatchQuery (with fuzziness disabled, Operator::And, +/// and using the simple tokenizer). If FTS index exists and suites the query, it will be used to +/// optimize the query. +/// +/// Usage +/// * Use `contains_tokens` in sql. +/// ```rust,ignore +/// let sql = "SELECT * FROM table WHERE contains_tokens(text_col, 'fox jumps dog')"; +/// let mut ds = Dataset::open(&ds_path).await?; +/// let ctx = SessionContext::new(); +/// ctx.register_table( +/// "table", +/// Arc::new(LanceTableProvider::new(dataset, false, false)), +/// )?; +/// register_functions(&ctx); +/// let df = ctx.sql(sql).await?; +/// ``` +fn contains_tokens() -> ScalarUDF { + let function = Arc::new(make_scalar_function( + |args: &[ArrayRef]| { + let column = args[0].as_any().downcast_ref::().ok_or( + datafusion::error::DataFusionError::Execution( + "First argument of contains_tokens can't be cast to string".to_string(), + ), + )?; + let scalar_str = args[1].as_any().downcast_ref::().ok_or( + datafusion::error::DataFusionError::Execution( + "Second argument of contains_tokens can't be cast to string".to_string(), + ), + )?; + + let tokens: Option> = match scalar_str.len() { + 0 => None, + _ => Some(collect_tokens(scalar_str.value(0))), + }; + + let result = column.iter().map(|text| { + text.map(|text| { + let text_tokens = collect_tokens(text); + if let Some(tokens) = &tokens { + tokens.len() + == tokens + .iter() + .filter(|token| text_tokens.contains(*token)) + .count() + } else { + true + } + }) + }); + + Ok(Arc::new(BooleanArray::from_iter(result)) as ArrayRef) + }, + vec![], + )); + + create_udf( + "contains_tokens", + vec![DataType::Utf8, DataType::Utf8], + DataType::Boolean, + Volatility::Immutable, + function, + ) +} + +/// Split tokens separated by punctuations and white spaces. +fn collect_tokens(text: &str) -> Vec<&str> { + text.split(|c: char| !c.is_alphanumeric()) + .filter(|word| !word.is_empty()) + .collect() +} + +pub static CONTAINS_TOKENS_UDF: LazyLock = LazyLock::new(contains_tokens); + +#[cfg(test)] +mod tests { + use crate::udf::CONTAINS_TOKENS_UDF; + use arrow_array::{Array, BooleanArray, StringArray}; + use arrow_schema::{DataType, Field}; + use datafusion::logical_expr::ScalarFunctionArgs; + use datafusion::physical_plan::ColumnarValue; + use std::sync::Arc; + + #[tokio::test] + async fn test_contains_tokens() { + // Prepare arguments + let contains_tokens = CONTAINS_TOKENS_UDF.clone(); + let text_col = Arc::new(StringArray::from(vec![ + "a cat catch a fish", + "a fish catch a cat", + "a white cat catch a big fish", + "cat catchup fish", + "cat fish catch", + ])); + let token = Arc::new(StringArray::from(vec![ + " cat catch fish.", + " cat catch fish.", + " cat catch fish.", + " cat catch fish.", + " cat catch fish.", + ])); + + let args = vec![ColumnarValue::Array(text_col), ColumnarValue::Array(token)]; + let arg_fields = vec![ + Arc::new(Field::new("text_col".to_string(), DataType::Utf8, false)), + Arc::new(Field::new("token".to_string(), DataType::Utf8, false)), + ]; + + let args = ScalarFunctionArgs { + args, + arg_fields, + number_rows: 5, + return_field: Arc::new(Field::new("res".to_string(), DataType::Boolean, false)), + config_options: Arc::new(Default::default()), + }; + + // Invoke contains_tokens manually + let values = contains_tokens.invoke_with_args(args).unwrap(); + + if let ColumnarValue::Array(array) = values { + let array = array.as_any().downcast_ref::().unwrap(); + assert_eq!( + array.clone(), + BooleanArray::from(vec![true, true, true, false, true]) + ); + } else { + panic!("Expected an Array but got {:?}", values); + } + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/udf/json.rs b/lance-artifact/rust/lance-datafusion/src/udf/json.rs new file mode 100644 index 000000000..1d0109a62 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/udf/json.rs @@ -0,0 +1,1392 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_array::builder::{ + BooleanBuilder, Float64Builder, Int64Builder, LargeBinaryBuilder, StringBuilder, +}; +use arrow_array::{Array, ArrayRef, LargeBinaryArray, StringArray}; +use arrow_schema::DataType; +use datafusion::error::{DataFusionError, Result}; +use datafusion::logical_expr::{ScalarUDF, Volatility}; +use datafusion::physical_plan::ColumnarValue; +use datafusion::prelude::create_udf; +use std::sync::Arc; + +/// Represents the type of a JSONB value +#[repr(u8)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum JsonbType { + Null = 0, + Boolean = 1, + Int64 = 2, + Float64 = 3, + String = 4, + Array = 5, + Object = 6, +} + +impl JsonbType { + /// Convert from u8 value + pub fn from_u8(value: u8) -> Option { + match value { + 0 => Some(Self::Null), + 1 => Some(Self::Boolean), + 2 => Some(Self::Int64), + 3 => Some(Self::Float64), + 4 => Some(Self::String), + 5 => Some(Self::Array), + 6 => Some(Self::Object), + _ => None, + } + } + + /// Convert to u8 value for storage in Arrow arrays + pub fn as_u8(self) -> u8 { + self as u8 + } +} + +/// Common helper functions and types for JSON UDFs +mod common { + use super::*; + + /// Convert ColumnarValue arguments to ArrayRef vector + /// + /// Note: This implementation currently broadcasts scalars to arrays. + /// Future optimization: handle scalars directly without broadcasting + /// to improve performance for scalar inputs. + pub fn columnar_to_arrays(args: &[ColumnarValue]) -> Vec { + args.iter() + .map(|arg| match arg { + ColumnarValue::Array(arr) => arr.clone(), + ColumnarValue::Scalar(scalar) => scalar.to_array().unwrap(), + }) + .collect() + } + + /// Create DataFusionError for execution failures (simplified error wrapping) + pub fn execution_error(msg: impl Into) -> DataFusionError { + DataFusionError::Execution(msg.into()) + } + + /// Validate argument count for UDF + pub fn validate_arg_count( + args: &[ArrayRef], + expected: usize, + function_name: &str, + ) -> Result<()> { + if args.len() != expected { + return Err(execution_error(format!( + "{} requires exactly {} arguments", + function_name, expected + ))); + } + Ok(()) + } + + /// Extract and validate LargeBinaryArray from first argument + pub fn extract_jsonb_array(args: &[ArrayRef]) -> Result<&LargeBinaryArray> { + args[0] + .as_any() + .downcast_ref::() + .ok_or_else(|| execution_error("First argument must be LargeBinary")) + } + + /// Extract and validate StringArray from specified argument + pub fn extract_string_array(args: &[ArrayRef], arg_index: usize) -> Result<&StringArray> { + args[arg_index] + .as_any() + .downcast_ref::() + .ok_or_else(|| execution_error(format!("Argument {} must be String", arg_index + 1))) + } + + /// Get string value at index, handling scalar broadcast case + /// When a scalar is converted to an array, it becomes a single-element array + /// This function handles accessing that value repeatedly for all rows + pub fn get_string_value_at(string_array: &StringArray, index: usize) -> Option<&str> { + // Handle scalar broadcast case: if array has only 1 element, always use index 0 + let actual_index = if string_array.len() == 1 { 0 } else { index }; + + if string_array.is_null(actual_index) { + None + } else { + Some(string_array.value(actual_index)) + } + } + + /// Get a JSON field or array element by key. + pub fn get_json_value_by_key( + raw_jsonb: &jsonb::RawJsonb, + key: &str, + ) -> Result> { + if raw_jsonb.is_object().unwrap_or(false) { + raw_jsonb + .get_by_name(key, false) + .map_err(|e| execution_error(format!("Failed to get field '{}': {}", key, e))) + } else if raw_jsonb.is_array().unwrap_or(false) { + match key.parse::() { + Ok(index) => raw_jsonb.get_by_index(index).map_err(|e| { + execution_error(format!("Failed to get array element [{}]: {}", index, e)) + }), + Err(_) => Ok(None), + } + } else { + Ok(None) + } + } + + /// Parse JSONPath with proper error handling (no false returns) + pub fn parse_json_path(path: &str) -> Result> { + jsonb::jsonpath::parse_json_path(path.as_bytes()) + .map_err(|e| execution_error(format!("Invalid JSONPath '{}': {}", path, e))) + } +} + +/// Convert JSONB value to string using jsonb's built-in serde (strict mode) +fn json_value_to_string(value: jsonb::OwnedJsonb) -> Result> { + let raw_jsonb = value.as_raw(); + + // Check for null first + if raw_jsonb + .is_null() + .map_err(|e| common::execution_error(format!("Failed to check null: {}", e)))? + { + return Ok(None); + } + + // Use jsonb's built-in to_str() method - strict conversion + raw_jsonb + .to_str() + .map(Some) + .map_err(|e| common::execution_error(format!("Failed to convert to string: {}", e))) +} + +/// Convert JSONB value to integer using jsonb's built-in serde (strict mode) +fn json_value_to_int(value: jsonb::OwnedJsonb) -> Result> { + let raw_jsonb = value.as_raw(); + + // Check for null first + if raw_jsonb + .is_null() + .map_err(|e| common::execution_error(format!("Failed to check null: {}", e)))? + { + return Ok(None); + } + + // Use jsonb's built-in to_i64() method - strict conversion + raw_jsonb + .to_i64() + .map(Some) + .map_err(|e| common::execution_error(format!("Failed to convert to integer: {}", e))) +} + +/// Convert JSONB value to float using jsonb's built-in serde (strict mode) +fn json_value_to_float(value: jsonb::OwnedJsonb) -> Result> { + let raw_jsonb = value.as_raw(); + + // Check for null first + if raw_jsonb + .is_null() + .map_err(|e| common::execution_error(format!("Failed to check null: {}", e)))? + { + return Ok(None); + } + + // Use jsonb's built-in to_f64() method - strict conversion + raw_jsonb + .to_f64() + .map(Some) + .map_err(|e| common::execution_error(format!("Failed to convert to float: {}", e))) +} + +/// Convert JSONB value to boolean using jsonb's built-in serde (strict mode) +fn json_value_to_bool(value: jsonb::OwnedJsonb) -> Result> { + let raw_jsonb = value.as_raw(); + + // Check for null first + if raw_jsonb + .is_null() + .map_err(|e| common::execution_error(format!("Failed to check null: {}", e)))? + { + return Ok(None); + } + + // Use jsonb's built-in to_bool() method - strict conversion + raw_jsonb + .to_bool() + .map(Some) + .map_err(|e| common::execution_error(format!("Failed to convert to boolean: {}", e))) +} + +/// Create the json_extract UDF for extracting JSONPath from JSON data +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: JSONPath expression as string (Utf8) +/// +/// # Returns +/// String representation of the extracted value, or null if path not found +pub fn json_extract_udf() -> ScalarUDF { + create_udf( + "json_extract", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Utf8, + Volatility::Immutable, + Arc::new(json_extract_columnar_impl), + ) +} + +/// Create the json_extract_with_type UDF that returns JSONB bytes with type information +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: JSONPath expression as string (Utf8) +/// +/// # Returns +/// A struct with two fields: +/// - value: LargeBinary (the extracted JSONB value) +/// - type_tag: UInt8 (type information: 0=null, 1=bool, 2=int64, 3=float64, 4=string, 5=array, 6=object) +pub fn json_extract_with_type_udf() -> ScalarUDF { + use arrow_schema::Fields; + + let return_type = DataType::Struct(Fields::from(vec![ + arrow_schema::Field::new("value", DataType::LargeBinary, true), + arrow_schema::Field::new("type_tag", DataType::UInt8, false), + ])); + + create_udf( + "json_extract_with_type", + vec![DataType::LargeBinary, DataType::Utf8], + return_type, + Volatility::Immutable, + Arc::new(json_extract_with_type_columnar_impl), + ) +} + +/// Implementation of json_extract function with ColumnarValue +fn json_extract_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_extract_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_extract_with_type function with ColumnarValue +fn json_extract_with_type_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_extract_with_type_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_extract function +fn json_extract_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_extract")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let path_array = common::extract_string_array(args, 1)?; + let mut builder = StringBuilder::with_capacity(jsonb_array.len(), 1024); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(path) = common::get_string_value_at(path_array, i) { + let jsonb_bytes = jsonb_array.value(i); + match extract_json_path(jsonb_bytes, path)? { + Some(value) => builder.append_value(&value), + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Implementation of json_extract_with_type function +fn json_extract_with_type_impl(args: &[ArrayRef]) -> Result { + use arrow_array::StructArray; + use arrow_array::builder::{LargeBinaryBuilder, UInt8Builder}; + + common::validate_arg_count(args, 2, "json_extract_with_type")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let path_array = common::extract_string_array(args, 1)?; + + let mut value_builder = LargeBinaryBuilder::with_capacity(jsonb_array.len(), 1024); + let mut type_builder = UInt8Builder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + value_builder.append_null(); + type_builder.append_value(JsonbType::Null.as_u8()); + } else if let Some(path) = common::get_string_value_at(path_array, i) { + let jsonb_bytes = jsonb_array.value(i); + match extract_json_path_with_type(jsonb_bytes, path)? { + Some((value_bytes, type_tag)) => { + value_builder.append_value(&value_bytes); + type_builder.append_value(type_tag); + } + None => { + value_builder.append_null(); + type_builder.append_value(JsonbType::Null.as_u8()); + } + } + } else { + value_builder.append_null(); + type_builder.append_value(JsonbType::Null.as_u8()); + } + } + + // Create struct array with two fields + let value_array = Arc::new(value_builder.finish()) as ArrayRef; + let type_array = Arc::new(type_builder.finish()) as ArrayRef; + + let struct_array = StructArray::from(vec![ + ( + Arc::new(arrow_schema::Field::new( + "value", + DataType::LargeBinary, + true, + )), + value_array, + ), + ( + Arc::new(arrow_schema::Field::new("type_tag", DataType::UInt8, false)), + type_array, + ), + ]); + + Ok(Arc::new(struct_array)) +} + +/// Extract value from JSONB using JSONPath and return with type information +/// Returns (JSONB bytes, type_tag) where type_tag represents the JsonbType +fn extract_json_path_with_type(jsonb_bytes: &[u8], path: &str) -> Result, u8)>> { + let json_path = common::parse_json_path(path)?; + + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + match selector.select_value(&json_path) { + Ok(Some(owned_value)) => { + let raw = owned_value.as_raw(); + + // Determine type using JsonbType enum + let jsonb_type = if raw.is_null().unwrap_or(false) { + JsonbType::Null + } else if raw.is_boolean().unwrap_or(false) { + JsonbType::Boolean + } else if raw.is_number().unwrap_or(false) { + let is_float_storage = + matches!(raw.as_number(), Ok(Some(jsonb::Number::Float64(_)))); + if !is_float_storage && raw.is_i64().unwrap_or(false) { + JsonbType::Int64 + } else { + JsonbType::Float64 + } + } else if raw.is_string().unwrap_or(false) { + JsonbType::String + } else if raw.is_array().unwrap_or(false) { + JsonbType::Array + } else if raw.is_object().unwrap_or(false) { + JsonbType::Object + } else { + JsonbType::String // default to string + }; + + // Return the JSONB bytes and type tag as u8 + Ok(Some((owned_value.to_vec(), jsonb_type.as_u8()))) + } + Ok(None) => Ok(None), + Err(e) => Err(common::execution_error(format!( + "Failed to select value from path '{}': {}", + path, e + ))), + } +} + +/// Extract value from JSONB using JSONPath +/// +/// Note: Uses `select_value` so JSONPath expressions matching multiple values +/// return a JSON array instead of silently dropping all but the first match. +fn extract_json_path(jsonb_bytes: &[u8], path: &str) -> Result> { + let json_path = common::parse_json_path(path)?; + + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + match selector.select_value(&json_path) { + Ok(value) => Ok(value.map(|value| value.to_string())), + Err(e) => Err(common::execution_error(format!( + "Failed to select value from path '{}': {}", + path, e + ))), + } +} + +/// Create the json_exists UDF for checking if a JSONPath exists +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: JSONPath expression as string (Utf8) +/// +/// # Returns +/// Boolean indicating whether the path exists in the JSON data +pub fn json_exists_udf() -> ScalarUDF { + create_udf( + "json_exists", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Boolean, + Volatility::Immutable, + Arc::new(json_exists_columnar_impl), + ) +} + +/// Implementation of json_exists function with ColumnarValue +fn json_exists_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_exists_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_exists function +fn json_exists_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_exists")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let path_array = common::extract_string_array(args, 1)?; + + let mut builder = BooleanBuilder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(path) = common::get_string_value_at(path_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let exists = check_json_path_exists(jsonb_bytes, path)?; + builder.append_value(exists); + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Check if a JSONPath exists in JSONB +fn check_json_path_exists(jsonb_bytes: &[u8], path: &str) -> Result { + let json_path = common::parse_json_path(path)?; + + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + match selector.exists(&json_path) { + Ok(exists) => Ok(exists), + Err(e) => Err(common::execution_error(format!( + "Failed to check existence of path '{}': {}", + path, e + ))), + } +} + +/// Create the json_get UDF for getting a field value as JSON string +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: Field name or array index as string (Utf8) +/// +/// # Returns +/// Raw JSONB bytes of the field value, or null if not found +pub fn json_get_udf() -> ScalarUDF { + create_udf( + "json_get", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::LargeBinary, + Volatility::Immutable, + Arc::new(json_get_columnar_impl), + ) +} + +/// Implementation of json_get function with ColumnarValue +fn json_get_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_get_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_get function +fn json_get_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_get")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let key_array = common::extract_string_array(args, 1)?; + + let mut builder = LargeBinaryBuilder::with_capacity(jsonb_array.len(), 0); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(key) = common::get_string_value_at(key_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + + match common::get_json_value_by_key(&raw_jsonb, key)? { + Some(value) => builder.append_value(value.as_raw().as_ref()), + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Create the json_get_string UDF for getting a string value +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: Field name or array index as string (Utf8) +/// +/// # Returns +/// String value with type coercion (numbers/booleans converted to strings) +pub fn json_get_string_udf() -> ScalarUDF { + create_udf( + "json_get_string", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Utf8, + Volatility::Immutable, + Arc::new(json_get_string_columnar_impl), + ) +} + +/// Implementation of json_get_string function with ColumnarValue +fn json_get_string_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_get_string_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_get_string function +fn json_get_string_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_get_string")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let key_array = common::extract_string_array(args, 1)?; + + let mut builder = StringBuilder::with_capacity(jsonb_array.len(), 1024); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(key) = common::get_string_value_at(key_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + + match common::get_json_value_by_key(&raw_jsonb, key)? { + Some(value) => match json_value_to_string(value)? { + Some(string_val) => builder.append_value(&string_val), + None => builder.append_null(), + }, + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Create the json_get_int UDF for getting an integer value +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: Field name or array index as string (Utf8) +/// +/// # Returns +/// Integer value with type coercion (strings/floats/booleans converted to int) +pub fn json_get_int_udf() -> ScalarUDF { + create_udf( + "json_get_int", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Int64, + Volatility::Immutable, + Arc::new(json_get_int_columnar_impl), + ) +} + +/// Implementation of json_get_int function with ColumnarValue +fn json_get_int_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_get_int_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_get_int function +fn json_get_int_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_get_int")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let key_array = common::extract_string_array(args, 1)?; + + let mut builder = Int64Builder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(key) = common::get_string_value_at(key_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + + match common::get_json_value_by_key(&raw_jsonb, key)? { + Some(value) => match json_value_to_int(value)? { + Some(int_val) => builder.append_value(int_val), + None => builder.append_null(), + }, + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Create the json_get_float UDF for getting a float value +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: Field name or array index as string (Utf8) +/// +/// # Returns +/// Float value with type coercion (strings/integers/booleans converted to float) +pub fn json_get_float_udf() -> ScalarUDF { + create_udf( + "json_get_float", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Float64, + Volatility::Immutable, + Arc::new(json_get_float_columnar_impl), + ) +} + +/// Implementation of json_get_float function with ColumnarValue +fn json_get_float_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_get_float_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_get_float function +fn json_get_float_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_get_float")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let key_array = common::extract_string_array(args, 1)?; + + let mut builder = Float64Builder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(key) = common::get_string_value_at(key_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + + match common::get_json_value_by_key(&raw_jsonb, key)? { + Some(value) => match json_value_to_float(value)? { + Some(float_val) => builder.append_value(float_val), + None => builder.append_null(), + }, + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Create the json_get_bool UDF for getting a boolean value +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: Field name or array index as string (Utf8) +/// +/// # Returns +/// Boolean value with flexible type coercion (strings like 'true'/'yes'/'1' become true) +pub fn json_get_bool_udf() -> ScalarUDF { + create_udf( + "json_get_bool", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Boolean, + Volatility::Immutable, + Arc::new(json_get_bool_columnar_impl), + ) +} + +/// Implementation of json_get_bool function with ColumnarValue +fn json_get_bool_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_get_bool_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_get_bool function +fn json_get_bool_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_get_bool")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let key_array = common::extract_string_array(args, 1)?; + + let mut builder = BooleanBuilder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(key) = common::get_string_value_at(key_array, i) { + let jsonb_bytes = jsonb_array.value(i); + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + + match common::get_json_value_by_key(&raw_jsonb, key)? { + Some(value) => match json_value_to_bool(value)? { + Some(bool_val) => builder.append_value(bool_val), + None => builder.append_null(), + }, + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Create the json_array_contains UDF for checking if array contains a value +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: JSONPath to array location (Utf8) +/// * Third parameter: Value to search for as string (Utf8) +/// +/// # Returns +/// Boolean indicating whether the array contains the specified value +pub fn json_array_contains_udf() -> ScalarUDF { + create_udf( + "json_array_contains", + vec![DataType::LargeBinary, DataType::Utf8, DataType::Utf8], + DataType::Boolean, + Volatility::Immutable, + Arc::new(json_array_contains_columnar_impl), + ) +} + +/// Implementation of json_array_contains function with ColumnarValue +fn json_array_contains_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_array_contains_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_array_contains function +fn json_array_contains_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 3, "json_array_contains")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let path_array = common::extract_string_array(args, 1)?; + let value_array = common::extract_string_array(args, 2)?; + + let mut builder = BooleanBuilder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else { + let path = common::get_string_value_at(path_array, i); + let value = common::get_string_value_at(value_array, i); + + match (path, value) { + (Some(p), Some(v)) => { + let jsonb_bytes = jsonb_array.value(i); + let contains = check_array_contains(jsonb_bytes, p, v)?; + builder.append_value(contains); + } + _ => builder.append_null(), + } + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Check if a JSON array at path contains a value +fn check_array_contains(jsonb_bytes: &[u8], path: &str, value: &str) -> Result { + let json_path = common::parse_json_path(path)?; + + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + match selector.select_values(&json_path) { + Ok(values) => { + for v in values { + // Convert to raw JSONB for direct access + let raw = v.as_raw(); + // Check if it's an array by trying to iterate + let mut index = 0; + loop { + match raw.get_by_index(index) { + Ok(Some(elem)) => { + let elem_str = elem.to_string(); + // Compare as JSON strings (with quotes for strings) + if elem_str == value || elem_str == format!("\"{}\"", value) { + return Ok(true); + } + index += 1; + } + Ok(None) => break, // End of array + Err(_) => break, // Not an array or error + } + } + } + Ok(false) + } + Err(e) => Err(common::execution_error(format!( + "Failed to check array contains at path '{}': {}", + path, e + ))), + } +} + +/// Create the json_array_length UDF for getting array length +/// +/// # Arguments +/// * First parameter: JSONB binary data (LargeBinary) +/// * Second parameter: JSONPath to array location (Utf8) +/// +/// # Returns +/// Integer length of the JSON array, or null if path doesn't point to an array +pub fn json_array_length_udf() -> ScalarUDF { + create_udf( + "json_array_length", + vec![DataType::LargeBinary, DataType::Utf8], + DataType::Int64, + Volatility::Immutable, + Arc::new(json_array_length_columnar_impl), + ) +} + +/// Implementation of json_array_length function with ColumnarValue +fn json_array_length_columnar_impl(args: &[ColumnarValue]) -> Result { + let arrays = common::columnar_to_arrays(args); + let result = json_array_length_impl(&arrays)?; + Ok(ColumnarValue::Array(result)) +} + +/// Implementation of json_array_length function +fn json_array_length_impl(args: &[ArrayRef]) -> Result { + common::validate_arg_count(args, 2, "json_array_length")?; + + let jsonb_array = common::extract_jsonb_array(args)?; + let path_array = common::extract_string_array(args, 1)?; + + let mut builder = Int64Builder::with_capacity(jsonb_array.len()); + + for i in 0..jsonb_array.len() { + if jsonb_array.is_null(i) { + builder.append_null(); + } else if let Some(path) = common::get_string_value_at(path_array, i) { + let jsonb_bytes = jsonb_array.value(i); + match get_array_length(jsonb_bytes, path)? { + Some(len) => builder.append_value(len), + None => builder.append_null(), + } + } else { + builder.append_null(); + } + } + + Ok(Arc::new(builder.finish())) +} + +/// Get the length of a JSON array at path +fn get_array_length(jsonb_bytes: &[u8], path: &str) -> Result> { + let json_path = common::parse_json_path(path)?; + + let raw_jsonb = jsonb::RawJsonb::new(jsonb_bytes); + let mut selector = jsonb::jsonpath::Selector::new(raw_jsonb); + match selector.select_values(&json_path) { + Ok(values) => { + if values.is_empty() { + return Ok(None); + } + let first = &values[0]; + let raw = first.as_raw(); + + // Count array elements by iterating + let mut count = 0; + loop { + match raw.get_by_index(count) { + Ok(Some(_)) => count += 1, + Ok(None) => break, // End of array + Err(_) => { + // Not an array + if count == 0 { + return Err(common::execution_error(format!( + "Path '{}' does not point to an array", + path + ))); + } + break; + } + } + } + Ok(Some(count as i64)) + } + Err(e) => Err(common::execution_error(format!( + "Failed to get array length at path '{}': {}", + path, e + ))), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow_array::builder::LargeBinaryBuilder; + use arrow_array::{BooleanArray, Float64Array, Int64Array}; + + fn create_test_jsonb(json_str: &str) -> Vec { + jsonb::parse_value(json_str.as_bytes()).unwrap().to_vec() + } + + #[test] + fn test_jsonb_type_enum() { + // Test enum conversion to/from u8 + assert_eq!(JsonbType::Null.as_u8(), 0); + assert_eq!(JsonbType::Boolean.as_u8(), 1); + assert_eq!(JsonbType::Int64.as_u8(), 2); + assert_eq!(JsonbType::Float64.as_u8(), 3); + assert_eq!(JsonbType::String.as_u8(), 4); + assert_eq!(JsonbType::Array.as_u8(), 5); + assert_eq!(JsonbType::Object.as_u8(), 6); + + // Test from_u8 conversion + assert_eq!(JsonbType::from_u8(0), Some(JsonbType::Null)); + assert_eq!(JsonbType::from_u8(1), Some(JsonbType::Boolean)); + assert_eq!(JsonbType::from_u8(2), Some(JsonbType::Int64)); + assert_eq!(JsonbType::from_u8(3), Some(JsonbType::Float64)); + assert_eq!(JsonbType::from_u8(4), Some(JsonbType::String)); + assert_eq!(JsonbType::from_u8(5), Some(JsonbType::Array)); + assert_eq!(JsonbType::from_u8(6), Some(JsonbType::Object)); + assert_eq!(JsonbType::from_u8(7), None); // Invalid value + } + + #[tokio::test] + async fn test_json_extract_udf() -> Result<()> { + let json = r#"{"user": {"name": "Alice", "age": 30}, "tags": ["python", "ml"]}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_null(); + + let jsonb_array = Arc::new(binary_builder.finish()); + let path_array = Arc::new(StringArray::from(vec![ + Some("$.user.name"), + Some("$.user.age"), + Some("$.tags[*]"), + Some("$.user.name"), + ])); + + let result = json_extract_impl(&[jsonb_array, path_array])?; + let string_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(string_array.len(), 4); + assert_eq!(string_array.value(0), "\"Alice\""); + assert_eq!(string_array.value(1), "30"); + assert_eq!(string_array.value(2), "[\"python\",\"ml\"]"); + assert!(string_array.is_null(3)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_exists_udf() -> Result<()> { + let json = r#"{"user": {"name": "Alice", "age": 30}, "tags": ["rust", "json"]}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_null(); + + let jsonb_array = Arc::new(binary_builder.finish()); + let path_array = Arc::new(StringArray::from(vec![ + Some("$.user.name"), + Some("$.user.email"), + Some("$.tags"), + Some("$.any"), + ])); + + let result = json_exists_impl(&[jsonb_array, path_array])?; + let bool_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(bool_array.len(), 4); + assert!(bool_array.value(0)); + assert!(!bool_array.value(1)); + assert!(bool_array.value(2)); + assert!(bool_array.is_null(3)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_get_string_udf() -> Result<()> { + // Test valid string conversions + let json = r#"{"str": "hello", "num": 123, "bool": true, "null": null}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("str"), + Some("num"), + Some("bool"), + Some("null"), + ])); + + let result = json_get_string_impl(&[jsonb_array, key_array])?; + let string_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(string_array.len(), 4); + assert_eq!(string_array.value(0), "hello"); + assert_eq!(string_array.value(1), "123"); + assert_eq!(string_array.value(2), "true"); + assert!(string_array.is_null(3)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_get_int_udf() -> Result<()> { + let json = r#"{"int": 42, "str_num": "99", "bool": true, "0": 7}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + for _ in 0..4 { + binary_builder.append_value(&jsonb_bytes); + } + + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("int"), + Some("str_num"), + Some("bool"), + Some("0"), + ])); + + let result = json_get_int_impl(&[jsonb_array, key_array])?; + let int_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(int_array.len(), 4); + assert_eq!(int_array.value(0), 42); + assert_eq!(int_array.value(1), 99); + assert_eq!(int_array.value(2), 1); // jsonb converts true to 1 + assert_eq!(int_array.value(3), 7); + + Ok(()) + } + + #[tokio::test] + async fn test_json_get_float_udf() -> Result<()> { + let json = r#"{ + "float_decimal": 1.5, + "float_neg": -2.5, + "float_int_value": 1.0, + "float_exp": 1e2, + "int_pos": 42, + "int_neg": -7, + "big_int": 9223372036854775808, + "str_num": "3.5", + "bool_true": true, + "null_val": null + }"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + for _ in 0..11 { + binary_builder.append_value(&jsonb_bytes); + } + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("float_decimal"), + Some("float_neg"), + Some("float_int_value"), + Some("float_exp"), + Some("int_pos"), + Some("int_neg"), + Some("big_int"), + Some("str_num"), + Some("bool_true"), + Some("null_val"), + Some("missing"), + ])); + + let result = json_get_float_impl(&[jsonb_array, key_array])?; + let float_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(float_array.len(), 11); + assert_eq!(float_array.value(0), 1.5); + assert_eq!(float_array.value(1), -2.5); + assert_eq!(float_array.value(2), 1.0); + assert_eq!(float_array.value(3), 100.0); + assert_eq!(float_array.value(4), 42.0); + assert_eq!(float_array.value(5), -7.0); + // 2^63 is exactly representable in f64. + assert_eq!(float_array.value(6), 9223372036854775808.0); + assert_eq!(float_array.value(7), 3.5); + assert_eq!(float_array.value(8), 1.0); // jsonb converts true to 1.0 + assert!(float_array.is_null(9)); + assert!(float_array.is_null(10)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_get_bool_udf() -> Result<()> { + let json = + r#"{"bool_true": true, "bool_false": false, "str_true": "true", "str_false": "false"}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("bool_true"), + Some("bool_false"), + Some("str_true"), + Some("str_false"), + ])); + + let result = json_get_bool_impl(&[jsonb_array, key_array])?; + let bool_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(bool_array.len(), 4); + assert!(bool_array.value(0)); + assert!(!bool_array.value(1)); + assert!(bool_array.value(2)); // "true" string converts to true + assert!(!bool_array.value(3)); // "false" string converts to false + + Ok(()) + } + + #[tokio::test] + async fn test_json_array_contains_udf() -> Result<()> { + let json = r#"{"tags": ["rust", "json", "database"], "nums": [1, 2, 3]}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_null(); + + let jsonb_array = Arc::new(binary_builder.finish()); + let path_array = Arc::new(StringArray::from(vec![ + Some("$.tags"), + Some("$.tags"), + Some("$.nums"), + Some("$.tags"), + ])); + let value_array = Arc::new(StringArray::from(vec![ + Some("rust"), + Some("python"), + Some("2"), + Some("any"), + ])); + + let result = json_array_contains_impl(&[jsonb_array, path_array, value_array])?; + let bool_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(bool_array.len(), 4); + assert!(bool_array.value(0)); + assert!(!bool_array.value(1)); + assert!(bool_array.value(2)); + assert!(bool_array.is_null(3)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_array_length_udf() -> Result<()> { + let json = r#"{"empty": [], "tags": ["a", "b", "c"], "nested": {"arr": [1, 2]}}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_null(); + + let jsonb_array = Arc::new(binary_builder.finish()); + let path_array = Arc::new(StringArray::from(vec![ + Some("$.empty"), + Some("$.tags"), + Some("$.nested.arr"), + Some("$.any"), + ])); + + let result = json_array_length_impl(&[jsonb_array, path_array])?; + let int_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(int_array.len(), 4); + assert_eq!(int_array.value(0), 0); + assert_eq!(int_array.value(1), 3); + assert_eq!(int_array.value(2), 2); + assert!(int_array.is_null(3)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_extract_with_type() -> Result<()> { + use arrow_array::StructArray; + use arrow_array::UInt8Array; + + let cases: &[(&str, JsonbType)] = &[ + (r#"{"v": 1}"#, JsonbType::Int64), + (r#"{"v": 0}"#, JsonbType::Int64), + (r#"{"v": -42}"#, JsonbType::Int64), + (r#"{"v": 9223372036854775807}"#, JsonbType::Int64), // i64::MAX + (r#"{"v": 9223372036854775808}"#, JsonbType::Float64), // i64::MAX + 1 + (r#"{"v": 1.0}"#, JsonbType::Float64), + (r#"{"v": 2.7}"#, JsonbType::Float64), + (r#"{"v": 1.5}"#, JsonbType::Float64), + (r#"{"v": -1.5}"#, JsonbType::Float64), + (r#"{"v": 1e2}"#, JsonbType::Float64), + ]; + + for (json, expected) in cases { + let bytes = create_test_jsonb(json); + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&bytes); + let jsonb_array: ArrayRef = Arc::new(binary_builder.finish()); + let path_array: ArrayRef = Arc::new(StringArray::from(vec![Some("$.v")])); + + let result = json_extract_with_type_impl(&[jsonb_array, path_array])?; + let struct_array = result.as_any().downcast_ref::().unwrap(); + let type_tags = struct_array + .column_by_name("type_tag") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + assert_eq!(type_tags.value(0), expected.as_u8()); + } + + Ok(()) + } + + #[tokio::test] + async fn test_json_extract_with_wildcard() -> Result<()> { + use arrow_array::StructArray; + use arrow_array::UInt8Array; + + let json = r#"{"items": [{"price": 1}, {"price": 2}]}"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + + let jsonb_array: ArrayRef = Arc::new(binary_builder.finish()); + let path_array: ArrayRef = Arc::new(StringArray::from(vec![Some("$.items[*].price")])); + + let result = json_extract_with_type_impl(&[jsonb_array, path_array])?; + let struct_array = result.as_any().downcast_ref::().unwrap(); + let values = struct_array + .column_by_name("value") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + let type_tags = struct_array + .column_by_name("type_tag") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap(); + + assert_eq!(jsonb::RawJsonb::new(values.value(0)).to_string(), "[1,2]"); + assert_eq!(type_tags.value(0), JsonbType::Array.as_u8()); + + Ok(()) + } + + #[tokio::test] + async fn test_json_array_access() -> Result<()> { + let json = r#"["first", "second", "third"]"#; + let jsonb_bytes = create_test_jsonb(json); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + binary_builder.append_value(&jsonb_bytes); + + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("0"), + Some("1"), + Some("10"), // Out of bounds + ])); + + let result = json_get_string_impl(&[jsonb_array, key_array])?; + let string_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(string_array.len(), 3); + assert_eq!(string_array.value(0), "first"); + assert_eq!(string_array.value(1), "second"); + assert!(string_array.is_null(2)); + + Ok(()) + } + + #[tokio::test] + async fn test_json_get_numeric_object_key() -> Result<()> { + let obj_bytes = create_test_jsonb(r#"{"0": "from_object", "1": 42}"#); + let arr_bytes = create_test_jsonb(r#"["zero", "one", "two"]"#); + + let mut binary_builder = LargeBinaryBuilder::new(); + binary_builder.append_value(&obj_bytes); + binary_builder.append_value(&arr_bytes); + binary_builder.append_value(&arr_bytes); + + let jsonb_array = Arc::new(binary_builder.finish()); + let key_array = Arc::new(StringArray::from(vec![ + Some("0"), // Numeric key on object: looks up the "0" field. + Some("0"), // Numeric key on array: looks up index 0. + Some("foo"), // Non-numeric key on array: no match. + ])); + + let result = json_get_string_impl(&[jsonb_array, key_array])?; + let string_array = result.as_any().downcast_ref::().unwrap(); + + assert_eq!(string_array.len(), 3); + assert_eq!(string_array.value(0), "from_object"); + assert_eq!(string_array.value(1), "zero"); + assert!(string_array.is_null(2)); + + Ok(()) + } +} diff --git a/lance-artifact/rust/lance-datafusion/src/utils.rs b/lance-artifact/rust/lance-datafusion/src/utils.rs new file mode 100644 index 000000000..e660c8ee4 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/utils.rs @@ -0,0 +1,257 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::borrow::Cow; + +use arrow::ffi_stream::ArrowArrayStreamReader; +use arrow_array::{RecordBatch, RecordBatchIterator, RecordBatchReader}; +use arrow_schema::{ArrowError, SchemaRef}; +use async_trait::async_trait; +use background_iterator::BackgroundIterator; +use datafusion::{ + execution::RecordBatchStream, + physical_plan::{ + SendableRecordBatchStream, + metrics::{ + Count, ExecutionPlanMetricsSet, Gauge, MetricBuilder, MetricValue, MetricsSet, Time, + }, + stream::RecordBatchStreamAdapter, + }, +}; +use datafusion_common::DataFusionError; +use futures::{StreamExt, TryStreamExt, stream}; +use lance_core::Result; +use lance_core::datatypes::Schema; +use tokio::task::spawn; + +pub mod background_iterator; + +/// A trait for [`RecordBatch`] iterators, readers and streams +/// that can be converted to a concrete stream type [`SendableRecordBatchStream`]. +/// +/// This also cam read the schema from the first batch +/// and then update the schema to reflect the dictionary columns. +#[async_trait] +pub trait StreamingWriteSource: Send { + /// Infer the Lance schema from the first batch stream. + /// + /// This will peek the first batch to get the dictionaries for dictionary columns. + /// + /// NOTE: this does not validate the schema. For example, for appends the schema + /// should be checked to make sure it matches the existing dataset schema before + /// writing. + async fn into_stream_and_schema(self) -> Result<(SendableRecordBatchStream, Schema)> + where + Self: Sized, + { + let mut stream = self.into_stream(); + let (stream, arrow_schema, schema) = spawn(async move { + let arrow_schema = stream.schema(); + let mut schema: Schema = Schema::try_from(arrow_schema.as_ref())?; + let first_batch = stream.try_next().await?; + if let Some(batch) = &first_batch { + schema.set_dictionary(batch)?; + } + let stream = stream::iter(first_batch.map(Ok)).chain(stream); + Result::Ok((stream, arrow_schema, schema)) + }) + .await + .unwrap()?; + schema.validate()?; + let adapter = RecordBatchStreamAdapter::new(arrow_schema, stream); + Ok((Box::pin(adapter), schema)) + } + + /// Returns the arrow schema. + fn arrow_schema(&self) -> SchemaRef; + + /// Convert to a stream. + /// + /// The conversion will be conducted in a background thread. + fn into_stream(self) -> SendableRecordBatchStream; +} + +impl StreamingWriteSource for ArrowArrayStreamReader { + #[inline] + fn arrow_schema(&self) -> SchemaRef { + RecordBatchReader::schema(self) + } + + #[inline] + fn into_stream(self) -> SendableRecordBatchStream { + reader_to_stream(Box::new(self)) + } +} + +impl StreamingWriteSource for RecordBatchIterator +where + Self: Send, + I: IntoIterator> + Send + 'static, +{ + #[inline] + fn arrow_schema(&self) -> SchemaRef { + RecordBatchReader::schema(self) + } + + #[inline] + fn into_stream(self) -> SendableRecordBatchStream { + reader_to_stream(Box::new(self)) + } +} + +impl StreamingWriteSource for Box +where + T: StreamingWriteSource, +{ + #[inline] + fn arrow_schema(&self) -> SchemaRef { + T::arrow_schema(&**self) + } + + #[inline] + fn into_stream(self) -> SendableRecordBatchStream { + T::into_stream(*self) + } +} + +impl StreamingWriteSource for Box { + #[inline] + fn arrow_schema(&self) -> SchemaRef { + RecordBatchReader::schema(self) + } + + #[inline] + fn into_stream(self) -> SendableRecordBatchStream { + reader_to_stream(self) + } +} + +impl StreamingWriteSource for SendableRecordBatchStream { + #[inline] + fn arrow_schema(&self) -> SchemaRef { + RecordBatchStream::schema(&**self) + } + + #[inline] + fn into_stream(self) -> SendableRecordBatchStream { + self + } +} + +/// Convert reader to a stream. +/// +/// The reader will be called in a background thread. +pub fn reader_to_stream(batches: Box) -> SendableRecordBatchStream { + let arrow_schema = batches.arrow_schema(); + let stream = RecordBatchStreamAdapter::new( + arrow_schema, + BackgroundIterator::new(batches) + .fuse() + .map_err(DataFusionError::from), + ); + Box::pin(stream) +} + +pub trait MetricsExt { + fn find_count(&self, name: &str) -> Option; + fn iter_counts(&self) -> impl Iterator, &Count)>; + fn iter_times(&self) -> impl Iterator, &Time)>; + fn iter_gauges(&self) -> impl Iterator, &Gauge)>; +} + +impl MetricsExt for MetricsSet { + fn find_count(&self, metric_name: &str) -> Option { + self.iter().find_map(|m| match m.value() { + MetricValue::Count { name, count } => { + if name == metric_name { + Some(count.clone()) + } else { + None + } + } + _ => None, + }) + } + + fn iter_counts(&self) -> impl Iterator, &Count)> { + self.iter().filter_map(|m| match m.value() { + MetricValue::Count { name, count } => Some((name, count)), + _ => None, + }) + } + + fn iter_times(&self) -> impl Iterator, &Time)> { + self.iter().filter_map(|m| match m.value() { + MetricValue::Time { name, time } => Some((name, time)), + _ => None, + }) + } + + fn iter_gauges(&self) -> impl Iterator, &Gauge)> { + self.iter().filter_map(|m| match m.value() { + MetricValue::Gauge { name, gauge } => Some((name, gauge)), + _ => None, + }) + } +} + +pub trait ExecutionPlanMetricsSetExt { + fn new_count(&self, name: &'static str, partition: usize) -> Count; + fn new_time(&self, name: &'static str, partition: usize) -> Time; + fn new_gauge(&self, name: &'static str, partition: usize) -> Gauge; +} + +impl ExecutionPlanMetricsSetExt for ExecutionPlanMetricsSet { + fn new_count(&self, name: &'static str, partition: usize) -> Count { + let count = Count::new(); + MetricBuilder::new(self) + .with_partition(partition) + .build(MetricValue::Count { + name: Cow::Borrowed(name), + count: count.clone(), + }); + count + } + + fn new_time(&self, name: &'static str, partition: usize) -> Time { + let time = Time::new(); + MetricBuilder::new(self) + .with_partition(partition) + .build(MetricValue::Time { + name: Cow::Borrowed(name), + time: time.clone(), + }); + time + } + + fn new_gauge(&self, name: &'static str, partition: usize) -> Gauge { + let gauge = Gauge::new(); + MetricBuilder::new(self) + .with_partition(partition) + .build(MetricValue::Gauge { + name: Cow::Borrowed(name), + gauge: gauge.clone(), + }); + gauge + } +} + +// Common metrics +pub const IOPS_METRIC: &str = "iops"; +pub const REQUESTS_METRIC: &str = "requests"; +pub const BYTES_READ_METRIC: &str = "bytes_read"; +pub const INDICES_LOADED_METRIC: &str = "indices_loaded"; +pub const PARTS_LOADED_METRIC: &str = "parts_loaded"; +pub const PARTITIONS_RANKED_METRIC: &str = "partitions_ranked"; +pub const INDEX_COMPARISONS_METRIC: &str = "index_comparisons"; +pub const INDEX_CACHE_HITS_METRIC: &str = "index_cache_hits"; +pub const INDEX_CACHE_MISSES_METRIC: &str = "index_cache_misses"; +pub const FRAGMENTS_SCANNED_METRIC: &str = "fragments_scanned"; +pub const RANGES_SCANNED_METRIC: &str = "ranges_scanned"; +pub const ROWS_SCANNED_METRIC: &str = "rows_scanned"; +pub const TASK_WAIT_TIME_METRIC: &str = "task_wait_time"; +pub const DELTAS_SEARCHED_METRIC: &str = "deltas_searched"; +pub const PARTITIONS_SEARCHED_METRIC: &str = "partitions_searched"; +pub const FIND_PARTITIONS_ELAPSED_METRIC: &str = "find_partitions_elapsed"; +pub const SCALAR_INDEX_SEARCH_TIME_METRIC: &str = "search_time"; +pub const SCALAR_INDEX_SER_TIME_METRIC: &str = "serialization_time"; diff --git a/lance-artifact/rust/lance-datafusion/src/utils/background_iterator.rs b/lance-artifact/rust/lance-datafusion/src/utils/background_iterator.rs new file mode 100644 index 000000000..27a8fdc15 --- /dev/null +++ b/lance-artifact/rust/lance-datafusion/src/utils/background_iterator.rs @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use futures::Stream; +use futures::ready; +use std::{ + future::Future, + panic, + pin::Pin, + task::{Context, Poll}, +}; +use tokio::task::JoinHandle; + +/// Wrap an iterator as a stream that executes the iterator in a background +/// blocking thread. +/// +/// The size hint is preserved, but the stream is not fused. +#[pin_project::pin_project] +pub struct BackgroundIterator { + #[pin] + state: BackgroundIterState, +} + +impl BackgroundIterator { + pub fn new(iter: I) -> Self { + Self { + state: BackgroundIterState::Current { iter }, + } + } +} + +impl Stream for BackgroundIterator +where + I::Item: Send + 'static, +{ + type Item = I::Item; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let mut this = self.project(); + + if let Some(mut iter) = this.state.as_mut().take_iter() { + this.state.set(BackgroundIterState::Running { + size_hint: iter.size_hint(), + task: tokio::task::spawn_blocking(move || { + let next = iter.next(); + next.map(|next| (iter, next)) + }), + }); + } + + let step = match this.state.as_mut().project_future() { + Some(task) => ready!(task.poll(cx)), + None => panic!( + "BackgroundIterator must not be polled after it returned `Poll::Ready(None)`" + ), + }; + + match step { + Ok(Some((iter, next))) => { + this.state.set(BackgroundIterState::Current { iter }); + Poll::Ready(Some(next)) + } + Ok(None) => { + this.state.set(BackgroundIterState::Empty); + Poll::Ready(None) + } + Err(err) => { + if err.is_panic() { + // Resume the panic on the main task + panic::resume_unwind(err.into_panic()); + } else { + panic!("Background task failed: {:?}", err); + } + } + } + } + + fn size_hint(&self) -> (usize, Option) { + match &self.state { + BackgroundIterState::Current { iter } => iter.size_hint(), + BackgroundIterState::Running { size_hint, .. } => *size_hint, + BackgroundIterState::Empty => (0, Some(0)), + } + } +} + +// Inspired by Unfold implementation: https://github.com/rust-lang/futures-rs/blob/master/futures-util/src/unfold_state.rs#L22 +#[pin_project::pin_project(project = StateProj, project_replace = StateReplace)] +enum BackgroundIterState { + Current { + iter: I, + }, + Running { + size_hint: (usize, Option), + #[pin] + task: NextHandle, + }, + Empty, +} + +type NextHandle = JoinHandle>; + +impl BackgroundIterState { + fn project_future(self: Pin<&mut Self>) -> Option>> { + match self.project() { + StateProj::Running { task, .. } => Some(task), + _ => None, + } + } + + fn take_iter(self: Pin<&mut Self>) -> Option { + match &*self { + Self::Current { .. } => match self.project_replace(Self::Empty) { + StateReplace::Current { iter } => Some(iter), + _ => None, + }, + _ => None, + } + } +} diff --git a/lance-artifact/rust/lance-datagen/Cargo.toml b/lance-artifact/rust/lance-datagen/Cargo.toml new file mode 100644 index 000000000..83b5aba36 --- /dev/null +++ b/lance-artifact/rust/lance-datagen/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "lance-datagen" +version = { workspace = true } +edition = { workspace = true } +license = { workspace = true } +repository = { workspace = true } +description = { workspace = true } +readme = { workspace = true } +keywords = { workspace = true } +categories = { workspace = true } + +[dependencies] +arrow = { workspace = true } +arrow-array = { workspace = true } +arrow-cast = { workspace = true } +arrow-schema = { workspace = true } +chrono = { workspace = true } +futures = { workspace = true } +half = { workspace = true } +hex = "0.4.3" +rand = { workspace = true } +rand_distr = { workspace = true } +rand_xoshiro = { workspace = true } + +[dev-dependencies] +criterion = { workspace = true } +lance-testing.workspace = true + +[lib] +bench = false + +[[bench]] +name = "array_gen" +harness = false + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-datagen/benches/array_gen.rs b/lance-artifact/rust/lance-datagen/benches/array_gen.rs new file mode 100644 index 000000000..899b349a9 --- /dev/null +++ b/lance-artifact/rust/lance-datagen/benches/array_gen.rs @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_array::types::{Float32Type, Int8Type, Int16Type, Int32Type, Int64Type}; +use criterion::{ + BenchmarkGroup, Criterion, Throughput, criterion_group, criterion_main, + measurement::Measurement, +}; + +use lance_datagen::{ + ArrayGeneratorExt, BatchCount, ByteCount, Dimension, RoundingBehavior, + generator::ArrayGenerator, +}; +#[cfg(target_os = "linux")] +use lance_testing::pprof::{Output, PProfProfiler}; + +const NUM_BATCHES: u32 = 100; +const KB_PER_BATCH: u64 = 128; +const BYTES_PER_BATCH: u64 = KB_PER_BATCH * 1024; +const BYTES_PER_BENCH: u64 = BYTES_PER_BATCH * NUM_BATCHES as u64; + +fn bench_gen( + group: &mut BenchmarkGroup, + id: &str, + gen_factory: impl Fn() -> Box, +) { + let num_batches: BatchCount = BatchCount::from(NUM_BATCHES); + + group.bench_function(id, |b| { + b.iter(|| { + let reader = lance_datagen::gen_batch() + .anon_col(gen_factory()) + .into_reader_bytes( + ByteCount::from(BYTES_PER_BATCH), + num_batches, + RoundingBehavior::ExactOrErr, + ) + .unwrap(); + reader.for_each(|batch| assert!(batch.is_ok())); + }) + }); +} + +fn bench_step_gen(c: &mut Criterion) { + let mut group = c.benchmark_group("step"); + group.throughput(Throughput::Bytes(BYTES_PER_BENCH)); + bench_gen(&mut group, "i8", || { + lance_datagen::array::step::() + }); + bench_gen(&mut group, "16", || { + lance_datagen::array::step::() + }); + bench_gen(&mut group, "i32", || { + lance_datagen::array::step::() + }); + bench_gen(&mut group, "i64", || { + lance_datagen::array::step::() + }); + group.finish(); +} + +fn bench_null_gen(c: &mut Criterion) { + let mut group = c.benchmark_group("null"); + group.throughput(Throughput::Bytes(BYTES_PER_BENCH)); + bench_gen(&mut group, "0.0", || { + lance_datagen::array::fill::(42).with_random_nulls(0.0) + }); + bench_gen(&mut group, "0.25", || { + lance_datagen::array::fill::(42).with_random_nulls(0.25) + }); + bench_gen(&mut group, "0.75", || { + lance_datagen::array::fill::(42).with_random_nulls(0.75) + }); + bench_gen(&mut group, "1.0", || { + lance_datagen::array::fill::(42).with_random_nulls(1.0) + }); + group.finish(); +} + +fn bench_fill_gen(c: &mut Criterion) { + let mut group = c.benchmark_group("fill"); + group.throughput(Throughput::Bytes(BYTES_PER_BENCH)); + bench_gen(&mut group, "fill_i8", || { + lance_datagen::array::fill::(42) + }); + bench_gen(&mut group, "fill_i16", || { + lance_datagen::array::fill::(42) + }); + bench_gen(&mut group, "fill_i32", || { + lance_datagen::array::fill::(42) + }); + bench_gen(&mut group, "fill_i64", || { + lance_datagen::array::fill::(42) + }); + bench_gen(&mut group, "fill_varbin", || { + lance_datagen::array::fill_varbin(vec![ + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, + ]) + }); + bench_gen(&mut group, "fill_utf8", || { + lance_datagen::array::fill_utf8("hello world!".to_string()) + }); + group.finish(); +} + +fn bench_rand_gen(c: &mut Criterion) { + let mut group = c.benchmark_group("rand"); + group.throughput(Throughput::Bytes(BYTES_PER_BENCH)); + bench_gen(&mut group, "rand_i8", || { + lance_datagen::array::rand::() + }); + bench_gen(&mut group, "rand_i16", || { + lance_datagen::array::rand::() + }); + bench_gen(&mut group, "rand_i32", || { + lance_datagen::array::rand::() + }); + bench_gen(&mut group, "rand_i64", || { + lance_datagen::array::rand::() + }); + bench_gen(&mut group, "rand_varbin", || { + lance_datagen::array::rand_fixedbin(ByteCount::from(12), false) + }); + bench_gen(&mut group, "rand_utf8", || { + lance_datagen::array::rand_utf8(ByteCount::from(12), false) + }); + bench_gen(&mut group, "rand_vec", || { + lance_datagen::array::rand_vec::(Dimension::from(512)) + }); + bench_gen(&mut group, "rand_dict_i32_utf8", || { + lance_datagen::array::dict::(lance_datagen::array::rand_utf8( + ByteCount::from(8), + false, + )) + }); + group.finish(); +} + +#[cfg(target_os = "linux")] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10) + .with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + targets = bench_step_gen, bench_fill_gen, bench_null_gen, bench_rand_gen); + +#[cfg(not(target_os = "linux"))] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10); + targets = bench_step_gen, bench_fill_gen, bench_null_gen, bench_rand_gen); + +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-datagen/src/generator.rs b/lance-artifact/rust/lance-datagen/src/generator.rs new file mode 100644 index 000000000..34fe1202e --- /dev/null +++ b/lance-artifact/rust/lance-datagen/src/generator.rs @@ -0,0 +1,3682 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashMap, iter, marker::PhantomData, sync::Arc, sync::LazyLock}; + +use arrow::{ + array::{ArrayData, AsArray, Float32Builder, GenericBinaryBuilder, GenericStringBuilder}, + buffer::{BooleanBuffer, Buffer, OffsetBuffer, ScalarBuffer}, + datatypes::{ + ArrowPrimitiveType, Float32Type, Int32Type, Int64Type, IntervalDayTime, + IntervalMonthDayNano, UInt32Type, + }, +}; +use arrow_array::{ + Array, BinaryArray, FixedSizeBinaryArray, FixedSizeListArray, Float32Array, LargeListArray, + LargeStringArray, ListArray, MapArray, NullArray, OffsetSizeTrait, PrimitiveArray, RecordBatch, + RecordBatchOptions, RecordBatchReader, StringArray, StructArray, make_array, + types::{ArrowDictionaryKeyType, BinaryType, ByteArrayType, Utf8Type}, +}; +use arrow_schema::{ArrowError, DataType, Field, Fields, IntervalUnit, Schema, SchemaRef}; +use futures::{StreamExt, stream::BoxStream}; +use rand::{Rng, RngCore, SeedableRng, distr::Uniform}; +use rand_distr::Zipf; + +use self::array::rand_with_distribution; + +#[derive(Copy, Clone, Debug, Default)] +pub struct RowCount(u64); +#[derive(Copy, Clone, Debug, Default)] +pub struct BatchCount(u32); +#[derive(Copy, Clone, Debug, Default)] +pub struct ByteCount(u64); +#[derive(Copy, Clone, Debug, Default)] +pub struct Dimension(u32); + +impl From for BatchCount { + fn from(n: u32) -> Self { + Self(n) + } +} + +impl From for RowCount { + fn from(n: u64) -> Self { + Self(n) + } +} + +impl From for ByteCount { + fn from(n: u64) -> Self { + Self(n) + } +} + +impl From for Dimension { + fn from(n: u32) -> Self { + Self(n) + } +} + +/// A trait for anything that can generate arrays of data +pub trait ArrayGenerator: Send + Sync + std::fmt::Debug { + /// Generate an array of the given length + /// + /// # Arguments + /// + /// * `length` - The number of elements to generate + /// * `rng` - The random number generator to use + /// + /// # Returns + /// + /// An array of the given length + /// + /// Note: Not every generator needs an rng. However, it is passed here because many do and this + /// lets us manage RNGs at the batch level instead of the array level. + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError>; + + /// Generate an array of the given length using a new RNG with the default seed + /// + /// # Arguments + /// + /// * `length` - The number of elements to generate + /// + /// # Returns + /// + /// An array of the given length + fn generate_default( + &mut self, + length: RowCount, + ) -> Result, ArrowError> { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + Self::generate(self, length, &mut rng) + } + /// Get the data type of the array that this generator produces + /// + /// # Returns + /// + /// The data type of the array that this generator produces + fn data_type(&self) -> &DataType; + /// Gets metadata that should be associated with the field generated by this generator + fn metadata(&self) -> Option> { + None + } + /// Get the size of each element in bytes + /// + /// # Returns + /// + /// The size of each element in bytes. Will be None if the size varies by element. + fn element_size_bytes(&self) -> Option; +} + +#[derive(Debug)] +pub struct CycleNullGenerator { + generator: Box, + validity: Vec, + idx: usize, +} +#[derive(Debug)] +pub struct CycleNanGenerator { + generator: Box, + nan_pattern: Vec, + idx: usize, +} + +impl ArrayGenerator for CycleNanGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let array = self.generator.generate(length, rng)?; + + // Only apply NaN pattern to float types + match array.data_type() { + DataType::Float16 => { + let float_array = array + .as_any() + .downcast_ref::() + .unwrap(); + let mut values: Vec = float_array.values().to_vec(); + + for (i, &should_be_nan) in self + .nan_pattern + .iter() + .cycle() + .skip(self.idx) + .take(length.0 as usize) + .enumerate() + { + if should_be_nan { + values[i] = half::f16::NAN; + } + } + + self.idx = (self.idx + (length.0 as usize)) % self.nan_pattern.len(); + Ok(Arc::new(arrow_array::Float16Array::from(values))) + } + DataType::Float32 => { + let float_array = array + .as_any() + .downcast_ref::() + .unwrap(); + let mut values: Vec = float_array.values().to_vec(); + + for (i, &should_be_nan) in self + .nan_pattern + .iter() + .cycle() + .skip(self.idx) + .take(length.0 as usize) + .enumerate() + { + if should_be_nan { + values[i] = f32::NAN; + } + } + + self.idx = (self.idx + (length.0 as usize)) % self.nan_pattern.len(); + Ok(Arc::new(arrow_array::Float32Array::from(values))) + } + DataType::Float64 => { + let float_array = array + .as_any() + .downcast_ref::() + .unwrap(); + let mut values: Vec = float_array.values().to_vec(); + + for (i, &should_be_nan) in self + .nan_pattern + .iter() + .cycle() + .skip(self.idx) + .take(length.0 as usize) + .enumerate() + { + if should_be_nan { + values[i] = f64::NAN; + } + } + + self.idx = (self.idx + (length.0 as usize)) % self.nan_pattern.len(); + Ok(Arc::new(arrow_array::Float64Array::from(values))) + } + _ => { + // For non-float types, just return the original array unchanged + Ok(array) + } + } + } + + fn data_type(&self) -> &DataType { + self.generator.data_type() + } + + fn element_size_bytes(&self) -> Option { + self.generator.element_size_bytes() + } +} + +impl ArrayGenerator for CycleNullGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let array = self.generator.generate(length, rng)?; + let data = array.to_data(); + let validity_itr = self + .validity + .iter() + .cycle() + .skip(self.idx) + .take(length.0 as usize) + .copied(); + let validity_bitmap = BooleanBuffer::from_iter(validity_itr); + + self.idx = (self.idx + (length.0 as usize)) % self.validity.len(); + unsafe { + let new_data = ArrayData::new_unchecked( + data.data_type().clone(), + data.len(), + None, + Some(validity_bitmap.into_inner()), + data.offset(), + data.buffers().to_vec(), + data.child_data().into(), + ); + Ok(make_array(new_data)) + } + } + + fn data_type(&self) -> &DataType { + self.generator.data_type() + } + + fn element_size_bytes(&self) -> Option { + self.generator.element_size_bytes() + } +} + +#[derive(Debug)] +pub struct MetadataGenerator { + generator: Box, + metadata: HashMap, +} + +impl ArrayGenerator for MetadataGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + self.generator.generate(length, rng) + } + + fn metadata(&self) -> Option> { + Some(self.metadata.clone()) + } + + fn data_type(&self) -> &DataType { + self.generator.data_type() + } + + fn element_size_bytes(&self) -> Option { + self.generator.element_size_bytes() + } +} + +#[derive(Debug)] +pub struct NullGenerator { + generator: Box, + null_probability: f64, +} + +impl ArrayGenerator for NullGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let array = self.generator.generate(length, rng)?; + let data = array.to_data(); + + if self.null_probability < 0.0 || self.null_probability > 1.0 { + return Err(ArrowError::InvalidArgumentError(format!( + "null_probability must be between 0 and 1, got {}", + self.null_probability + ))); + } + + let (null_count, new_validity) = if self.null_probability == 0.0 { + if data.null_count() == 0 { + return Ok(array); + } else { + (0_usize, None) + } + } else if self.null_probability == 1.0 { + if data.null_count() == data.len() { + return Ok(array); + } else { + let all_nulls = BooleanBuffer::new_unset(array.len()); + (array.len(), Some(all_nulls.into_inner())) + } + } else { + let array_len = array.len(); + let num_validity_bytes = array_len.div_ceil(8); + let mut null_count = 0; + // Sampling the RNG once per bit is kind of slow so we do this to sample once + // per byte. We only get 8 bits of RNG resolution but that should be good enough. + let threshold = (self.null_probability * u8::MAX as f64) as u8; + let bytes = (0..num_validity_bytes) + .map(|byte_idx| { + let mut sample = rng.random::(); + let mut byte: u8 = 0; + for bit_idx in 0..8 { + // We could probably overshoot and fill in extra bits with random data but + // this is cleaner and that would mess up the null count + byte <<= 1; + let pos = byte_idx * 8 + (7 - bit_idx); + if pos < array_len { + let sample_piece = sample & 0xFF; + let is_null = (sample_piece as u8) < threshold; + byte |= (!is_null) as u8; + null_count += is_null as usize; + } + sample >>= 8; + } + byte + }) + .collect::>(); + let new_validity = Buffer::from_iter(bytes); + (null_count, Some(new_validity)) + }; + + unsafe { + let new_data = ArrayData::new_unchecked( + data.data_type().clone(), + data.len(), + Some(null_count), + new_validity, + data.offset(), + data.buffers().to_vec(), + data.child_data().into(), + ); + Ok(make_array(new_data)) + } + } + + fn metadata(&self) -> Option> { + self.generator.metadata() + } + + fn data_type(&self) -> &DataType { + self.generator.data_type() + } + + fn element_size_bytes(&self) -> Option { + self.generator.element_size_bytes() + } +} + +pub trait ArrayGeneratorExt { + /// Replaces the validity bitmap of generated arrays, inserting nulls with a given probability + fn with_random_nulls(self, null_probability: f64) -> Box; + /// Replaces the validity bitmap of generated arrays with the inverse of `nulls`, cycling if needed + fn with_nulls(self, nulls: &[bool]) -> Box; + /// Replaces the values of generated arrays with NaN values, cycling if needed + /// + /// Will have no effect if the data type is not a floating point data type + fn with_nans(self, nans: &[bool]) -> Box; + /// Replaces the validity bitmap of generated arrays with `validity`, cycling if needed + fn with_validity(self, nulls: &[bool]) -> Box; + fn with_metadata(self, metadata: HashMap) -> Box; +} + +impl ArrayGeneratorExt for Box { + fn with_random_nulls(self, null_probability: f64) -> Box { + Box::new(NullGenerator { + generator: self, + null_probability, + }) + } + + fn with_nulls(self, nulls: &[bool]) -> Box { + Box::new(CycleNullGenerator { + generator: self, + validity: nulls.iter().map(|v| !*v).collect(), + idx: 0, + }) + } + + fn with_nans(self, nans: &[bool]) -> Box { + Box::new(CycleNanGenerator { + generator: self, + nan_pattern: nans.to_vec(), + idx: 0, + }) + } + + fn with_validity(self, validity: &[bool]) -> Box { + Box::new(CycleNullGenerator { + generator: self, + validity: validity.to_vec(), + idx: 0, + }) + } + + fn with_metadata(self, metadata: HashMap) -> Box { + Box::new(MetadataGenerator { + generator: self, + metadata, + }) + } +} + +pub struct NTimesIter +where + I::Item: Copy, +{ + iter: I, + n: u32, + cur: I::Item, + count: u32, +} + +// Note: if this is used then there is a performance hit as the +// inner loop cannot experience vectorization +// +// TODO: maybe faster to build the vec and then repeat it into +// the destination array? +impl Iterator for NTimesIter +where + I::Item: Copy, +{ + type Item = I::Item; + + fn next(&mut self) -> Option { + if self.count == 0 { + self.count = self.n - 1; + self.cur = self.iter.next()?; + } else { + self.count -= 1; + } + Some(self.cur) + } + + fn size_hint(&self) -> (usize, Option) { + let (lower, upper) = self.iter.size_hint(); + let lower = lower * self.n as usize; + let upper = upper.map(|u| u * self.n as usize); + (lower, upper) + } +} + +pub struct FnGen T> +where + T: Copy + Default, + ArrayType: arrow_array::Array + From>, +{ + data_type: DataType, + generator: F, + array_type: PhantomData, + repeat: u32, + leftover: T, + leftover_count: u32, + element_size_bytes: Option, +} + +impl T> std::fmt::Debug + for FnGen +where + T: Copy + Default, + ArrayType: arrow_array::Array + From>, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FnGen") + .field("data_type", &self.data_type) + .field("array_type", &self.array_type) + .field("repeat", &self.repeat) + .field("leftover_count", &self.leftover_count) + .field("element_size_bytes", &self.element_size_bytes) + .finish() + } +} + +impl T> FnGen +where + T: Copy + Default, + ArrayType: arrow_array::Array + From>, +{ + fn new_known_size( + data_type: DataType, + generator: F, + repeat: u32, + element_size_bytes: ByteCount, + ) -> Self { + Self { + data_type, + generator, + array_type: PhantomData, + repeat, + leftover: T::default(), + leftover_count: 0, + element_size_bytes: Some(element_size_bytes), + } + } + + fn new_unknown_size(data_type: DataType, generator: F, repeat: u32) -> Self { + Self { + data_type, + generator, + array_type: PhantomData, + repeat, + leftover: T::default(), + leftover_count: 0, + element_size_bytes: None, + } + } +} + +impl T> ArrayGenerator + for FnGen +where + T: Copy + Default + Send + Sync, + ArrayType: arrow_array::Array + From> + 'static, + F: Send + Sync, +{ + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let iter = (0..length.0).map(|_| (self.generator)(rng)); + let values = if self.repeat > 1 { + Vec::from_iter( + NTimesIter { + iter, + n: self.repeat, + cur: self.leftover, + count: self.leftover_count, + } + .take(length.0 as usize), + ) + } else { + Vec::from_iter(iter) + }; + self.leftover_count = ((self.leftover_count as u64 + length.0) % self.repeat as u64) as u32; + self.leftover = values.last().copied().unwrap_or(T::default()); + let array = ArrayType::from(values); + // `ArrayType::from` uses the primitive type's default metadata. For + // timezone-aware timestamps this drops the timezone, so restore the + // generator's declared type when it differs. + if array.data_type() == &self.data_type { + return Ok(Arc::new(array)); + } + let data = array + .into_data() + .into_builder() + .data_type(self.data_type.clone()) + .build()?; + Ok(make_array(data)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + self.element_size_bytes + } +} + +#[derive(Copy, Clone, Debug)] +pub struct Seed(pub u64); +pub const DEFAULT_SEED: Seed = Seed(42); + +impl From for Seed { + fn from(n: u64) -> Self { + Self(n) + } +} + +#[derive(Debug)] +pub struct CycleVectorGenerator { + underlying_gen: Box, + dimension: Dimension, + data_type: DataType, +} + +impl CycleVectorGenerator { + pub fn new(underlying_gen: Box, dimension: Dimension) -> Self { + let data_type = DataType::FixedSizeList( + Arc::new(Field::new("item", underlying_gen.data_type().clone(), true)), + dimension.0 as i32, + ); + Self { + underlying_gen, + dimension, + data_type, + } + } +} + +impl ArrayGenerator for CycleVectorGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let values = self + .underlying_gen + .generate(RowCount::from(length.0 * self.dimension.0 as u64), rng)?; + let field = Arc::new(Field::new("item", values.data_type().clone(), true)); + let values = Arc::new(values); + + let array = FixedSizeListArray::try_new(field, self.dimension.0 as i32, values, None)?; + + Ok(Arc::new(array)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + self.underlying_gen + .element_size_bytes() + .map(|byte_count| ByteCount::from(byte_count.0 * self.dimension.0 as u64)) + } +} + +#[derive(Debug)] +pub struct CycleListGenerator { + underlying_gen: Box, + lengths_gen: Box, + data_type: DataType, +} + +impl CycleListGenerator { + pub fn new( + underlying_gen: Box, + min_list_size: Dimension, + max_list_size: Dimension, + ) -> Self { + let data_type = DataType::List(Arc::new(Field::new( + "item", + underlying_gen.data_type().clone(), + true, + ))); + let lengths_dist = Uniform::new(min_list_size.0, max_list_size.0).unwrap(); + let lengths_gen = rand_with_distribution::>(lengths_dist); + Self { + underlying_gen, + lengths_gen, + data_type, + } + } +} + +impl ArrayGenerator for CycleListGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let lengths = self.lengths_gen.generate(length, rng)?; + let lengths = lengths.as_primitive::(); + let total_length = lengths.values().iter().map(|i| *i as u64).sum::(); + let offsets = OffsetBuffer::from_lengths(lengths.values().iter().map(|v| *v as usize)); + let values = self + .underlying_gen + .generate(RowCount::from(total_length), rng)?; + let field = Arc::new(Field::new("item", values.data_type().clone(), true)); + let values = Arc::new(values); + + let array = ListArray::try_new(field, offsets, values, None)?; + + Ok(Arc::new(array)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + None + } +} + +#[derive(Debug, Default)] +pub struct PseudoUuidGenerator {} + +impl ArrayGenerator for PseudoUuidGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + Ok(Arc::new(FixedSizeBinaryArray::try_from_iter( + (0..length.0).map(|_| { + let mut data = vec![0; 16]; + rng.fill_bytes(&mut data); + data + }), + )?)) + } + + fn data_type(&self) -> &DataType { + &DataType::FixedSizeBinary(16) + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(16)) + } +} + +#[derive(Debug, Default)] +pub struct PseudoUuidHexGenerator {} + +impl ArrayGenerator for PseudoUuidHexGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut data = vec![0; 16 * length.0 as usize]; + rng.fill_bytes(&mut data); + let data_hex = hex::encode(data); + + Ok(Arc::new(StringArray::from_iter_values( + (0..length.0 as usize).map(|i| data_hex.get(i * 32..(i + 1) * 32).unwrap()), + ))) + } + + fn data_type(&self) -> &DataType { + &DataType::Utf8 + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(16)) + } +} + +#[derive(Debug, Default)] +pub struct RandomBooleanGenerator {} + +impl ArrayGenerator for RandomBooleanGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let num_bytes = length.0.div_ceil(8); + let mut bytes = vec![0; num_bytes as usize]; + rng.fill_bytes(&mut bytes); + let bytes = BooleanBuffer::new(Buffer::from(bytes), 0, length.0 as usize); + Ok(Arc::new(arrow_array::BooleanArray::new(bytes, None))) + } + + fn data_type(&self) -> &DataType { + &DataType::Boolean + } + + fn element_size_bytes(&self) -> Option { + // We can't say 1/8th of a byte and 1 byte would be a pretty extreme over-count so let's leave + // it at None until someone needs this. Then we can probably special case this (e.g. make a ByteCount::ONE_BIT) + None + } +} + +// Instead of using the "standard distribution" and generating values there are some cases (e.g. f16 / decimal) +// where we just generate random bytes because there is no rand support +pub struct RandomBytesGenerator { + phantom: PhantomData, + data_type: DataType, +} + +impl std::fmt::Debug for RandomBytesGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RandomBytesGenerator") + .field("data_type", &self.data_type) + .finish() + } +} + +impl RandomBytesGenerator { + fn new(data_type: DataType) -> Self { + Self { + phantom: Default::default(), + data_type, + } + } + + fn byte_width() -> Result { + T::DATA_TYPE.primitive_width().ok_or_else(|| ArrowError::InvalidArgumentError(format!("Cannot generate the data type {} with the RandomBytesGenerator because it is not a fixed-width bytes type", T::DATA_TYPE))).map(|val| val as u64) + } +} + +impl ArrayGenerator for RandomBytesGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let num_bytes = length.0 * Self::byte_width()?; + let mut bytes = vec![0; num_bytes as usize]; + rng.fill_bytes(&mut bytes); + let bytes = ScalarBuffer::new(Buffer::from(bytes), 0, length.0 as usize); + Ok(Arc::new( + PrimitiveArray::::new(bytes, None).with_data_type(self.data_type.clone()), + )) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + Self::byte_width().map(ByteCount::from).ok() + } +} + +// This is pretty much the same thing as RandomBinaryGenerator but we can't use that +// because there is no ArrowPrimitiveType for FixedSizeBinary +#[derive(Debug)] +pub struct RandomFixedSizeBinaryGenerator { + data_type: DataType, + size: i32, +} + +impl RandomFixedSizeBinaryGenerator { + fn new(size: i32) -> Self { + Self { + size, + data_type: DataType::FixedSizeBinary(size), + } + } +} + +impl ArrayGenerator for RandomFixedSizeBinaryGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let num_bytes = length.0 * self.size as u64; + let mut bytes = vec![0; num_bytes as usize]; + rng.fill_bytes(&mut bytes); + Ok(Arc::new(FixedSizeBinaryArray::new( + self.size, + Buffer::from(bytes), + None, + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(self.size as u64)) + } +} + +#[derive(Debug)] +pub struct RandomIntervalGenerator { + unit: IntervalUnit, + data_type: DataType, +} + +impl RandomIntervalGenerator { + pub fn new(unit: IntervalUnit) -> Self { + Self { + unit, + data_type: DataType::Interval(unit), + } + } +} + +impl ArrayGenerator for RandomIntervalGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + match self.unit { + IntervalUnit::YearMonth => { + let months = (0..length.0) + .map(|_| rng.random::()) + .collect::>(); + Ok(Arc::new(arrow_array::IntervalYearMonthArray::from(months))) + } + IntervalUnit::MonthDayNano => { + let day_time_array = (0..length.0) + .map(|_| IntervalMonthDayNano::new(rng.random(), rng.random(), rng.random())) + .collect::>(); + Ok(Arc::new(arrow_array::IntervalMonthDayNanoArray::from( + day_time_array, + ))) + } + IntervalUnit::DayTime => { + let day_time_array = (0..length.0) + .map(|_| IntervalDayTime::new(rng.random(), rng.random())) + .collect::>(); + Ok(Arc::new(arrow_array::IntervalDayTimeArray::from( + day_time_array, + ))) + } + } + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(12)) + } +} +#[derive(Debug)] +pub struct RandomBinaryGenerator { + bytes_per_element: ByteCount, + scale_to_utf8: bool, + is_large: bool, + data_type: DataType, +} + +impl RandomBinaryGenerator { + pub fn new(bytes_per_element: ByteCount, scale_to_utf8: bool, is_large: bool) -> Self { + Self { + bytes_per_element, + scale_to_utf8, + is_large, + data_type: match (scale_to_utf8, is_large) { + (false, false) => DataType::Binary, + (false, true) => DataType::LargeBinary, + (true, false) => DataType::Utf8, + (true, true) => DataType::LargeUtf8, + }, + } + } +} + +impl ArrayGenerator for RandomBinaryGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut bytes = vec![0; (self.bytes_per_element.0 * length.0) as usize]; + rng.fill_bytes(&mut bytes); + if self.scale_to_utf8 { + // This doesn't give us the full UTF-8 range and it isn't statistically correct but + // it's fast and probably good enough for most cases + bytes = bytes.into_iter().map(|val| (val % 95) + 32).collect(); + } + let bytes = Buffer::from(bytes); + if self.is_large { + let offsets = OffsetBuffer::from_lengths(iter::repeat_n( + self.bytes_per_element.0 as usize, + length.0 as usize, + )); + if self.scale_to_utf8 { + // This is safe because we are only using printable characters + unsafe { + Ok(Arc::new(arrow_array::LargeStringArray::new_unchecked( + offsets, bytes, None, + ))) + } + } else { + unsafe { + Ok(Arc::new(arrow_array::LargeBinaryArray::new_unchecked( + offsets, bytes, None, + ))) + } + } + } else { + let offsets = OffsetBuffer::from_lengths(iter::repeat_n( + self.bytes_per_element.0 as usize, + length.0 as usize, + )); + if self.scale_to_utf8 { + // This is safe because we are only using printable characters + unsafe { + Ok(Arc::new(arrow_array::StringArray::new_unchecked( + offsets, bytes, None, + ))) + } + } else { + unsafe { + Ok(Arc::new(arrow_array::BinaryArray::new_unchecked( + offsets, bytes, None, + ))) + } + } + } + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + // Not exactly correct since there are N + 1 4-byte offsets and this only counts N + Some(ByteCount::from( + self.bytes_per_element.0 + std::mem::size_of::() as u64, + )) + } +} + +/// Generate a sequence of strings with a prefix and a counter +/// +/// For example, if the prefix is "user_" the strings will be "user_0", "user_1", ... +#[derive(Debug)] +pub struct PrefixPlusCounterGenerator { + prefix: String, + is_large: bool, + data_type: DataType, + current_counter: u64, +} + +impl PrefixPlusCounterGenerator { + pub fn new(prefix: String, is_large: bool) -> Self { + Self { + prefix, + is_large, + data_type: if is_large { + DataType::LargeUtf8 + } else { + DataType::Utf8 + }, + current_counter: 0, + } + } + + fn generate_values( + &self, + start: u64, + num_values: u64, + ) -> Result, ArrowError> { + let max_counter = start + num_values; + let max_digits_per_counter = (max_counter as f64).log10().ceil() as u64; + let max_bytes_per_str = max_digits_per_counter + self.prefix.len() as u64; + let max_bytes = max_bytes_per_str * num_values; + let mut builder = + GenericStringBuilder::::with_capacity(num_values as usize, max_bytes as usize); + let mut word = String::with_capacity(max_bytes_per_str as usize); + word.push_str(&self.prefix); + for i in 0..num_values { + let counter = start + i; + word.truncate(self.prefix.len()); + word.push_str(&counter.to_string()); + builder.append_value(&word); + } + Ok(Arc::new(builder.finish())) + } +} + +impl ArrayGenerator for PrefixPlusCounterGenerator { + fn generate( + &mut self, + length: RowCount, + _rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let start = self.current_counter; + self.current_counter += length.0; + if self.is_large { + self.generate_values::(start, length.0) + } else { + self.generate_values::(start, length.0) + } + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + // It's not consistent + None + } +} + +/// Generate a sequence of binary strings with a prefix and a counter +/// +/// The counter will be encoded (little-endian) as a u8, u16, u32, or u64 and added to the prefix +/// As long as more than 256 values are generated then the resulting array will have +/// variable width +#[derive(Debug)] +pub struct BinaryPrefixPlusCounterGenerator { + prefix: Arc<[u8]>, + is_large: bool, + data_type: DataType, + current_counter: u64, +} + +impl BinaryPrefixPlusCounterGenerator { + pub fn new(prefix: Arc<[u8]>, is_large: bool) -> Self { + Self { + prefix, + is_large, + data_type: if is_large { + DataType::LargeBinary + } else { + DataType::Binary + }, + current_counter: 0, + } + } + + fn generate_values( + &self, + start: u64, + num_values: u64, + ) -> Result, ArrowError> { + let max_bytes = (self.prefix.len() + std::mem::size_of::()) * num_values as usize; + let mut builder = GenericBinaryBuilder::::with_capacity(num_values as usize, max_bytes); + let mut word = Vec::with_capacity(self.prefix.len() + std::mem::size_of::()); + word.extend_from_slice(&self.prefix); + for i in 0..num_values { + let counter = start + i; + word.truncate(self.prefix.len()); + if counter < u8::MAX as u64 { + word.push(counter as u8); + } else if counter < u16::MAX as u64 { + word.extend_from_slice(&(counter as u16).to_le_bytes()); + } else if counter < u32::MAX as u64 { + word.extend_from_slice(&(counter as u32).to_le_bytes()); + } else { + word.extend_from_slice(&counter.to_le_bytes()); + } + builder.append_value(&word); + } + Ok(Arc::new(builder.finish())) + } +} + +impl ArrayGenerator for BinaryPrefixPlusCounterGenerator { + fn generate( + &mut self, + length: RowCount, + _rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let start = self.current_counter; + self.current_counter += length.0; + if self.is_large { + self.generate_values::(start, length.0) + } else { + self.generate_values::(start, length.0) + } + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + // It's not consistent + None + } +} + +// Common English stop words placed at the front to be sampled more frequently. +const STOP_WORDS: &[&str] = &[ + "a", "an", "and", "are", "as", "at", "be", "but", "by", "for", "if", "in", "into", "is", "it", + "no", "not", "of", "on", "or", "such", "that", "the", "their", "then", "there", "these", + "they", "this", "to", "was", "will", "with", +]; + +const ENGLISH_WORDS: &[&str] = &[ + "ability", + "able", + "about", + "above", + "accept", + "access", + "account", + "across", + "action", + "active", + "activity", + "actual", + "address", + "adjust", + "admin", + "advance", + "agent", + "align", + "allow", + "amount", + "analysis", + "answer", + "application", + "archive", + "array", + "asset", + "async", + "attribute", + "available", + "balance", + "batch", + "binary", + "bitmap", + "block", + "branch", + "buffer", + "build", + "cache", + "capacity", + "catalog", + "change", + "chunk", + "client", + "cluster", + "column", + "commit", + "common", + "compare", + "compile", + "compute", + "condition", + "config", + "connect", + "content", + "context", + "control", + "convert", + "copy", + "core", + "count", + "create", + "current", + "cursor", + "data", + "dataset", + "decode", + "default", + "delete", + "delta", + "depend", + "derive", + "design", + "detail", + "detect", + "device", + "direct", + "display", + "document", + "domain", + "drive", + "dynamic", + "encode", + "engine", + "error", + "event", + "example", + "execute", + "expand", + "expect", + "export", + "extend", + "feature", + "field", + "filter", + "final", + "finish", + "format", + "fragment", + "future", + "generate", + "global", + "group", + "handle", + "header", + "index", + "input", + "insert", + "inspect", + "instance", + "integer", + "internal", + "item", + "join", + "kernel", + "large", + "layer", + "layout", + "length", + "level", + "limit", + "linear", + "local", + "logical", + "lookup", + "manage", + "manifest", + "memory", + "merge", + "metric", + "model", + "module", + "namespace", + "native", + "node", + "normal", + "number", + "object", + "offset", + "option", + "output", + "package", + "page", + "parallel", + "parse", + "partition", + "pattern", + "physical", + "plan", + "policy", + "prefix", + "prepare", + "primary", + "process", + "profile", + "project", + "property", + "query", + "range", + "reader", + "record", + "region", + "registry", + "request", + "resolve", + "resource", + "result", + "return", + "row", + "runtime", + "scalar", + "scan", + "schema", + "search", + "segment", + "select", + "session", + "setting", + "source", + "stable", + "stage", + "state", + "static", + "storage", + "stream", + "string", + "struct", + "table", + "target", + "task", + "thread", + "token", + "trace", + "transform", + "type", + "update", + "upload", + "value", + "vector", + "version", + "view", + "write", + "writer", +]; + +/// Word list with stop words at the front for Zipf sampling, computed once. +static SENTENCE_WORDS: LazyLock> = LazyLock::new(|| { + let mut words = Vec::with_capacity(STOP_WORDS.len() + ENGLISH_WORDS.len()); + words.extend(STOP_WORDS.iter().copied()); + words.extend(ENGLISH_WORDS.iter().copied()); + words +}); + +struct RandomSentenceGenerator { + min_words: usize, + max_words: usize, + /// Zipf distribution for word selection (favors lower indices) + zipf: Zipf, + is_large: bool, +} + +impl std::fmt::Debug for RandomSentenceGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RandomSentenceGenerator") + .field("min_words", &self.min_words) + .field("max_words", &self.max_words) + .field("num_words", &SENTENCE_WORDS.len()) + .field("is_large", &self.is_large) + .finish() + } +} + +impl RandomSentenceGenerator { + pub fn new(min_words: usize, max_words: usize, is_large: bool) -> Self { + // Zipf distribution with exponent ~1.0 approximates natural language + let zipf = Zipf::new(SENTENCE_WORDS.len() as f64, 1.0).unwrap(); + + Self { + min_words, + max_words, + zipf, + is_large, + } + } +} + +impl ArrayGenerator for RandomSentenceGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut values = Vec::with_capacity(length.0 as usize); + + for _ in 0..length.0 { + let num_words = rng.random_range(self.min_words..=self.max_words); + let sentence: String = (0..num_words) + .map(|_| { + // Zipf returns 1-indexed values, subtract 1 for 0-indexed array + let idx = rng.sample(self.zipf) as usize - 1; + SENTENCE_WORDS[idx] + }) + .collect::>() + .join(" "); + values.push(sentence); + } + + if self.is_large { + Ok(Arc::new(LargeStringArray::from(values))) + } else { + Ok(Arc::new(StringArray::from(values))) + } + } + + fn data_type(&self) -> &DataType { + if self.is_large { + &DataType::LargeUtf8 + } else { + &DataType::Utf8 + } + } + + fn element_size_bytes(&self) -> Option { + // Estimate average word length as 5, plus space + // See https://arxiv.org/pdf/1208.6109 + let avg_word_length = 6; + let avg_words = (self.min_words + self.max_words) / 2; + Some(ByteCount::from((avg_word_length * avg_words) as u64)) + } +} + +#[derive(Debug)] +struct RandomWordGenerator { + words: &'static [&'static str], + is_large: bool, +} + +impl RandomWordGenerator { + pub fn new(is_large: bool) -> Self { + let words = ENGLISH_WORDS; + Self { words, is_large } + } +} + +impl ArrayGenerator for RandomWordGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut values = Vec::with_capacity(length.0 as usize); + + for _ in 0..length.0 { + let word = self.words[rng.random_range(0..self.words.len())]; + values.push(word.to_string()); + } + + if self.is_large { + Ok(Arc::new(LargeStringArray::from(values))) + } else { + Ok(Arc::new(StringArray::from(values))) + } + } + + fn data_type(&self) -> &DataType { + if self.is_large { + &DataType::LargeUtf8 + } else { + &DataType::Utf8 + } + } + + fn element_size_bytes(&self) -> Option { + // Average English word length is ~5 characters + Some(ByteCount::from(5)) + } +} + +#[derive(Debug)] +pub struct VariableRandomBinaryGenerator { + lengths_gen: Box, + data_type: DataType, +} + +impl VariableRandomBinaryGenerator { + pub fn new(min_bytes_per_element: ByteCount, max_bytes_per_element: ByteCount) -> Self { + let lengths_dist = Uniform::new_inclusive( + min_bytes_per_element.0 as i32, + max_bytes_per_element.0 as i32, + ) + .unwrap(); + let lengths_gen = rand_with_distribution::>(lengths_dist); + + Self { + lengths_gen, + data_type: DataType::Binary, + } + } +} + +impl ArrayGenerator for VariableRandomBinaryGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let lengths = self.lengths_gen.generate(length, rng)?; + let lengths = lengths.as_primitive::(); + let total_length = lengths.values().iter().map(|i| *i as usize).sum::(); + let offsets = OffsetBuffer::from_lengths(lengths.values().iter().map(|v| *v as usize)); + let mut bytes = vec![0; total_length]; + rng.fill_bytes(&mut bytes); + let bytes = Buffer::from(bytes); + Ok(Arc::new(BinaryArray::try_new(offsets, bytes, None)?)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + None + } +} + +pub struct CycleBinaryGenerator { + values: Vec, + lengths: Vec, + data_type: DataType, + array_type: PhantomData, + width: Option, + idx: usize, +} + +impl std::fmt::Debug for CycleBinaryGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CycleBinaryGenerator") + .field("values", &self.values) + .field("lengths", &self.lengths) + .field("data_type", &self.data_type) + .field("width", &self.width) + .field("idx", &self.idx) + .finish() + } +} + +impl CycleBinaryGenerator { + pub fn from_strings(values: &[&str]) -> Self { + if values.is_empty() { + panic!("Attempt to create a cycle generator with no values"); + } + let lengths = values.iter().map(|s| s.len()).collect::>(); + let typical_length = lengths[0]; + let width = if lengths.iter().all(|item| *item == typical_length) { + Some(ByteCount::from( + typical_length as u64 + std::mem::size_of::() as u64, + )) + } else { + None + }; + let values = values + .iter() + .flat_map(|s| s.as_bytes().iter().copied()) + .collect::>(); + Self { + values, + lengths, + data_type: T::DATA_TYPE, + array_type: PhantomData, + width, + idx: 0, + } + } +} + +impl ArrayGenerator for CycleBinaryGenerator { + fn generate( + &mut self, + length: RowCount, + _: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let lengths = self + .lengths + .iter() + .copied() + .cycle() + .skip(self.idx) + .take(length.0 as usize); + let num_bytes = lengths.clone().sum(); + let byte_offset = self.lengths[0..self.idx].iter().sum(); + let bytes = self + .values + .iter() + .cycle() + .skip(byte_offset) + .copied() + .take(num_bytes) + .collect::>(); + let bytes = Buffer::from(bytes); + let offsets = OffsetBuffer::from_lengths(lengths); + self.idx = (self.idx + length.0 as usize) % self.lengths.len(); + Ok(Arc::new(arrow_array::GenericByteArray::::new( + offsets, bytes, None, + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + self.width + } +} + +pub struct FixedBinaryGenerator { + value: Vec, + data_type: DataType, + array_type: PhantomData, +} + +impl std::fmt::Debug for FixedBinaryGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FixedBinaryGenerator") + .field("value", &self.value) + .field("data_type", &self.data_type) + .finish() + } +} + +impl FixedBinaryGenerator { + pub fn new(value: Vec) -> Self { + Self { + value, + data_type: T::DATA_TYPE, + array_type: PhantomData, + } + } +} + +impl ArrayGenerator for FixedBinaryGenerator { + fn generate( + &mut self, + length: RowCount, + _: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let bytes = Buffer::from(Vec::from_iter( + self.value + .iter() + .cycle() + .take((length.0 * self.value.len() as u64) as usize) + .copied(), + )); + let offsets = + OffsetBuffer::from_lengths(iter::repeat_n(self.value.len(), length.0 as usize)); + Ok(Arc::new(arrow_array::GenericByteArray::::new( + offsets, bytes, None, + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + // Not exactly correct since there are N + 1 4-byte offsets and this only counts N + Some(ByteCount::from( + self.value.len() as u64 + std::mem::size_of::() as u64, + )) + } +} + +pub struct DictionaryGenerator { + generator: Box, + data_type: DataType, + key_type: PhantomData, + key_width: u64, +} + +impl std::fmt::Debug for DictionaryGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DictionaryGenerator") + .field("generator", &self.generator) + .field("data_type", &self.data_type) + .field("key_width", &self.key_width) + .finish() + } +} + +impl DictionaryGenerator { + fn new(generator: Box) -> Self { + let key_type = Box::new(K::DATA_TYPE); + let key_width = key_type + .primitive_width() + .expect("dictionary key types should have a known width") + as u64; + let val_type = Box::new(generator.data_type().clone()); + let dict_type = DataType::Dictionary(key_type, val_type); + Self { + generator, + data_type: dict_type, + key_type: PhantomData, + key_width, + } + } +} + +impl ArrayGenerator for DictionaryGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let underlying = self.generator.generate(length, rng)?; + arrow_cast::cast::cast(&underlying, &self.data_type) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + self.generator + .element_size_bytes() + .map(|size_bytes| ByteCount::from(size_bytes.0 + self.key_width)) + } +} + +/// Generator that produces low-cardinality data by generating a fixed set of +/// unique values and then randomly selecting from them. +struct LowCardinalityGenerator { + inner: Box, + cardinality: usize, + /// Cached unique values, generated on first call + unique_values: Option>, +} + +impl std::fmt::Debug for LowCardinalityGenerator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("LowCardinalityGenerator") + .field("inner", &self.inner) + .field("cardinality", &self.cardinality) + .field("initialized", &self.unique_values.is_some()) + .finish() + } +} + +impl LowCardinalityGenerator { + fn new(inner: Box, cardinality: usize) -> Self { + Self { + inner, + cardinality, + unique_values: None, + } + } +} + +impl ArrayGenerator for LowCardinalityGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + // Generate unique values on first call + if self.unique_values.is_none() { + self.unique_values = Some( + self.inner + .generate(RowCount::from(self.cardinality as u64), rng)?, + ); + } + + let unique_values = self.unique_values.as_ref().unwrap(); + + // Generate random indices into the unique values + let indices: Vec = (0..length.0) + .map(|_| rng.random_range(0..self.cardinality)) + .collect(); + + // Use arrow's take to select values + let indices_array = + arrow_array::UInt32Array::from(indices.iter().map(|&i| i as u32).collect::>()); + arrow::compute::take(unique_values.as_ref(), &indices_array, None) + .map(|arr| arr as Arc) + } + + fn data_type(&self) -> &DataType { + self.inner.data_type() + } + + fn element_size_bytes(&self) -> Option { + self.inner.element_size_bytes() + } +} + +#[derive(Debug)] +struct RandomListGenerator { + field: Arc, + child_field: Arc, + items_gen: Box, + lengths_gen: Box, + is_large: bool, +} + +impl RandomListGenerator { + // Creates a list generator that generates random lists with lengths between 0 and 10 (inclusive) + fn new(items_gen: Box, is_large: bool) -> Self { + let child_field = Arc::new(Field::new("item", items_gen.data_type().clone(), true)); + let list_type = if is_large { + DataType::LargeList(child_field.clone()) + } else { + DataType::List(child_field.clone()) + }; + let field = Field::new("", list_type, true); + let lengths_gen = if is_large { + let lengths_dist = Uniform::new_inclusive(0, 10).unwrap(); + rand_with_distribution::>(lengths_dist) + } else { + let lengths_dist = Uniform::new_inclusive(0, 10).unwrap(); + rand_with_distribution::>(lengths_dist) + }; + Self { + field: Arc::new(field), + child_field, + items_gen, + lengths_gen, + is_large, + } + } +} + +impl ArrayGenerator for RandomListGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let lengths = self.lengths_gen.generate(length, rng)?; + if self.is_large { + let lengths = lengths.as_primitive::(); + let total_length = lengths.values().iter().sum::() as u64; + let offsets = OffsetBuffer::from_lengths(lengths.values().iter().map(|v| *v as usize)); + let items = self.items_gen.generate(RowCount::from(total_length), rng)?; + Ok(Arc::new(LargeListArray::try_new( + self.child_field.clone(), + offsets, + items, + None, + )?)) + } else { + let lengths = lengths.as_primitive::(); + let total_length = lengths.values().iter().sum::() as u64; + let offsets = OffsetBuffer::from_lengths(lengths.values().iter().map(|v| *v as usize)); + let items = self.items_gen.generate(RowCount::from(total_length), rng)?; + Ok(Arc::new(ListArray::try_new( + self.child_field.clone(), + offsets, + items, + None, + )?)) + } + } + + fn data_type(&self) -> &DataType { + self.field.data_type() + } + + fn element_size_bytes(&self) -> Option { + None + } +} + +/// Generates random map arrays where each map has 0-4 entries. +#[derive(Debug)] +struct RandomMapGenerator { + field: Arc, + entries_field: Arc, + keys_gen: Box, + values_gen: Box, + lengths_gen: Box, +} + +impl RandomMapGenerator { + fn new(keys_gen: Box, values_gen: Box) -> Self { + let entries_fields = Fields::from(vec![ + Field::new("keys", keys_gen.data_type().clone(), false), + Field::new("values", values_gen.data_type().clone(), true), + ]); + let entries_field = Arc::new(Field::new( + "entries", + DataType::Struct(entries_fields), + false, + )); + let map_type = DataType::Map(entries_field.clone(), false); + let field = Arc::new(Field::new("", map_type, true)); + let lengths_dist = Uniform::new_inclusive(0_i32, 4).unwrap(); + let lengths_gen = rand_with_distribution::>(lengths_dist); + + Self { + field, + entries_field, + keys_gen, + values_gen, + lengths_gen, + } + } +} + +impl ArrayGenerator for RandomMapGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let lengths = self.lengths_gen.generate(length, rng)?; + let lengths = lengths.as_primitive::(); + let total_entries = lengths.values().iter().sum::() as u64; + let offsets = OffsetBuffer::from_lengths(lengths.values().iter().map(|v| *v as usize)); + + let keys = self.keys_gen.generate(RowCount::from(total_entries), rng)?; + let values = self + .values_gen + .generate(RowCount::from(total_entries), rng)?; + + let entries = StructArray::new( + Fields::from(vec![ + Field::new("keys", keys.data_type().clone(), false), + Field::new("values", values.data_type().clone(), true), + ]), + vec![keys, values], + None, + ); + + Ok(Arc::new(MapArray::try_new( + self.entries_field.clone(), + offsets, + entries, + None, + false, + )?)) + } + + fn data_type(&self) -> &DataType { + self.field.data_type() + } + + fn element_size_bytes(&self) -> Option { + None + } +} + +#[derive(Debug)] +struct NullArrayGenerator {} + +impl ArrayGenerator for NullArrayGenerator { + fn generate( + &mut self, + length: RowCount, + _: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + Ok(Arc::new(NullArray::new(length.0 as usize))) + } + + fn data_type(&self) -> &DataType { + &DataType::Null + } + + fn element_size_bytes(&self) -> Option { + None + } +} + +/// Generates 2 dimensional vectors along the unit circle, with a configurable number of steps per circle. +#[derive(Debug)] +struct RadialStepGenerator { + num_steps_per_circle: u32, + data_field: Arc, + data_type: DataType, + current_step: u32, +} + +impl RadialStepGenerator { + fn new(num_steps_per_circle: u32) -> Self { + let data_field = Arc::new(Field::new("item", DataType::Float32, false)); + let data_type = DataType::FixedSizeList(data_field.clone(), 2); + Self { + num_steps_per_circle, + data_field, + data_type, + current_step: 0, + } + } +} + +impl ArrayGenerator for RadialStepGenerator { + fn generate( + &mut self, + length: RowCount, + _rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut values_builder = Float32Builder::with_capacity(length.0 as usize * 2); + for _ in 0..length.0 { + let angle = (self.current_step as f32) / (self.num_steps_per_circle as f32) + * 2.0 + * std::f32::consts::PI; + values_builder.append_value(angle.cos()); + values_builder.append_value(angle.sin()); + self.current_step = (self.current_step + 1) % self.num_steps_per_circle; + } + let values = values_builder.finish(); + let vectors = + FixedSizeListArray::try_new(self.data_field.clone(), 2, Arc::new(values), None)?; + Ok(Arc::new(vectors)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(8)) + } +} + +/// Cycles through a set of centroids, adding noise to each point +#[derive(Debug)] +struct JitterCentroidsGenerator { + centroids: Float32Array, + dimension: u32, + noise_level: f32, + data_type: DataType, + data_field: Arc, + + offset: usize, +} + +impl JitterCentroidsGenerator { + fn try_new(centroids: Arc, noise_level: f32) -> Result { + let DataType::FixedSizeList(values_field, dimension) = centroids.data_type() else { + return Err(ArrowError::InvalidArgumentError( + "Centroids must be a FixedSizeList".to_string(), + )); + }; + if values_field.data_type() != &DataType::Float32 { + return Err(ArrowError::InvalidArgumentError( + "Centroids values must be a Float32".to_string(), + )); + } + let data_type = DataType::FixedSizeList(values_field.clone(), *dimension); + Ok(Self { + centroids: centroids + .as_fixed_size_list() + .values() + .as_primitive::() + .clone(), + dimension: *dimension as u32, + noise_level, + data_type, + data_field: values_field.clone(), + offset: 0, + }) + } +} + +impl ArrayGenerator for JitterCentroidsGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + let mut values_builder = + Float32Builder::with_capacity(length.0 as usize * self.dimension as usize); + for _ in 0..length.0 { + // Generate random N dimensional point + let mut noise = (0..self.dimension as usize) + .map(|_| rng.random::()) + .collect::>(); + // Scale point to noise_level length + let scale = self.noise_level / noise.iter().map(|v| v * v).sum::().sqrt(); + noise.iter_mut().for_each(|v| *v *= scale); + + // Add noise to centroid and store in values + for (i, noise) in noise.into_iter().enumerate() { + let centroid_val = self.centroids.value(self.offset + i); + let jittered_val = centroid_val + noise; + values_builder.append_value(jittered_val); + } + // Advance to next centroid + self.offset = (self.offset + self.dimension as usize) % self.centroids.len(); + } + let values = values_builder.finish(); + let vectors = FixedSizeListArray::try_new( + self.data_field.clone(), + self.dimension as i32, + Arc::new(values), + None, + )?; + Ok(Arc::new(vectors)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + Some(ByteCount::from(self.dimension as u64 * 4)) + } +} +#[derive(Debug)] +struct RandomStructGenerator { + fields: Fields, + data_type: DataType, + child_gens: Vec>, +} + +impl RandomStructGenerator { + fn new(fields: Fields, child_gens: Vec>) -> Self { + let data_type = DataType::Struct(fields.clone()); + Self { + fields, + data_type, + child_gens, + } + } +} + +impl ArrayGenerator for RandomStructGenerator { + fn generate( + &mut self, + length: RowCount, + rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> Result, ArrowError> { + if self.child_gens.is_empty() { + // Have to create empty struct arrays specially to ensure they have the correct + // row count + let struct_arr = StructArray::new_empty_fields(length.0 as usize, None); + return Ok(Arc::new(struct_arr)); + } + let child_arrays = self + .child_gens + .iter_mut() + .map(|genn| genn.generate(length, rng)) + .collect::, ArrowError>>()?; + let struct_arr = StructArray::new(self.fields.clone(), child_arrays, None); + Ok(Arc::new(struct_arr)) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn element_size_bytes(&self) -> Option { + let mut sum = 0; + for child_gen in &self.child_gens { + sum += child_gen.element_size_bytes()?.0; + } + Some(ByteCount::from(sum)) + } +} + +/// A RecordBatchReader that generates batches of the given size from the given array generators +pub struct FixedSizeBatchGenerator { + rng: rand_xoshiro::Xoshiro256PlusPlus, + generators: Vec>, + batch_size: RowCount, + num_batches: BatchCount, + schema: SchemaRef, +} + +impl FixedSizeBatchGenerator { + fn new( + generators: Vec<(Option, Box)>, + batch_size: RowCount, + num_batches: BatchCount, + seed: Option, + default_null_probability: Option, + ) -> Self { + let mut fields = Vec::with_capacity(generators.len()); + for (field_index, field_gen) in generators.iter().enumerate() { + let (name, genn) = field_gen; + let default_name = format!("field_{}", field_index); + let name = name.clone().unwrap_or(default_name); + let mut field = Field::new(name, genn.data_type().clone(), true); + if let Some(metadata) = genn.metadata() { + field = field.with_metadata(metadata); + } + fields.push(field); + } + let mut generators = generators + .into_iter() + .map(|(_, genn)| genn) + .collect::>(); + if let Some(null_probability) = default_null_probability { + generators = generators + .into_iter() + .map(|genn| genn.with_random_nulls(null_probability)) + .collect(); + } + let schema = Arc::new(Schema::new(fields)); + Self { + rng: rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64( + seed.map(|s| s.0).unwrap_or(DEFAULT_SEED.0), + ), + generators, + batch_size, + num_batches, + schema, + } + } + + fn gen_next(&mut self) -> Result { + let mut arrays = Vec::with_capacity(self.generators.len()); + for genn in self.generators.iter_mut() { + let arr = genn.generate(self.batch_size, &mut self.rng)?; + arrays.push(arr); + } + self.num_batches.0 -= 1; + Ok(RecordBatch::try_new_with_options( + self.schema.clone(), + arrays, + &RecordBatchOptions::new().with_row_count(Some(self.batch_size.0 as usize)), + ) + .unwrap()) + } +} + +impl Iterator for FixedSizeBatchGenerator { + type Item = Result; + + fn next(&mut self) -> Option { + if self.num_batches.0 == 0 { + return None; + } + Some(self.gen_next()) + } +} + +impl RecordBatchReader for FixedSizeBatchGenerator { + fn schema(&self) -> SchemaRef { + self.schema.clone() + } +} + +/// A builder to create a record batch reader with generated data +/// +/// This type is meant to be used in a fluent builder style to define the schema and generators +/// for a record batch reader. +#[derive(Default)] +pub struct BatchGeneratorBuilder { + generators: Vec<(Option, Box)>, + default_null_probability: Option, + seed: Option, +} + +pub enum RoundingBehavior { + ExactOrErr, + RoundUp, + RoundDown, +} + +impl BatchGeneratorBuilder { + /// Create a new BatchGeneratorBuilder with a default random seed + pub fn new() -> Self { + Default::default() + } + + /// Create a new BatchGeneratorBuilder with the given seed + pub fn new_with_seed(seed: Seed) -> Self { + Self { + seed: Some(seed), + ..Default::default() + } + } + + /// Adds a new column to the generator + /// + /// See [`crate::generator::array`] for methods to create generators + pub fn col(mut self, name: impl Into, genn: Box) -> Self { + self.generators.push((Some(name.into()), genn)); + self + } + + /// Adds a new column to the generator with a generated unique name + /// + /// See [`crate::generator::array`] for methods to create generators + pub fn anon_col(mut self, genn: Box) -> Self { + self.generators.push((None, genn)); + self + } + + pub fn into_batch_rows(self, batch_size: RowCount) -> Result { + let mut reader = self.into_reader_rows(batch_size, BatchCount::from(1)); + reader + .next() + .expect("Asked for 1 batch but reader was empty") + } + + pub fn into_batch_bytes( + self, + batch_size: ByteCount, + rounding: RoundingBehavior, + ) -> Result { + let mut reader = self.into_reader_bytes(batch_size, BatchCount::from(1), rounding)?; + reader + .next() + .expect("Asked for 1 batch but reader was empty") + } + + /// Create a RecordBatchReader that generates batches of the given size (in rows) + pub fn into_reader_rows( + self, + batch_size: RowCount, + num_batches: BatchCount, + ) -> impl RecordBatchReader { + FixedSizeBatchGenerator::new( + self.generators, + batch_size, + num_batches, + self.seed, + self.default_null_probability, + ) + } + + pub fn into_reader_stream( + self, + batch_size: RowCount, + num_batches: BatchCount, + ) -> ( + BoxStream<'static, Result>, + Arc, + ) { + // TODO: this is pretty lazy and could be optimized + let reader = self.into_reader_rows(batch_size, num_batches); + let schema = reader.schema(); + let batches = reader.collect::>(); + (futures::stream::iter(batches).boxed(), schema) + } + + /// Create a RecordBatchReader that generates batches of the given size (in bytes) + pub fn into_reader_bytes( + self, + batch_size_bytes: ByteCount, + num_batches: BatchCount, + rounding: RoundingBehavior, + ) -> Result { + let bytes_per_row = self + .generators + .iter() + .map(|genn| genn.1.element_size_bytes().map(|byte_count| byte_count.0).ok_or( + ArrowError::NotYetImplemented("The function into_reader_bytes currently requires each array generator to have a fixed element size".to_string()) + ) + ) + .sum::>()?; + let mut num_rows = RowCount::from(batch_size_bytes.0 / bytes_per_row); + if !batch_size_bytes.0.is_multiple_of(bytes_per_row) { + match rounding { + RoundingBehavior::ExactOrErr => { + return Err(ArrowError::NotYetImplemented(format!( + "Exact rounding requested but not possible. Batch size requested {}, row size: {}", + batch_size_bytes.0, bytes_per_row + ))); + } + RoundingBehavior::RoundUp => { + num_rows = RowCount::from(num_rows.0 + 1); + } + RoundingBehavior::RoundDown => (), + } + } + Ok(self.into_reader_rows(num_rows, num_batches)) + } + + /// Set the seed for the generator + pub fn with_seed(mut self, seed: Seed) -> Self { + self.seed = Some(seed); + self + } + + /// Adds nulls (with the given probability) to all columns + pub fn with_random_nulls(&mut self, default_null_probability: f64) { + self.default_null_probability = Some(default_null_probability); + } +} + +/// Factory for creating a single random array +pub struct ArrayGeneratorBuilder { + generator: Box, + seed: Option, +} + +impl ArrayGeneratorBuilder { + fn new(generator: Box) -> Self { + Self { + generator, + seed: None, + } + } + + /// Use the given seed for the generator + pub fn with_seed(mut self, seed: Seed) -> Self { + self.seed = Some(seed); + self + } + + /// Generate a single array with the given length + pub fn into_array_rows( + mut self, + length: RowCount, + ) -> Result, ArrowError> { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64( + self.seed.map(|s| s.0).unwrap_or(DEFAULT_SEED.0), + ); + self.generator.generate(length, &mut rng) + } +} + +const MS_PER_DAY: i64 = 86400000; + +pub mod array { + + use arrow::datatypes::{Int8Type, Int16Type, Int64Type}; + use arrow_array::types::{ + Decimal128Type, Decimal256Type, DurationMicrosecondType, DurationMillisecondType, + DurationNanosecondType, DurationSecondType, Float16Type, Float32Type, Float64Type, + UInt8Type, UInt16Type, UInt32Type, UInt64Type, + }; + use arrow_array::{ + ArrowNativeTypeOp, BooleanArray, Date32Array, Date64Array, Time32MillisecondArray, + Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray, + TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray, + TimestampSecondArray, + }; + use arrow_schema::{IntervalUnit, TimeUnit}; + use chrono::Utc; + use rand::prelude::Distribution; + + use super::*; + + /// Create a generator of vectors by continuously calling the given generator + /// + /// For example, given a step generator and a dimension of 3 this will generate vectors like + /// [0, 1, 2], [3, 4, 5], [6, 7, 8], ... + pub fn cycle_vec( + generator: Box, + dimension: Dimension, + ) -> Box { + Box::new(CycleVectorGenerator::new(generator, dimension)) + } + + /// Create a generator of list vectors by continuously calling the given generator + /// + /// The lists will have lengths uniformly distributed between `min_list_size` (inclusive) and + /// `max_list_size` (exclusive). + pub fn cycle_vec_var( + generator: Box, + min_list_size: Dimension, + max_list_size: Dimension, + ) -> Box { + Box::new(CycleListGenerator::new( + generator, + min_list_size, + max_list_size, + )) + } + + /// Create a generator of vectors around unit circle + /// + /// Vectors will be equally spaced around the unit circle so that there are num_steps + /// vectors per circle. + pub fn cycle_unit_circle(num_steps: u32) -> Box { + Box::new(RadialStepGenerator::new(num_steps)) + } + + /// Create a generator of vectors by cycling through a given set of vectors + /// + /// Each value will be spaced in slightly away from the previous value on a ball of radius jitter + pub fn jitter_centroids(centroids: Arc, jitter: f32) -> Box { + Box::new(JitterCentroidsGenerator::try_new(centroids, jitter).unwrap()) + } + + /// Create a generator from a vector of values + /// + /// If more rows are requested than the length of values then it will restart + /// from the beginning of the vector. + pub fn cycle(values: Vec) -> Box + where + DataType::Native: Copy + 'static, + DataType: ArrowPrimitiveType, + PrimitiveArray: From> + 'static, + { + let mut values_idx = 0; + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |_| { + let y = values[values_idx]; + values_idx = (values_idx + 1) % values.len(); + y + }, + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + /// Create a generator from a vector of booleans + /// + /// If more rows are requested than the length of values then it will restart from + /// the beginning of the vector + pub fn cycle_bool(values: Vec) -> Box { + let mut values_idx = 0; + Box::new(FnGen::::new_unknown_size( + DataType::Boolean, + move |_| { + let val = values[values_idx]; + values_idx = (values_idx + 1) % values.len(); + val + }, + 1, + )) + } + + /// Create a generator that starts at 0 and increments by 1 for each element + pub fn step() -> Box + where + DataType::Native: Copy + Default + std::ops::AddAssign + 'static, + DataType: ArrowPrimitiveType, + PrimitiveArray: From> + 'static, + { + let mut x = DataType::Native::default(); + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |_| { + let y = x; + x += DataType::Native::ONE; + y + }, + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + pub fn blob() -> Box { + let mut blob_meta = HashMap::new(); + blob_meta.insert("lance-encoding:blob".to_string(), "true".to_string()); + rand_fixedbin(ByteCount::from(4 * 1024 * 1024), true).with_metadata(blob_meta) + } + + /// Create a generator that starts at a given value and increments by a given step for each element + pub fn step_custom( + start: DataType::Native, + step: DataType::Native, + ) -> Box + where + DataType::Native: Copy + Default + std::ops::AddAssign + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + { + let mut x = start; + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |_| { + let y = x; + x += step; + y + }, + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + /// Create a generator that fills each element with the given primitive value + pub fn fill(value: DataType::Native) -> Box + where + DataType::Native: Copy + 'static, + DataType: ArrowPrimitiveType, + PrimitiveArray: From> + 'static, + { + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |_| value, + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + /// Create a generator that fills each element with the given binary value + pub fn fill_varbin(value: Vec) -> Box { + Box::new(FixedBinaryGenerator::::new(value)) + } + + /// Create a generator that fills each element with the given string value + pub fn fill_utf8(value: String) -> Box { + Box::new(FixedBinaryGenerator::::new(value.into_bytes())) + } + + pub fn cycle_utf8_literals(values: &[&'static str]) -> Box { + Box::new(CycleBinaryGenerator::::from_strings(values)) + } + + /// Create a generator of primitive values that are randomly sampled from the entire range available for the value + pub fn rand() -> Box + where + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + rand::distr::StandardUniform: rand::distr::Distribution, + { + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |rng| rng.random(), + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + /// Create a generator of primitive values that are randomly sampled from the entire range available for the value + pub fn rand_with_distribution< + DataType, + Dist: rand::distr::Distribution + Clone + Send + Sync + 'static, + >( + dist: Dist, + ) -> Box + where + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + { + Box::new( + FnGen::, _>::new_known_size( + DataType::DATA_TYPE, + move |rng| rng.sample(dist.clone()), + 1, + DataType::DATA_TYPE + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Primitive types should have a fixed width"), + ), + ) + } + + /// Create a generator of 1d vectors (of a primitive type) consisting of randomly sampled primitive values + pub fn rand_vec(dimension: Dimension) -> Box + where + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + rand::distr::StandardUniform: rand::distr::Distribution, + { + let underlying = rand::(); + cycle_vec(underlying, dimension) + } + + /// Create a generator of 1d vectors (of a primitive type) consisting of randomly sampled nullable values + pub fn rand_vec_nullable( + dimension: Dimension, + null_probability: f64, + ) -> Box + where + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + rand::distr::StandardUniform: rand::distr::Distribution, + { + let underlying = rand::().with_random_nulls(null_probability); + cycle_vec(underlying, dimension) + } + + /// Create a generator of randomly sampled time32 values covering the entire + /// range of 1 day + pub fn rand_time32(resolution: &TimeUnit) -> Box { + let start = 0; + let end = match resolution { + TimeUnit::Second => 86_400, + TimeUnit::Millisecond => 86_400_000, + _ => panic!(), + }; + + let data_type = DataType::Time32(*resolution); + let size = ByteCount::from(data_type.primitive_width().unwrap() as u64); + let dist = Uniform::new(start, end).unwrap(); + let sample_fn = move |rng: &mut _| dist.sample(rng); + + match resolution { + TimeUnit::Second => Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, size, + )), + TimeUnit::Millisecond => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, size, + )) + } + _ => panic!(), + } + } + + /// Create a generator of randomly sampled time64 values covering the entire + /// range of 1 day + pub fn rand_time64(resolution: &TimeUnit) -> Box { + let start = 0_i64; + let end: i64 = match resolution { + TimeUnit::Microsecond => 86_400_000, + TimeUnit::Nanosecond => 86_400_000_000, + _ => panic!(), + }; + + let data_type = DataType::Time64(*resolution); + let size = ByteCount::from(data_type.primitive_width().unwrap() as u64); + let dist = Uniform::new(start, end).unwrap(); + let sample_fn = move |rng: &mut _| dist.sample(rng); + + match resolution { + TimeUnit::Microsecond => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, size, + )) + } + TimeUnit::Nanosecond => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, size, + )) + } + _ => panic!(), + } + } + + /// Create a generator of random UUIDs, stored as fixed size binary values + /// + /// Note, these are "pseudo UUIDs". They are 16-byte randomish values but they + /// are not guaranteed to be unique. We use a simplistic RNG that trades uniqueness + /// for speed. + pub fn rand_pseudo_uuid() -> Box { + Box::::default() + } + + /// Create a generator of random UUIDs, stored as 32-character strings (hex encoding + /// of the 16-byte binary value) + /// + /// Note, these are "pseudo UUIDs". They are 16-byte randomish values but they + /// are not guaranteed to be unique. We use a simplistic RNG that trades uniqueness + /// for speed. + pub fn rand_pseudo_uuid_hex() -> Box { + Box::::default() + } + + pub fn rand_primitive( + data_type: DataType, + ) -> Box { + Box::new(RandomBytesGenerator::::new(data_type)) + } + + pub fn rand_fsb(size: i32) -> Box { + Box::new(RandomFixedSizeBinaryGenerator::new(size)) + } + + pub fn rand_interval(unit: IntervalUnit) -> Box { + Box::new(RandomIntervalGenerator::new(unit)) + } + + /// The default sampling range for temporal generators: the 365 days ending at + /// 2024-01-01T00:00:00Z (exclusive) + /// + /// The range must be a fixed anchor and not derived from the wall clock + /// (e.g. `Utc::now()`), otherwise the same RNG seed would generate different + /// values depending on when the generator was created, breaking + /// reproducibility (e.g. of saved fuzz inputs). Callers that need a + /// time-relative range can use the `*_in_range` variants. + fn default_temporal_range() -> (chrono::DateTime, chrono::DateTime) { + let end = chrono::DateTime::::from_timestamp(1_704_067_200, 0) + .expect("2024-01-01T00:00:00Z is a valid timestamp"); + let start = end - chrono::TimeDelta::try_days(365).expect("TimeDelta try_days"); + (start, end) + } + + /// Create a generator of randomly sampled date32 values + /// + /// Instead of sampling the entire range, all values will be drawn from a fixed + /// one-year range (the 365 days ending at 2024-01-01 UTC) as this is a more + /// common use pattern. Use [`rand_date32_in_range`] to control the range. + pub fn rand_date32() -> Box { + let (start, end) = default_temporal_range(); + rand_date32_in_range(start, end) + } + + /// Create a generator of randomly sampled date32 values in the given range + pub fn rand_date32_in_range( + start: chrono::DateTime, + end: chrono::DateTime, + ) -> Box { + let data_type = DataType::Date32; + let end_ms = end.timestamp_millis(); + let end_days = (end_ms / MS_PER_DAY) as i32; + let start_ms = start.timestamp_millis(); + let start_days = (start_ms / MS_PER_DAY) as i32; + let dist = Uniform::new(start_days, end_days).unwrap(); + + Box::new(FnGen::::new_known_size( + data_type, + move |rng| dist.sample(rng), + 1, + DataType::Date32 + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Date32 should have a fixed width"), + )) + } + + /// Create a generator of randomly sampled date64 values + /// + /// Instead of sampling the entire range, all values will be drawn from a fixed + /// one-year range (the 365 days ending at 2024-01-01 UTC) as this is a more + /// common use pattern. Use [`rand_date64_in_range`] to control the range. + pub fn rand_date64() -> Box { + let (start, end) = default_temporal_range(); + rand_date64_in_range(start, end) + } + + /// Create a generator of randomly sampled timestamp values in the given range + /// + /// Currently just samples the entire range of u64 values and casts to timestamp + pub fn rand_timestamp_in_range( + start: chrono::DateTime, + end: chrono::DateTime, + data_type: &DataType, + ) -> Box { + let end_ms = end.timestamp_millis(); + let start_ms = start.timestamp_millis(); + let (start_ticks, end_ticks) = match data_type { + DataType::Timestamp(TimeUnit::Nanosecond, _) => { + (start_ms * 1000 * 1000, end_ms * 1000 * 1000) + } + DataType::Timestamp(TimeUnit::Microsecond, _) => (start_ms * 1000, end_ms * 1000), + DataType::Timestamp(TimeUnit::Millisecond, _) => (start_ms, end_ms), + DataType::Timestamp(TimeUnit::Second, _) => (start.timestamp(), end.timestamp()), + _ => panic!(), + }; + let dist = Uniform::new(start_ticks, end_ticks).unwrap(); + + let data_type = data_type.clone(); + let sample_fn = move |rng: &mut _| dist.sample(rng); + let width = data_type + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .unwrap(); + + match data_type { + DataType::Timestamp(TimeUnit::Nanosecond, _) => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, width, + )) + } + DataType::Timestamp(TimeUnit::Microsecond, _) => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, width, + )) + } + DataType::Timestamp(TimeUnit::Millisecond, _) => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, width, + )) + } + DataType::Timestamp(TimeUnit::Second, _) => { + Box::new(FnGen::::new_known_size( + data_type, sample_fn, 1, width, + )) + } + _ => panic!(), + } + } + + /// Create a generator of randomly sampled timestamp values + /// + /// Instead of sampling the entire range, all values will be drawn from a fixed + /// one-year range (the 365 days ending at 2024-01-01 UTC) as this is a more + /// common use pattern. Use [`rand_timestamp_in_range`] to control the range. + pub fn rand_timestamp(data_type: &DataType) -> Box { + let (start, end) = default_temporal_range(); + rand_timestamp_in_range(start, end, data_type) + } + + /// Create a generator of randomly sampled date64 values + /// + /// Instead of sampling the entire range, all values will be drawn from the last year as this + /// is a more common use pattern + pub fn rand_date64_in_range( + start: chrono::DateTime, + end: chrono::DateTime, + ) -> Box { + let data_type = DataType::Date64; + let end_ms = end.timestamp_millis(); + let end_days = end_ms / MS_PER_DAY; + let start_ms = start.timestamp_millis(); + let start_days = start_ms / MS_PER_DAY; + let dist = Uniform::new(start_days, end_days).unwrap(); + + Box::new(FnGen::::new_known_size( + data_type, + move |rng| (dist.sample(rng)) * MS_PER_DAY, + 1, + DataType::Date64 + .primitive_width() + .map(|width| ByteCount::from(width as u64)) + .expect("Date64 should have a fixed width"), + )) + } + + /// Create a generator of random binary values where each value has a fixed number of bytes + pub fn rand_fixedbin(bytes_per_element: ByteCount, is_large: bool) -> Box { + Box::new(RandomBinaryGenerator::new( + bytes_per_element, + false, + is_large, + )) + } + + /// Create a generator of random binary values where each value has a variable number of bytes + /// + /// The number of bytes per element will be randomly sampled from the given (inclusive) range + pub fn rand_varbin( + min_bytes_per_element: ByteCount, + max_bytes_per_element: ByteCount, + ) -> Box { + Box::new(VariableRandomBinaryGenerator::new( + min_bytes_per_element, + max_bytes_per_element, + )) + } + + /// Create a generator of random strings + /// + /// All strings will consist entirely of printable ASCII characters + pub fn rand_utf8(bytes_per_element: ByteCount, is_large: bool) -> Box { + Box::new(RandomBinaryGenerator::new( + bytes_per_element, + true, + is_large, + )) + } + + /// Creates a generator of strings with a prefix and a counter + /// + /// For example, if the prefix is "user_" the strings will be "user_0", "user_1", ... + pub fn utf8_prefix_plus_counter( + prefix: impl Into, + is_large: bool, + ) -> Box { + Box::new(PrefixPlusCounterGenerator::new(prefix.into(), is_large)) + } + + pub fn binary_prefix_plus_counter( + prefix: Arc<[u8]>, + is_large: bool, + ) -> Box { + Box::new(BinaryPrefixPlusCounterGenerator::new(prefix, is_large)) + } + + /// Create a random generator of boolean values + pub fn rand_boolean() -> Box { + Box::::default() + } + + /// Create a generator of random sentences + /// + /// Generates strings containing between min_words and max_words random English words joined by spaces + pub fn random_sentence( + min_words: usize, + max_words: usize, + is_large: bool, + ) -> Box { + Box::new(RandomSentenceGenerator::new(min_words, max_words, is_large)) + } + + /// Create a generator of random words (one word per row) + /// + /// Generates strings containing a single random English word per row + pub fn random_word(is_large: bool) -> Box { + Box::new(RandomWordGenerator::new(is_large)) + } + + pub fn rand_list(item_type: &DataType, is_large: bool) -> Box { + let child_gen = rand_type(item_type); + Box::new(RandomListGenerator::new(child_gen, is_large)) + } + + pub fn rand_list_any( + item_gen: Box, + is_large: bool, + ) -> Box { + Box::new(RandomListGenerator::new(item_gen, is_large)) + } + + /// Generates random map arrays where each map has 0-4 entries. + pub fn rand_map(key_type: &DataType, value_type: &DataType) -> Box { + let keys_gen = rand_type(key_type); + let values_gen = rand_type(value_type); + Box::new(RandomMapGenerator::new(keys_gen, values_gen)) + } + + pub fn rand_struct(fields: Fields) -> Box { + let child_gens = fields + .iter() + .map(|f| rand_type(f.data_type())) + .collect::>(); + Box::new(RandomStructGenerator::new(fields, child_gens)) + } + + pub fn null_type() -> Box { + Box::new(NullArrayGenerator {}) + } + + /// Create a generator of random values + pub fn rand_type(data_type: &DataType) -> Box { + match data_type { + DataType::Boolean => rand_boolean(), + DataType::Int8 => rand::(), + DataType::Int16 => rand::(), + DataType::Int32 => rand::(), + DataType::Int64 => rand::(), + DataType::UInt8 => rand::(), + DataType::UInt16 => rand::(), + DataType::UInt32 => rand::(), + DataType::UInt64 => rand::(), + DataType::Float16 => rand_primitive::(data_type.clone()), + DataType::Float32 => rand::(), + DataType::Float64 => rand::(), + DataType::Decimal128(_, _) => rand_primitive::(data_type.clone()), + DataType::Decimal256(_, _) => rand_primitive::(data_type.clone()), + DataType::Utf8 => rand_utf8(ByteCount::from(12), false), + DataType::LargeUtf8 => rand_utf8(ByteCount::from(12), true), + DataType::Binary => rand_fixedbin(ByteCount::from(12), false), + DataType::LargeBinary => rand_fixedbin(ByteCount::from(12), true), + DataType::Dictionary(key_type, value_type) => { + dict_type(rand_type(value_type), key_type) + } + DataType::FixedSizeList(child, dimension) => cycle_vec( + rand_type(child.data_type()), + Dimension::from(*dimension as u32), + ), + DataType::FixedSizeBinary(size) => rand_fsb(*size), + DataType::List(child) => rand_list(child.data_type(), false), + DataType::LargeList(child) => rand_list(child.data_type(), true), + DataType::Map(entries_field, _) => { + let DataType::Struct(fields) = entries_field.data_type() else { + panic!("Map entries field must be a struct"); + }; + let key_type = fields[0].data_type(); + let value_type = fields[1].data_type(); + rand_map(key_type, value_type) + } + DataType::Duration(unit) => match unit { + TimeUnit::Second => rand::(), + TimeUnit::Millisecond => rand::(), + TimeUnit::Microsecond => rand::(), + TimeUnit::Nanosecond => rand::(), + }, + DataType::Interval(unit) => rand_interval(*unit), + DataType::Date32 => rand_date32(), + DataType::Date64 => rand_date64(), + DataType::Time32(resolution) => rand_time32(resolution), + DataType::Time64(resolution) => rand_time64(resolution), + DataType::Timestamp(_, _) => rand_timestamp(data_type), + DataType::Struct(fields) => rand_struct(fields.clone()), + DataType::Null => null_type(), + _ => unimplemented!("random generation of {}", data_type), + } + } + + /// Encodes arrays generated by the underlying generator as dictionaries with the given key type + /// + /// Note that this may not be very realistic if the underlying generator is something like a random + /// generator since most of the underlying values will be unique and the common case for dictionary + /// encoding is when there is a small set of possible values. + pub fn dict( + generator: Box, + ) -> Box { + Box::new(DictionaryGenerator::::new(generator)) + } + + /// Encodes arrays generated by the underlying generator as dictionaries with the given key type + pub fn dict_type( + generator: Box, + key_type: &DataType, + ) -> Box { + match key_type { + DataType::Int8 => dict::(generator), + DataType::Int16 => dict::(generator), + DataType::Int32 => dict::(generator), + DataType::Int64 => dict::(generator), + DataType::UInt8 => dict::(generator), + DataType::UInt16 => dict::(generator), + DataType::UInt32 => dict::(generator), + DataType::UInt64 => dict::(generator), + _ => unimplemented!(), + } + } + + /// Wraps a generator to produce low-cardinality data. + /// + /// Generates `cardinality` unique values on first call, then randomly + /// selects from them for all subsequent rows. + pub fn low_cardinality( + generator: Box, + cardinality: usize, + ) -> Box { + Box::new(LowCardinalityGenerator::new(generator, cardinality)) + } +} + +/// Create a BatchGeneratorBuilder to start generating batch data +pub fn gen_batch() -> BatchGeneratorBuilder { + BatchGeneratorBuilder::default() +} + +/// Create an ArrayGeneratorBuilder to start generating array data +pub fn gen_array(genn: Box) -> ArrayGeneratorBuilder { + ArrayGeneratorBuilder::new(genn) +} + +/// Metadata key to specify content type for string generation. +/// Set to "sentence" to use the sentence generator with Zipf distribution. +pub const CONTENT_TYPE_KEY: &str = "lance-datagen:content-type"; + +/// Metadata key to specify cardinality for low-cardinality data generation. +/// Set to a numeric string (e.g., "100") to limit unique values. +pub const CARDINALITY_KEY: &str = "lance-datagen:cardinality"; + +/// Create a generator for a field, checking metadata for content type hints. +/// +/// Supported metadata keys: +/// - `lance-datagen:content-type`: Set to "sentence" for Utf8/LargeUtf8 fields +/// to use the sentence generator with Zipf distribution. +/// - `lance-datagen:cardinality`: Set to a number to limit unique values. +/// The generator will produce only that many unique values and randomly +/// select from them. +pub fn rand_field(field: &Field) -> Box { + let mut generator = if let Some(content_type) = field.metadata().get(CONTENT_TYPE_KEY) { + match (content_type.as_str(), field.data_type()) { + ("sentence", DataType::Utf8) => array::random_sentence(1, 10, false), + ("sentence", DataType::LargeUtf8) => array::random_sentence(1, 10, true), + _ => array::rand_type(field.data_type()), + } + } else { + array::rand_type(field.data_type()) + }; + + if let Some(cardinality_str) = field.metadata().get(CARDINALITY_KEY) + && let Ok(cardinality) = cardinality_str.parse::() + && cardinality > 0 + { + generator = array::low_cardinality(generator, cardinality); + } + + generator +} + +/// Create a BatchGeneratorBuilder with the given schema +/// +/// You can add more columns or convert this into a reader immediately. +/// +/// Supported field metadata: +/// - `lance-datagen:content-type` = `"sentence"`: Use sentence generator with +/// Zipf distribution for more realistic text (Utf8/LargeUtf8 only). +/// - `lance-datagen:cardinality` = `""`: Limit to N unique values. +pub fn rand(schema: &Schema) -> BatchGeneratorBuilder { + let mut builder = BatchGeneratorBuilder::default(); + for field in schema.fields() { + builder = builder.col(field.name(), rand_field(field)); + } + builder +} + +#[cfg(test)] +mod tests { + + use arrow::datatypes::{Float32Type, Int8Type, Int16Type, TimeUnit, UInt32Type}; + use arrow_array::{ + BooleanArray, Date32Array, Date64Array, Float32Array, Int8Array, Int16Array, Int32Array, + TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray, + TimestampSecondArray, UInt32Array, + }; + + use super::*; + + #[test] + fn test_timestamp_timezone_is_preserved() { + let data_type = DataType::Timestamp(TimeUnit::Millisecond, Some("UTC".into())); + let mut generator = array::rand_type(&data_type); + let generated = generator.generate_default(RowCount::from(2)).unwrap(); + assert_eq!(generated.data_type(), &data_type); + + let fields = Fields::from(vec![Field::new("timestamp", data_type, true)]); + let mut generator = array::rand_struct(fields.clone()); + let generated = generator.generate_default(RowCount::from(2)).unwrap(); + assert_eq!(generated.data_type(), &DataType::Struct(fields)); + } + + #[test] + fn test_fn_gen_propagates_array_data_build_error() { + // FnGen constructors are internal. Use an incompatible declared type to + // verify that ArrayDataBuilder validation failures are propagated. + let mut generator = FnGen::::new_unknown_size(DataType::Utf8, |_| 0, 1); + + assert!(matches!( + generator.generate_default(RowCount::from(1)), + Err(ArrowError::InvalidArgumentError(_)) + )); + } + + #[test] + fn test_step() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::step::(); + assert_eq!( + *genn.generate(RowCount::from(5), &mut rng).unwrap(), + Int32Array::from_iter([0, 1, 2, 3, 4]) + ); + assert_eq!( + *genn.generate(RowCount::from(5), &mut rng).unwrap(), + Int32Array::from_iter([5, 6, 7, 8, 9]) + ); + + let mut genn = array::step::(); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Int8Array::from_iter([0, 1, 2]) + ); + + let mut genn = array::step::(); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Float32Array::from_iter([0.0, 1.0, 2.0]) + ); + + let mut genn = array::step_custom::(4, 8); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Int16Array::from_iter([4, 12, 20]) + ); + assert_eq!( + *genn.generate(RowCount::from(2), &mut rng).unwrap(), + Int16Array::from_iter([28, 36]) + ); + } + + #[test] + fn test_cycle() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::cycle::(vec![1, 2, 3]); + assert_eq!( + *genn.generate(RowCount::from(5), &mut rng).unwrap(), + Int32Array::from_iter([1, 2, 3, 1, 2]) + ); + + let mut genn = array::cycle_utf8_literals(&["abc", "def", "xyz"]); + assert_eq!( + *genn.generate(RowCount::from(5), &mut rng).unwrap(), + StringArray::from_iter_values(["abc", "def", "xyz", "abc", "def"]) + ); + assert_eq!( + *genn.generate(RowCount::from(1), &mut rng).unwrap(), + StringArray::from_iter_values(["xyz"]) + ); + + let mut genn = array::cycle_bool(vec![false, false, true]); + assert_eq!( + *genn.generate(RowCount::from(5), &mut rng).unwrap(), + BooleanArray::from_iter(vec![false, false, true, false, false].into_iter().map(Some)) + ); + assert_eq!( + *genn.generate(RowCount::from(1), &mut rng).unwrap(), + BooleanArray::from_iter(vec![Some(true)]) + ) + } + + #[test] + fn test_fill() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::fill::(42); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Int32Array::from_iter([42, 42, 42]) + ); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Int32Array::from_iter([42, 42, 42]) + ); + + let mut genn = array::fill_varbin(vec![0, 1, 2]); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::BinaryArray::from_iter_values([ + "\x00\x01\x02", + "\x00\x01\x02", + "\x00\x01\x02" + ]) + ); + + let mut genn = array::fill_utf8("xyz".to_string()); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::StringArray::from_iter_values(["xyz", "xyz", "xyz"]) + ); + } + + #[test] + fn test_utf8_prefix_plus_counter() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::utf8_prefix_plus_counter("user_", false); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::StringArray::from_iter_values(["user_0", "user_1", "user_2"]) + ); + + let mut genn = array::utf8_prefix_plus_counter("user_", true); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::LargeStringArray::from_iter_values(["user_0", "user_1", "user_2"]) + ); + } + + #[test] + fn test_rng() { + // Note: these tests are heavily dependent on the default seed. + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::rand::(); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + Int32Array::from_iter([-797553329, 1369325940, -69174021]) + ); + + let mut genn = array::rand_fixedbin(ByteCount::from(3), false); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::BinaryArray::from_iter_values([ + [184, 53, 216], + [12, 96, 159], + [125, 179, 56] + ]) + ); + + let mut genn = array::rand_utf8(ByteCount::from(3), false); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::StringArray::from_iter_values([">@p", "n `", "NWa"]) + ); + + let mut genn = array::random_sentence(1, 5, false); + let words = genn.generate(RowCount::from(10), &mut rng).unwrap(); + assert_eq!(words.data_type(), &DataType::Utf8); + let words_array = words.as_any().downcast_ref::().unwrap(); + // Verify each string contains 1-5 words + for i in 0..10 { + let sentence = words_array.value(i); + let word_count = sentence.split_whitespace().count(); + assert!((1..=5).contains(&word_count)); + } + + let mut genn = array::rand_date32(); + let days_32 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + assert_eq!(days_32.data_type(), &DataType::Date32); + + let mut genn = array::rand_date64(); + let days_64 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + assert_eq!(days_64.data_type(), &DataType::Date64); + + let mut genn = array::rand_boolean(); + let bools = genn.generate(RowCount::from(1024), &mut rng).unwrap(); + assert_eq!(bools.data_type(), &DataType::Boolean); + let bools = bools.as_any().downcast_ref::().unwrap(); + // Sanity check to ensure we're getting at least some rng + assert!(bools.false_count() > 100); + assert!(bools.true_count() > 100); + + let mut genn = array::rand_varbin(ByteCount::from(2), ByteCount::from(4)); + assert_eq!( + *genn.generate(RowCount::from(3), &mut rng).unwrap(), + arrow_array::BinaryArray::from_iter_values([ + vec![111, 9, 80], + vec![86, 118, 13, 209], + vec![68, 33, 202] + ]) + ); + } + + #[test] + fn test_rng_temporal_deterministic() { + // The default temporal generators must not depend on the wall clock: the + // same seed must produce the same values no matter when the generator is + // created (https://github.com/lance-format/lance/issues/7913). These + // exact values pin both the RNG stream and the fixed default sampling + // range (the 365 days ending at 2024-01-01 UTC). + fn gen_values(mut genn: Box) -> Arc { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + genn.generate(RowCount::from(3), &mut rng).unwrap() + } + + assert_eq!( + *gen_values(array::rand_date32()), + Date32Array::from(vec![19655, 19474, 19717]) + ); + assert_eq!( + *gen_values(array::rand_date64()), + Date64Array::from(vec![ + 1_698_192_000_000, + 1_682_553_600_000, + 1_703_548_800_000 + ]) + ); + assert_eq!( + *gen_values(array::rand_timestamp(&DataType::Timestamp( + TimeUnit::Second, + None + ))), + TimestampSecondArray::from(vec![1_698_211_127, 1_682_585_540, 1_703_559_286]) + ); + assert_eq!( + *gen_values(array::rand_timestamp(&DataType::Timestamp( + TimeUnit::Millisecond, + None + ))), + TimestampMillisecondArray::from(vec![ + 1_698_211_127_056, + 1_682_585_540_319, + 1_703_559_286_487 + ]) + ); + assert_eq!( + *gen_values(array::rand_timestamp(&DataType::Timestamp( + TimeUnit::Microsecond, + None + ))), + TimestampMicrosecondArray::from(vec![ + 1_698_211_127_056_596, + 1_682_585_540_319_384, + 1_703_559_286_487_645 + ]) + ); + assert_eq!( + *gen_values(array::rand_timestamp(&DataType::Timestamp( + TimeUnit::Nanosecond, + None + ))), + TimestampNanosecondArray::from(vec![ + 1_698_211_127_056_596_085, + 1_682_585_540_319_384_548, + 1_703_559_286_487_645_287 + ]) + ); + } + + #[test] + fn test_rng_list() { + // Note: these tests are heavily dependent on the default seed. + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::rand_list(&DataType::Int32, false); + let arr = genn.generate(RowCount::from(100), &mut rng).unwrap(); + // Make sure we can generate empty lists (note, test is dependent on seed) + let arr = arr.as_list::(); + assert!(arr.iter().any(|l| l.unwrap().is_empty())); + // Shouldn't generate any giant lists (don't kill performance in normal datagen) + assert!(arr.iter().any(|l| l.unwrap().len() < 11)); + } + + #[test] + fn test_rng_distribution() { + // Sanity test to make sure we our RNG is giving us well distributed values + // We generates some 4-byte integers, histogram them into 8 buckets, and make + // sure each bucket has a good # of values + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::rand::(); + for _ in 0..10 { + let arr = genn.generate(RowCount::from(10000), &mut rng).unwrap(); + let int_arr = arr.as_any().downcast_ref::().unwrap(); + let mut buckets = vec![0_u32; 256]; + for val in int_arr.values() { + buckets[(*val >> 24) as usize] += 1; + } + for bucket in buckets { + // Perfectly even distribution would have 10000 / 256 values (~40) per bucket + // We test for 15 which should be "good enough" and statistically unlikely to fail + assert!(bucket > 15); + } + } + } + + #[test] + fn test_nulls() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::rand::().with_random_nulls(0.3); + + let arr = genn.generate(RowCount::from(1000), &mut rng).unwrap(); + + // This assert depends on the default seed + assert_eq!(arr.null_count(), 297); + + for len in 0..100 { + let arr = genn.generate(RowCount::from(len), &mut rng).unwrap(); + // Make sure the null count we came up with matches the actual # of unset bits + assert_eq!( + arr.null_count(), + arr.nulls() + .map(|nulls| (len as usize) + - nulls.buffer().count_set_bits_offset(0, len as usize)) + .unwrap_or(0) + ); + } + + let mut genn = array::rand::().with_random_nulls(0.0); + let arr = genn.generate(RowCount::from(10), &mut rng).unwrap(); + + assert_eq!(arr.null_count(), 0); + + let mut genn = array::rand::().with_random_nulls(1.0); + let arr = genn.generate(RowCount::from(10), &mut rng).unwrap(); + + assert_eq!(arr.null_count(), 10); + assert!((0..10).all(|idx| arr.is_null(idx))); + + let mut genn = array::rand::().with_nulls(&[false, false, true]); + let arr = genn.generate(RowCount::from(7), &mut rng).unwrap(); + assert!((0..2).all(|idx| arr.is_valid(idx))); + assert!(arr.is_null(2)); + assert!((3..5).all(|idx| arr.is_valid(idx))); + assert!(arr.is_null(5)); + assert!(arr.is_valid(6)); + } + + #[test] + fn test_unit_circle() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::cycle_unit_circle(4); + let arr = genn.generate(RowCount::from(6), &mut rng).unwrap(); + + let arr_values = arr + .as_fixed_size_list() + .values() + .as_primitive::() + .values() + .to_vec(); + assert_eq!(arr_values.len(), 12); + let expected_values = [1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0]; + for (actual, expected) in arr_values.iter().zip(expected_values.iter()) { + assert!((actual - expected).abs() < 0.0001); + } + } + + #[test] + fn test_jitter_centroids() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut centroids_gen = array::cycle_unit_circle(4); + let centroids = centroids_gen.generate(RowCount::from(4), &mut rng).unwrap(); + + let centroid_values = centroids + .as_fixed_size_list() + .values() + .as_primitive::() + .values() + .to_vec(); + + let mut jitter_jen = array::jitter_centroids(centroids, 0.001); + let jittered = jitter_jen.generate(RowCount::from(100), &mut rng).unwrap(); + + let values = jittered + .as_fixed_size_list() + .values() + .as_primitive::() + .values() + .to_vec(); + + for i in 0..100 { + let centroid = i % 4; + let centroid_x = centroid_values[centroid * 2]; + let centroid_y = centroid_values[centroid * 2 + 1]; + let value_x = values[i * 2]; + let value_y = values[i * 2 + 1]; + + let l2_dist = ((value_x - centroid_x).powi(2) + (value_y - centroid_y).powi(2)).sqrt(); + assert!(l2_dist < 0.001001); + assert!(l2_dist > 0.000999); + } + } + + #[test] + fn test_rand_schema() { + let schema = Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Utf8, true), + Field::new("c", DataType::Float32, true), + Field::new("d", DataType::Int32, true), + Field::new("e", DataType::Int32, true), + ]); + let rbr = rand(&schema) + .into_reader_bytes( + ByteCount::from(1024 * 1024), + BatchCount::from(8), + RoundingBehavior::ExactOrErr, + ) + .unwrap(); + assert_eq!(*rbr.schema(), schema); + + let batches = rbr.map(|val| val.unwrap()).collect::>(); + assert_eq!(batches.len(), 8); + + for batch in batches { + assert_eq!(batch.num_rows(), 1024 * 1024 / 32); + assert_eq!(batch.num_columns(), 5); + } + } +} diff --git a/lance-artifact/rust/lance-datagen/src/lib.rs b/lance-artifact/rust/lance-datagen/src/lib.rs new file mode 100644 index 000000000..3cead7f1a --- /dev/null +++ b/lance-artifact/rust/lance-datagen/src/lib.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod generator; + +pub use generator::*; diff --git a/lance-artifact/rust/lance-derive/Cargo.toml b/lance-artifact/rust/lance-derive/Cargo.toml new file mode 100644 index 000000000..4bb99d3ac --- /dev/null +++ b/lance-artifact/rust/lance-derive/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "lance-derive" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme.workspace = true +description = "Derive macros for Lance" +keywords.workspace = true +categories.workspace = true + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0.67" +quote = "1.0.33" +syn = { version = "2.0.37", features = ["full"] } + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-derive/src/lib.rs b/lance-artifact/rust/lance-derive/src/lib.rs new file mode 100644 index 000000000..d0486133d --- /dev/null +++ b/lance-artifact/rust/lance-derive/src/lib.rs @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use proc_macro::TokenStream; +use quote::quote; +use syn::{Data, DeriveInput, Fields, parse_macro_input}; + +/// Derive macro for the `DeepSizeOf` trait. +/// +/// Generates an implementation that sums the `deep_size_of_children` of all +/// fields (for structs) or the active variant's fields (for enums). +#[proc_macro_derive(DeepSizeOf)] +pub fn derive_deep_size_of(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = &input.ident; + let generics = &input.generics; + + // Add DeepSizeOf bounds to all type parameters + let mut bounded_generics = generics.clone(); + for param in &mut bounded_generics.params { + if let syn::GenericParam::Type(ref mut type_param) = *param { + type_param + .bounds + .push(syn::parse_quote!(lance_core::deepsize::DeepSizeOf)); + } + } + let (impl_generics, _, where_clause) = bounded_generics.split_for_impl(); + let (_, ty_generics, _) = generics.split_for_impl(); + + let body = match &input.data { + Data::Struct(data) => generate_struct_body(&data.fields), + Data::Enum(data) => { + let arms: Vec<_> = data + .variants + .iter() + .map(|variant| { + let variant_ident = &variant.ident; + match &variant.fields { + Fields::Unit => { + quote! { Self::#variant_ident => 0 } + } + Fields::Unnamed(fields) => { + let bindings: Vec<_> = (0..fields.unnamed.len()) + .map(|i| { + syn::Ident::new( + &format!("__field_{}", i), + proc_macro2::Span::call_site(), + ) + }) + .collect(); + let sum = bindings.iter().map(|b| { + quote! { lance_core::deepsize::DeepSizeOf::deep_size_of_children(#b, __context) } + }); + quote! { + Self::#variant_ident(#(#bindings),*) => { + 0 #(+ #sum)* + } + } + } + Fields::Named(fields) => { + let field_names: Vec<_> = + fields.named.iter().map(|f| &f.ident).collect(); + let sum = field_names.iter().map(|f| { + quote! { lance_core::deepsize::DeepSizeOf::deep_size_of_children(#f, __context) } + }); + quote! { + Self::#variant_ident { #(#field_names),* } => { + 0 #(+ #sum)* + } + } + } + } + }) + .collect(); + quote! { + match self { + #(#arms),* + } + } + } + Data::Union(_) => { + return syn::Error::new_spanned(&input, "DeepSizeOf cannot be derived for unions") + .to_compile_error() + .into(); + } + }; + + let expanded = quote! { + impl #impl_generics lance_core::deepsize::DeepSizeOf for #name #ty_generics #where_clause { + fn deep_size_of_children(&self, __context: &mut lance_core::deepsize::Context) -> usize { + #body + } + } + }; + + TokenStream::from(expanded) +} + +fn generate_struct_body(fields: &Fields) -> proc_macro2::TokenStream { + match fields { + Fields::Named(fields) => { + let field_sizes = fields.named.iter().map(|f| { + let name = &f.ident; + quote! { lance_core::deepsize::DeepSizeOf::deep_size_of_children(&self.#name, __context) } + }); + quote! { 0 #(+ #field_sizes)* } + } + Fields::Unnamed(fields) => { + let field_sizes = (0..fields.unnamed.len()).map(|i| { + let index = syn::Index::from(i); + quote! { lance_core::deepsize::DeepSizeOf::deep_size_of_children(&self.#index, __context) } + }); + quote! { 0 #(+ #field_sizes)* } + } + Fields::Unit => { + quote! { 0 } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/.gitignore b/lance-artifact/rust/lance-encoding/.gitignore new file mode 100644 index 000000000..a122654a7 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/.gitignore @@ -0,0 +1,2 @@ +# proptests +proptest-regressions/ diff --git a/lance-artifact/rust/lance-encoding/Cargo.toml b/lance-artifact/rust/lance-encoding/Cargo.toml new file mode 100644 index 000000000..d14227f58 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/Cargo.toml @@ -0,0 +1,86 @@ +[package] +name = "lance-encoding" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme = "README.md" +description = "Encoders and decoders for the Lance file format" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[dependencies] +lance-arrow.workspace = true +lance-core.workspace = true +arrow-arith.workspace = true +arrow-array.workspace = true +arrow-data.workspace = true +arrow-buffer.workspace = true +arrow-cast.workspace = true +arrow-schema.workspace = true +arrow-select.workspace = true +lance-bitpacking = { workspace = true, optional = true } +bytes.workspace = true +futures.workspace = true +fsst.workspace = true +hex = "0.4.3" +itertools.workspace = true +log.workspace = true +num-traits.workspace = true +prost.workspace = true +hyperloglogplus.workspace = true +rand.workspace = true +tokio.workspace = true +tracing.workspace = true +xxhash-rust = { version = "0.8.15", features = ["xxh3"] } +bytemuck = { version = "1.14", features = ["extern_crate_alloc"] } +byteorder.workspace = true +lz4 = { version = "1", optional = true } +zstd = { version = "0.13", optional = true } + +[dev-dependencies] +lance-datagen.workspace = true +arrow-ord.workspace = true +rand.workspace = true +rstest.workspace = true +test-log.workspace = true +criterion = { workspace = true } +lance-testing.workspace = true +rand_xoshiro = { workspace = true } +proptest.workspace = true +serial_test.workspace = true + +[build-dependencies] +prost-build.workspace = true +protobuf-src = { version = "2.1", optional = true } + +[features] +default = ["lz4", "zstd", "bitpacking"] +protoc = ["dep:protobuf-src"] +bitpacking = ["dep:lance-bitpacking"] +lz4 = ["dep:lz4"] +zstd = ["dep:zstd"] + +[package.metadata.docs.rs] +# docs.rs uses an older version of Ubuntu that does not have the necessary protoc version +features = ["protoc"] + +[package.metadata.cargo-machete] +ignored = ["prost"] + +[[bench]] +name = "decoder" +harness = false + +[[bench]] +name = "encoder" +harness = false + +[[bench]] +name = "buffer" +harness = false + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-encoding/README.md b/lance-artifact/rust/lance-encoding/README.md new file mode 100644 index 000000000..3e6f7c554 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/README.md @@ -0,0 +1,6 @@ +# lance-encoding + +`lance-encoding` is an internal sub-crate, containing encoders and decoders +for the Lance file format. + +**Important Note**: This crate is **not intended for external usage**. diff --git a/lance-artifact/rust/lance-encoding/benches/buffer.rs b/lance-artifact/rust/lance-encoding/benches/buffer.rs new file mode 100644 index 000000000..04e0718a7 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/benches/buffer.rs @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::hint::black_box; + +use criterion::{Criterion, Throughput, criterion_group, criterion_main}; +use lance_encoding::buffer::LanceBuffer; + +const NUM_VALUES: &[usize] = &[1024 * 1024, 32 * 1024, 8 * 1024]; + +fn bench_zip(c: &mut Criterion) { + for num_values in NUM_VALUES { + let num_values = *num_values; + let mut group = c.benchmark_group(format!("zip_{}Ki", num_values / 1024)); + + group.throughput(Throughput::Bytes((num_values * 6) as u64)); + + group.bench_function("2_4_zip_into_6", move |b| { + // Zip together a 2-byte-per-buffer and an 8-byte-per-buffer array, each with 1Mi items + let random_shorts: Vec = + (0..num_values * 2).map(|_| rand::random::()).collect(); + let random_ints: Vec = (0..num_values * 4).map(|_| rand::random::()).collect(); + let mut buffers = vec![ + (LanceBuffer::from(random_shorts), 16), + (LanceBuffer::from(random_ints), 32), + ]; + let buffers = &mut buffers; + + b.iter(move || { + let buffers = buffers + .iter_mut() + .map(|(buf, bits_per_value)| (buf.clone(), *bits_per_value)) + .collect::>(); + black_box(LanceBuffer::zip_into_one(buffers, num_values as u64).unwrap()); + }) + }); + + group.bench_function("2_2_2_zip_into_6", move |b| { + // Zip together a 2-byte-per-buffer and an 8-byte-per-buffer array, each with 1Mi items + let random_shorts1: Vec = + (0..num_values * 2).map(|_| rand::random::()).collect(); + let random_shorts2: Vec = + (0..num_values * 2).map(|_| rand::random::()).collect(); + let random_shorts3: Vec = + (0..num_values * 2).map(|_| rand::random::()).collect(); + let mut buffers = vec![ + (LanceBuffer::from(random_shorts1), 16), + (LanceBuffer::from(random_shorts2), 16), + (LanceBuffer::from(random_shorts3), 16), + ]; + let buffers = &mut buffers; + + b.iter(move || { + let buffers = buffers + .iter_mut() + .map(|(buf, bits_per_value)| (buf.clone(), *bits_per_value)) + .collect::>(); + black_box(LanceBuffer::zip_into_one(buffers, num_values as u64).unwrap()); + }) + }); + } +} + +#[cfg(target_os = "linux")] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10) + .with_profiler(lance_testing::pprof::PProfProfiler::new(100, lance_testing::pprof::Output::Flamegraph(None))); + targets = bench_zip); + +// Non-linux version does not support pprof. +#[cfg(not(target_os = "linux"))] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10); + targets = bench_zip); + +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-encoding/benches/common/mod.rs b/lance-artifact/rust/lance-encoding/benches/common/mod.rs new file mode 100644 index 000000000..3539fdd90 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/benches/common/mod.rs @@ -0,0 +1,278 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_schema::DataType; +use lance_core::{Error, Result, datatypes::Field}; +use lance_encoding::{ + compression::{ + BlockCompressor, CompressionStrategy, field_metadata_params, finalize_miniblock_compressor, + reject_packed_struct_per_value, try_bitpacking_block, try_bitpacking_miniblock, + try_byte_stream_split_miniblock, try_fixed_packed_struct_miniblock, try_fixed_u8_rle_block, + try_fixed_u8_rle_miniblock, try_general_block, try_raw_block, + try_raw_fixed_size_list_miniblock, try_raw_fixed_width_miniblock, try_raw_per_value, + try_uncompressed_fixed_width_miniblock, try_variable_packed_struct_per_value, + try_variable_width_miniblock, try_variable_width_per_value, + }, + compression_config::{CompressionFieldParams, CompressionParams}, + data::DataBlock, + encoder::{ + ColumnIndexSequence, FieldEncoder, FieldEncodingContext, FieldEncodingStrategy, + structural::{ + PrimitiveFieldEncoding, PrimitivePageEncoding, try_create_binary_blob, try_create_list, + try_create_map, try_create_struct, try_create_structural_blob, + try_create_structural_fixed_size_list, + }, + }, + encodings::logical::primitive::{fullzip::PerValueCompressor, miniblock::MiniBlockCompressor}, + format::pb21::CompressiveEncoding, +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BenchEncoding { + Array, + StructuralU16, + StructuralU32, +} + +impl std::fmt::Display for BenchEncoding { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::Array => "array", + Self::StructuralU16 => "structural-u16", + Self::StructuralU32 => "structural-u32", + }) + } +} + +#[derive(Debug, Clone)] +struct BenchCompressionStrategy { + encoding: BenchEncoding, + params: CompressionParams, +} + +impl BenchCompressionStrategy { + fn field_params(&self, field: &Field) -> CompressionFieldParams { + let mut params = self + .params + .get_field_params(&field.name, &field.data_type()); + let mut metadata = field_metadata_params(field); + if self.encoding == BenchEncoding::StructuralU16 + && metadata + .minichunk_size + .is_some_and(|size| size >= 32 * 1024) + { + metadata.minichunk_size = None; + } + params.merge(&metadata); + params + } +} + +impl CompressionStrategy for BenchCompressionStrategy { + fn create_miniblock_compressor( + &self, + field: &Field, + data: &DataBlock, + ) -> Result> { + let params = self.field_params(field); + let compressor = + if let Some(compressor) = try_uncompressed_fixed_width_miniblock(data, ¶ms) { + compressor + } else if let Some(compressor) = try_byte_stream_split_miniblock(data, ¶ms) { + compressor + } else if let Some(compressor) = try_fixed_u8_rle_miniblock(data, ¶ms) { + compressor + } else if let Some(compressor) = try_bitpacking_miniblock(data) { + compressor + } else if let Some(compressor) = try_raw_fixed_width_miniblock(data) { + compressor + } else if let Some(compressor) = try_variable_width_miniblock(field, data, ¶ms)? { + compressor + } else if let Some(compressor) = try_fixed_packed_struct_miniblock(data)? { + compressor + } else if let Some(compressor) = try_raw_fixed_size_list_miniblock(data) { + compressor + } else { + return Err(Error::not_supported_source( + format!( + "Mini-block compression not yet supported for block type {}", + data.name() + ) + .into(), + )); + }; + finalize_miniblock_compressor(data, compressor, ¶ms) + } + + fn create_per_value( + &self, + field: &Field, + data: &DataBlock, + ) -> Result> { + let params = self.field_params(field); + if let Some(compressor) = try_raw_per_value(data) { + return Ok(compressor); + } + let packed = match self.encoding { + BenchEncoding::StructuralU16 => reject_packed_struct_per_value(field, data)?, + BenchEncoding::StructuralU32 => { + try_variable_packed_struct_per_value(Arc::new(self.clone()), field, data)? + } + BenchEncoding::Array => unreachable!(), + }; + if let Some(compressor) = packed { + return Ok(compressor); + } + if let Some(compressor) = try_variable_width_per_value(field, data, ¶ms)? { + return Ok(compressor); + } + Err(Error::not_supported_source( + format!( + "Per-value compression not yet supported for block type {}", + data.name() + ) + .into(), + )) + } + + fn create_block_compressor( + &self, + field: &Field, + data: &DataBlock, + ) -> Result<(Box, CompressiveEncoding)> { + let params = self.field_params(field); + if self.encoding == BenchEncoding::StructuralU32 + && let Some(compressor) = try_fixed_u8_rle_block(data, ¶ms)? + { + return Ok(compressor); + } + if let Some(compressor) = try_bitpacking_block(data) { + return Ok(compressor); + } + if self.encoding == BenchEncoding::StructuralU32 + && let Some(compressor) = try_general_block(data, ¶ms)? + { + return Ok(compressor); + } + if let Some(compressor) = try_raw_block(data) { + return Ok(compressor); + } + Err(Error::not_supported_source( + format!( + "Block compression not yet supported for block type {}", + data.name() + ) + .into(), + )) + } +} + +#[derive(Debug)] +struct BenchFieldEncodingStrategy { + encoding: BenchEncoding, + primitive: PrimitiveFieldEncoding, +} + +impl FieldEncodingStrategy for BenchFieldEncodingStrategy { + fn create_field_encoder( + &self, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, + ) -> Result> { + if let Some(encoder) = + try_create_binary_blob(&self.primitive, field, column_index, context)? + { + return Ok(encoder); + } + if self.encoding == BenchEncoding::StructuralU32 + && let Some(encoder) = + try_create_structural_blob(&self.primitive, field, column_index, context)? + { + return Ok(encoder); + } + if field.is_blob() { + return Err(Error::invalid_input_source( + format!( + "Blob encoding is not available for field '{}' with data type {}", + field.name, + field.data_type() + ) + .into(), + )); + } + if self.encoding == BenchEncoding::StructuralU32 { + if let Some(encoder) = try_create_map(field, column_index, context)? { + return Ok(encoder); + } + if let Some(encoder) = + try_create_structural_fixed_size_list(field, column_index, context)? + { + return Ok(encoder); + } + } + if let Some(encoder) = self.primitive.try_create(field, column_index, context)? { + return Ok(encoder); + } + if self.encoding == BenchEncoding::StructuralU16 { + if matches!( + field.data_type(), + DataType::FixedSizeList(item, _) + if matches!(item.data_type(), DataType::Struct(_)) + ) { + return Err(Error::not_supported_source( + "FixedSizeList is not enabled by the selected file format".into(), + )); + } + if matches!(field.data_type(), DataType::Map(_, _)) { + return Err(Error::not_supported_source( + "Map data type is not enabled by the selected file format".into(), + )); + } + } + if let Some(encoder) = try_create_list(field, column_index, context)? { + return Ok(encoder); + } + if let Some(encoder) = try_create_struct(field, column_index, context)? { + return Ok(encoder); + } + Err(Error::not_supported_source( + format!( + "{} has no field encoding for '{}' with data type {}", + self.encoding, + field.name, + field.data_type() + ) + .into(), + )) + } +} + +pub fn encoding_strategy(encoding: BenchEncoding) -> Box { + if encoding == BenchEncoding::Array { + return Box::new(lance_encoding::encoder::ArrayFieldEncodingStrategy::new()); + } + + let compression = Arc::new(BenchCompressionStrategy { + encoding, + params: CompressionParams::default(), + }); + let page_encodings = match encoding { + BenchEncoding::StructuralU16 => vec![ + PrimitivePageEncoding::reject_sparse(), + PrimitivePageEncoding::dense_u16(compression), + ], + BenchEncoding::StructuralU32 => vec![ + PrimitivePageEncoding::reject_sparse(), + PrimitivePageEncoding::constant(), + PrimitivePageEncoding::dense_u32(compression), + ], + BenchEncoding::Array => unreachable!(), + }; + Box::new(BenchFieldEncodingStrategy { + encoding, + primitive: PrimitiveFieldEncoding::new(page_encodings), + }) +} diff --git a/lance-artifact/rust/lance-encoding/benches/decoder.rs b/lance-artifact/rust/lance-encoding/benches/decoder.rs new file mode 100644 index 000000000..98d7caaf9 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/benches/decoder.rs @@ -0,0 +1,738 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors +use std::{collections::HashMap, hint::black_box, sync::Arc}; + +use arrow_array::{RecordBatch, UInt32Array}; +#[cfg(feature = "bitpacking")] +use arrow_buffer::ArrowNativeType; +use arrow_schema::{DataType, Field, Schema, TimeUnit}; +use arrow_select::take::take; +#[cfg(feature = "bitpacking")] +use bytemuck::Pod; +use criterion::{Criterion, criterion_group, criterion_main}; +use futures::StreamExt; +#[cfg(feature = "bitpacking")] +use lance_bitpacking::BitPacking; +use lance_core::cache::LanceCache; +use lance_datagen::ArrayGeneratorExt; +#[cfg(feature = "bitpacking")] +use lance_encoding::buffer::LanceBuffer; +#[cfg(feature = "bitpacking")] +use lance_encoding::compression::BlockDecompressor; +#[cfg(feature = "bitpacking")] +use lance_encoding::data::{BlockInfo, DataBlock, FixedWidthDataBlock}; +#[cfg(feature = "bitpacking")] +use lance_encoding::encodings::physical::bitpacking::{ELEMS_PER_CHUNK, InlineBitpacking}; +use lance_encoding::{ + decoder::{ + DecodeBatchScheduler, DecoderConfig, DecoderPlugins, EncodedBatchLayout, FilterExpression, + create_decode_stream, + }, + encoder::{EncodingOptions, encode_batch}, +}; +use tokio::sync::mpsc::unbounded_channel; + +use rand::Rng; + +pub mod common; +use common::{BenchEncoding, encoding_strategy}; + +const PRIMITIVE_TYPES: &[DataType] = &[ + DataType::Date32, + DataType::Date64, + DataType::Int8, + DataType::Int16, + DataType::Int32, + DataType::Int64, + DataType::UInt8, + DataType::UInt16, + DataType::UInt32, + DataType::UInt64, + DataType::Float16, + DataType::Float32, + DataType::Float64, + DataType::Decimal128(10, 10), + DataType::Decimal256(10, 10), + DataType::Timestamp(TimeUnit::Nanosecond, None), + DataType::Time32(TimeUnit::Second), + DataType::Time64(TimeUnit::Nanosecond), + DataType::Duration(TimeUnit::Second), + // The Interval type is supported by the reader but the writer works with Lance schema + // at the moment and Lance schema can't parse interval + // DataType::Interval(IntervalUnit::DayTime), +]; + +// Some types are supported by the encoder/decoder but Lance +// schema doesn't yet parse them in the context of a fixed size list. +const PRIMITIVE_TYPES_FOR_FSL: &[DataType] = &[DataType::Int8, DataType::Float32]; + +fn encoded_batch_layout(encoding: BenchEncoding) -> EncodedBatchLayout { + match encoding { + BenchEncoding::Array => EncodedBatchLayout::Array, + BenchEncoding::StructuralU16 | BenchEncoding::StructuralU32 => { + EncodedBatchLayout::Structural + } + } +} + +fn bench_decode(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_primitive"); + const NUM_BYTES: u64 = 1024 * 1024 * 128; + group.throughput(criterion::Throughput::Bytes(NUM_BYTES)); + for data_type in PRIMITIVE_TYPES { + let func_name = format!("{:?}", data_type).to_lowercase(); + let num_rows = NUM_BYTES / data_type.primitive_width().unwrap() as u64; + group.bench_function(func_name, |b| { + let data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(data_type)) + .into_batch_rows(lance_datagen::RowCount::from(num_rows)) + .unwrap(); + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(data.schema().as_ref()).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU16); + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Structural, + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }); + } +} + +fn bench_decode_fsl(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_fsl"); + const NUM_BYTES: u64 = 1024 * 1024 * 128; + for encoding in [ + BenchEncoding::Array, + BenchEncoding::StructuralU16, + BenchEncoding::StructuralU32, + ] { + for data_type in PRIMITIVE_TYPES_FOR_FSL { + for dimension in [4, 16, 32, 64, 128] { + let nullable_choices: &[bool] = if encoding == BenchEncoding::Array { + &[false] + } else { + &[false, true] + }; + for nullable in nullable_choices { + let func_name = format!( + "{:?}_{}_v{}_null{}", + data_type, dimension, encoding, nullable + ) + .to_lowercase(); + group.throughput(criterion::Throughput::Bytes(NUM_BYTES)); + group.bench_function(func_name, |b| { + let num_rows = + NUM_BYTES / (dimension * data_type.primitive_width().unwrap() as u64); + let mut arraygen = + lance_datagen::array::rand_type(&DataType::FixedSizeList( + Arc::new(Field::new("item", data_type.clone(), true)), + dimension as i32, + )); + if *nullable { + arraygen = arraygen.with_random_nulls(0.5); + } + let data = lance_datagen::gen_batch() + .anon_col(arraygen) + .into_batch_rows(lance_datagen::RowCount::from(num_rows)) + .unwrap(); + let lance_schema = Arc::new( + lance_core::datatypes::Schema::try_from(data.schema().as_ref()) + .unwrap(), + ); + let encoding_strategy = encoding_strategy(encoding); + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + encoded_batch_layout(encoding), + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }); + } + } + } + } +} + +fn bench_decode_str_with_dict_encoding(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_primitive"); + const NUM_ROWS: u64 = 100000; + + let data_type = DataType::Utf8; + // generate string column with 20 rows + let string_data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(&DataType::Utf8)) + .into_batch_rows(lance_datagen::RowCount::from(20)) + .unwrap(); + + group.throughput(criterion::Throughput::Bytes( + NUM_ROWS * std::mem::size_of::() as u64 + string_data.get_array_memory_size() as u64, + )); + + let func_name = format!("{:?}", data_type).to_lowercase(); + group.bench_function(func_name, |b| { + let string_array = string_data.column(0); + + // generate random int column with 100000 rows + let mut rng = rand::rng(); + let integer_arr: Vec = (0..100_000).map(|_| rng.random_range(0..20)).collect(); + let integer_array = UInt32Array::from(integer_arr); + + let mapped_strings = take(string_array, &integer_array, None).unwrap(); + + let schema = Arc::new(Schema::new(vec![Field::new( + "string", + DataType::Utf8, + false, + )])); + + let data = RecordBatch::try_new(schema, vec![Arc::new(mapped_strings)]).unwrap(); + + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(data.schema().as_ref()).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU16); + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Structural, + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }); +} + +fn bench_decode_packed_struct(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_primitive"); + + const NUM_ROWS: u64 = 10000; + let size_bytes = + ((6 * std::mem::size_of::() as u64) + std::mem::size_of::() as u64) * NUM_ROWS; + group.throughput(criterion::Throughput::Bytes(size_bytes)); + + let func_name = "struct"; + group.bench_function(func_name, |b| { + let fields = vec![ + Arc::new(Field::new("int_field", DataType::Int32, false)), + Arc::new(Field::new("float_field", DataType::Float32, false)), + Arc::new(Field::new( + "fsl_field", + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Int32, true)), 5), + false, + )), + ] + .into(); + + // generate struct column with 1M rows + let data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(&DataType::Struct(fields))) + .into_batch_rows(lance_datagen::RowCount::from(NUM_ROWS)) + .unwrap(); + + let schema = data.schema(); + let new_fields: Vec> = schema + .fields() + .iter() + .map(|field| { + if matches!(field.data_type(), &DataType::Struct(_)) { + let mut metadata = HashMap::new(); + metadata.insert("packed".to_string(), "true".to_string()); + let field = + Field::new(field.name(), field.data_type().clone(), field.is_nullable()); + Arc::new(field.with_metadata(metadata)) + } else { + field.clone() + } + }) + .collect(); + + let new_schema = Schema::new(new_fields); + let data = + RecordBatch::try_new(Arc::new(new_schema.clone()), data.columns().to_vec()).unwrap(); + + let lance_schema = Arc::new(lance_core::datatypes::Schema::try_from(&new_schema).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU32); + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Structural, + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }); +} + +#[cfg(target_os = "linux")] +fn bench_decode_str_with_fixed_size_binary_encoding(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_primitive"); + + const NUM_ROWS: u64 = 10000; + // Randomly generated strings are always 12 characters (at the moment) + // Plus we need 4 bytes for the offset + const NUM_BYTES: u64 = NUM_ROWS * 16; + group.throughput(criterion::Throughput::Bytes(NUM_BYTES)); + + let func_name = "fixed-utf8".to_string(); + group.bench_function(func_name, |b| { + // generate string column with 10k rows + // Currently the generator generates fixed size strings by default + // This function will need to be updated once that changes. + let string_data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(&DataType::Utf8)) + .into_batch_rows(lance_datagen::RowCount::from(10000)) + .unwrap(); + + let schema = Arc::new(Schema::new(vec![Field::new( + "string", + DataType::Utf8, + false, + )])); + + let data = RecordBatch::try_new(schema, string_data.columns().to_vec()).unwrap(); + + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(data.schema().as_ref()).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU16); + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Structural, + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }); +} + +fn bench_decode_compressed(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_compressed"); + + const NUM_ROWS: usize = 5_000_000; + const NUM_COLUMNS: usize = 10; + + // Generate compressible string data - high cardinality but compressible + // (unique values to avoid dictionary encoding, repeated prefix for compression) + let array: Arc = Arc::new(arrow_array::StringArray::from_iter_values( + (0..NUM_ROWS).map(|i| format!("prefix_that_compresses_well_{}", i)), + )); + + for compression in ["zstd", "lz4"] { + let mut metadata = HashMap::new(); + metadata.insert( + "lance-encoding:compression".to_string(), + compression.to_string(), + ); + // Disable dictionary encoding to ensure we hit the compression path + metadata.insert( + "lance-encoding:dict-divisor".to_string(), + "100000".to_string(), + ); + // Force miniblock encoding (the path that benefits from compressor caching) + metadata.insert( + "lance-encoding:structural-encoding".to_string(), + "miniblock".to_string(), + ); + let fields: Vec = (0..NUM_COLUMNS) + .map(|i| { + Field::new(format!("s{}", i), DataType::Utf8, false).with_metadata(metadata.clone()) + }) + .collect(); + let columns: Vec> = + (0..NUM_COLUMNS).map(|_| array.clone()).collect(); + let schema = Arc::new(Schema::new(fields)); + let data = RecordBatch::try_new(schema.clone(), columns).unwrap(); + + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(schema.as_ref()).unwrap()); + // V2_2+ required for general compression + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU32); + + // Encode once during setup + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + + group.throughput(criterion::Throughput::Elements( + (NUM_ROWS * NUM_COLUMNS) as u64, + )); + group.bench_function( + format!("{}_strings_{}cols", compression, NUM_COLUMNS), + |b| { + b.iter(|| { + let batch = rt + .block_on(lance_encoding::decoder::decode_batch( + &encoded, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Structural, + Some(Arc::new(LanceCache::no_cache())), + )) + .unwrap(); + assert_eq!(data.num_rows(), batch.num_rows()); + }) + }, + ); + } +} + +/// Benchmark parallel decoding with multiple concurrent batch decode tasks. +/// This creates contention on the shared decompressor mutex when multiple +/// batches from the same page are decoded in parallel. +fn bench_decode_compressed_parallel(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("decode_compressed_parallel"); + + const NUM_ROWS: u64 = 1_000_000; + const NUM_COLUMNS: usize = 10; + // Small batch size to create many batches that will contend on the same decompressor + const BATCH_SIZE: u32 = 100_000; + + let array: Arc = Arc::new(arrow_array::StringArray::from_iter_values( + (0..NUM_ROWS as usize).map(|i| format!("prefix_that_compresses_well_{}", i)), + )); + + for compression in ["zstd", "lz4"] { + let mut metadata = HashMap::new(); + metadata.insert( + "lance-encoding:compression".to_string(), + compression.to_string(), + ); + metadata.insert( + "lance-encoding:dict-divisor".to_string(), + "100000".to_string(), + ); + metadata.insert( + "lance-encoding:structural-encoding".to_string(), + "miniblock".to_string(), + ); + let fields: Vec = (0..NUM_COLUMNS) + .map(|i| { + Field::new(format!("s{}", i), DataType::Utf8, false).with_metadata(metadata.clone()) + }) + .collect(); + let columns: Vec> = + (0..NUM_COLUMNS).map(|_| array.clone()).collect(); + let schema = Arc::new(Schema::new(fields)); + let data = RecordBatch::try_new(schema.clone(), columns).unwrap(); + + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(schema.as_ref()).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU32); + + let encoded = rt + .block_on(encode_batch( + &data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); + + let encoded = Arc::new(encoded); + + // Test with different parallelism levels to see impact of mutex contention + // parallelism=1 is sequential (no contention), higher values cause contention + for parallelism in [1, 8] { + group.throughput(criterion::Throughput::Elements( + NUM_ROWS * NUM_COLUMNS as u64, + )); + group.bench_function( + format!( + "{}_{}cols_parallel_{}", + compression, NUM_COLUMNS, parallelism + ), + |b| { + b.iter(|| { + rt.block_on(async { + let io_scheduler = Arc::new(lance_encoding::BufferScheduler::new( + encoded.data.clone(), + )) + as Arc; + let cache = Arc::new(LanceCache::no_cache()); + let filter = FilterExpression::no_filter(); + + let mut decode_scheduler = DecodeBatchScheduler::try_new( + encoded.schema.as_ref(), + &encoded.top_level_columns, + &encoded.page_table, + &vec![], + encoded.num_rows, + Arc::::default(), + io_scheduler.clone(), + cache, + &filter, + &DecoderConfig::default(), + ) + .await + .unwrap(); + + let (tx, rx) = unbounded_channel(); + decode_scheduler.schedule_range( + 0..encoded.num_rows, + &filter, + tx, + io_scheduler, + ); + + let decode_stream = create_decode_stream( + &encoded.schema, + encoded.num_rows, + BATCH_SIZE, + true, // is_structural for V2_2 + false, + false, + rx, + None, + ) + .unwrap(); + + // Buffer multiple batch decodes in parallel - this causes contention + let batches: Vec<_> = decode_stream + .map(|task| task.task) + .buffered(parallelism) + .collect() + .await; + + let total_rows: usize = + batches.iter().map(|b| b.as_ref().unwrap().num_rows()).sum(); + assert_eq!(total_rows, NUM_ROWS as usize); + }) + }) + }, + ); + } + } +} + +#[cfg(feature = "bitpacking")] +fn make_inline_bitpacking_chunk(bit_width: usize) -> LanceBuffer +where + T: ArrowNativeType + BitPacking + Pod, +{ + let value_range = 1_usize << bit_width; + let values: Vec = (0..ELEMS_PER_CHUNK as usize) + .map(|i| T::from_usize((i * 31 + 7) % value_range).unwrap()) + .collect(); + let packed_words = ELEMS_PER_CHUNK as usize * bit_width / (std::mem::size_of::() * 8); + + let mut chunk = Vec::with_capacity(1 + packed_words); + chunk.push(T::from_usize(bit_width).unwrap()); + let payload_start = chunk.len(); + chunk.resize(payload_start + packed_words, T::from_usize(0).unwrap()); + unsafe { + BitPacking::unchecked_pack(bit_width, &values, &mut chunk[payload_start..]); + } + + LanceBuffer::reinterpret_vec(chunk) +} + +#[cfg(feature = "bitpacking")] +fn read_little_endian_header(bytes: &[u8]) -> usize { + bytes[..std::mem::size_of::()] + .iter() + .enumerate() + .fold(0_u64, |value, (idx, byte)| { + value | ((*byte as u64) << (idx * 8)) + }) as usize +} + +#[cfg(feature = "bitpacking")] +fn legacy_copy_unchunk(data: LanceBuffer, num_values: u64) -> DataBlock +where + T: ArrowNativeType + BitPacking + Pod, +{ + assert!(data.len() >= std::mem::size_of::()); + assert!(num_values <= ELEMS_PER_CHUNK); + + let chunk_in_u8 = data.to_vec(); + let bit_width_value = read_little_endian_header::(&chunk_in_u8); + let chunk = bytemuck::cast_slice(&chunk_in_u8[std::mem::size_of::()..]); + assert!(std::mem::size_of_val(chunk) == bit_width_value * ELEMS_PER_CHUNK as usize / 8); + + let mut decompressed = vec![T::from_usize(0).unwrap(); ELEMS_PER_CHUNK as usize]; + unsafe { + BitPacking::unchecked_unpack(bit_width_value, chunk, &mut decompressed); + } + + decompressed.truncate(num_values as usize); + DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(decompressed), + bits_per_value: (std::mem::size_of::() * 8) as u64, + num_values, + block_info: BlockInfo::new(), + }) +} + +#[cfg(feature = "bitpacking")] +fn typed_view_unchunk(buffer: LanceBuffer, uncompressed_bits: u64, num_values: u64) -> DataBlock { + InlineBitpacking::new(uncompressed_bits) + .decompress(buffer, num_values) + .unwrap() +} + +#[cfg(feature = "bitpacking")] +fn assert_same_fixed_width_payloads(legacy: &DataBlock, typed_view: &DataBlock) { + let legacy = legacy.as_fixed_width_ref().unwrap(); + let typed_view = typed_view.as_fixed_width_ref().unwrap(); + + assert_eq!(legacy.num_values, typed_view.num_values); + assert_eq!(legacy.bits_per_value, typed_view.bits_per_value); + assert_eq!(legacy.data.as_ref(), typed_view.data.as_ref()); +} + +#[cfg(feature = "bitpacking")] +fn bench_inline_bitpacking_case( + group: &mut criterion::BenchmarkGroup<'_, criterion::measurement::WallTime>, + name: &str, + bit_width: usize, +) where + T: ArrowNativeType + BitPacking + Pod, +{ + let buffer = make_inline_bitpacking_chunk::(bit_width); + let compressed_bytes = buffer.len() as u64; + let uncompressed_bits = (std::mem::size_of::() * 8) as u64; + group.throughput(criterion::Throughput::Bytes(compressed_bytes)); + + let legacy = legacy_copy_unchunk::(buffer.clone(), ELEMS_PER_CHUNK); + let typed_view = typed_view_unchunk(buffer.clone(), uncompressed_bits, ELEMS_PER_CHUNK); + assert_same_fixed_width_payloads(&legacy, &typed_view); + + group.bench_function(format!("{name}/legacy_copy/compressed_bytes"), |b| { + b.iter(|| { + let decoded = + legacy_copy_unchunk::(black_box(buffer.clone()), black_box(ELEMS_PER_CHUNK)); + let fixed = decoded.as_fixed_width().unwrap(); + black_box(fixed.data.as_ref()); + }) + }); + + group.bench_function(format!("{name}/typed_view/compressed_bytes"), |b| { + b.iter(|| { + let decoded = typed_view_unchunk( + black_box(buffer.clone()), + black_box(uncompressed_bits), + black_box(ELEMS_PER_CHUNK), + ); + let fixed = decoded.as_fixed_width().unwrap(); + black_box(fixed.data.as_ref()); + }) + }); +} + +#[cfg(feature = "bitpacking")] +fn bench_decode_inline_bitpacking_unchunk(c: &mut Criterion) { + let mut group = c.benchmark_group("decode_inline_bitpacking_unchunk"); + bench_inline_bitpacking_case::(&mut group, "u32_bw12_1024", 12); + bench_inline_bitpacking_case::(&mut group, "u64_bw23_1024", 23); + group.finish(); +} + +#[cfg(not(feature = "bitpacking"))] +fn bench_decode_inline_bitpacking_unchunk(c: &mut Criterion) { + let mut group = c.benchmark_group("decode_inline_bitpacking_unchunk"); + group.bench_function("bitpacking_feature_disabled", |b| b.iter(|| black_box(()))); + group.finish(); +} + +#[cfg(target_os = "linux")] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10) + .with_profiler(lance_testing::pprof::PProfProfiler::new(100, lance_testing::pprof::Output::Flamegraph(None))); + targets = bench_decode, bench_decode_fsl, bench_decode_str_with_dict_encoding, bench_decode_packed_struct, + bench_decode_str_with_fixed_size_binary_encoding, bench_decode_compressed, + bench_decode_compressed_parallel, bench_decode_inline_bitpacking_unchunk); + +// Non-linux version does not support pprof. +#[cfg(not(target_os = "linux"))] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10); + targets = bench_decode, bench_decode_fsl, bench_decode_str_with_dict_encoding, bench_decode_packed_struct, + bench_decode_compressed, bench_decode_compressed_parallel, bench_decode_inline_bitpacking_unchunk); +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-encoding/benches/encoder.rs b/lance-artifact/rust/lance-encoding/benches/encoder.rs new file mode 100644 index 000000000..02ffd9208 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/benches/encoder.rs @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashMap, sync::Arc}; + +use arrow_array::{ArrayRef, BooleanArray, ListArray, RecordBatch}; +use arrow_buffer::{OffsetBuffer, ScalarBuffer}; +use arrow_schema::{DataType, Field, Schema}; +use criterion::{Criterion, criterion_group, criterion_main}; +use lance_encoding::encoder::{EncodingOptions, encode_batch}; + +pub mod common; +use common::{BenchEncoding, encoding_strategy}; + +fn encode_batch_sync(rt: &tokio::runtime::Runtime, data: &RecordBatch) { + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(data.schema().as_ref()).unwrap()); + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU32); + + rt.block_on(encode_batch( + data, + lance_schema, + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap(); +} + +fn bench_encode_compressed(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("encode_compressed"); + + const NUM_ROWS: usize = 5_000_000; + const NUM_COLUMNS: usize = 10; + + // Generate compressible string data - high cardinality but compressible + // (unique values to avoid dictionary encoding, repeated prefix for compression) + let array: Arc = Arc::new(arrow_array::StringArray::from_iter_values( + (0..NUM_ROWS).map(|i| format!("prefix_that_compresses_well_{}", i)), + )); + + for compression in ["zstd", "lz4"] { + let mut metadata = HashMap::new(); + metadata.insert( + "lance-encoding:compression".to_string(), + compression.to_string(), + ); + // Disable dictionary encoding to ensure we hit the compression path + metadata.insert( + "lance-encoding:dict-divisor".to_string(), + "100000".to_string(), + ); + // Force miniblock encoding (the path that benefits from compressor caching) + metadata.insert( + "lance-encoding:structural-encoding".to_string(), + "miniblock".to_string(), + ); + let fields: Vec = (0..NUM_COLUMNS) + .map(|i| { + Field::new(format!("s{}", i), DataType::Utf8, false).with_metadata(metadata.clone()) + }) + .collect(); + let columns: Vec> = + (0..NUM_COLUMNS).map(|_| array.clone()).collect(); + let schema = Arc::new(Schema::new(fields)); + let data = RecordBatch::try_new(schema.clone(), columns).unwrap(); + + let lance_schema = + Arc::new(lance_core::datatypes::Schema::try_from(schema.as_ref()).unwrap()); + // V2_2+ required for general compression + let encoding_strategy = encoding_strategy(BenchEncoding::StructuralU32); + + group.throughput(criterion::Throughput::Elements( + (NUM_ROWS * NUM_COLUMNS) as u64, + )); + group.bench_function( + format!("{}_strings_{}cols", compression, NUM_COLUMNS), + |b| { + b.iter(|| { + rt.block_on(encode_batch( + &data, + lance_schema.clone(), + encoding_strategy.as_ref(), + &EncodingOptions::default(), + )) + .unwrap() + }) + }, + ); + } +} + +fn make_boolean_list_batch(array: ArrayRef, nullable: bool) -> RecordBatch { + let item_field = Arc::new(Field::new("item", DataType::Boolean, true)); + let field = Field::new("values", DataType::List(item_field), nullable); + let schema = Arc::new(Schema::new(vec![field])); + RecordBatch::try_new(schema, vec![array]).unwrap() +} + +fn dense_boolean_list(num_rows: usize, booleans_per_list: usize) -> ArrayRef { + let mut offsets = Vec::with_capacity(num_rows + 1); + let mut values = Vec::with_capacity(num_rows * booleans_per_list); + offsets.push(0i32); + + for _ in 0..num_rows { + values.extend((0..booleans_per_list).map(|idx| idx % 2 == 0)); + offsets.push(values.len() as i32); + } + + Arc::new(ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(BooleanArray::from(values)), + None, + )) +} + +fn sparse_boolean_list( + num_rows: usize, + num_non_empty: usize, + booleans_per_list: usize, +) -> ArrayRef { + let step = num_rows / num_non_empty; + let mut offsets = Vec::with_capacity(num_rows + 1); + let mut values = Vec::with_capacity(num_non_empty * booleans_per_list); + offsets.push(0i32); + + let mut next_non_empty = step / 2; + for row in 0..num_rows { + if row == next_non_empty { + values.extend((0..booleans_per_list).map(|idx| idx % 2 == 0)); + next_non_empty += step; + } + offsets.push(values.len() as i32); + } + + Arc::new(ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(BooleanArray::from(values)), + None, + )) +} + +fn bench_encode_structural_pages(c: &mut Criterion) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut group = c.benchmark_group("encode_structural_pages"); + + const NUM_ROWS: usize = 200_000; + const BOOLEANS_PER_LIST: usize = 8; + + let dense = make_boolean_list_batch(dense_boolean_list(NUM_ROWS, BOOLEANS_PER_LIST), false); + let sparse = + make_boolean_list_batch(sparse_boolean_list(NUM_ROWS, 10, BOOLEANS_PER_LIST), false); + + group.throughput(criterion::Throughput::Elements(NUM_ROWS as u64)); + group.bench_function("dense_boolean_list", |b| { + b.iter(|| encode_batch_sync(&rt, &dense)) + }); + group.bench_function("sparse_boolean_list", |b| { + b.iter(|| encode_batch_sync(&rt, &sparse)) + }); +} + +#[cfg(target_os = "linux")] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10) + .with_profiler(lance_testing::pprof::PProfProfiler::new(100, lance_testing::pprof::Output::Flamegraph(None))); + targets = bench_encode_compressed, bench_encode_structural_pages); + +#[cfg(not(target_os = "linux"))] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10); + targets = bench_encode_compressed, bench_encode_structural_pages); + +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-encoding/build.rs b/lance-artifact/rust/lance-encoding/build.rs new file mode 100644 index 000000000..92fe03589 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/build.rs @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::io::Result; + +fn main() -> Result<()> { + println!("cargo:rerun-if-changed=protos"); + + #[cfg(feature = "protoc")] + // Use vendored protobuf compiler if requested. + unsafe { + std::env::set_var("PROTOC", protobuf_src::protoc()); + } + + let mut prost_build = prost_build::Config::new(); + prost_build.protoc_arg("--experimental_allow_proto3_optional"); + prost_build.enable_type_names(); + prost_build.bytes(["."]); // Enable Bytes type for all messages to avoid Vec clones. + prost_build.compile_protos(&["./protos/encodings_v2_0.proto"], &["./protos"])?; + prost_build.compile_protos(&["./protos/encodings_v2_1.proto"], &["./protos"])?; + + Ok(()) +} diff --git a/lance-artifact/rust/lance-encoding/protos b/lance-artifact/rust/lance-encoding/protos new file mode 120000 index 000000000..3d021e597 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/protos @@ -0,0 +1 @@ +../../protos/ \ No newline at end of file diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding.rs b/lance-artifact/rust/lance-encoding/src/array_encoding.rs new file mode 100644 index 000000000..a76c6a186 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Encoding and decoding mechanisms described by [`crate::format::pb::ArrayEncoding`]. +//! +//! File versions decide which mechanisms to compose and accept. This module +//! contains only the reusable implementation of that persisted grammar. + +pub mod logical; +pub mod physical; +mod strategy; + +pub use strategy::ArrayFieldEncodingStrategy; diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical.rs new file mode 100644 index 000000000..bdc981b2c --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical.rs @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod binary; +pub mod blob; +pub mod list; +pub mod primitive; +pub mod r#struct; diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical/binary.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/binary.rs new file mode 100644 index 000000000..697c1503e --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/binary.rs @@ -0,0 +1,186 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_array::{ + Array, ArrayRef, GenericByteArray, GenericListArray, + cast::AsArray, + types::{BinaryType, ByteArrayType, LargeBinaryType, LargeUtf8Type, UInt8Type, Utf8Type}, +}; + +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; +use lance_core::Result; +use log::trace; + +use crate::{ + decoder::{ + DecodeArrayTask, FilterExpression, MessageType, NextDecodeTask, PriorityRange, + ScheduledScanLine, SchedulerContext, + }, + decoder::{DecoderReady, FieldScheduler, LogicalPageDecoder, SchedulingJob}, +}; + +/// Wraps a varbin scheduler and uses a BinaryPageDecoder to cast +/// the result to the appropriate type +#[derive(Debug)] +pub struct BinarySchedulingJob<'a> { + scheduler: &'a BinaryFieldScheduler, + inner: Box, +} + +impl SchedulingJob for BinarySchedulingJob<'_> { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result { + let inner_scan = self.inner.schedule_next(context, priority)?; + let wrapped_decoders = inner_scan + .decoders + .into_iter() + .map(|message| { + let decoder = message.into_array(); + MessageType::DecoderReady(DecoderReady { + path: decoder.path, + decoder: Box::new(BinaryPageDecoder { + inner: decoder.decoder, + data_type: self.scheduler.data_type.clone(), + }), + }) + }) + .collect::>(); + Ok(ScheduledScanLine { + decoders: wrapped_decoders, + rows_scheduled: inner_scan.rows_scheduled, + }) + } + + fn num_rows(&self) -> u64 { + self.inner.num_rows() + } +} + +/// A logical scheduler for utf8/binary pages which assumes the data are encoded as `List` +#[derive(Debug)] +pub struct BinaryFieldScheduler { + varbin_scheduler: Arc, + data_type: DataType, +} + +impl BinaryFieldScheduler { + // Create a new ListPageScheduler + pub fn new(varbin_scheduler: Arc, data_type: DataType) -> Self { + Self { + varbin_scheduler, + data_type, + } + } +} + +impl FieldScheduler for BinaryFieldScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[std::ops::Range], + filter: &FilterExpression, + ) -> Result> { + trace!("Scheduling binary for {} ranges", ranges.len()); + let varbin_job = self.varbin_scheduler.schedule_ranges(ranges, filter)?; + Ok(Box::new(BinarySchedulingJob { + scheduler: self, + inner: varbin_job, + })) + } + + fn num_rows(&self) -> u64 { + self.varbin_scheduler.num_rows() + } + + fn initialize<'a>( + &'a self, + _filter: &'a FilterExpression, + _context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + // 2.0 schedulers do not need to initialize + std::future::ready(Ok(())).boxed() + } +} + +#[derive(Debug)] +pub struct BinaryPageDecoder { + inner: Box, + data_type: DataType, +} + +impl LogicalPageDecoder for BinaryPageDecoder { + fn wait_for_loaded(&mut self, num_rows: u64) -> BoxFuture<'_, Result<()>> { + self.inner.wait_for_loaded(num_rows) + } + + fn drain(&mut self, num_rows: u64) -> Result { + let inner_task = self.inner.drain(num_rows)?; + Ok(NextDecodeTask { + num_rows: inner_task.num_rows, + task: Box::new(BinaryArrayDecoder { + inner: inner_task.task, + data_type: self.data_type.clone(), + }), + }) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } + + fn rows_loaded(&self) -> u64 { + self.inner.rows_loaded() + } + + fn num_rows(&self) -> u64 { + self.inner.num_rows() + } + + fn rows_drained(&self) -> u64 { + self.inner.rows_drained() + } +} + +pub struct BinaryArrayDecoder { + inner: Box, + data_type: DataType, +} + +impl BinaryArrayDecoder { + fn from_list_array(array: &GenericListArray) -> ArrayRef { + let values = array + .values() + .as_primitive::() + .values() + .inner() + .clone(); + let offsets = array.offsets().clone(); + Arc::new(GenericByteArray::::new( + offsets, + values, + array.nulls().cloned(), + )) + } +} + +impl DecodeArrayTask for BinaryArrayDecoder { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let data_type = self.data_type; + let (arr, _) = self.inner.decode()?; + let result = match data_type { + DataType::Binary => Self::from_list_array::(arr.as_list::()), + DataType::LargeBinary => Self::from_list_array::(arr.as_list::()), + DataType::Utf8 => Self::from_list_array::(arr.as_list::()), + DataType::LargeUtf8 => Self::from_list_array::(arr.as_list::()), + _ => panic!("Binary decoder does not support this data type"), + }; + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok((result, 0)) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical/blob.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/blob.rs new file mode 100644 index 000000000..0bf3a39fc --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/blob.rs @@ -0,0 +1,532 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::VecDeque, sync::Arc, vec}; + +use arrow_array::{ + Array, ArrayRef, LargeBinaryArray, PrimitiveArray, StructArray, UInt64Array, cast::AsArray, + types::UInt64Type, +}; +use arrow_buffer::{ + BooleanBuffer, BooleanBufferBuilder, Buffer, NullBuffer, OffsetBuffer, ScalarBuffer, +}; +use arrow_schema::DataType; +use bytes::Bytes; +use futures::{FutureExt, future::BoxFuture}; + +use lance_core::{Error, Result, datatypes::BLOB_DESC_FIELDS}; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + decoder::{ + DecodeArrayTask, FilterExpression, MessageType, NextDecodeTask, PriorityRange, + ScheduledScanLine, SchedulerContext, + }, + decoder::{DecoderReady, FieldScheduler, LogicalPageDecoder, SchedulingJob}, + encoder::{EncodeTask, FieldEncoder, OutOfLineBuffers}, + format::pb::{Blob, ColumnEncoding, column_encoding}, + repdef::RepDefBuilder, +}; + +/// A field scheduler for large binary data +/// +/// Large binary data (1MiB+) can be inefficient if we store as a regular primitive. We +/// essentially end up with 1 page per row (or a few rows) and the overhead of the +/// metadata can be significant. +/// +/// At the same time the benefits of using pages (contiguous arrays) are pretty small since +/// we can generally perform random access at these sizes without much penalty. +/// +/// This encoder gives up the random access and stores the large binary data out of line. This +/// keeps the metadata small. +#[derive(Debug)] +pub struct BlobFieldScheduler { + descriptions_scheduler: Arc, +} + +impl BlobFieldScheduler { + pub fn new(descriptions_scheduler: Arc) -> Self { + Self { + descriptions_scheduler, + } + } +} + +#[derive(Debug)] +struct BlobFieldSchedulingJob<'a> { + descriptions_job: Box, +} + +impl SchedulingJob for BlobFieldSchedulingJob<'_> { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result { + let next_descriptions = self.descriptions_job.schedule_next(context, priority)?; + let mut priority = priority.current_priority(); + let decoders = next_descriptions.decoders.into_iter().map(|decoder| { + let decoder = decoder.into_array(); + let path = decoder.path; + let mut decoder = decoder.decoder; + let num_rows = decoder.num_rows(); + let descriptions_fut = async move { + decoder + .wait_for_loaded(decoder.num_rows() - 1) + .await + .unwrap(); + let descriptions_task = decoder.drain(decoder.num_rows()).unwrap(); + descriptions_task.task.decode().map(|(arr, _)| arr) + } + .boxed(); + let decoder = Box::new(BlobFieldDecoder { + io: context.io().clone(), + unloaded_descriptions: Some(descriptions_fut), + positions: PrimitiveArray::::from_iter_values(vec![]), + sizes: PrimitiveArray::::from_iter_values(vec![]), + num_rows, + loaded: VecDeque::new(), + validity: VecDeque::new(), + rows_loaded: 0, + rows_drained: 0, + base_priority: priority, + }); + priority += num_rows; + MessageType::DecoderReady(DecoderReady { decoder, path }) + }); + Ok(ScheduledScanLine { + decoders: decoders.collect(), + rows_scheduled: next_descriptions.rows_scheduled, + }) + } + + fn num_rows(&self) -> u64 { + self.descriptions_job.num_rows() + } +} + +impl FieldScheduler for BlobFieldScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[std::ops::Range], + filter: &FilterExpression, + ) -> Result> { + let descriptions_job = self + .descriptions_scheduler + .schedule_ranges(ranges, filter)?; + Ok(Box::new(BlobFieldSchedulingJob { descriptions_job })) + } + + fn num_rows(&self) -> u64 { + self.descriptions_scheduler.num_rows() + } + + fn initialize<'a>( + &'a self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + self.descriptions_scheduler.initialize(filter, context) + } +} + +pub struct BlobFieldDecoder { + io: Arc, + unloaded_descriptions: Option>>, + positions: PrimitiveArray, + sizes: PrimitiveArray, + num_rows: u64, + loaded: VecDeque, + validity: VecDeque, + rows_loaded: u64, + rows_drained: u64, + base_priority: u64, +} + +impl BlobFieldDecoder { + fn drain_validity(&mut self, num_values: usize) -> Result> { + let mut validity = BooleanBufferBuilder::new(num_values); + let mut remaining = num_values; + while remaining > 0 { + let next = self.validity.front_mut().unwrap(); + if remaining < next.len() { + let slice = next.slice(0, remaining); + validity.append_buffer(&slice); + *next = next.slice(remaining, next.len() - remaining); + remaining = 0; + } else { + validity.append_buffer(next); + remaining -= next.len(); + self.validity.pop_front(); + } + } + let nulls = NullBuffer::new(validity.finish()); + if nulls.null_count() == 0 { + Ok(None) + } else { + Ok(Some(nulls)) + } + } +} + +impl std::fmt::Debug for BlobFieldDecoder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("BlobFieldDecoder") + .field("num_rows", &self.num_rows) + .field("rows_loaded", &self.rows_loaded) + .field("rows_drained", &self.rows_drained) + .finish() + } +} + +impl LogicalPageDecoder for BlobFieldDecoder { + fn wait_for_loaded(&mut self, loaded_need: u64) -> BoxFuture<'_, Result<()>> { + async move { + if self.unloaded_descriptions.is_some() { + let descriptions = self.unloaded_descriptions.take().unwrap().await?; + let descriptions = descriptions.as_struct(); + self.positions = descriptions.column(0).as_primitive().clone(); + self.sizes = descriptions.column(1).as_primitive().clone(); + } + let start = self.rows_loaded as usize; + let end = (loaded_need + 1).min(self.num_rows) as usize; + let positions = self.positions.values().slice(start, end - start); + let sizes = self.sizes.values().slice(start, end - start); + let ranges = positions + .iter() + .zip(sizes.iter()) + .map(|(position, size)| *position..(*position + *size)) + .collect::>(); + let validity = positions + .iter() + .zip(sizes.iter()) + .map(|(p, s)| *p != 1 || *s != 0) + .collect::(); + // Run the I/O before mutating decoder state, so a failed load + // leaves the decoder untouched and `wait_for_loaded` is the single, + // clean point of failure. + let bytes = self + .io + .submit_request(ranges, self.base_priority + start as u64) + .await?; + self.validity.push_back(validity); + self.loaded.extend(bytes); + self.rows_loaded = end as u64; + Ok(()) + } + .boxed() + } + + fn rows_loaded(&self) -> u64 { + self.rows_loaded + } + + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn rows_drained(&self) -> u64 { + self.rows_drained + } + + fn drain(&mut self, num_rows: u64) -> Result { + if num_rows as usize > self.loaded.len() { + // `loaded` is populated by `wait_for_loaded`; guard the + // load-before-drain contract so a violation surfaces as an error + // rather than an out-of-bounds drain panic. + return Err(Error::internal(format!( + "BlobFieldDecoder was asked to drain {num_rows} rows but only \ + {} are loaded", + self.loaded.len(), + ))); + } + let bytes = self.loaded.drain(0..num_rows as usize).collect::>(); + let validity = self.drain_validity(num_rows as usize)?; + self.rows_drained += num_rows; + Ok(NextDecodeTask { + num_rows, + task: Box::new(BlobArrayDecodeTask::new(bytes, validity)), + }) + } + + fn data_type(&self) -> &DataType { + &DataType::LargeBinary + } +} + +struct BlobArrayDecodeTask { + bytes: Vec, + validity: Option, +} + +impl BlobArrayDecodeTask { + fn new(bytes: Vec, validity: Option) -> Self { + Self { bytes, validity } + } +} + +impl DecodeArrayTask for BlobArrayDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let num_bytes = self.bytes.iter().map(|b| b.len()).sum::(); + let offsets = self + .bytes + .iter() + .scan(0, |state, b| { + let start = *state; + *state += b.len(); + Some(start as i64) + }) + .chain(std::iter::once(num_bytes as i64)) + .collect::>(); + let offsets = OffsetBuffer::new(ScalarBuffer::from(offsets)); + let mut buffer = Vec::with_capacity(num_bytes); + for bytes in self.bytes { + buffer.extend_from_slice(&bytes); + } + let data_buf = Buffer::from_vec(buffer); + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok(( + Arc::new(LargeBinaryArray::new(offsets, data_buf, self.validity)), + 0, + )) + } +} + +pub struct BlobFieldEncoder { + description_encoder: Box, +} + +impl BlobFieldEncoder { + pub fn new(description_encoder: Box) -> Self { + Self { + description_encoder, + } + } + + fn write_bins(array: ArrayRef, external_buffers: &mut OutOfLineBuffers) -> Result { + let binarray = array.as_binary_opt::().ok_or_else(|| { + Error::invalid_input_source( + format!("Expected large_binary and received {}", array.data_type()).into(), + ) + })?; + let mut positions = Vec::with_capacity(array.len()); + let mut sizes = Vec::with_capacity(array.len()); + let data = binarray.values(); + let nulls = binarray + .nulls() + .cloned() + .unwrap_or(NullBuffer::new_valid(binarray.len())); + for (w, is_valid) in binarray.value_offsets().windows(2).zip(&nulls) { + if is_valid { + let start = w[0] as u64; + let end = w[1] as u64; + let size = end - start; + if size > 0 { + let val = data.slice_with_length(start as usize, size as usize); + let position = external_buffers.add_buffer(LanceBuffer::from(val)); + positions.push(position); + sizes.push(size); + } else { + // Empty values are always (0,0) + positions.push(0); + sizes.push(0); + } + } else { + // Null values are always (1, 0) + positions.push(1); + sizes.push(0); + } + } + let positions = Arc::new(UInt64Array::from(positions)); + let sizes = Arc::new(UInt64Array::from(sizes)); + let descriptions = Arc::new(StructArray::new( + BLOB_DESC_FIELDS.clone(), + vec![positions, sizes], + None, + )); + Ok(descriptions) + } +} + +impl FieldEncoder for BlobFieldEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let descriptions = Self::write_bins(array, external_buffers)?; + self.description_encoder.maybe_encode( + descriptions, + external_buffers, + repdef, + row_number, + num_rows, + ) + } + + // If there is any data left in the buffer then create an encode task from it + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.description_encoder.flush(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.description_encoder.num_columns() + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + let inner_finished = self.description_encoder.finish(external_buffers); + async move { + let mut cols = inner_finished.await?; + assert_eq!(cols.len(), 1); + let encoding = std::mem::take(&mut cols[0].encoding); + let wrapped_encoding = ColumnEncoding { + column_encoding: Some(column_encoding::ColumnEncoding::Blob(Box::new(Blob { + inner: Some(Box::new(encoding)), + }))), + }; + cols[0].encoding = wrapped_encoding; + Ok(cols) + } + .boxed() + } +} + +#[cfg(test)] +mod tests { + use std::{ + collections::{HashMap, VecDeque}, + ops::Range, + sync::{Arc, LazyLock}, + }; + + use arrow_array::{LargeBinaryArray, PrimitiveArray, types::UInt64Type}; + use arrow_schema::{DataType, Field}; + use bytes::Bytes; + use futures::{FutureExt, future::BoxFuture}; + use lance_arrow::BLOB_META_KEY; + use lance_core::{Error, Result}; + + use super::BlobFieldDecoder; + use crate::{ + EncodingsIo, + decoder::LogicalPageDecoder, + format::pb::column_encoding, + testing::TestEncoding, + testing::{TestCases, check_round_trip_encoding_of_data, check_specific_random}, + }; + + static BLOB_META: LazyLock> = LazyLock::new(|| { + [(BLOB_META_KEY.to_string(), "true".to_string())] + .iter() + .cloned() + .collect::>() + }); + + #[test_log::test(tokio::test)] + async fn test_basic_blob() { + let field = Field::new("", DataType::LargeBinary, false).with_metadata(BLOB_META.clone()); + check_specific_random(field, TestCases::basic().with_array_and_u16_encodings()).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_blob() { + let val1: &[u8] = &[1, 2, 3]; + let val2: &[u8] = &[7, 8, 9]; + let array = Arc::new(LargeBinaryArray::from(vec![Some(val1), None, Some(val2)])); + let test_cases = TestCases::default() + .with_array_and_u16_encodings() + .with_expected_encoding("packed_struct") + .with_verify_encoding(Arc::new(|cols, encoding| { + if *encoding == TestEncoding::Array { + // In 2.0 we used a special "column encoding" to mark blob fields. In 2.1 we + // don't do this and just rely on the regular page encoding. + assert_eq!(cols.len(), 1); + let col = &cols[0]; + assert!(matches!( + col.encoding.column_encoding.as_ref().unwrap(), + column_encoding::ColumnEncoding::Blob(_) + )); + } + })); + // Use blob encoding if requested + check_round_trip_encoding_of_data(vec![array.clone()], &test_cases, BLOB_META.clone()) + .await; + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_verify_encoding(Arc::new(|cols, encoding| { + if *encoding == TestEncoding::Array { + assert_eq!(cols.len(), 1); + let col = &cols[0]; + assert!(!matches!( + col.encoding.column_encoding.as_ref().unwrap(), + column_encoding::ColumnEncoding::Blob(_) + )); + } + })); + // Don't use blob encoding if not requested + check_round_trip_encoding_of_data(vec![array], &test_cases, Default::default()).await; + } + + /// An `EncodingsIo` that rejects every request, simulating cloud storage + /// returning a retryable error (e.g. an exhausted HTTP 503 retry budget). + #[derive(Debug)] + struct FailingScheduler; + + impl EncodingsIo for FailingScheduler { + fn submit_request( + &self, + _ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, Result>> { + std::future::ready(Err(Error::io("simulated HTTP 503 from cloud storage"))).boxed() + } + } + + /// A failed blob load must surface through `wait_for_loaded` and leave the + /// decoder untouched -- never half-advanced into a state where a later + /// `drain` reads rows that never loaded (which panicked with "range end + /// index N out of range for slice of length 0"). + #[test_log::test(tokio::test)] + async fn test_io_failure_leaves_blob_decoder_consistent() { + let num_rows = 8u64; + // `positions`/`sizes` only need `num_rows` entries; the failing + // scheduler rejects the request regardless of the ranges it is given. + let descs = PrimitiveArray::::from_iter_values(std::iter::repeat_n( + 0u64, + num_rows as usize, + )); + + let mut decoder = BlobFieldDecoder { + io: Arc::new(FailingScheduler), + unloaded_descriptions: None, + positions: descs.clone(), + sizes: descs, + num_rows, + loaded: VecDeque::new(), + validity: VecDeque::new(), + rows_loaded: 0, + rows_drained: 0, + base_priority: 0, + }; + + // `wait_for_loaded` propagates the I/O failure... + assert!(decoder.wait_for_loaded(num_rows - 1).await.is_err()); + + // ...and leaves no half-loaded state behind. + assert_eq!(decoder.rows_loaded, 0); + assert!(decoder.loaded.is_empty()); + assert!(decoder.validity.is_empty()); + + // A drain in this state errors instead of panicking on the empty buffer. + assert!(decoder.drain(num_rows).is_err()); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical/list.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/list.rs new file mode 100644 index 000000000..d76532ab3 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/list.rs @@ -0,0 +1,1283 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + collections::VecDeque, + ops::Range, + sync::{Arc, OnceLock}, +}; + +use arrow_array::{ + Array, ArrayRef, BooleanArray, Int32Array, Int64Array, LargeListArray, ListArray, UInt64Array, + cast::AsArray, + new_empty_array, + types::{Int32Type, Int64Type, UInt64Type}, +}; +use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder, Buffer, NullBuffer, OffsetBuffer}; +use arrow_schema::{DataType, Field, Fields}; +use futures::{FutureExt, future::BoxFuture}; +use lance_core::{Error, Result, cache::LanceCache, utils::parse::str_is_truthy}; +use log::trace; +use tokio::task::JoinHandle; + +use crate::{ + EncodingsIo, + array_encoding::logical::r#struct::{SimpleStructDecoder, SimpleStructScheduler}, + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, FixedWidthDataBlock}, + decoder::{ + DecodeArrayTask, DecodeBatchScheduler, FieldScheduler, FilterExpression, ListPriorityRange, + LogicalPageDecoder, MessageType, NextDecodeTask, PageEncoding, PriorityRange, + ScheduledScanLine, SchedulerContext, SchedulingJob, + }, + encoder::{ + ArrayEncoder, EncodeTask, EncodedArray, EncodedColumn, EncodedPage, FieldEncoder, + OutOfLineBuffers, + }, + format::pb, + repdef::RepDefBuilder, + utils::accumulation::AccumulationQueue, +}; + +/// When set, indirect I/O in the 2.0 list scheduler bypasses the backpressure system. +/// +/// This can be a blunt instrument to avoid deadlocks in 2.0 scenarios +/// Set LANCE_BYPASS_INDIRECT_IO_BACKPRESSURE=1 to enable. +static BYPASS_INDIRECT_IO_BACKPRESSURE: OnceLock = OnceLock::new(); + +fn bypass_indirect_io_backpressure() -> bool { + *BYPASS_INDIRECT_IO_BACKPRESSURE.get_or_init(|| { + std::env::var("LANCE_BYPASS_INDIRECT_IO_BACKPRESSURE") + .map(|val| str_is_truthy(&val)) + .unwrap_or(false) + }) +} + +// Scheduling lists is tricky. Imagine the following scenario: +// +// * There are 2000 offsets per offsets page +// * The user requests range 8000..8500 +// +// First, since 8000 matches the start of an offsets page, we don't need to read an extra offset. +// +// Since this range matches the start of a page, we know we will get an offsets array like +// [0, ...] +// +// We need to restore nulls, which relies on a null offset adjustment, which is unique to each offsets +// page. +// +// We need to map this to [X, ...] where X is the sum of the number of items in the 0-2000, 2000-4000, +// and 4000-6000 pages. +// +// This gets even trickier if a range spans multiple offsets pages. For example, given the same +// scenario but the user requests 7999..8500. In this case the first page read will include an +// extra offset (e.g. we need to read 7998..8000), the null adjustment will be different between the +// two, and the items offset will be different. +// +// To handle this, we take the incoming row requests, look at the page info, and then calculate +// list requests. + +#[derive(Debug)] +struct ListRequest { + /// How many lists this request maps to + num_lists: u64, + /// Did this request include an extra offset + includes_extra_offset: bool, + /// The null offset adjustment for this request + null_offset_adjustment: u64, + /// items offset to apply + items_offset: u64, +} + +#[derive(Debug)] +struct ListRequestsIter { + // The bool triggers whether we need to skip an offset or not + list_requests: VecDeque, + offsets_requests: Vec>, +} + +impl ListRequestsIter { + // TODO: This logic relies on row_ranges being ordered and may be a problem when we + // add proper support for out-of-order take + fn new(row_ranges: &[Range], page_infos: &[OffsetPageInfo]) -> Self { + let mut items_offset = 0; + let mut offsets_offset = 0; + let mut page_infos_iter = page_infos.iter(); + let mut cur_page_info = page_infos_iter.next().unwrap(); + let mut list_requests = VecDeque::new(); + let mut offsets_requests = Vec::new(); + + // Each row range maps to at least one list request. It may map to more if the + // range spans multiple offsets pages. + for range in row_ranges { + let mut range = range.clone(); + + // Skip any offsets pages that are before the range + while offsets_offset + (cur_page_info.offsets_in_page) <= range.start { + trace!("Skipping null offset adjustment chunk {:?}", offsets_offset); + offsets_offset += cur_page_info.offsets_in_page; + items_offset += cur_page_info.num_items_referenced_by_page; + cur_page_info = page_infos_iter.next().unwrap(); + } + + // If the range starts at the beginning of an offsets page we don't need + // to read an extra offset + let mut includes_extra_offset = range.start != offsets_offset; + if includes_extra_offset { + offsets_requests.push(range.start - 1..range.end); + } else { + offsets_requests.push(range.clone()); + } + + // At this point our range overlaps the current page (cur_page_info) and + // we can start slicing it into list requests + while !range.is_empty() { + // The end of the list request is the min of the end of the range + // and the end of the current page + let end = offsets_offset + cur_page_info.offsets_in_page; + let last = end >= range.end; + let end = end.min(range.end); + list_requests.push_back(ListRequest { + num_lists: end - range.start, + includes_extra_offset, + null_offset_adjustment: cur_page_info.null_offset_adjustment, + items_offset, + }); + + includes_extra_offset = false; + range.start = end; + // If there is still more data in the range, we need to move to the + // next page + if !last { + offsets_offset += cur_page_info.offsets_in_page; + items_offset += cur_page_info.num_items_referenced_by_page; + cur_page_info = page_infos_iter.next().unwrap(); + } + } + } + Self { + list_requests, + offsets_requests, + } + } + + // Given a page of offset data, grab the corresponding list requests + fn next(&mut self, mut num_offsets: u64) -> Vec { + let mut list_requests = Vec::new(); + while num_offsets > 0 { + let req = self.list_requests.front_mut().unwrap(); + // If the request did not start at zero then we need to read an extra offset + if req.includes_extra_offset { + num_offsets -= 1; + debug_assert_ne!(num_offsets, 0); + } + if num_offsets >= req.num_lists { + num_offsets -= req.num_lists; + list_requests.push(self.list_requests.pop_front().unwrap()); + } else { + let sub_req = ListRequest { + num_lists: num_offsets, + includes_extra_offset: req.includes_extra_offset, + null_offset_adjustment: req.null_offset_adjustment, + items_offset: req.items_offset, + }; + + list_requests.push(sub_req); + req.includes_extra_offset = false; + req.num_lists -= num_offsets; + num_offsets = 0; + } + } + list_requests + } +} + +/// Given a list of offsets and a list of requested list row ranges we need to rewrite the offsets so that +/// they appear as expected for a list array. This involves a number of tasks: +/// +/// * Nulls in the offsets are represented by oversize values and these need to be converted to +/// the appropriate length +/// * For each range we (usually) load N + 1 offsets, so if we have 5 ranges we have 5 extra values +/// and we need to drop 4 of those. +/// * Ranges may not start at 0 and, while we don't strictly need to, we want to go ahead and normalize +/// the offsets so that the first offset is 0. +/// +/// Throughout the comments we will consider the following example case: +/// +/// The user requests the following ranges of lists (list_row_ranges): [0..3, 5..6] +/// +/// This is a total of 4 lists. The loaded offsets are [10, 20, 120, 150, 60]. The last valid offset is 99. +/// The null_offset_adjustment will be 100. +/// +/// Our desired output offsets are going to be [0, 10, 20, 20, 30] and the item ranges are [0..20] and [50..60] +/// The validity array is [true, true, false, true] +fn decode_offsets( + offsets: &dyn Array, + list_requests: &[ListRequest], + null_offset_adjustment: u64, +) -> (VecDeque>, Vec, BooleanBuffer) { + // In our example this is [10, 20, 120, 50, 60] + let numeric_offsets = offsets.as_primitive::(); + // In our example there are 4 total lists + let total_num_lists = list_requests.iter().map(|req| req.num_lists).sum::() as u32; + let mut normalized_offsets = Vec::with_capacity(total_num_lists as usize); + let mut validity_buffer = BooleanBufferBuilder::new(total_num_lists as usize); + // The first output offset is always 0 no matter what + normalized_offsets.push(0); + let mut last_normalized_offset = 0; + let offsets_values = numeric_offsets.values(); + + let mut item_ranges = VecDeque::new(); + let mut offsets_offset: u32 = 0; + // All ranges should be non-empty + debug_assert!(list_requests.iter().all(|r| r.num_lists > 0)); + for req in list_requests { + // The # of lists in this particular range + let num_lists = req.num_lists; + + // Because we know the first offset is always 0 we don't store that. This means we have special + // logic if a range starts at 0 (we didn't need to read an extra offset value in that case) + // In our example we enter this special case on the first range (0..3) but not the second (5..6) + // This means the first range, which has 3 lists, maps to 3 values in our offsets array [10, 20, 120] + // However, the second range, which has 1 list, maps to 2 values in our offsets array [150, 60] + let (items_range, offsets_to_norm_start, num_offsets_to_norm) = + if !req.includes_extra_offset { + // In our example items start is 0 and items_end is 20 + let first_offset_idx = 0_usize; + let num_offsets = num_lists as usize; + let items_start = 0; + let items_end = offsets_values[num_offsets - 1] % null_offset_adjustment; + let items_range = items_start..items_end; + (items_range, first_offset_idx, num_offsets) + } else { + // In our example, offsets_offset will be 3, items_start will be 50, and items_end will + // be 60 + let first_offset_idx = offsets_offset as usize; + let num_offsets = num_lists as usize + 1; + let items_start = offsets_values[first_offset_idx] % null_offset_adjustment; + let items_end = + offsets_values[first_offset_idx + num_offsets - 1] % null_offset_adjustment; + let items_range = items_start..items_end; + (items_range, first_offset_idx, num_offsets) + }; + + // TODO: Maybe consider writing whether there are nulls or not as part of the + // page description. Then we can skip all validity work. Not clear if that will + // be any benefit though. + + // We calculate validity from all elements but the first (or all elements + // if this is the special zero-start case) + // + // So, in our first pass through, we consider [10, 20, 120] (1 null) + // In our second pass through we only consider [60] (0 nulls) + // Note that the 150 is null but we only loaded it to know where the 50-60 list started + // and it doesn't actually correspond to a list (e.g. list 4 is null but we aren't loading it + // here) + let validity_start = if !req.includes_extra_offset { + 0 + } else { + offsets_to_norm_start + 1 + }; + for off in offsets_values + .slice(validity_start, num_lists as usize) + .iter() + { + validity_buffer.append(*off < null_offset_adjustment); + } + + // In our special case we need to account for the offset 0-first_item + if !req.includes_extra_offset { + let first_item = offsets_values[0] % null_offset_adjustment; + normalized_offsets.push(first_item); + last_normalized_offset = first_item; + } + + // Finally, we go through and shift the offsets. If we just returned them as is (taking care of + // nulls) we would get [0, 10, 20, 20, 60] but our last list only has 10 items, not 40 and so we + // need to shift that 60 to a 40. + normalized_offsets.extend( + offsets_values + .slice(offsets_to_norm_start, num_offsets_to_norm) + .windows(2) + .map(|w| { + let start = w[0] % null_offset_adjustment; + let end = w[1] % null_offset_adjustment; + if end < start { + panic!("End is less than start in window {:?} with null_offset_adjustment={} we get start={} and end={}", w, null_offset_adjustment, start, end); + } + let length = end - start; + last_normalized_offset += length; + last_normalized_offset + }), + ); + trace!( + "List offsets range of {} lists maps to item range {:?}", + num_lists, items_range + ); + offsets_offset += num_offsets_to_norm as u32; + if !items_range.is_empty() { + let items_range = + items_range.start + req.items_offset..items_range.end + req.items_offset; + item_ranges.push_back(items_range); + } + } + + let validity = validity_buffer.finish(); + (item_ranges, normalized_offsets, validity) +} + +/// After scheduling the offsets we immediately launch this task as a new tokio task +/// This task waits for the offsets to arrive, decodes them, and then schedules the I/O +/// for the items. +/// +/// This task does not wait for the items data. That happens on the main decode loop (unless +/// we have list of list of ... in which case it happens in the outer indirect decode loop) +#[allow(clippy::too_many_arguments)] +async fn indirect_schedule_task( + mut offsets_decoder: Box, + list_requests: Vec, + null_offset_adjustment: u64, + items_scheduler: Arc, + items_type: DataType, + io: Arc, + cache: Arc, + priority: Box, +) -> Result { + let num_offsets = offsets_decoder.num_rows(); + // We know the offsets are a primitive array and thus will not need additional + // pages. We can use a dummy receiver to match the decoder API + offsets_decoder.wait_for_loaded(num_offsets - 1).await?; + let decode_task = offsets_decoder.drain(num_offsets)?; + let (offsets, _) = decode_task.task.decode()?; + + let (item_ranges, offsets, validity) = + decode_offsets(offsets.as_ref(), &list_requests, null_offset_adjustment); + + trace!( + "Indirectly scheduling items ranges {:?} from list items column with {} rows (and priority {:?})", + item_ranges, + items_scheduler.num_rows(), + priority + ); + let offsets: Arc<[u64]> = offsets.into(); + + // All requested lists are empty + if item_ranges.is_empty() { + debug_assert!(item_ranges.iter().all(|r| r.start == r.end)); + return Ok(IndirectlyLoaded { + root_decoder: None, + offsets, + validity, + }); + } + let item_ranges = item_ranges.into_iter().collect::>(); + let num_items = item_ranges.iter().map(|r| r.end - r.start).sum::(); + + // Create a new root scheduler, which has one column, which is our items data + let root_fields = Fields::from(vec![Field::new("item", items_type, true)]); + let indirect_root_scheduler = + SimpleStructScheduler::new(vec![items_scheduler], root_fields.clone(), num_items); + #[allow(deprecated)] + let mut indirect_scheduler = DecodeBatchScheduler::from_scheduler( + Arc::new(indirect_root_scheduler), + root_fields.clone(), + cache, + ); + let mut root_decoder = SimpleStructDecoder::new(root_fields, num_items); + + let priority = Box::new(ListPriorityRange::new(priority, offsets.clone())); + + let indirect_messages = indirect_scheduler.schedule_ranges_to_vec( + &item_ranges, + // Can't push filters into list items + &FilterExpression::no_filter(), + io, + Some(priority), + )?; + + for message in indirect_messages { + for decoder in message.decoders { + let decoder = decoder.into_array(); + if !decoder.path.is_empty() { + root_decoder.accept_child(decoder)?; + } + } + } + + Ok(IndirectlyLoaded { + offsets, + validity, + root_decoder: Some(root_decoder), + }) +} + +#[derive(Debug)] +struct ListFieldSchedulingJob<'a> { + scheduler: &'a ListFieldScheduler, + offsets: Box, + num_rows: u64, + list_requests_iter: ListRequestsIter, +} + +impl<'a> ListFieldSchedulingJob<'a> { + fn try_new( + scheduler: &'a ListFieldScheduler, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result { + let list_requests_iter = ListRequestsIter::new(ranges, &scheduler.offset_page_info); + let num_rows = ranges.iter().map(|r| r.end - r.start).sum::(); + let offsets = scheduler + .offsets_scheduler + .schedule_ranges(&list_requests_iter.offsets_requests, filter)?; + Ok(Self { + scheduler, + offsets, + list_requests_iter, + num_rows, + }) + } +} + +impl SchedulingJob for ListFieldSchedulingJob<'_> { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result { + let next_offsets = self.offsets.schedule_next(context, priority)?; + let offsets_scheduled = next_offsets.rows_scheduled; + let list_reqs = self.list_requests_iter.next(offsets_scheduled); + trace!( + "Scheduled {} offsets which maps to list requests: {:?}", + offsets_scheduled, list_reqs + ); + let null_offset_adjustment = list_reqs[0].null_offset_adjustment; + // It shouldn't be possible for `list_reqs` to span more than one offsets page and so it shouldn't + // be possible for the null_offset_adjustment to change + debug_assert!( + list_reqs + .iter() + .all(|req| req.null_offset_adjustment == null_offset_adjustment) + ); + let num_rows = list_reqs.iter().map(|req| req.num_lists).sum::(); + // offsets is a uint64 which is guaranteed to create one decoder on each call to schedule_next + let next_offsets_decoder = next_offsets + .decoders + .into_iter() + .next() + .unwrap() + .into_array() + .decoder; + + let items_scheduler = self.scheduler.items_scheduler.clone(); + let items_type = self.scheduler.items_field.data_type().clone(); + let base_io = context.io().clone(); + let io = if bypass_indirect_io_backpressure() { + base_io.with_bypass_backpressure().unwrap_or(base_io) + } else { + base_io + }; + let cache = context.cache().clone(); + + // Immediately spawn the indirect scheduling + let indirect_fut = tokio::spawn(indirect_schedule_task( + next_offsets_decoder, + list_reqs, + null_offset_adjustment, + items_scheduler, + items_type, + io, + cache, + priority.box_clone(), + )); + + // Return a decoder + let decoder = Box::new(ListPageDecoder { + offsets: Arc::new([]), + validity: BooleanBuffer::new(Buffer::from_vec(Vec::::default()), 0, 0), + item_decoder: None, + rows_drained: 0, + rows_loaded: 0, + items_field: self.scheduler.items_field.clone(), + num_rows, + unloaded: Some(indirect_fut), + offset_type: self.scheduler.offset_type.clone(), + data_type: self.scheduler.list_type.clone(), + }); + #[allow(deprecated)] + let decoder = context.locate_decoder(decoder); + Ok(ScheduledScanLine { + decoders: vec![MessageType::DecoderReady(decoder)], + rows_scheduled: num_rows, + }) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// A page scheduler for list fields that encodes offsets in one field and items in another +/// +/// The list scheduler is somewhat unique because it requires indirect I/O. We cannot know the +/// ranges we need simply by looking at the metadata. This means that list scheduling doesn't +/// fit neatly into the two-thread schedule-loop / decode-loop model. To handle this, when a +/// list page is scheduled, we only schedule the I/O for the offsets and then we immediately +/// launch a new tokio task. This new task waits for the offsets, decodes them, and then +/// schedules the I/O for the items. Keep in mind that list items can be lists themselves. If +/// that is the case then this indirection will continue. The decode task that is returned will +/// only finish `wait`ing when all of the I/O has completed. +/// +/// Whenever we schedule follow-up I/O like this the priority is based on the top-level row +/// index. This helps ensure that earlier rows get finished completely (including follow up +/// tasks) before we perform I/O for later rows. +#[derive(Debug)] +pub struct ListFieldScheduler { + offsets_scheduler: Arc, + items_scheduler: Arc, + items_field: Arc, + offset_type: DataType, + list_type: DataType, + offset_page_info: Vec, +} + +/// The offsets are stored in a uint64 encoded column. For each page we +/// store some supplementary data that helps us understand the offsets. +/// This is needed to construct the scheduler +#[derive(Debug)] +pub struct OffsetPageInfo { + pub offsets_in_page: u64, + pub null_offset_adjustment: u64, + pub num_items_referenced_by_page: u64, +} + +impl ListFieldScheduler { + // Create a new ListPageScheduler + pub fn new( + offsets_scheduler: Arc, + items_scheduler: Arc, + items_field: Arc, + // Should be int32 or int64 + offset_type: DataType, + offset_page_info: Vec, + ) -> Self { + let list_type = match &offset_type { + DataType::Int32 => DataType::List(items_field.clone()), + DataType::Int64 => DataType::LargeList(items_field.clone()), + _ => panic!("Unexpected offset type {}", offset_type), + }; + Self { + offsets_scheduler, + items_scheduler, + items_field, + offset_type, + offset_page_info, + list_type, + } + } +} + +impl FieldScheduler for ListFieldScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + Ok(Box::new(ListFieldSchedulingJob::try_new( + self, ranges, filter, + )?)) + } + + fn num_rows(&self) -> u64 { + self.offsets_scheduler.num_rows() + } + + fn initialize<'a>( + &'a self, + _filter: &'a FilterExpression, + _context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + // 2.0 schedulers do not need to initialize + std::future::ready(Ok(())).boxed() + } +} + +/// As soon as the first call to decode comes in we wait for all indirect I/O to +/// complete. +/// +/// Once the indirect I/O is finished we pull items out of `unawaited`, wait them +/// (this wait should return immediately) and then push them into `item_decoders`. +/// +/// We then drain from `item_decoders`, popping item pages off as we finish with +/// them. +/// +/// TODO: Test the case where a single list page has multiple items pages +#[derive(Debug)] +struct ListPageDecoder { + unloaded: Option>>, + // offsets and validity will have already been decoded as part of the indirect I/O + offsets: Arc<[u64]>, + validity: BooleanBuffer, + item_decoder: Option, + num_rows: u64, + rows_drained: u64, + rows_loaded: u64, + items_field: Arc, + offset_type: DataType, + data_type: DataType, +} + +struct ListDecodeTask { + offsets: Vec, + validity: BooleanBuffer, + // Will be None if there are no items (all empty / null lists) + items: Option>, + items_field: Arc, + offset_type: DataType, +} + +impl DecodeArrayTask for ListDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let items = self + .items + .map(|items| { + // When we run the indirect I/O we wrap things in a struct array with a single field + // named "item". We can unwrap that now. + let (wrapped_items, _) = items.decode()?; + Result::Ok(wrapped_items.as_struct().column(0).clone()) + }) + .unwrap_or_else(|| Ok(new_empty_array(self.items_field.data_type())))?; + + // The offsets are already decoded but they need to be shifted back to 0 and cast + // to the appropriate type + // + // Although, in some cases, the shift IS strictly required since the unshifted offsets + // may cross i32::MAX even though the shifted offsets do not + let offsets = UInt64Array::from(self.offsets); + let validity = NullBuffer::new(self.validity); + let validity = if validity.null_count() == 0 { + None + } else { + Some(validity) + }; + let min_offset = UInt64Array::new_scalar(offsets.value(0)); + let offsets = arrow_arith::numeric::sub(&offsets, &min_offset)?; + let array: ArrayRef = match &self.offset_type { + DataType::Int32 => { + let offsets = arrow_cast::cast(&offsets, &DataType::Int32)?; + let offsets_i32 = offsets.as_primitive::(); + let offsets = OffsetBuffer::new(offsets_i32.values().clone()); + + Arc::new(ListArray::try_new( + self.items_field.clone(), + offsets, + items, + validity, + )?) + } + DataType::Int64 => { + let offsets = arrow_cast::cast(&offsets, &DataType::Int64)?; + let offsets_i64 = offsets.as_primitive::(); + let offsets = OffsetBuffer::new(offsets_i64.values().clone()); + + Arc::new(LargeListArray::try_new( + self.items_field.clone(), + offsets, + items, + validity, + )?) + } + _ => panic!("ListDecodeTask with data type that is not i32 or i64"), + }; + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok((array, 0)) + } +} + +// Helper method that performs binary search. However, once the +// target is found it walks past any duplicates. E.g. if the +// input list is [0, 3, 5, 5, 5, 7] then this will only return +// 0, 1, 4, or 5. +fn binary_search_to_end(to_search: &[u64], target: u64) -> u64 { + let mut result = match to_search.binary_search(&target) { + Ok(idx) => idx, + Err(idx) => idx - 1, + }; + while result < (to_search.len() - 1) && to_search[result + 1] == target { + result += 1; + } + result as u64 +} + +impl LogicalPageDecoder for ListPageDecoder { + fn wait_for_loaded(&mut self, loaded_need: u64) -> BoxFuture<'_, Result<()>> { + async move { + // wait for the indirect I/O to finish, run the scheduler for the indirect + // I/O and then wait for enough items to arrive + if self.unloaded.is_some() { + trace!("List scheduler needs to wait for indirect I/O to complete"); + let indirectly_loaded = self.unloaded.take().unwrap().await; + if let Err(err) = indirectly_loaded { + match err.try_into_panic() { + Ok(err) => std::panic::resume_unwind(err), + Err(err) => panic!("{:?}", err), + }; + } + let indirectly_loaded = indirectly_loaded.unwrap()?; + + self.offsets = indirectly_loaded.offsets; + self.validity = indirectly_loaded.validity; + self.item_decoder = indirectly_loaded.root_decoder; + } + if self.rows_loaded > loaded_need { + return Ok(()); + } + + let boundary = loaded_need as usize; + debug_assert!(boundary < self.num_rows as usize); + // We need more than X lists which means we need at least X+1 lists which means + // we need at least offsets[X+1] items which means we need more than offsets[X+1]-1 items. + let items_needed = self.offsets[boundary + 1].saturating_sub(1); + trace!( + "List decoder is waiting for more than {} rows to be loaded and {}/{} are already loaded. To satisfy this we need more than {} loaded items", + loaded_need, + self.rows_loaded, + self.num_rows, + items_needed, + ); + + let items_loaded = if let Some(item_decoder) = self.item_decoder.as_mut() { + item_decoder.wait_for_loaded(items_needed).await?; + item_decoder.rows_loaded() + } else { + 0 + }; + + self.rows_loaded = binary_search_to_end(&self.offsets, items_loaded); + trace!("List decoder now has {} loaded rows", self.rows_loaded); + + Ok(()) + } + .boxed() + } + + fn drain(&mut self, num_rows: u64) -> Result { + // We already have the offsets but need to drain the item pages + let mut actual_num_rows = num_rows; + let item_start = self.offsets[self.rows_drained as usize]; + if self.offset_type != DataType::Int64 { + // We might not be able to drain `num_rows` because that request might contain more than 2^31 items + // so we need to figure out how many rows we can actually drain. + while actual_num_rows > 0 { + let num_items = + self.offsets[(self.rows_drained + actual_num_rows) as usize] - item_start; + if num_items <= i32::MAX as u64 { + break; + } + // TODO: This could be slow. Maybe faster to start from zero or do binary search. Investigate when + // actually adding support for smaller than requested batches + actual_num_rows -= 1; + } + } + if actual_num_rows < num_rows { + // TODO: We should be able to automatically + // shrink the read batch size if we detect the batches are going to be huge (maybe + // even achieve this with a read_batch_bytes parameter, though some estimation may + // still be required) + return Err(Error::not_supported_source(format!("loading a batch of {} lists would require creating an array with over i32::MAX items and we don't yet support returning smaller than requested batches", num_rows).into())); + } + let offsets = self.offsets + [self.rows_drained as usize..(self.rows_drained + actual_num_rows + 1) as usize] + .to_vec(); + let validity = self + .validity + .slice(self.rows_drained as usize, actual_num_rows as usize); + let start = offsets[0]; + let end = offsets[offsets.len() - 1]; + let num_items_to_drain = end - start; + + let item_decode = if num_items_to_drain == 0 { + None + } else { + self.item_decoder + .as_mut() + .map(|item_decoder| Result::Ok(item_decoder.drain(num_items_to_drain)?.task)) + .transpose()? + }; + + self.rows_drained += num_rows; + Ok(NextDecodeTask { + num_rows, + task: Box::new(ListDecodeTask { + offsets, + validity, + items_field: self.items_field.clone(), + items: item_decode, + offset_type: self.offset_type.clone(), + }) as Box, + }) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn rows_loaded(&self) -> u64 { + self.rows_loaded + } + + fn rows_drained(&self) -> u64 { + self.rows_drained + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +struct IndirectlyLoaded { + offsets: Arc<[u64]>, + validity: BooleanBuffer, + root_decoder: Option, +} + +impl std::fmt::Debug for IndirectlyLoaded { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("IndirectlyLoaded") + .field("offsets", &self.offsets) + .field("validity", &self.validity) + .finish() + } +} + +/// An encoder for list offsets that "stitches" offsets and encodes nulls into the offsets +/// +/// If we need to encode several list arrays into a single page then we need to "stitch" the offsets +/// For example, imagine we have list arrays [[0, 1], [2]] and [[3, 4, 5]]. +/// +/// We will have offset arrays [0, 2, 3] and [0, 3]. We don't want to encode [0, 2, 3, 0, 3]. What +/// we want is [0, 2, 3, 6] +/// +/// This encoder also handles validity by converting a null value into an oversized offset. For example, +/// if we have four lists with offsets [0, 20, 20, 20, 30] and the list at index 2 is null (note that +/// the list at index 1 is empty) then we turn this into offsets [0, 20, 20, 51, 30]. We replace a null +/// offset with previous_offset + max_offset + 1. This makes it possible to load a single item from the +/// list array. +/// +/// These offsets are always stored on disk as a u64 array. First, this is because its simply much more +/// likely than one expects that this is needed, even if our lists are not massive. This is because we +/// only write an offsets page when we have enough data. This means we will probably accumulate a million +/// offsets or more before we bother to write a page. If our lists have a few thousand items a piece then +/// we end up passing the u32::MAX boundary. +/// +/// The second reason is that list offsets are very easily compacted with delta + bit packing and so those +/// u64 offsets should easily be shrunk down before being put on disk. +/// +/// This encoder can encode both lists and large lists. It can decode the resulting column into either type +/// as well. (TODO: Test and enable large lists) +/// +/// You can even write as a large list and decode as a regular list (as long as no single list has more than +/// 2^31 items) or vice versa. You could even encode a mixed stream of list and large list (but unclear that +/// would ever be useful) +#[derive(Debug)] +struct ListOffsetsEncoder { + // An accumulation queue, we insert both offset arrays and validity arrays into this queue + accumulation_queue: AccumulationQueue, + // The inner encoder of offset values + inner_encoder: Arc, + column_index: u32, +} + +impl ListOffsetsEncoder { + fn new( + cache_bytes: u64, + keep_original_array: bool, + column_index: u32, + inner_encoder: Arc, + ) -> Self { + Self { + accumulation_queue: AccumulationQueue::new( + cache_bytes, + column_index, + keep_original_array, + ), + inner_encoder, + column_index, + } + } + + /// Given a list array, return the offsets as a standalone ArrayRef (either an Int32Array or Int64Array) + fn extract_offsets(list_arr: &dyn Array) -> ArrayRef { + match list_arr.data_type() { + DataType::List(_) => { + let offsets = list_arr.as_list::().offsets().clone(); + Arc::new(Int32Array::new(offsets.into_inner(), None)) + } + DataType::LargeList(_) => { + let offsets = list_arr.as_list::().offsets().clone(); + Arc::new(Int64Array::new(offsets.into_inner(), None)) + } + _ => panic!(), + } + } + + /// Converts the validity of a list array into a boolean array. If there is no validity information + /// then this is an empty boolean array. + fn extract_validity(list_arr: &dyn Array) -> ArrayRef { + if let Some(validity) = list_arr.nulls() { + Arc::new(BooleanArray::new(validity.inner().clone(), None)) + } else { + // We convert None validity into an empty array because the accumulation queue can't + // handle Option + new_empty_array(&DataType::Boolean) + } + } + + fn make_encode_task(&self, arrays: Vec) -> EncodeTask { + let inner_encoder = self.inner_encoder.clone(); + let column_idx = self.column_index; + // At this point we should have 2*N arrays where the even-indexed arrays are integer offsets + // and the odd-indexed arrays are boolean validity bitmaps + let offset_arrays = arrays.iter().step_by(2).cloned().collect::>(); + let validity_arrays = arrays.into_iter().skip(1).step_by(2).collect::>(); + + tokio::task::spawn(async move { + let num_rows = + offset_arrays.iter().map(|arr| arr.len()).sum::() - offset_arrays.len(); + let num_rows = num_rows as u64; + let mut buffer_index = 0; + let array = Self::do_encode( + offset_arrays, + validity_arrays, + &mut buffer_index, + num_rows, + inner_encoder, + )?; + let (data, description) = array.into_buffers(); + Ok(EncodedPage { + data, + description: PageEncoding::Legacy(description), + num_rows, + column_idx, + row_number: 0, // V2.0 encoders do not use + }) + }) + .map(|res_res| res_res.unwrap()) + .boxed() + } + + fn maybe_encode_offsets_and_validity(&mut self, list_arr: &dyn Array) -> Option { + let offsets = Self::extract_offsets(list_arr); + let validity = Self::extract_validity(list_arr); + let num_rows = offsets.len() as u64; + // Either inserting the offsets OR inserting the validity could cause the + // accumulation queue to fill up + if let Some(mut arrays) = self + .accumulation_queue + .insert(offsets, /*row_number=*/ 0, num_rows) + { + arrays.0.push(validity); + Some(self.make_encode_task(arrays.0)) + } else if let Some(arrays) = self + .accumulation_queue + .insert(validity, /*row_number=*/ 0, num_rows) + { + Some(self.make_encode_task(arrays.0)) + } else { + None + } + } + + fn flush(&mut self) -> Option { + if let Some(arrays) = self.accumulation_queue.flush() { + Some(self.make_encode_task(arrays.0)) + } else { + None + } + } + + // Get's the total number of items covered by an array of offsets (keeping in + // mind that the first offset may not be zero) + fn get_offset_span(array: &dyn Array) -> u64 { + match array.data_type() { + DataType::Int32 => { + let arr_i32 = array.as_primitive::(); + (arr_i32.value(arr_i32.len() - 1) - arr_i32.value(0)) as u64 + } + DataType::Int64 => { + let arr_i64 = array.as_primitive::(); + (arr_i64.value(arr_i64.len() - 1) - arr_i64.value(0)) as u64 + } + _ => panic!(), + } + } + + // This is where we do the work to actually shift the offsets and encode nulls + // Note that the output is u64 and the input could be i32 OR i64. + fn extend_offsets_vec_u64( + dest: &mut Vec, + offsets: &dyn Array, + validity: Option<&BooleanArray>, + // The offset of this list into the destination + base: u64, + null_offset_adjustment: u64, + ) { + match offsets.data_type() { + DataType::Int32 => { + let offsets_i32 = offsets.as_primitive::(); + let start = offsets_i32.value(0) as u64; + // If we want to take a list from start..X and change it into + // a list from end..X then we need to add (base - start) to all elements + // Note that `modifier` may be negative but (item + modifier) will always be >= 0 + let modifier = base as i64 - start as i64; + if let Some(validity) = validity { + dest.extend( + offsets_i32 + .values() + .iter() + .skip(1) + .zip(validity.values().iter()) + .map(|(&off, valid)| { + (off as i64 + modifier) as u64 + + (!valid as u64 * null_offset_adjustment) + }), + ); + } else { + dest.extend( + offsets_i32 + .values() + .iter() + .skip(1) + // Subtract by `start` so offsets start at 0 + .map(|&v| (v as i64 + modifier) as u64), + ); + } + } + DataType::Int64 => { + let offsets_i64 = offsets.as_primitive::(); + let start = offsets_i64.value(0) as u64; + // If we want to take a list from start..X and change it into + // a list from end..X then we need to add (base - start) to all elements + // Note that `modifier` may be negative but (item + modifier) will always be >= 0 + let modifier = base as i64 - start as i64; + if let Some(validity) = validity { + dest.extend( + offsets_i64 + .values() + .iter() + .skip(1) + .zip(validity.values().iter()) + .map(|(&off, valid)| { + (off + modifier) as u64 + (!valid as u64 * null_offset_adjustment) + }), + ) + } else { + dest.extend( + offsets_i64 + .values() + .iter() + .skip(1) + .map(|&v| (v + modifier) as u64), + ); + } + } + _ => panic!("Invalid list offsets data type {:?}", offsets.data_type()), + } + } + + fn do_encode_u64( + offset_arrays: Vec, + validity: Vec>, + num_offsets: u64, + null_offset_adjustment: u64, + buffer_index: &mut u32, + inner_encoder: Arc, + ) -> Result { + let mut offsets = Vec::with_capacity(num_offsets as usize); + for (offsets_arr, validity_arr) in offset_arrays.iter().zip(validity) { + let last_prev_offset = offsets.last().copied().unwrap_or(0) % null_offset_adjustment; + Self::extend_offsets_vec_u64( + &mut offsets, + &offsets_arr, + validity_arr, + last_prev_offset, + null_offset_adjustment, + ); + } + let offsets_data = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::reinterpret_vec(offsets), + num_values: num_offsets, + block_info: BlockInfo::new(), + }); + inner_encoder.encode(offsets_data, &DataType::UInt64, buffer_index) + } + + fn do_encode( + offset_arrays: Vec, + validity_arrays: Vec, + buffer_index: &mut u32, + num_offsets: u64, + inner_encoder: Arc, + ) -> Result { + let validity_arrays = validity_arrays + .iter() + .map(|v| { + if v.is_empty() { + None + } else { + Some(v.as_boolean()) + } + }) + .collect::>(); + debug_assert_eq!(offset_arrays.len(), validity_arrays.len()); + let total_span = offset_arrays + .iter() + .map(|arr| Self::get_offset_span(arr.as_ref())) + .sum::(); + // See encodings.proto for reasoning behind this value + let null_offset_adjustment = total_span + 1; + let encoded_offsets = Self::do_encode_u64( + offset_arrays, + validity_arrays, + num_offsets, + null_offset_adjustment, + buffer_index, + inner_encoder, + )?; + Ok(EncodedArray { + data: encoded_offsets.data, + encoding: pb::ArrayEncoding { + array_encoding: Some(pb::array_encoding::ArrayEncoding::List(Box::new( + pb::List { + offsets: Some(Box::new(encoded_offsets.encoding)), + null_offset_adjustment, + num_items: total_span, + }, + ))), + }, + }) + } +} + +pub struct ListFieldEncoder { + offsets_encoder: ListOffsetsEncoder, + items_encoder: Box, +} + +impl ListFieldEncoder { + pub fn new( + items_encoder: Box, + inner_offsets_encoder: Arc, + cache_bytes_per_columns: u64, + keep_original_array: bool, + column_index: u32, + ) -> Self { + Self { + offsets_encoder: ListOffsetsEncoder::new( + cache_bytes_per_columns, + keep_original_array, + column_index, + inner_offsets_encoder, + ), + items_encoder, + } + } + + fn combine_tasks( + offsets_tasks: Vec, + item_tasks: Vec, + ) -> Result> { + let mut all_tasks = offsets_tasks; + let item_tasks = item_tasks; + all_tasks.extend(item_tasks); + Ok(all_tasks) + } +} + +impl FieldEncoder for ListFieldEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + // The list may have an offset / shorter length which means the underlying + // values array could be longer than what we need to encode and so we need + // to slice down to the region of interest. + let items = match array.data_type() { + DataType::List(_) => { + let list_arr = array.as_list::(); + let items_start = list_arr.value_offsets()[list_arr.offset()] as usize; + let items_end = + list_arr.value_offsets()[list_arr.offset() + list_arr.len()] as usize; + list_arr + .values() + .slice(items_start, items_end - items_start) + } + DataType::LargeList(_) => { + let list_arr = array.as_list::(); + let items_start = list_arr.value_offsets()[list_arr.offset()] as usize; + let items_end = + list_arr.value_offsets()[list_arr.offset() + list_arr.len()] as usize; + list_arr + .values() + .slice(items_start, items_end - items_start) + } + _ => panic!(), + }; + let offsets_tasks = self + .offsets_encoder + .maybe_encode_offsets_and_validity(array.as_ref()) + .map(|task| vec![task]) + .unwrap_or_default(); + let mut item_tasks = self.items_encoder.maybe_encode( + items, + external_buffers, + repdef, + row_number, + num_rows, + )?; + if !offsets_tasks.is_empty() && item_tasks.is_empty() { + // An items page cannot currently be shared by two different offsets pages. This is + // a limitation in the current scheduler and could be addressed in the future. As a result + // we always need to encode the items page if we encode the offsets page. + // + // In practice this isn't usually too bad unless we are targeting very small pages. + item_tasks = self.items_encoder.flush(external_buffers)?; + } + Self::combine_tasks(offsets_tasks, item_tasks) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + let offsets_tasks = self + .offsets_encoder + .flush() + .map(|task| vec![task]) + .unwrap_or_default(); + let item_tasks = self.items_encoder.flush(external_buffers)?; + Self::combine_tasks(offsets_tasks, item_tasks) + } + + fn num_columns(&self) -> u32 { + self.items_encoder.num_columns() + 1 + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + let inner_columns = self.items_encoder.finish(external_buffers); + async move { + let mut columns = vec![EncodedColumn::default()]; + let inner_columns = inner_columns.await?; + columns.extend(inner_columns); + Ok(columns) + } + .boxed() + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical/primitive.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/primitive.rs new file mode 100644 index 000000000..3a8e9f73e --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/primitive.rs @@ -0,0 +1,533 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{fmt::Debug, ops::Range, sync::Arc, vec}; + +use arrow_array::{Array, ArrayRef, cast::AsArray, make_array}; +use arrow_buffer::bit_util; +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; +use log::trace; + +use crate::decoder::{ColumnBuffers, PageBuffers}; +use crate::decoder::{FieldScheduler, LogicalPageDecoder, SchedulingJob}; +use crate::encoder::ArrayEncodingStrategy; +use crate::utils::accumulation::AccumulationQueue; +use crate::{array_encoding::physical::decoder_from_array_encoding, data::DataBlock}; +use lance_core::{Error, Result, datatypes::Field}; + +use crate::{ + decoder::{ + DecodeArrayTask, FilterExpression, MessageType, NextDecodeTask, PageEncoding, PageInfo, + PageScheduler, PrimitivePageDecoder, PriorityRange, ScheduledScanLine, SchedulerContext, + }, + encoder::{ + EncodeTask, EncodedColumn, EncodedPage, EncodingOptions, FieldEncoder, OutOfLineBuffers, + }, + repdef::RepDefBuilder, +}; + +#[derive(Debug)] +struct PrimitivePage { + scheduler: Box, + num_rows: u64, + page_index: u32, +} + +/// A field scheduler for primitive fields +/// +/// This maps to exactly one column and it assumes that the top-level +/// encoding of each page is "basic". The basic encoding decodes into an +/// optional buffer of validity and a fixed-width buffer of values +/// which is exactly what we need to create a primitive array. +/// +/// Note: we consider booleans and fixed-size-lists of primitive types to be +/// primitive types. This is slightly different than arrow-rs's definition +#[derive(Debug)] +pub struct PrimitiveFieldScheduler { + data_type: DataType, + page_schedulers: Vec, + num_rows: u64, + should_validate: bool, + column_index: u32, +} + +impl PrimitiveFieldScheduler { + pub fn new( + column_index: u32, + data_type: DataType, + pages: Arc<[PageInfo]>, + buffers: ColumnBuffers, + should_validate: bool, + ) -> Self { + let page_schedulers = pages + .iter() + .enumerate() + // Buggy versions of Lance could sometimes create empty pages + .filter(|(page_index, page)| { + log::trace!("Skipping empty page with index {}", page_index); + page.num_rows > 0 + }) + .map(|(page_index, page)| { + let page_buffers = PageBuffers { + column_buffers: buffers, + positions_and_sizes: &page.buffer_offsets_and_sizes, + }; + let scheduler = decoder_from_array_encoding( + page.encoding.as_legacy(), + &page_buffers, + &data_type, + ); + PrimitivePage { + scheduler, + num_rows: page.num_rows, + page_index: page_index as u32, + } + }) + .collect::>(); + let num_rows = page_schedulers.iter().map(|p| p.num_rows).sum(); + Self { + data_type, + page_schedulers, + num_rows, + should_validate, + column_index, + } + } +} + +#[derive(Debug)] +struct PrimitiveFieldSchedulingJob<'a> { + scheduler: &'a PrimitiveFieldScheduler, + ranges: Vec>, + page_idx: usize, + range_idx: usize, + range_offset: u64, + global_row_offset: u64, +} + +impl<'a> PrimitiveFieldSchedulingJob<'a> { + pub fn new(scheduler: &'a PrimitiveFieldScheduler, ranges: Vec>) -> Self { + Self { + scheduler, + ranges, + page_idx: 0, + range_idx: 0, + range_offset: 0, + global_row_offset: 0, + } + } +} + +impl SchedulingJob for PrimitiveFieldSchedulingJob<'_> { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result { + debug_assert!(self.range_idx < self.ranges.len()); + // Get our current range + let mut range = self.ranges[self.range_idx].clone(); + range.start += self.range_offset; + + let mut cur_page = &self.scheduler.page_schedulers[self.page_idx]; + trace!( + "Current range is {:?} and current page has {} rows", + range, cur_page.num_rows + ); + // Skip entire pages until we have some overlap with our next range + while cur_page.num_rows + self.global_row_offset <= range.start { + self.global_row_offset += cur_page.num_rows; + self.page_idx += 1; + trace!("Skipping entire page of {} rows", cur_page.num_rows); + cur_page = &self.scheduler.page_schedulers[self.page_idx]; + } + + // Now the cur_page has overlap with range. Continue looping through ranges + // until we find a range that exceeds the current page + + let mut ranges_in_page = Vec::new(); + while cur_page.num_rows + self.global_row_offset > range.start { + range.start = range.start.max(self.global_row_offset); + let start_in_page = range.start - self.global_row_offset; + let end_in_page = start_in_page + (range.end - range.start); + let end_in_page = end_in_page.min(cur_page.num_rows); + let last_in_range = (end_in_page + self.global_row_offset) >= range.end; + + ranges_in_page.push(start_in_page..end_in_page); + if last_in_range { + self.range_idx += 1; + if self.range_idx == self.ranges.len() { + break; + } + range = self.ranges[self.range_idx].clone(); + } else { + break; + } + } + + let num_rows_in_next = ranges_in_page.iter().map(|r| r.end - r.start).sum(); + trace!( + "Scheduling {} rows across {} ranges from page with {} rows (priority={}, column_index={}, page_index={})", + num_rows_in_next, + ranges_in_page.len(), + cur_page.num_rows, + priority.current_priority(), + self.scheduler.column_index, + cur_page.page_index, + ); + + self.global_row_offset += cur_page.num_rows; + self.page_idx += 1; + + let physical_decoder = cur_page.scheduler.schedule_ranges( + &ranges_in_page, + context.io(), + priority.current_priority(), + ); + + let logical_decoder = PrimitiveFieldDecoder { + data_type: self.scheduler.data_type.clone(), + column_index: self.scheduler.column_index, + unloaded_physical_decoder: Some(physical_decoder), + physical_decoder: None, + rows_drained: 0, + num_rows: num_rows_in_next, + should_validate: self.scheduler.should_validate, + page_index: cur_page.page_index, + }; + + let decoder = Box::new(logical_decoder); + #[allow(deprecated)] + let decoder_ready = context.locate_decoder(decoder); + Ok(ScheduledScanLine { + decoders: vec![MessageType::DecoderReady(decoder_ready)], + rows_scheduled: num_rows_in_next, + }) + } + + fn num_rows(&self) -> u64 { + self.ranges.iter().map(|r| r.end - r.start).sum() + } +} + +impl FieldScheduler for PrimitiveFieldScheduler { + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn schedule_ranges<'a>( + &'a self, + ranges: &[std::ops::Range], + // TODO: Could potentially use filter to simplify decode, something of a micro-optimization probably + _filter: &FilterExpression, + ) -> Result> { + Ok(Box::new(PrimitiveFieldSchedulingJob::new( + self, + ranges.to_vec(), + ))) + } + + fn initialize<'a>( + &'a self, + _filter: &'a FilterExpression, + _context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + // 2.0 schedulers do not need to initialize + std::future::ready(Ok(())).boxed() + } +} + +pub struct PrimitiveFieldDecoder { + data_type: DataType, + unloaded_physical_decoder: Option>>>, + physical_decoder: Option>, + should_validate: bool, + num_rows: u64, + rows_drained: u64, + column_index: u32, + page_index: u32, +} + +impl PrimitiveFieldDecoder { + pub fn new_from_data( + physical_decoder: Arc, + data_type: DataType, + num_rows: u64, + should_validate: bool, + ) -> Self { + Self { + data_type, + unloaded_physical_decoder: None, + physical_decoder: Some(physical_decoder), + should_validate, + num_rows, + rows_drained: 0, + column_index: u32::MAX, + page_index: u32::MAX, + } + } +} + +impl Debug for PrimitiveFieldDecoder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PrimitiveFieldDecoder") + .field("data_type", &self.data_type) + .field("num_rows", &self.num_rows) + .field("rows_drained", &self.rows_drained) + .finish() + } +} + +struct PrimitiveFieldDecodeTask { + rows_to_skip: u64, + rows_to_take: u64, + should_validate: bool, + physical_decoder: Arc, + data_type: DataType, +} + +impl DecodeArrayTask for PrimitiveFieldDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let block = self + .physical_decoder + .decode(self.rows_to_skip, self.rows_to_take)?; + + let array = make_array(block.into_arrow(self.data_type.clone(), self.should_validate)?); + + // This is a bit of a hack to work around https://github.com/apache/arrow-rs/issues/6302 + // + // We change from nulls-in-dictionary (storage format) to nulls-in-indices (arrow-rs preferred + // format) + // + // The calculation of logical_nulls is not free and would be good to avoid in the future + if let DataType::Dictionary(_, _) = self.data_type { + let dict = array.as_any_dictionary(); + if let Some(nulls) = array.logical_nulls() { + let new_indices = dict.keys().to_data(); + let new_array = make_array( + new_indices + .into_builder() + .nulls(Some(nulls)) + .add_child_data(dict.values().to_data()) + .data_type(dict.data_type().clone()) + .build()?, + ); + return Ok((new_array, 0)); + } + } + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok((array, 0)) + } +} + +impl LogicalPageDecoder for PrimitiveFieldDecoder { + // TODO: In the future, at some point, we may consider partially waiting for primitive pages by + // breaking up large I/O into smaller I/O as a way to accelerate the "time-to-first-decode" + fn wait_for_loaded(&mut self, loaded_need: u64) -> BoxFuture<'_, Result<()>> { + log::trace!( + "primitive wait for more than {} rows on column {} and page {} (page has {} rows)", + loaded_need, + self.column_index, + self.page_index, + self.num_rows + ); + async move { + let physical_decoder = self.unloaded_physical_decoder.take().unwrap().await?; + self.physical_decoder = Some(Arc::from(physical_decoder)); + Ok(()) + } + .boxed() + } + + fn drain(&mut self, num_rows: u64) -> Result { + if self.physical_decoder.as_ref().is_none() { + return Err(lance_core::Error::internal(format!( + "drain was called on primitive field decoder for data type {} on column {} but the decoder was never awaited", + self.data_type, self.column_index + ))); + } + + let rows_to_skip = self.rows_drained; + let rows_to_take = num_rows; + + self.rows_drained += rows_to_take; + + let task = Box::new(PrimitiveFieldDecodeTask { + rows_to_skip, + rows_to_take, + should_validate: self.should_validate, + physical_decoder: self.physical_decoder.as_ref().unwrap().clone(), + data_type: self.data_type.clone(), + }); + + Ok(NextDecodeTask { + task, + num_rows: rows_to_take, + }) + } + + fn rows_loaded(&self) -> u64 { + if self.unloaded_physical_decoder.is_some() { + 0 + } else { + self.num_rows + } + } + + fn rows_drained(&self) -> u64 { + if self.unloaded_physical_decoder.is_some() { + 0 + } else { + self.rows_drained + } + } + + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +pub struct PrimitiveFieldEncoder { + accumulation_queue: AccumulationQueue, + array_encoding_strategy: Arc, + column_index: u32, + field: Field, + max_page_bytes: u64, +} + +impl PrimitiveFieldEncoder { + pub fn try_new( + options: &EncodingOptions, + array_encoding_strategy: Arc, + column_index: u32, + field: Field, + ) -> Result { + Ok(Self { + accumulation_queue: AccumulationQueue::new( + options.cache_bytes_per_column, + column_index, + options.keep_original_array, + ), + column_index, + max_page_bytes: options.max_page_bytes, + array_encoding_strategy, + field, + }) + } + + fn create_encode_task(&mut self, arrays: Vec) -> Result { + let encoder = self + .array_encoding_strategy + .create_array_encoder(&arrays, &self.field)?; + let column_idx = self.column_index; + let data_type = self.field.data_type(); + + Ok(tokio::task::spawn(async move { + let num_values = arrays.iter().map(|arr| arr.len() as u64).sum(); + let data = DataBlock::from_arrays(&arrays, num_values); + let mut buffer_index = 0; + let array = encoder.encode(data, &data_type, &mut buffer_index)?; + let (data, description) = array.into_buffers(); + Ok(EncodedPage { + data, + description: PageEncoding::Legacy(description), + num_rows: num_values, + column_idx, + row_number: 0, // v2.0 encoders do not use + }) + }) + .map(|res_res| { + res_res.unwrap_or_else(|err| { + Err(Error::internal(format!( + "Encoding task failed with error: {:?}", + err + ))) + }) + }) + .boxed()) + } + + // Creates an encode task, consuming all buffered data + fn do_flush(&mut self, arrays: Vec) -> Result> { + if arrays.len() == 1 { + let array = arrays.into_iter().next().unwrap(); + let size_bytes = array.get_buffer_memory_size(); + let num_parts = bit_util::ceil(size_bytes, self.max_page_bytes as usize); + // Can't slice it finer than 1 page per row + let num_parts = num_parts.min(array.len()); + if num_parts <= 1 { + // One part and it fits in a page + Ok(vec![self.create_encode_task(vec![array])?]) + } else { + // One part and it needs to be sliced into multiple pages + + // This isn't perfect (items in the array might not all have the same size) + // but it's a reasonable stab for now) + let mut tasks = Vec::with_capacity(num_parts); + let mut offset = 0; + let part_size = bit_util::ceil(array.len(), num_parts); + for _ in 0..num_parts { + let avail = array.len() - offset; + if avail == 0 { + break; + } + let chunk_size = avail.min(part_size); + let part = array.slice(offset, chunk_size); + let task = self.create_encode_task(vec![part])?; + tasks.push(task); + offset += chunk_size; + } + Ok(tasks) + } + } else { + // Multiple parts that (presumably) all fit in a page + // + // TODO: Could check here if there are any jumbo parts in the mix that need splitting + Ok(vec![self.create_encode_task(arrays)?]) + } + } +} + +impl FieldEncoder for PrimitiveFieldEncoder { + // Buffers data, if there is enough to write a page then we create an encode task + fn maybe_encode( + &mut self, + array: ArrayRef, + _external_buffers: &mut OutOfLineBuffers, + _repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + if let Some(arrays) = self.accumulation_queue.insert(array, row_number, num_rows) { + Ok(self.do_flush(arrays.0)?) + } else { + Ok(vec![]) + } + } + + // If there is any data left in the buffer then create an encode task from it + fn flush(&mut self, _external_buffers: &mut OutOfLineBuffers) -> Result> { + if let Some(arrays) = self.accumulation_queue.flush() { + Ok(self.do_flush(arrays.0)?) + } else { + Ok(vec![]) + } + } + + fn num_columns(&self) -> u32 { + 1 + } + + fn finish( + &mut self, + _external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + std::future::ready(Ok(vec![EncodedColumn::default()])).boxed() + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/logical/struct.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/struct.rs new file mode 100644 index 000000000..045b3bca7 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/logical/struct.rs @@ -0,0 +1,617 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + collections::{BinaryHeap, VecDeque}, + ops::Range, + sync::Arc, +}; + +use crate::{ + decoder::{ + DecodeArrayTask, FilterExpression, MessageType, NextDecodeTask, PriorityRange, + ScheduledScanLine, SchedulerContext, + }, + decoder::{DecoderReady, FieldScheduler, LogicalPageDecoder, SchedulingJob}, +}; +use arrow_array::{ArrayRef, StructArray}; +use arrow_schema::{DataType, Field, Fields}; +use futures::{FutureExt, StreamExt, TryStreamExt, future::BoxFuture, stream::FuturesUnordered}; +use lance_core::{Error, Result}; +use log::trace; + +#[derive(Debug)] +struct SchedulingJobWithStatus<'a> { + col_idx: u32, + col_name: &'a str, + job: Box, + rows_scheduled: u64, + rows_remaining: u64, +} + +impl PartialEq for SchedulingJobWithStatus<'_> { + fn eq(&self, other: &Self) -> bool { + self.col_idx == other.col_idx + } +} + +impl Eq for SchedulingJobWithStatus<'_> {} + +impl PartialOrd for SchedulingJobWithStatus<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for SchedulingJobWithStatus<'_> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + // Note this is reversed to make it min-heap + other.rows_scheduled.cmp(&self.rows_scheduled) + } +} + +#[derive(Debug)] +struct EmptyStructDecodeTask { + num_rows: u64, +} + +impl DecodeArrayTask for EmptyStructDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok(( + Arc::new(StructArray::new_empty_fields(self.num_rows as usize, None)), + 0, + )) + } +} + +#[derive(Debug)] +struct EmptyStructDecoder { + num_rows: u64, + rows_drained: u64, + data_type: DataType, +} + +impl EmptyStructDecoder { + fn new(num_rows: u64) -> Self { + Self { + num_rows, + rows_drained: 0, + data_type: DataType::Struct(Fields::from(Vec::::default())), + } + } +} + +impl LogicalPageDecoder for EmptyStructDecoder { + fn wait_for_loaded(&mut self, _loaded_need: u64) -> BoxFuture<'_, Result<()>> { + Box::pin(std::future::ready(Ok(()))) + } + fn rows_loaded(&self) -> u64 { + self.num_rows + } + fn rows_unloaded(&self) -> u64 { + 0 + } + fn num_rows(&self) -> u64 { + self.num_rows + } + fn rows_drained(&self) -> u64 { + self.rows_drained + } + fn drain(&mut self, num_rows: u64) -> Result { + self.rows_drained += num_rows; + Ok(NextDecodeTask { + num_rows, + task: Box::new(EmptyStructDecodeTask { num_rows }), + }) + } + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +#[derive(Debug)] +struct EmptyStructSchedulerJob { + num_rows: u64, +} + +impl SchedulingJob for EmptyStructSchedulerJob { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + _priority: &dyn PriorityRange, + ) -> Result { + let empty_decoder = Box::new(EmptyStructDecoder::new(self.num_rows)); + #[allow(deprecated)] + let struct_decoder = context.locate_decoder(empty_decoder); + Ok(ScheduledScanLine { + decoders: vec![MessageType::DecoderReady(struct_decoder)], + rows_scheduled: self.num_rows, + }) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// Scheduling job for struct data +/// +/// The order in which we schedule the children is important. We want to schedule the child +/// with the least amount of data first. +/// +/// This allows us to decode entire rows as quickly as possible +#[derive(Debug)] +struct SimpleStructSchedulerJob<'a> { + scheduler: &'a SimpleStructScheduler, + /// A min-heap whose key is the # of rows currently scheduled + children: BinaryHeap>, + rows_scheduled: u64, + num_rows: u64, + initialized: bool, +} + +impl<'a> SimpleStructSchedulerJob<'a> { + fn new( + scheduler: &'a SimpleStructScheduler, + children: Vec>, + num_rows: u64, + ) -> Self { + let children = children + .into_iter() + .enumerate() + .map(|(idx, job)| SchedulingJobWithStatus { + col_idx: idx as u32, + col_name: scheduler.child_fields[idx].name(), + job, + rows_scheduled: 0, + rows_remaining: num_rows, + }) + .collect::>(); + Self { + scheduler, + children, + rows_scheduled: 0, + num_rows, + initialized: false, + } + } +} + +impl SchedulingJob for SimpleStructSchedulerJob<'_> { + fn schedule_next( + &mut self, + mut context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result { + let mut decoders = Vec::new(); + if !self.initialized { + // Send info to the decoder thread so it knows a struct is here. In the future we will also + // send validity info here. + let struct_decoder = Box::new(SimpleStructDecoder::new( + self.scheduler.child_fields.clone(), + self.num_rows, + )); + #[allow(deprecated)] + let struct_decoder = context.locate_decoder(struct_decoder); + decoders.push(MessageType::DecoderReady(struct_decoder)); + self.initialized = true; + } + let old_rows_scheduled = self.rows_scheduled; + // Schedule as many children as we need to until we have scheduled at least one + // complete row + while old_rows_scheduled == self.rows_scheduled { + let mut next_child = self.children.pop().unwrap(); + trace!("Scheduling more rows for child {}", next_child.col_idx); + let scoped = context.push(next_child.col_name, next_child.col_idx); + let child_scan = next_child.job.schedule_next(scoped.context, priority)?; + trace!( + "Scheduled {} rows for child {}", + child_scan.rows_scheduled, next_child.col_idx + ); + next_child.rows_scheduled += child_scan.rows_scheduled; + next_child.rows_remaining -= child_scan.rows_scheduled; + decoders.extend(child_scan.decoders); + self.children.push(next_child); + self.rows_scheduled = self.children.peek().unwrap().rows_scheduled; + context = scoped.pop(); + } + let struct_rows_scheduled = self.rows_scheduled - old_rows_scheduled; + Ok(ScheduledScanLine { + decoders, + rows_scheduled: struct_rows_scheduled, + }) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// A scheduler for structs +/// +/// The implementation is actually a bit more tricky than one might initially think. We can't just +/// go through and schedule each column one after the other. This would mean our decode can't start +/// until nearly all the data has arrived (since we need data from each column) +/// +/// Instead, we schedule in row-major fashion +/// +/// Note: this scheduler is the starting point for all decoding. This is because we treat the top-level +/// record batch as a non-nullable struct. +#[derive(Debug)] +pub struct SimpleStructScheduler { + children: Vec>, + child_fields: Fields, + num_rows: u64, +} + +impl SimpleStructScheduler { + pub fn new( + children: Vec>, + child_fields: Fields, + num_rows: u64, + ) -> Self { + let num_rows = children + .first() + .map(|child| child.num_rows()) + .unwrap_or(num_rows); + debug_assert!(children.iter().all(|child| child.num_rows() == num_rows)); + Self { + children, + child_fields, + num_rows, + } + } +} + +impl FieldScheduler for SimpleStructScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + if self.children.is_empty() { + return Ok(Box::new(EmptyStructSchedulerJob { + num_rows: ranges.iter().map(|r| r.end - r.start).sum(), + })); + } + let child_schedulers = self + .children + .iter() + .map(|child| child.schedule_ranges(ranges, filter)) + .collect::>>()?; + let num_rows = child_schedulers[0].num_rows(); + Ok(Box::new(SimpleStructSchedulerJob::new( + self, + child_schedulers, + num_rows, + ))) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn initialize<'a>( + &'a self, + _filter: &'a FilterExpression, + _context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + let futures = self + .children + .iter() + .map(|child| child.initialize(_filter, _context)) + .collect::>(); + async move { + futures + .map(|res| res.map(|_| ())) + .try_collect::>() + .await?; + Ok(()) + } + .boxed() + } +} + +#[derive(Debug)] +struct ChildState { + // As child decoders are scheduled they are added to this queue + // Once the decoder is fully drained it is popped from this queue + // + // TODO: It may be a minor perf optimization, in some rare cases, if we have a separate + // "fully awaited but not yet drained" queue so we don't loop through fully awaited pages + // during each call to wait. + // + // Note: This queue may have more than one page in it if the batch size is very large + // or pages are very small + // TODO: Test this case + scheduled: VecDeque>, + // Rows that have been awaited + rows_loaded: u64, + // Rows that have drained + rows_drained: u64, + // Rows that have been popped (the decoder has been completely drained and removed from `scheduled`) + rows_popped: u64, + // Total number of rows in the struct + num_rows: u64, + // The field index in the struct (used for debugging / logging) + field_index: u32, +} + +impl ChildState { + fn new(num_rows: u64, field_index: u32) -> Self { + Self { + scheduled: VecDeque::new(), + rows_loaded: 0, + rows_drained: 0, + rows_popped: 0, + num_rows, + field_index, + } + } + + // Wait for the next set of rows to arrive + // + // Wait until we have at least `loaded_need` loaded and stop as soon as we + // go above that limit. + async fn wait_for_loaded(&mut self, loaded_need: u64) -> Result<()> { + trace!( + "Struct child {} waiting for more than {} rows to be loaded and {} are fully loaded already", + self.field_index, loaded_need, self.rows_loaded, + ); + let mut fully_loaded = self.rows_popped; + for (page_idx, next_decoder) in self.scheduled.iter_mut().enumerate() { + if next_decoder.rows_unloaded() > 0 { + let mut current_need = loaded_need; + current_need -= fully_loaded; + let rows_in_page = next_decoder.num_rows(); + let need_for_page = (rows_in_page - 1).min(current_need); + trace!( + "Struct child {} page {} will wait until more than {} rows loaded from page with {} rows", + self.field_index, page_idx, need_for_page, rows_in_page, + ); + // We might only await part of a page. This is important for things + // like the struct> case where we have one outer page, one + // middle page, and then a bunch of inner pages. If we await the entire + // middle page then we will have to wait for all the inner pages to arrive + // before we can start decoding. + next_decoder.wait_for_loaded(need_for_page).await?; + let now_loaded = next_decoder.rows_loaded(); + fully_loaded += now_loaded; + trace!( + "Struct child {} page {} await and now has {} loaded rows and we have {} fully loaded", + self.field_index, page_idx, now_loaded, fully_loaded + ); + } else { + fully_loaded += next_decoder.num_rows(); + } + if fully_loaded > loaded_need { + break; + } + } + self.rows_loaded = fully_loaded; + trace!( + "Struct child {} loaded {} new rows and now {} are loaded", + self.field_index, fully_loaded, self.rows_loaded + ); + Ok(()) + } + + fn drain(&mut self, num_rows: u64) -> Result { + trace!("Struct draining {} rows", num_rows); + + trace!( + "Draining {} rows from struct page with {} rows already drained", + num_rows, self.rows_drained + ); + let mut remaining = num_rows; + let mut composite = CompositeDecodeTask { + tasks: Vec::new(), + num_rows: 0, + has_more: true, + }; + while remaining > 0 { + let next = self.scheduled.front_mut().unwrap(); + let rows_to_take = remaining.min(next.rows_left()); + let next_task = next.drain(rows_to_take)?; + if next.rows_left() == 0 { + trace!("Completely drained page"); + self.rows_popped += next.num_rows(); + self.scheduled.pop_front(); + } + remaining -= rows_to_take; + composite.tasks.push(next_task.task); + composite.num_rows += next_task.num_rows; + } + self.rows_drained += num_rows; + composite.has_more = self.rows_drained != self.num_rows; + Ok(composite) + } +} + +// Wrapper around ChildState that orders using rows_unawaited +struct WaitOrder<'a>(&'a mut ChildState); + +impl Eq for WaitOrder<'_> {} +impl PartialEq for WaitOrder<'_> { + fn eq(&self, other: &Self) -> bool { + self.0.rows_loaded == other.0.rows_loaded + } +} +impl Ord for WaitOrder<'_> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + // Note: this is inverted so we have a min-heap + other.0.rows_loaded.cmp(&self.0.rows_loaded) + } +} +impl PartialOrd for WaitOrder<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[derive(Debug)] +pub struct SimpleStructDecoder { + children: Vec, + child_fields: Fields, + data_type: DataType, + num_rows: u64, +} + +impl SimpleStructDecoder { + pub fn new(child_fields: Fields, num_rows: u64) -> Self { + let data_type = DataType::Struct(child_fields.clone()); + Self { + children: child_fields + .iter() + .enumerate() + .map(|(idx, _)| ChildState::new(num_rows, idx as u32)) + .collect(), + child_fields, + data_type, + num_rows, + } + } + + async fn do_wait_for_loaded(&mut self, loaded_need: u64) -> Result<()> { + let mut wait_orders = self + .children + .iter_mut() + .filter_map(|child| { + if child.rows_loaded <= loaded_need { + Some(WaitOrder(child)) + } else { + None + } + }) + .collect::>(); + while !wait_orders.is_empty() { + let next_waiter = wait_orders.pop().unwrap(); + let next_highest = wait_orders + .peek() + .map(|w| w.0.rows_loaded) + .unwrap_or(u64::MAX); + // Wait until you have the number of rows needed, or at least more than the + // next highest waiter + let limit = loaded_need.min(next_highest); + next_waiter.0.wait_for_loaded(limit).await?; + log::trace!( + "Struct child {} finished await pass and now {} are loaded", + next_waiter.0.field_index, + next_waiter.0.rows_loaded + ); + if next_waiter.0.rows_loaded <= loaded_need { + wait_orders.push(next_waiter); + } + } + Ok(()) + } +} + +impl LogicalPageDecoder for SimpleStructDecoder { + fn accept_child(&mut self, mut child: DecoderReady) -> Result<()> { + // children with empty path should not be delivered to this method + let child_idx = child.path.pop_front().unwrap(); + if child.path.is_empty() { + // This decoder is intended for us + self.children[child_idx as usize] + .scheduled + .push_back(child.decoder); + } else { + // This decoder is intended for one of our children + let intended = self.children[child_idx as usize].scheduled.back_mut().ok_or_else(|| Error::internal(format!("Decoder scheduled for child at index {} but we don't have any child at that index yet", child_idx)))?; + intended.accept_child(child)?; + } + Ok(()) + } + + fn wait_for_loaded(&mut self, loaded_need: u64) -> BoxFuture<'_, Result<()>> { + self.do_wait_for_loaded(loaded_need).boxed() + } + + fn drain(&mut self, num_rows: u64) -> Result { + let child_tasks = self + .children + .iter_mut() + .map(|child| child.drain(num_rows)) + .collect::>>()?; + let num_rows = child_tasks[0].num_rows; + debug_assert!(child_tasks.iter().all(|task| task.num_rows == num_rows)); + Ok(NextDecodeTask { + task: Box::new(SimpleStructDecodeTask { + children: child_tasks, + child_fields: self.child_fields.clone(), + }), + num_rows, + }) + } + + fn rows_loaded(&self) -> u64 { + self.children.iter().map(|c| c.rows_loaded).min().unwrap() + } + + fn rows_drained(&self) -> u64 { + // All children should have the same number of rows drained + debug_assert!( + self.children + .iter() + .all(|c| c.rows_drained == self.children[0].rows_drained) + ); + self.children[0].rows_drained + } + + fn num_rows(&self) -> u64 { + self.num_rows + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +struct CompositeDecodeTask { + // One per child + tasks: Vec>, + num_rows: u64, + has_more: bool, +} + +impl CompositeDecodeTask { + fn decode(self) -> Result { + let arrays = self + .tasks + .into_iter() + .map(|task| task.decode().map(|(arr, _)| arr)) + .collect::>>()?; + let array_refs = arrays.iter().map(|arr| arr.as_ref()).collect::>(); + // TODO: If this is a primitive column we should be able to avoid this + // allocation + copy with "page bridging" which could save us a few CPU + // cycles. + // + // This optimization is probably most important for super fast storage like NVME + // where the page size can be smaller. + Ok(arrow_select::concat::concat(&array_refs)?) + } +} + +struct SimpleStructDecodeTask { + children: Vec, + child_fields: Fields, +} + +impl DecodeArrayTask for SimpleStructDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let child_arrays = self + .children + .into_iter() + .map(|child| child.decode()) + .collect::>>()?; + // data_size is only tracked in the v2.1 structural decode path; the v2.0 array + // v2.0 path does not need it so we return 0. + Ok(( + Arc::new(StructArray::try_new(self.child_fields, child_arrays, None)?), + 0, + )) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical.rs new file mode 100644 index 000000000..66af9e806 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical.rs @@ -0,0 +1,329 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_schema::DataType; +use lance_arrow::DataTypeExt; + +use crate::{ + array_encoding::physical::{ + basic::BasicPageScheduler, binary::BinaryPageScheduler, bitmap::DenseBitmapScheduler, + dictionary::DictionaryPageScheduler, fixed_size_list::FixedListScheduler, + fsst::FsstPageScheduler, packed_struct::PackedStructPageScheduler, + value::ValuePageScheduler, + }, + buffer::LanceBuffer, + decoder::{PageBuffers, PageScheduler}, + encodings::physical::block::{CompressionConfig, CompressionScheme}, + format::pb::{self, PackedStruct}, +}; + +pub mod basic; +pub mod binary; +pub mod bitmap; +#[cfg(feature = "bitpacking")] +pub mod bitpack; +pub mod block; +pub mod dictionary; +pub mod fixed_size_binary; +pub mod fixed_size_list; +pub mod fsst; +pub mod packed_struct; +pub mod value; + +// Translate a protobuf buffer description into a position in the file. This could be a page +// buffer, a column buffer, or a file buffer. +fn get_buffer(buffer_desc: &pb::Buffer, buffers: &PageBuffers) -> (u64, u64) { + let index = buffer_desc.buffer_index as usize; + + match pb::buffer::BufferType::try_from(buffer_desc.buffer_type).unwrap() { + pb::buffer::BufferType::Page => buffers.positions_and_sizes[index], + pb::buffer::BufferType::Column => buffers.column_buffers.positions_and_sizes[index], + pb::buffer::BufferType::File => { + buffers.column_buffers.file_buffers.positions_and_sizes[index] + } + } +} + +/// Convert a protobuf buffer encoding into a physical page scheduler +fn get_buffer_decoder(encoding: &pb::Flat, buffers: &PageBuffers) -> Box { + let (buffer_offset, buffer_size) = get_buffer(encoding.buffer.as_ref().unwrap(), buffers); + let compression_config: CompressionConfig = match encoding.compression.as_ref() { + None => CompressionConfig::new(CompressionScheme::None, None), + Some(compression) => CompressionConfig::new( + compression.scheme.as_str().parse().unwrap(), + compression.level, + ), + }; + match encoding.bits_per_value { + 1 => Box::new(DenseBitmapScheduler::new(buffer_offset)), + bits_per_value => { + if bits_per_value % 8 != 0 { + todo!( + "bits_per_value ({}) that is not a multiple of 8", + bits_per_value + ); + } + Box::new(ValuePageScheduler::new( + bits_per_value / 8, + buffer_offset, + buffer_size, + compression_config, + )) + } + } +} + +#[cfg(feature = "bitpacking")] +fn get_bitpacked_buffer_decoder( + encoding: &pb::Bitpacked, + buffers: &PageBuffers, +) -> Box { + let (buffer_offset, _buffer_size) = get_buffer(encoding.buffer.as_ref().unwrap(), buffers); + + Box::new(bitpack::BitpackedScheduler::new( + encoding.compressed_bits_per_value, + encoding.uncompressed_bits_per_value, + buffer_offset, + encoding.signed, + )) +} + +#[cfg(feature = "bitpacking")] +fn get_bitpacked_for_non_neg_buffer_decoder( + encoding: &pb::BitpackedForNonNeg, + buffers: &PageBuffers, +) -> Box { + let (buffer_offset, _buffer_size) = get_buffer(encoding.buffer.as_ref().unwrap(), buffers); + + Box::new(bitpack::BitpackedForNonNegScheduler::new( + encoding.compressed_bits_per_value, + encoding.uncompressed_bits_per_value, + buffer_offset, + )) +} + +fn decoder_from_packed_struct( + packed_struct: &PackedStruct, + buffers: &PageBuffers, + data_type: &DataType, +) -> Box { + let inner_encodings = &packed_struct.inner; + let fields = match data_type { + DataType::Struct(fields) => Some(fields), + _ => None, + } + .unwrap(); + + let inner_datatypes = fields + .iter() + .map(|field| field.data_type()) + .collect::>(); + + let mut inner_schedulers = Vec::with_capacity(fields.len()); + for i in 0..fields.len() { + let inner_encoding = &inner_encodings[i]; + let inner_datatype = inner_datatypes[i]; + let inner_scheduler = decoder_from_array_encoding(inner_encoding, buffers, inner_datatype); + inner_schedulers.push(inner_scheduler); + } + + let packed_buffer = packed_struct.buffer.as_ref().unwrap(); + let (buffer_offset, _) = get_buffer(packed_buffer, buffers); + + Box::new(PackedStructPageScheduler::new( + inner_schedulers, + data_type.clone(), + buffer_offset, + )) +} + +/// Convert a protobuf array encoding into a physical page scheduler +pub fn decoder_from_array_encoding( + encoding: &pb::ArrayEncoding, + buffers: &PageBuffers, + data_type: &DataType, +) -> Box { + match encoding.array_encoding.as_ref().unwrap() { + pb::array_encoding::ArrayEncoding::Nullable(basic) => { + match basic.nullability.as_ref().unwrap() { + pb::nullable::Nullability::NoNulls(no_nulls) => Box::new( + BasicPageScheduler::new_non_nullable(decoder_from_array_encoding( + no_nulls.values.as_ref().unwrap(), + buffers, + data_type, + )), + ), + pb::nullable::Nullability::SomeNulls(some_nulls) => { + Box::new(BasicPageScheduler::new_nullable( + decoder_from_array_encoding( + some_nulls.validity.as_ref().unwrap(), + buffers, + data_type, + ), + decoder_from_array_encoding( + some_nulls.values.as_ref().unwrap(), + buffers, + data_type, + ), + )) + } + pb::nullable::Nullability::AllNulls(_) => { + Box::new(BasicPageScheduler::new_all_null()) + } + } + } + #[cfg(feature = "bitpacking")] + pb::array_encoding::ArrayEncoding::Bitpacked(bitpacked) => { + get_bitpacked_buffer_decoder(bitpacked, buffers) + } + #[cfg(not(feature = "bitpacking"))] + pb::array_encoding::ArrayEncoding::Bitpacked(_) => { + panic!("Runtime built without bitpacking support") + } + pb::array_encoding::ArrayEncoding::Flat(flat) => get_buffer_decoder(flat, buffers), + pb::array_encoding::ArrayEncoding::FixedSizeList(fixed_size_list) => { + let item_encoding = fixed_size_list.items.as_ref().unwrap(); + let item_scheduler = decoder_from_array_encoding(item_encoding, buffers, data_type); + Box::new(FixedListScheduler::new( + item_scheduler, + fixed_size_list.dimension, + )) + } + // This is a column containing the list offsets. This wrapper is superfluous at the moment + // since we know it is a list based on the schema. In the future there may be different ways + // of storing the list offsets. + pb::array_encoding::ArrayEncoding::List(list) => { + decoder_from_array_encoding(list.offsets.as_ref().unwrap(), buffers, data_type) + } + pb::array_encoding::ArrayEncoding::Binary(binary) => { + let indices_encoding = binary.indices.as_ref().unwrap(); + let bytes_encoding = binary.bytes.as_ref().unwrap(); + + let indices_scheduler = + decoder_from_array_encoding(indices_encoding, buffers, data_type); + let bytes_scheduler = decoder_from_array_encoding(bytes_encoding, buffers, data_type); + + let offset_type = match data_type { + DataType::LargeBinary | DataType::LargeUtf8 => DataType::Int64, + _ => DataType::Int32, + }; + + Box::new(BinaryPageScheduler::new( + indices_scheduler.into(), + bytes_scheduler.into(), + offset_type, + binary.null_adjustment, + )) + } + pb::array_encoding::ArrayEncoding::Fsst(fsst) => { + let inner = + decoder_from_array_encoding(fsst.binary.as_ref().unwrap(), buffers, data_type); + + Box::new(FsstPageScheduler::new( + inner, + LanceBuffer::from_bytes(fsst.symbol_table.clone(), 1), + )) + } + pb::array_encoding::ArrayEncoding::Dictionary(dictionary) => { + let indices_encoding = dictionary.indices.as_ref().unwrap(); + let items_encoding = dictionary.items.as_ref().unwrap(); + let num_dictionary_items = dictionary.num_dictionary_items; + + // We can get here in 2 ways. The data is dictionary encoded and the user wants a dictionary or + // the data is dictionary encoded, as an optimization, and the user wants the value type. Figure + // out the value type. + let value_type = if let DataType::Dictionary(_, value_type) = data_type { + value_type + } else { + data_type + }; + + // Note: we don't actually know the indices type here, passing down `data_type` works ok because + // the dictionary indices are always integers and we don't need the data_type to figure out how + // to decode integers. + let indices_scheduler = + decoder_from_array_encoding(indices_encoding, buffers, data_type); + + let items_scheduler = decoder_from_array_encoding(items_encoding, buffers, value_type); + + let should_decode_dict = !data_type.is_dictionary(); + + Box::new(DictionaryPageScheduler::new( + indices_scheduler.into(), + items_scheduler.into(), + num_dictionary_items, + should_decode_dict, + )) + } + pb::array_encoding::ArrayEncoding::FixedSizeBinary(fixed_size_binary) => { + let bytes_encoding = fixed_size_binary.bytes.as_ref().unwrap(); + let bytes_scheduler = decoder_from_array_encoding(bytes_encoding, buffers, data_type); + let bytes_per_offset = match data_type { + DataType::LargeBinary | DataType::LargeUtf8 => 8, + DataType::Binary | DataType::Utf8 => 4, + _ => panic!("FixedSizeBinary only supports binary and utf8 types"), + }; + + Box::new(fixed_size_binary::FixedSizeBinaryPageScheduler::new( + bytes_scheduler, + fixed_size_binary.byte_width, + bytes_per_offset, + )) + } + pb::array_encoding::ArrayEncoding::PackedStruct(packed_struct) => { + decoder_from_packed_struct(packed_struct, buffers, data_type) + } + #[cfg(feature = "bitpacking")] + pb::array_encoding::ArrayEncoding::BitpackedForNonNeg(bitpacked) => { + get_bitpacked_for_non_neg_buffer_decoder(bitpacked, buffers) + } + #[cfg(not(feature = "bitpacking"))] + pb::array_encoding::ArrayEncoding::BitpackedForNonNeg(_) => { + panic!("Runtime built without bitpacking support") + } + // Currently there is no way to encode struct nullability and structs are encoded with a "header" column + // (that has no data). We never actually decode that column and so this branch is never actually encountered. + // + // This will change in the future when we add support for struct nullability. + pb::array_encoding::ArrayEncoding::Struct(_) => unreachable!(), + // 2.1 only + _ => unreachable!("Unsupported array encoding: {:?}", encoding), + } +} + +#[cfg(test)] +mod tests { + use crate::array_encoding::physical::get_buffer_decoder; + use crate::decoder::{ColumnBuffers, FileBuffers, PageBuffers}; + use crate::format::pb; + + #[test] + fn test_get_buffer_decoder_for_compressed_buffer() { + let page_scheduler = get_buffer_decoder( + &pb::Flat { + buffer: Some(pb::Buffer { + buffer_index: 0, + buffer_type: pb::buffer::BufferType::File as i32, + }), + bits_per_value: 8, + compression: Some(pb::Compression { + scheme: "zstd".to_string(), + level: Some(0), + }), + }, + &PageBuffers { + column_buffers: ColumnBuffers { + file_buffers: FileBuffers { + positions_and_sizes: &[(0, 100)], + }, + positions_and_sizes: &[], + }, + positions_and_sizes: &[], + }, + ); + assert_eq!( + format!("{:?}", page_scheduler).as_str(), + "ValuePageScheduler { bytes_per_value: 1, buffer_offset: 0, buffer_size: 100, compression_config: CompressionConfig { scheme: Zstd, level: Some(0) } }" + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/basic.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/basic.rs new file mode 100644 index 000000000..6dd7326a9 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/basic.rs @@ -0,0 +1,236 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; +use log::trace; + +use crate::{ + EncodingsIo, + data::{AllNullDataBlock, BlockInfo, DataBlock, NullableDataBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, + format::ProtobufUtils, +}; + +use lance_core::Result; + +struct DataDecoders { + validity: Box, + values: Box, +} + +enum DataNullStatus { + // Neither validity nor values + All, + // Values only + None(Box), + // Validity and values + Some(DataDecoders), +} + +#[derive(Debug)] +struct DataSchedulers { + validity: Box, + values: Box, +} + +#[derive(Debug)] +enum SchedulerNullStatus { + // Values only + None(Box), + // Validity and values + Some(DataSchedulers), + // Neither validity nor values + All, +} + +impl SchedulerNullStatus { + fn values_scheduler(&self) -> Option<&dyn PageScheduler> { + match self { + Self::All => None, + Self::None(values) => Some(values.as_ref()), + Self::Some(schedulers) => Some(schedulers.values.as_ref()), + } + } +} + +/// A physical scheduler for "basic" fields. These are fields that have an optional +/// validity bitmap and some kind of values buffer. +/// +/// No actual decoding happens here, we are simply aggregating the two buffers. +/// +/// If everything is null then there are no data buffers at all. +// TODO: Add support/tests for primitive nulls +// TODO: Add tests for the all-null case +// +// Right now this is always present on primitive fields. In the future we may use a +// sentinel encoding instead. +#[derive(Debug)] +pub struct BasicPageScheduler { + mode: SchedulerNullStatus, +} + +impl BasicPageScheduler { + /// Creates a new instance that expects a validity bitmap + pub fn new_nullable( + validity_decoder: Box, + values_decoder: Box, + ) -> Self { + Self { + mode: SchedulerNullStatus::Some(DataSchedulers { + validity: validity_decoder, + values: values_decoder, + }), + } + } + + /// Create a new instance that does not need a validity bitmap because no item is null + pub fn new_non_nullable(values_decoder: Box) -> Self { + Self { + mode: SchedulerNullStatus::None(values_decoder), + } + } + + /// Create a new instance where all values are null + /// + /// It may seem strange we need `values_decoder` here but Arrow requires that value + /// buffers still be allocated / sized even if everything is null. So we need the value + /// decoder to calculate the capacity of the garbage buffer. + pub fn new_all_null() -> Self { + Self { + mode: SchedulerNullStatus::All, + } + } +} + +impl PageScheduler for BasicPageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let validity_future = match &self.mode { + SchedulerNullStatus::None(_) | SchedulerNullStatus::All => None, + SchedulerNullStatus::Some(schedulers) => Some(schedulers.validity.schedule_ranges( + ranges, + scheduler, + top_level_row, + )), + }; + + let values_future = if let Some(values_scheduler) = self.mode.values_scheduler() { + Some( + values_scheduler + .schedule_ranges(ranges, scheduler, top_level_row) + .boxed(), + ) + } else { + trace!("No values fetch needed since values all null"); + None + }; + + async move { + let mode = match (values_future, validity_future) { + (None, None) => DataNullStatus::All, + (Some(values_future), None) => DataNullStatus::None(values_future.await?), + (Some(values_future), Some(validity_future)) => { + DataNullStatus::Some(DataDecoders { + values: values_future.await?, + validity: validity_future.await?, + }) + } + _ => unreachable!(), + }; + Ok(Box::new(BasicPageDecoder { mode }) as Box) + } + .boxed() + } +} + +struct BasicPageDecoder { + mode: DataNullStatus, +} + +impl PrimitivePageDecoder for BasicPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + match &self.mode { + DataNullStatus::Some(decoders) => { + let validity = decoders.validity.decode(rows_to_skip, num_rows)?; + let validity = validity.as_fixed_width().unwrap(); + let values = decoders.values.decode(rows_to_skip, num_rows)?; + Ok(DataBlock::Nullable(NullableDataBlock { + data: Box::new(values), + nulls: validity.data, + block_info: BlockInfo::new(), + })) + } + DataNullStatus::All => Ok(DataBlock::AllNull(AllNullDataBlock { + num_values: num_rows, + })), + DataNullStatus::None(values) => values.decode(rows_to_skip, num_rows), + } + } +} + +#[derive(Debug)] +pub struct BasicEncoder { + values_encoder: Box, +} + +impl BasicEncoder { + pub fn new(values_encoder: Box) -> Self { + Self { values_encoder } + } +} + +impl ArrayEncoder for BasicEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + match data { + DataBlock::AllNull(_) => { + let encoding = ProtobufUtils::basic_all_null_encoding(); + Ok(EncodedArray { data, encoding }) + } + DataBlock::Nullable(nullable) => { + let validity_buffer_index = *buffer_index; + *buffer_index += 1; + + let validity_desc = ProtobufUtils::flat_encoding( + 1, + validity_buffer_index, + /*compression=*/ None, + ); + let encoded_values = + self.values_encoder + .encode(*nullable.data, data_type, buffer_index)?; + let encoding = + ProtobufUtils::basic_some_null_encoding(validity_desc, encoded_values.encoding); + let encoded = DataBlock::Nullable(NullableDataBlock { + data: Box::new(encoded_values.data), + nulls: nullable.nulls, + block_info: BlockInfo::new(), + }); + Ok(EncodedArray { + data: encoded, + encoding, + }) + } + _ => { + let encoded_values = self.values_encoder.encode(data, data_type, buffer_index)?; + let encoding = ProtobufUtils::basic_no_null_encoding(encoded_values.encoding); + Ok(EncodedArray { + data: encoded_values.data, + encoding, + }) + } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/binary.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/binary.rs new file mode 100644 index 000000000..294b295de --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/binary.rs @@ -0,0 +1,571 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use core::panic; +use std::sync::Arc; + +use arrow_array::ArrayRef; +use arrow_array::cast::AsArray; +use arrow_array::types::UInt64Type; +use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder, NullBuffer, ScalarBuffer, bit_util}; +use futures::TryFutureExt; + +use futures::{FutureExt, future::BoxFuture}; + +use crate::array_encoding::logical::primitive::PrimitiveFieldDecoder; +use crate::buffer::LanceBuffer; +use crate::data::{ + BlockInfo, DataBlock, FixedWidthDataBlock, NullableDataBlock, VariableWidthBlock, +}; +use crate::decoder::LogicalPageDecoder; +use crate::encoder::{ArrayEncoder, EncodedArray}; +use crate::encodings::physical::block::{ + BufferCompressor, CompressionConfig, GeneralBufferCompressor, +}; +use crate::format::ProtobufUtils; +use crate::{ + EncodingsIo, + decoder::{PageScheduler, PrimitivePageDecoder}, +}; + +use arrow_array::{PrimitiveArray, UInt64Array}; +use arrow_schema::DataType; +use lance_core::Result; + +struct IndicesNormalizer { + indices: Vec, + validity: BooleanBufferBuilder, + null_adjustment: u64, +} + +impl IndicesNormalizer { + fn new(num_rows: u64, null_adjustment: u64) -> Self { + let mut indices = Vec::with_capacity(num_rows as usize); + indices.push(0); + Self { + indices, + validity: BooleanBufferBuilder::new(num_rows as usize), + null_adjustment, + } + } + + fn normalize(&self, val: u64) -> (bool, u64) { + if val >= self.null_adjustment { + (false, val - self.null_adjustment) + } else { + (true, val) + } + } + + fn extend(&mut self, new_indices: &PrimitiveArray, is_start: bool) -> Result<()> { + let mut last = *self.indices.last().unwrap(); + if is_start { + let (is_valid, val) = self.normalize(new_indices.value(0)); + self.indices.push(val); + self.validity.append(is_valid); + last += val; + } + let mut prev = self.normalize(*new_indices.values().first().unwrap()).1; + for (i, w) in new_indices.values().windows(2).enumerate() { + let (is_valid, val) = self.normalize(w[1]); + let next = match val.checked_sub(prev) { + Some(delta) => delta + last, + None => { + return Err(lance_core::Error::invalid_input(format!( + "corrupt binary page: normalized offset {} is less than previous offset {} \ + at index {}, null_adjustment={}, raw values were [{}, {}]. \ + This usually indicates the file data has been corrupted.", + val, prev, i, self.null_adjustment, w[0], w[1] + ))); + } + }; + self.indices.push(next); + self.validity.append(is_valid); + prev = val; + last = next; + } + Ok(()) + } + + fn into_parts(mut self) -> (Vec, BooleanBuffer) { + (self.indices, self.validity.finish()) + } +} + +#[derive(Debug)] +pub struct BinaryPageScheduler { + indices_scheduler: Arc, + bytes_scheduler: Arc, + offsets_type: DataType, + null_adjustment: u64, +} + +impl BinaryPageScheduler { + pub fn new( + indices_scheduler: Arc, + bytes_scheduler: Arc, + offsets_type: DataType, + null_adjustment: u64, + ) -> Self { + Self { + indices_scheduler, + bytes_scheduler, + offsets_type, + null_adjustment, + } + } + + fn decode_indices(decoder: Arc, num_rows: u64) -> Result { + let mut primitive_wrapper = + PrimitiveFieldDecoder::new_from_data(decoder, DataType::UInt64, num_rows, false); + let drained_task = primitive_wrapper.drain(num_rows)?; + let indices_decode_task = drained_task.task; + indices_decode_task.decode().map(|(arr, _)| arr) + } +} + +struct IndirectData { + decoded_indices: UInt64Array, + offsets_type: DataType, + validity: BooleanBuffer, + bytes_decoder_fut: BoxFuture<'static, Result>>, +} + +impl PageScheduler for BinaryPageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + // ranges corresponds to row ranges that the user wants to fetch. + // if user wants row range a..b + // Case 1: if a != 0, we need indices a-1..b to decode + // Case 2: if a = 0, we need indices 0..b to decode + let indices_ranges = ranges + .iter() + .map(|range| { + if range.start != 0 { + (range.start - 1)..range.end + } else { + 0..range.end + } + }) + .collect::>>(); + + // We schedule all the indices for decoding together + // This is more efficient compared to scheduling them one by one (reduces speed significantly for random access) + let indices_page_decoder = + self.indices_scheduler + .schedule_ranges(&indices_ranges, scheduler, top_level_row); + + let num_rows = ranges.iter().map(|r| r.end - r.start).sum::(); + let indices_num_rows = indices_ranges.iter().map(|r| r.end - r.start).sum::(); + + let ranges = ranges.to_vec(); + let copy_scheduler = scheduler.clone(); + let copy_bytes_scheduler = self.bytes_scheduler.clone(); + let null_adjustment = self.null_adjustment; + let offsets_type = self.offsets_type.clone(); + + tokio::spawn(async move { + // For the following data: + // "abcd", "hello", "abcd", "apple", "hello", "abcd" + // 4, 9, 13, 18, 23, 27 + // e.g. want to scan rows 0, 2, 4 + // i.e. offsets are 4 | 9, 13 | 18, 23 + // Normalization is required for decoding later on + // Normalize each part: 0, 4 | 0, 4 | 0, 5 + // Remove leading zeros except first one: 0, 4 | 4 | 5 + // Cumulative sum: 0, 4 | 8 | 13 + // These are the normalized offsets stored in decoded_indices + // Rest of the workflow is continued later in BinaryPageDecoder + let indices_decoder = Arc::from(indices_page_decoder.await?); + let indices = Self::decode_indices(indices_decoder, indices_num_rows)?; + let decoded_indices = indices.as_primitive::(); + + let mut indices_builder = IndicesNormalizer::new(num_rows, null_adjustment); + let mut bytes_ranges = Vec::new(); + let mut curr_offset_index = 0; + + for curr_row_range in ranges.iter() { + let row_start = curr_row_range.start; + let curr_range_len = (curr_row_range.end - row_start) as usize; + + let curr_indices; + + if row_start == 0 { + curr_indices = decoded_indices.slice(0, curr_range_len); + curr_offset_index = curr_range_len; + } else { + curr_indices = decoded_indices.slice(curr_offset_index, curr_range_len + 1); + curr_offset_index += curr_range_len + 1; + } + + let first = if row_start == 0 { + 0 + } else { + indices_builder + .normalize(*curr_indices.values().first().unwrap()) + .1 + }; + let last = indices_builder + .normalize(*curr_indices.values().last().unwrap()) + .1; + + if first != last { + bytes_ranges.push(first..last); + } + + indices_builder.extend(&curr_indices, row_start == 0)?; + } + + let (indices, validity) = indices_builder.into_parts(); + let decoded_indices = UInt64Array::from(indices); + + // In the indirect task we schedule the bytes, but we do not await them. We don't want to + // await the bytes until the decoder is ready for them so that we don't release the backpressure + // too early + let bytes_decoder_fut = + copy_bytes_scheduler.schedule_ranges(&bytes_ranges, ©_scheduler, top_level_row); + + Ok(IndirectData { + decoded_indices, + validity, + offsets_type, + bytes_decoder_fut, + }) + }) + // Propagate join panic + .map(|join_handle| join_handle.unwrap()) + .and_then(|indirect_data| { + async move { + // Later, this will be called once the decoder actually starts polling. At that point + // we await the bytes (releasing the backpressure) + let bytes_decoder = indirect_data.bytes_decoder_fut.await?; + Ok(Box::new(BinaryPageDecoder { + decoded_indices: indirect_data.decoded_indices, + offsets_type: indirect_data.offsets_type, + validity: indirect_data.validity, + bytes_decoder, + }) as Box) + } + }) + .boxed() + } +} + +struct BinaryPageDecoder { + decoded_indices: UInt64Array, + offsets_type: DataType, + validity: BooleanBuffer, + bytes_decoder: Box, +} + +impl PrimitivePageDecoder for BinaryPageDecoder { + // Continuing the example from BinaryPageScheduler + // Suppose batch_size = 2. Then first, rows_to_skip=0, num_rows=2 + // Need to scan 2 rows + // First row will be 4-0=4 bytes, second also 8-4=4 bytes. + // Allocate 8 bytes capacity. + // Next rows_to_skip=2, num_rows=1 + // Skip 8 bytes. Allocate 5 bytes capacity. + // + // The normalized offsets are [0, 4, 8, 13] + // We only need [8, 13] to decode in this case. + // These need to be normalized in order to build the string later + // So return [0, 5] + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + // STEP 1: validity buffer + let target_validity = self + .validity + .slice(rows_to_skip as usize, num_rows as usize); + let has_nulls = target_validity.count_set_bits() < target_validity.len(); + + let validity_buffer = if has_nulls { + let num_validity_bits = arrow_buffer::bit_util::ceil(num_rows as usize, 8); + let mut validity_buffer = Vec::with_capacity(num_validity_bits); + + if rows_to_skip == 0 { + validity_buffer.extend_from_slice(target_validity.inner().as_slice()); + } else { + // Need to copy the buffer because there may be a bit offset in first byte + let target_validity = BooleanBuffer::from_iter(target_validity.iter()); + validity_buffer.extend_from_slice(target_validity.inner().as_slice()); + } + Some(validity_buffer) + } else { + None + }; + + // STEP 2: offsets buffer + // Currently we always do a copy here, we need to cast to the appropriate type + // and we go ahead and normalize so the starting offset is 0 (though we could skip + // this) + let bytes_per_offset = match self.offsets_type { + DataType::Int32 => 4, + DataType::Int64 => 8, + _ => panic!("Unsupported offsets type"), + }; + + let target_offsets = self + .decoded_indices + .slice(rows_to_skip as usize, (num_rows + 1) as usize); + + // Normalize and cast (TODO: could fuse these into one pass for micro-optimization) + let target_vec = target_offsets.values(); + let start = target_vec[0]; + let offsets_buffer = + match bytes_per_offset { + 4 => ScalarBuffer::from_iter(target_vec.iter().map(|x| (x - start) as i32)) + .into_inner(), + 8 => ScalarBuffer::from_iter(target_vec.iter().map(|x| (x - start) as i64)) + .into_inner(), + _ => panic!("Unsupported offsets type"), + }; + + let bytes_to_skip = self.decoded_indices.value(rows_to_skip as usize); + let num_bytes = self + .decoded_indices + .value((rows_to_skip + num_rows) as usize) + - bytes_to_skip; + + let bytes = self.bytes_decoder.decode(bytes_to_skip, num_bytes)?; + let bytes = bytes.as_fixed_width().unwrap(); + debug_assert_eq!(bytes.bits_per_value, 8); + + let string_data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: bytes_per_offset * 8, + data: bytes.data, + num_values: num_rows, + offsets: LanceBuffer::from(offsets_buffer), + block_info: BlockInfo::new(), + }); + if let Some(validity) = validity_buffer { + Ok(DataBlock::Nullable(NullableDataBlock { + data: Box::new(string_data), + nulls: LanceBuffer::from(validity), + block_info: BlockInfo::new(), + })) + } else { + Ok(string_data) + } + } +} + +#[derive(Debug)] +pub struct BinaryEncoder { + indices_encoder: Box, + compression_config: Option, + buffer_compressor: Option>, +} + +impl BinaryEncoder { + pub fn try_new( + indices_encoder: Box, + compression_config: Option, + ) -> Result { + let buffer_compressor = compression_config + .map(GeneralBufferCompressor::get_compressor) + .transpose()?; + Ok(Self { + indices_encoder, + compression_config, + buffer_compressor, + }) + } + + // In 2.1 we will materialize nulls higher up (in the primitive encoder). Unfortunately, + // in 2.0 we actually need to write the offsets. + fn all_null_variable_width(data_type: &DataType, num_values: u64) -> VariableWidthBlock { + if matches!(data_type, DataType::Binary | DataType::Utf8) { + VariableWidthBlock { + bits_per_offset: 32, + data: LanceBuffer::empty(), + num_values, + offsets: LanceBuffer::reinterpret_vec(vec![0_u32; num_values as usize + 1]), + block_info: BlockInfo::new(), + } + } else { + VariableWidthBlock { + bits_per_offset: 64, + data: LanceBuffer::empty(), + num_values, + offsets: LanceBuffer::reinterpret_vec(vec![0_u64; num_values as usize + 1]), + block_info: BlockInfo::new(), + } + } + } +} + +// Creates indices arrays from string arrays +// Strings are a vector of arrays corresponding to each record batch +// Zero offset is removed from the start of the offsets array +// The indices array is computed across all arrays in the vector +fn get_indices_from_string_arrays( + offsets: LanceBuffer, + bits_per_offset: u8, + nulls: Option, + num_rows: usize, +) -> (DataBlock, u64) { + let mut indices = Vec::with_capacity(num_rows); + let mut last_offset = 0_u64; + if bits_per_offset == 32 { + let offsets = offsets.borrow_to_typed_slice::(); + indices.extend(offsets.as_ref().windows(2).map(|w| { + let strlen = (w[1] - w[0]) as u64; + last_offset += strlen; + last_offset + })); + } else if bits_per_offset == 64 { + let offsets = offsets.borrow_to_typed_slice::(); + indices.extend(offsets.as_ref().windows(2).map(|w| { + let strlen = (w[1] - w[0]) as u64; + last_offset += strlen; + last_offset + })); + } + + if indices.is_empty() { + return ( + DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::empty(), + num_values: 0, + block_info: BlockInfo::new(), + }), + 0, + ); + } + + let last_offset = *indices.last().expect("Indices array is empty"); + // 8 exabytes in a single array seems unlikely but...just in case + assert!( + last_offset < u64::MAX / 2, + "Indices array with strings up to 2^63 is too large for this encoding" + ); + let null_adjustment: u64 = *indices.last().expect("Indices array is empty") + 1; + + if let Some(nulls) = nulls { + let nulls = NullBuffer::new(BooleanBuffer::new(nulls.into_buffer(), 0, num_rows)); + indices + .iter_mut() + .zip(nulls.iter()) + .for_each(|(index, is_valid)| { + if !is_valid { + *index += null_adjustment; + } + }); + } + let indices = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::reinterpret_vec(indices), + num_values: num_rows as u64, + block_info: BlockInfo::new(), + }); + (indices, null_adjustment) +} + +impl ArrayEncoder for BinaryEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let (mut data, nulls) = match data { + DataBlock::Nullable(nullable) => { + let data = nullable.data.as_variable_width().unwrap(); + (data, Some(nullable.nulls)) + } + DataBlock::VariableWidth(variable) => (variable, None), + DataBlock::AllNull(all_null) => { + let data = Self::all_null_variable_width(data_type, all_null.num_values); + let validity = + LanceBuffer::all_unset(bit_util::ceil(all_null.num_values as usize, 8)); + (data, Some(validity)) + } + _ => panic!("Expected variable width data block but got {}", data.name()), + }; + + let (indices, null_adjustment) = get_indices_from_string_arrays( + data.offsets, + data.bits_per_offset, + nulls, + data.num_values as usize, + ); + let encoded_indices = + self.indices_encoder + .encode(indices, &DataType::UInt64, buffer_index)?; + + let encoded_indices_data = encoded_indices.data.as_fixed_width().unwrap(); + + assert!(encoded_indices_data.bits_per_value <= 64); + + if let Some(buffer_compressor) = &self.buffer_compressor { + let mut compressed_data = Vec::with_capacity(data.data.len()); + buffer_compressor.compress(&data.data, &mut compressed_data)?; + data.data = LanceBuffer::from(compressed_data); + } + + let data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: encoded_indices_data.bits_per_value as u8, + offsets: encoded_indices_data.data, + data: data.data, + num_values: data.num_values, + block_info: BlockInfo::new(), + }); + + let bytes_buffer_index = *buffer_index; + *buffer_index += 1; + + let bytes_encoding = ProtobufUtils::flat_encoding( + /*bits_per_value=*/ 8, + bytes_buffer_index, + self.compression_config, + ); + + let encoding = + ProtobufUtils::binary(encoded_indices.encoding, bytes_encoding, null_adjustment); + + Ok(EncodedArray { data, encoding }) + } +} + +#[cfg(test)] +mod tests { + use arrow_array::StringArray; + + use super::*; + + #[test] + fn test_encode_indices_adjusts_nulls() { + // Null entries in string arrays should be adjusted + let string_array = Arc::new(StringArray::from(vec![ + None, + Some("foo"), + Some("foo"), + None, + None, + None, + ])) as ArrayRef; + let string_data = DataBlock::from(string_array).as_nullable().unwrap(); + let nulls = string_data.nulls; + let string_data = string_data.data.as_variable_width().unwrap(); + + let (indices, null_adjustment) = get_indices_from_string_arrays( + string_data.offsets, + string_data.bits_per_offset, + Some(nulls), + string_data.num_values as usize, + ); + + let indices = indices.as_fixed_width().unwrap(); + assert_eq!(indices.bits_per_value, 64); + assert_eq!( + indices.data, + LanceBuffer::reinterpret_vec(vec![7_u64, 3, 6, 13, 13, 13]) + ); + assert_eq!(null_adjustment, 7); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitmap.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitmap.rs new file mode 100644 index 000000000..2168aff24 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitmap.rs @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ops::Range, sync::Arc}; + +use arrow_buffer::BooleanBufferBuilder; +use bytes::Bytes; + +use futures::{FutureExt, future::BoxFuture}; +use lance_core::Result; +use log::trace; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, FixedWidthDataBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, +}; + +/// A physical scheduler for bitmap buffers encoded densely as 1 bit per value +/// with bit-endianness(e.g. what Arrow uses for validity bitmaps and boolean arrays) +/// +/// This decoder decodes from one buffer of disk data into one buffer of memory data +#[derive(Debug, Clone, Copy)] +pub struct DenseBitmapScheduler { + buffer_offset: u64, +} + +impl DenseBitmapScheduler { + pub fn new(buffer_offset: u64) -> Self { + Self { buffer_offset } + } +} + +impl PageScheduler for DenseBitmapScheduler { + fn schedule_ranges( + &self, + ranges: &[Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let mut min = u64::MAX; + let mut max = 0; + let chunk_reqs = ranges + .iter() + .map(|range| { + debug_assert_ne!(range.start, range.end); + let start = self.buffer_offset + range.start / 8; + let bit_offset = range.start % 8; + let end = self.buffer_offset + range.end.div_ceil(8); + let byte_range = start..end; + min = min.min(start); + max = max.max(end); + (byte_range, bit_offset, range.end - range.start) + }) + .collect::>(); + + let byte_ranges = chunk_reqs + .iter() + .map(|(range, _, _)| range.clone()) + .collect::>(); + trace!( + "Scheduling I/O for {} ranges across byte range {}..{}", + byte_ranges.len(), + min, + max + ); + let bytes = scheduler.submit_request(byte_ranges, top_level_row); + + async move { + let bytes = bytes.await?; + let chunks = bytes + .into_iter() + .zip(chunk_reqs) + .map(|(bytes, (_, bit_offset, length))| BitmapData { + data: bytes, + bit_offset, + length, + }) + .collect::>(); + Ok(Box::new(BitmapDecoder { chunks }) as Box) + } + .boxed() + } +} + +struct BitmapData { + data: Bytes, + bit_offset: u64, + length: u64, +} + +struct BitmapDecoder { + chunks: Vec, +} + +impl PrimitivePageDecoder for BitmapDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let mut rows_to_skip = rows_to_skip; + let mut dest_builder = BooleanBufferBuilder::new(num_rows as usize); + + let mut rows_remaining = num_rows; + for chunk in &self.chunks { + if chunk.length <= rows_to_skip { + rows_to_skip -= chunk.length; + } else { + let start = rows_to_skip + chunk.bit_offset; + let num_vals_to_take = rows_remaining.min(chunk.length - rows_to_skip); + let end = start + num_vals_to_take; + dest_builder.append_packed_range(start as usize..end as usize, &chunk.data); + rows_to_skip = 0; + rows_remaining -= num_vals_to_take; + } + } + + let bool_buffer = dest_builder.finish().into_inner(); + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(bool_buffer), + bits_per_value: 1, + num_values: num_rows, + block_info: BlockInfo::new(), + })) + } +} + +#[cfg(test)] +mod tests { + + use arrow_array::BooleanArray; + use arrow_schema::{DataType, Field}; + use bytes::Bytes; + use std::{collections::HashMap, sync::Arc}; + + use crate::array_encoding::physical::bitmap::BitmapData; + use crate::data::{DataBlock, FixedWidthDataBlock}; + use crate::decoder::PrimitivePageDecoder; + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + use super::BitmapDecoder; + + #[test_log::test(tokio::test)] + async fn test_bitmap_boolean() { + let field = Field::new("", DataType::Boolean, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_fsl_bitmap_boolean() { + let field = Field::new("", DataType::Boolean, true); + let field = Field::new("", DataType::FixedSizeList(Arc::new(field), 3), true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_boolean() { + let array = BooleanArray::from(vec![ + Some(false), + Some(true), + None, + Some(false), + Some(true), + None, + Some(false), + None, + None, + ]); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..9) + .with_indices(vec![0, 1, 3, 4]); + check_round_trip_encoding_of_data(vec![Arc::new(array)], &test_cases, HashMap::default()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_tiny_boolean() { + // Test case for a tiny boolean array that is technically smaller than 1 byte + let array = BooleanArray::from(vec![Some(false), Some(true), None]); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_range(1..3) + .with_indices(vec![0, 2]); + check_round_trip_encoding_of_data(vec![Arc::new(array)], &test_cases, HashMap::default()) + .await; + } + + #[test] + fn test_bitmap_decoder_edge_cases() { + // Regression for a case where the row skip and the bit offset + // require us to read from the second Bytes instead of the first + let decoder = BitmapDecoder { + chunks: vec![ + BitmapData { + data: Bytes::from_static(&[0b11111111]), + bit_offset: 4, + length: 4, + }, + BitmapData { + data: Bytes::from_static(&[0b00000000]), + bit_offset: 4, + length: 4, + }, + ], + }; + + // Read from first and second chunk + let result = decoder.decode(2, 4).unwrap(); + let DataBlock::FixedWidth(FixedWidthDataBlock { data, .. }) = result else { + panic!("expected fixed width data block"); + }; + assert_eq!(data.as_ref(), &[0b00000011]); + + // Read from second chunk + let result = decoder.decode(5, 1).unwrap(); + let DataBlock::FixedWidth(FixedWidthDataBlock { data, .. }) = result else { + panic!("expected fixed width data block"); + }; + assert_eq!(data.as_ref(), &[0b00000000]); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitpack.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitpack.rs new file mode 100644 index 000000000..1d930af41 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/bitpack.rs @@ -0,0 +1,880 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_buffer::bit_util::ceil; +use bytes::Bytes; +use futures::future::{BoxFuture, FutureExt}; +use log::trace; + +use lance_bitpacking::BitPacking; +use lance_core::{Error, Result}; + +use crate::buffer::LanceBuffer; +use crate::data::BlockInfo; +use crate::data::{DataBlock, FixedWidthDataBlock}; +use crate::decoder::{PageScheduler, PrimitivePageDecoder}; +use bytemuck::cast_slice; + +const LOG_ELEMS_PER_CHUNK: u8 = 10; +const ELEMS_PER_CHUNK: u64 = 1 << LOG_ELEMS_PER_CHUNK; + +#[derive(Debug)] +pub struct BitpackedForNonNegScheduler { + compressed_bit_width: u64, + uncompressed_bits_per_value: u64, + buffer_offset: u64, +} + +impl BitpackedForNonNegScheduler { + pub fn new( + compressed_bit_width: u64, + uncompressed_bits_per_value: u64, + buffer_offset: u64, + ) -> Self { + Self { + compressed_bit_width, + uncompressed_bits_per_value, + buffer_offset, + } + } + + fn locate_chunk_start(&self, relative_row_num: u64) -> u64 { + let chunk_size = ELEMS_PER_CHUNK * self.compressed_bit_width / 8; + self.buffer_offset + (relative_row_num / ELEMS_PER_CHUNK * chunk_size) + } + + fn locate_chunk_end(&self, relative_row_num: u64) -> u64 { + let chunk_size = ELEMS_PER_CHUNK * self.compressed_bit_width / 8; + self.buffer_offset + (relative_row_num / ELEMS_PER_CHUNK * chunk_size) + chunk_size + } +} + +impl PageScheduler for BitpackedForNonNegScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + assert!(!ranges.is_empty()); + + let mut byte_ranges = vec![]; + + // map one bytes to multiple ranges, one bytes has at least one range corresponding to it + let mut bytes_idx_to_range_indices = vec![]; + let first_byte_range = std::ops::Range { + start: self.locate_chunk_start(ranges[0].start), + end: self.locate_chunk_end(ranges[0].end - 1), + }; // the ranges are half-open + byte_ranges.push(first_byte_range); + bytes_idx_to_range_indices.push(vec![ranges[0].clone()]); + + for (i, range) in ranges.iter().enumerate().skip(1) { + let this_start = self.locate_chunk_start(range.start); + let this_end = self.locate_chunk_end(range.end - 1); + + // when the current range start is in the same chunk as the previous range's end, we colaesce this two bytes ranges + // when the current range start is not in the same chunk as the previous range's end, we create a new bytes range + if this_start == self.locate_chunk_start(ranges[i - 1].end - 1) { + byte_ranges.last_mut().unwrap().end = this_end; + bytes_idx_to_range_indices + .last_mut() + .unwrap() + .push(range.clone()); + } else { + byte_ranges.push(this_start..this_end); + bytes_idx_to_range_indices.push(vec![range.clone()]); + } + } + + trace!( + "Scheduling I/O for {} ranges spread across byte range {}..{}", + byte_ranges.len(), + byte_ranges[0].start, + byte_ranges.last().unwrap().end + ); + + let bytes = scheduler.submit_request(byte_ranges.clone(), top_level_row); + + // copy the necessary data from `self` to move into the async block + let compressed_bit_width = self.compressed_bit_width; + let uncompressed_bits_per_value = self.uncompressed_bits_per_value; + let num_rows = ranges.iter().map(|range| range.end - range.start).sum(); + + async move { + let bytes = bytes.await?; + let decompressed_output = bitpacked_for_non_neg_decode( + compressed_bit_width, + uncompressed_bits_per_value, + &bytes, + &bytes_idx_to_range_indices, + num_rows, + ); + Ok(Box::new(BitpackedForNonNegPageDecoder { + uncompressed_bits_per_value, + decompressed_buf: decompressed_output, + }) as Box) + } + .boxed() + } +} + +#[derive(Debug)] +struct BitpackedForNonNegPageDecoder { + // number of bits in the uncompressed value. E.g. this will be 32 for DataType::UInt32 + uncompressed_bits_per_value: u64, + + decompressed_buf: LanceBuffer, +} + +impl PrimitivePageDecoder for BitpackedForNonNegPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + if ![8, 16, 32, 64].contains(&self.uncompressed_bits_per_value) { + return Err(Error::invalid_input_source("BitpackedForNonNegPageDecoder should only has uncompressed_bits_per_value of 8, 16, 32, or 64".into())); + } + + let elem_size_in_bytes = self.uncompressed_bits_per_value / 8; + + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: self.decompressed_buf.slice_with_length( + (rows_to_skip * elem_size_in_bytes) as usize, + (num_rows * elem_size_in_bytes) as usize, + ), + bits_per_value: self.uncompressed_bits_per_value, + num_values: num_rows, + block_info: BlockInfo::new(), + })) + } +} + +macro_rules! bitpacked_decode { + ($uncompressed_type:ty, $compressed_bit_width:expr, $data:expr, $bytes_idx_to_range_indices:expr, $num_rows:expr) => {{ + let mut decompressed: Vec<$uncompressed_type> = Vec::with_capacity($num_rows as usize); + let packed_chunk_size_in_byte: usize = (ELEMS_PER_CHUNK * $compressed_bit_width) as usize / 8; + let mut decompress_chunk_buf = vec![0 as $uncompressed_type; ELEMS_PER_CHUNK as usize]; + + for (i, bytes) in $data.iter().enumerate() { + let mut ranges_idx = 0; + let mut curr_range_start = $bytes_idx_to_range_indices[i][0].start; + let mut chunk_num = 0; + + while chunk_num * packed_chunk_size_in_byte < bytes.len() { + // Copy for memory alignment + // TODO: This copy should not be needed + let chunk_in_u8: Vec = bytes[chunk_num * packed_chunk_size_in_byte..] + [..packed_chunk_size_in_byte] + .to_vec(); + chunk_num += 1; + let chunk = cast_slice(&chunk_in_u8); + unsafe { + BitPacking::unchecked_unpack( + $compressed_bit_width as usize, + chunk, + &mut decompress_chunk_buf, + ); + } + + loop { + // Case 1: All the elements after (curr_range_start % ELEMS_PER_CHUNK) inside this chunk are needed. + let elems_after_curr_range_start_in_this_chunk = + ELEMS_PER_CHUNK - curr_range_start % ELEMS_PER_CHUNK; + if curr_range_start + elems_after_curr_range_start_in_this_chunk + <= $bytes_idx_to_range_indices[i][ranges_idx].end + { + decompressed.extend_from_slice( + &decompress_chunk_buf[(curr_range_start % ELEMS_PER_CHUNK) as usize..], + ); + curr_range_start += elems_after_curr_range_start_in_this_chunk; + break; + } else { + // Case 2: Only part of the elements after (curr_range_start % ELEMS_PER_CHUNK) inside this chunk are needed. + let elems_this_range_needed_in_this_chunk = + ($bytes_idx_to_range_indices[i][ranges_idx].end - curr_range_start) + .min(ELEMS_PER_CHUNK - curr_range_start % ELEMS_PER_CHUNK); + decompressed.extend_from_slice( + &decompress_chunk_buf[(curr_range_start % ELEMS_PER_CHUNK) as usize..] + [..elems_this_range_needed_in_this_chunk as usize], + ); + if curr_range_start + elems_this_range_needed_in_this_chunk + == $bytes_idx_to_range_indices[i][ranges_idx].end + { + ranges_idx += 1; + if ranges_idx == $bytes_idx_to_range_indices[i].len() { + break; + } + curr_range_start = $bytes_idx_to_range_indices[i][ranges_idx].start; + } else { + curr_range_start += elems_this_range_needed_in_this_chunk; + } + } + } + } + } + + LanceBuffer::reinterpret_vec(decompressed) + }}; +} + +fn bitpacked_for_non_neg_decode( + compressed_bit_width: u64, + uncompressed_bits_per_value: u64, + data: &[Bytes], + bytes_idx_to_range_indices: &[Vec>], + num_rows: u64, +) -> LanceBuffer { + match uncompressed_bits_per_value { + 8 => bitpacked_decode!( + u8, + compressed_bit_width, + data, + bytes_idx_to_range_indices, + num_rows + ), + 16 => bitpacked_decode!( + u16, + compressed_bit_width, + data, + bytes_idx_to_range_indices, + num_rows + ), + 32 => bitpacked_decode!( + u32, + compressed_bit_width, + data, + bytes_idx_to_range_indices, + num_rows + ), + 64 => bitpacked_decode!( + u64, + compressed_bit_width, + data, + bytes_idx_to_range_indices, + num_rows + ), + _ => unreachable!( + "bitpacked_for_non_neg_decode only supports 8, 16, 32, 64 uncompressed_bits_per_value" + ), + } +} + +// A physical scheduler for bitpacked buffers +#[derive(Debug, Clone, Copy)] +pub struct BitpackedScheduler { + bits_per_value: u64, + uncompressed_bits_per_value: u64, + buffer_offset: u64, + signed: bool, +} + +impl BitpackedScheduler { + pub fn new( + bits_per_value: u64, + uncompressed_bits_per_value: u64, + buffer_offset: u64, + signed: bool, + ) -> Self { + Self { + bits_per_value, + uncompressed_bits_per_value, + buffer_offset, + signed, + } + } +} + +impl PageScheduler for BitpackedScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let mut min = u64::MAX; + let mut max = 0; + + let mut buffer_bit_start_offsets: Vec = vec![]; + let mut buffer_bit_end_offsets: Vec> = vec![]; + let byte_ranges = ranges + .iter() + .map(|range| { + let start_byte_offset = range.start * self.bits_per_value / 8; + let mut end_byte_offset = range.end * self.bits_per_value / 8; + if !(range.end * self.bits_per_value).is_multiple_of(8) { + // If the end of the range is not byte-aligned, we need to read one more byte + end_byte_offset += 1; + + let end_bit_offset = range.end * self.bits_per_value % 8; + buffer_bit_end_offsets.push(Some(end_bit_offset as u8)); + } else { + buffer_bit_end_offsets.push(None); + } + + let start_bit_offset = range.start * self.bits_per_value % 8; + buffer_bit_start_offsets.push(start_bit_offset as u8); + + let start = self.buffer_offset + start_byte_offset; + let end = self.buffer_offset + end_byte_offset; + min = min.min(start); + max = max.max(end); + + start..end + }) + .collect::>(); + + trace!( + "Scheduling I/O for {} ranges spread across byte range {}..{}", + byte_ranges.len(), + min, + max + ); + + let bytes = scheduler.submit_request(byte_ranges, top_level_row); + + let bits_per_value = self.bits_per_value; + let uncompressed_bits_per_value = self.uncompressed_bits_per_value; + let signed = self.signed; + async move { + let bytes = bytes.await?; + Ok(Box::new(BitpackedPageDecoder { + buffer_bit_start_offsets, + buffer_bit_end_offsets, + bits_per_value, + uncompressed_bits_per_value, + signed, + data: bytes, + }) as Box) + } + .boxed() + } +} + +#[derive(Debug)] +struct BitpackedPageDecoder { + // bit offsets of the first value within each buffer + buffer_bit_start_offsets: Vec, + + // bit offsets of the last value within each buffer. e.g. if there was a buffer + // with 2 values, packed into 5 bits, this would be [Some(3)], indicating that + // the bits from the 3rd->8th bit in the last byte shouldn't be decoded. + buffer_bit_end_offsets: Vec>, + + // the number of bits used to represent a compressed value. E.g. if the max value + // in the page was 7 (0b111), then this will be 3 + bits_per_value: u64, + + // number of bits in the uncompressed value. E.g. this will be 32 for u32 + uncompressed_bits_per_value: u64, + + // whether or not to use the msb as a sign bit during decoding + signed: bool, + + data: Vec, +} + +impl PrimitivePageDecoder for BitpackedPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let num_bytes = self.uncompressed_bits_per_value / 8 * num_rows; + let mut dest = vec![0; num_bytes as usize]; + + // current maximum supported bits per value = 64 + debug_assert!(self.bits_per_value <= 64); + + let mut rows_to_skip = rows_to_skip; + let mut rows_taken = 0; + let byte_len = self.uncompressed_bits_per_value / 8; + let mut dst_idx = 0; // index for current byte being written to destination buffer + + // create bit mask for source bits + let mask = u64::MAX >> (64 - self.bits_per_value); + + for i in 0..self.data.len() { + let src = &self.data[i]; + let (mut src_idx, mut src_offset) = match compute_start_offset( + rows_to_skip, + src.len(), + self.bits_per_value, + self.buffer_bit_start_offsets[i], + self.buffer_bit_end_offsets[i], + ) { + StartOffset::SkipFull(rows_to_skip_here) => { + rows_to_skip -= rows_to_skip_here; + continue; + } + StartOffset::SkipSome(buffer_start_offset) => ( + buffer_start_offset.index, + buffer_start_offset.bit_offset as u64, + ), + }; + + while src_idx < src.len() && rows_taken < num_rows { + rows_taken += 1; + let mut curr_mask = mask; // copy mask + + // current source byte being written to destination + let mut curr_src = src[src_idx] & (curr_mask << src_offset) as u8; + + // how many bits from the current source value have been written to destination + let mut src_bits_written = 0; + + // the offset within the current destination byte to write to + let mut dst_offset = 0; + + let is_negative = is_encoded_item_negative( + src, + src_idx, + src_offset, + self.bits_per_value as usize, + ); + + while src_bits_written < self.bits_per_value { + // write bits from current source byte into destination + dest[dst_idx] += (curr_src >> src_offset) << dst_offset; + let bits_written = (self.bits_per_value - src_bits_written) + .min(8 - src_offset) + .min(8 - dst_offset); + src_bits_written += bits_written; + dst_offset += bits_written; + src_offset += bits_written; + curr_mask >>= bits_written; + + if dst_offset == 8 { + dst_idx += 1; + dst_offset = 0; + } + + if src_offset == 8 { + src_idx += 1; + src_offset = 0; + if src_idx == src.len() { + break; + } + curr_src = src[src_idx] & curr_mask as u8; + } + } + + // if the type is signed, need to pad out the rest of the byte with 1s + let mut negative_padded_current_byte = false; + if self.signed && is_negative && dst_offset > 0 { + negative_padded_current_byte = true; + while dst_offset < 8 { + dest[dst_idx] |= 1 << dst_offset; + dst_offset += 1; + } + } + + // advance destination offset to the next location + // note that we don't need to do this if we wrote the full number of bits + // because source index would have been advanced by the inner loop above + if self.uncompressed_bits_per_value != self.bits_per_value { + let partial_bytes_written = ceil(self.bits_per_value as usize, 8); + + // we also want to move one location to the next location in destination, + // unless we wrote something byte-aligned in which case the logic above + // would have already advanced dst_idx + let mut to_next_byte = 1; + if self.bits_per_value.is_multiple_of(8) { + to_next_byte = 0; + } + let next_dst_idx = + dst_idx + byte_len as usize - partial_bytes_written + to_next_byte; + + // pad remaining bytes with 1 for negative signed numbers + if self.signed && is_negative { + if !negative_padded_current_byte { + dest[dst_idx] = 0xFF; + } + for i in dest.iter_mut().take(next_dst_idx).skip(dst_idx + 1) { + *i = 0xFF; + } + } + + dst_idx = next_dst_idx; + } + + // If we've reached the last byte, there may be some extra bits from the + // next value outside the range. We don't want to be taking those. + if let Some(buffer_bit_end_offset) = self.buffer_bit_end_offsets[i] + && src_idx == src.len() - 1 + && src_offset >= buffer_bit_end_offset as u64 + { + break; + } + } + } + + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(dest), + bits_per_value: self.uncompressed_bits_per_value, + num_values: num_rows, + block_info: BlockInfo::new(), + })) + } +} + +fn is_encoded_item_negative(src: &Bytes, src_idx: usize, src_offset: u64, num_bits: usize) -> bool { + let mut last_byte_idx = src_idx + ((src_offset as usize + num_bits) / 8); + let shift_amount = (src_offset as usize + num_bits) % 8; + let shift_amount = if shift_amount == 0 { + last_byte_idx -= 1; + 7 + } else { + shift_amount - 1 + }; + let last_byte = src[last_byte_idx]; + let sign_bit_mask = 1 << shift_amount; + let sign_bit = last_byte & sign_bit_mask; + + sign_bit > 0 +} + +#[derive(Debug, PartialEq)] +struct BufferStartOffset { + index: usize, + bit_offset: u8, +} + +#[derive(Debug, PartialEq)] +enum StartOffset { + // skip the full buffer. The value is how many rows are skipped + // by skipping the full buffer (e.g., # rows in buffer) + SkipFull(u64), + + // skip to some start offset in the buffer + SkipSome(BufferStartOffset), +} + +/// compute how far ahead in this buffer should we skip ahead and start reading +/// +/// * `rows_to_skip` - how many rows to skip +/// * `buffer_len` - length buf buffer (in bytes) +/// * `bits_per_value` - number of bits used to represent a single bitpacked value +/// * `buffer_start_bit_offset` - offset of the start of the first value within the +/// buffer's first byte +/// * `buffer_end_bit_offset` - end bit of the last value within the buffer. Can be +/// `None` if the end of the last value is byte aligned with end of buffer. +fn compute_start_offset( + rows_to_skip: u64, + buffer_len: usize, + bits_per_value: u64, + buffer_start_bit_offset: u8, + buffer_end_bit_offset: Option, +) -> StartOffset { + let rows_in_buffer = rows_in_buffer( + buffer_len, + bits_per_value, + buffer_start_bit_offset, + buffer_end_bit_offset, + ); + if rows_to_skip >= rows_in_buffer { + return StartOffset::SkipFull(rows_in_buffer); + } + + let start_bit = rows_to_skip * bits_per_value + buffer_start_bit_offset as u64; + let start_byte = start_bit / 8; + + StartOffset::SkipSome(BufferStartOffset { + index: start_byte as usize, + bit_offset: (start_bit % 8) as u8, + }) +} + +/// calculates the number of rows in a buffer +fn rows_in_buffer( + buffer_len: usize, + bits_per_value: u64, + buffer_start_bit_offset: u8, + buffer_end_bit_offset: Option, +) -> u64 { + let mut bits_in_buffer = (buffer_len * 8) as u64 - buffer_start_bit_offset as u64; + + // if the end of the last value of the buffer isn't byte aligned, subtract the + // end offset from the total number of bits in buffer + if let Some(buffer_end_bit_offset) = buffer_end_bit_offset { + bits_in_buffer -= (8 - buffer_end_bit_offset) as u64; + } + + bits_in_buffer / bits_per_value +} + +#[cfg(test)] +pub mod test { + use crate::testing::{ArrayGeneratorProvider, TestCases, check_round_trip_encoding_generated}; + + use super::*; + use std::marker::PhantomData; + + use arrow_array::{ + ArrowPrimitiveType, PrimitiveArray, + types::{Int16Type, Int32Type, Int64Type, UInt8Type, UInt32Type, UInt64Type}, + }; + + use arrow_schema::{DataType, Field}; + use lance_datagen::{ArrayGenerator, array::rand_with_distribution}; + use rand::distr::Uniform; + + #[test] + fn test_rows_in_buffer() { + let test_cases = vec![ + (5usize, 5u64, 0u8, None, 8u64), + (2, 3, 0, Some(5), 4), + (2, 3, 7, Some(6), 2), + ]; + + for ( + buffer_len, + bits_per_value, + buffer_start_bit_offset, + buffer_end_bit_offset, + expected, + ) in test_cases + { + let result = rows_in_buffer( + buffer_len, + bits_per_value, + buffer_start_bit_offset, + buffer_end_bit_offset, + ); + assert_eq!(expected, result); + } + } + + #[test] + fn test_compute_start_offset() { + let result = compute_start_offset(0, 5, 5, 0, None); + assert_eq!( + StartOffset::SkipSome(BufferStartOffset { + index: 0, + bit_offset: 0 + }), + result + ); + + let result = compute_start_offset(10, 5, 5, 0, None); + assert_eq!(StartOffset::SkipFull(8), result); + } + + struct DistributionArrayGeneratorProvider< + DataType, + Dist: rand::distr::Distribution + Clone + Send + Sync + 'static, + > + where + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + { + phantom: PhantomData, + distribution: Dist, + } + + impl DistributionArrayGeneratorProvider + where + Dist: rand::distr::Distribution + Clone + Send + Sync + 'static, + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + { + fn new(dist: Dist) -> Self { + Self { + distribution: dist, + phantom: Default::default(), + } + } + } + + impl ArrayGeneratorProvider for DistributionArrayGeneratorProvider + where + Dist: rand::distr::Distribution + Clone + Send + Sync + 'static, + DataType::Native: Copy + 'static, + PrimitiveArray: From> + 'static, + DataType: ArrowPrimitiveType, + { + fn provide(&self) -> Box { + rand_with_distribution::(self.distribution.clone()) + } + + fn copy(&self) -> Box { + Box::new(Self { + phantom: self.phantom, + distribution: self.distribution.clone(), + }) + } + } + + #[test_log::test(tokio::test)] + async fn test_bitpack_primitive() { + let bitpacked_test_cases: &Vec<(DataType, Box)> = &vec![ + // check less than one byte for multi-byte type + ( + DataType::UInt32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(0, 19).unwrap(), + ), + ), + ), + // // check that more than one byte for multi-byte type + ( + DataType::UInt32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(5 << 7, 6 << 7).unwrap(), + ), + ), + ), + ( + DataType::UInt64, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(5 << 42, 6 << 42).unwrap(), + ), + ), + ), + // check less than one byte for single-byte type + ( + DataType::UInt8, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(0, 19).unwrap(), + ), + ), + ), + // check less than one byte for single-byte type + ( + DataType::UInt64, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(129, 259).unwrap(), + ), + ), + ), + // check byte aligned for single byte + ( + DataType::UInt32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + // this range should always give 8 bits + Uniform::new(200, 250).unwrap(), + ), + ), + ), + // check where the num_bits divides evenly into the bit length of the type + ( + DataType::UInt64, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(1, 3).unwrap(), // 2 bits + ), + ), + ), + // check byte aligned for multiple bytes + ( + DataType::UInt32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + // this range should always always give 16 bits + Uniform::new(200 << 8, 250 << 8).unwrap(), + ), + ), + ), + // check byte aligned where the num bits doesn't divide evenly into the byte length + ( + DataType::UInt64, + Box::new( + DistributionArrayGeneratorProvider::>::new( + // this range should always give 24 hits + Uniform::new(200 << 16, 250 << 16).unwrap(), + ), + ), + ), + // check that we can still encode an all-0 array + ( + DataType::UInt32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(0, 1).unwrap(), + ), + ), + ), + // check for signed types + ( + DataType::Int16, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(-5, 5).unwrap(), + ), + ), + ), + ( + DataType::Int64, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(-(5 << 42), 6 << 42).unwrap(), + ), + ), + ), + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(-(5 << 7), 6 << 7).unwrap(), + ), + ), + ), + // check signed where packed to < 1 byte for multi-byte type + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(-19, 19).unwrap(), + ), + ), + ), + // check signed byte aligned to single byte + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + // this range should always give 8 bits + Uniform::new(-120, 120).unwrap(), + ), + ), + ), + // check signed byte aligned to multiple bytes + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + // this range should always give 16 bits + Uniform::new(-120 << 8, 120 << 8).unwrap(), + ), + ), + ), + // check that it works for all positive integers even if type is signed + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(10, 20).unwrap(), + ), + ), + ), + // check that all 0 works for signed type + ( + DataType::Int32, + Box::new( + DistributionArrayGeneratorProvider::>::new( + Uniform::new(0, 1).unwrap(), + ), + ), + ), + ]; + + for (data_type, array_gen_provider) in bitpacked_test_cases { + let field = Field::new("", data_type.clone(), false); + let test_cases = TestCases::basic().with_structural_encodings(); + check_round_trip_encoding_generated(field, array_gen_provider.copy(), test_cases).await; + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/block.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/block.rs new file mode 100644 index 000000000..3bbd966e2 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/block.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_schema::DataType; + +use crate::{ + data::{BlockInfo, DataBlock, OpaqueBlock}, + encoder::{ArrayEncoder, EncodedArray}, + encodings::physical::block::{CompressedBufferEncoder, CompressionConfig, CompressionScheme}, + format::ProtobufUtils, +}; + +use lance_core::Result; + +impl ArrayEncoder for CompressedBufferEncoder { + fn encode( + &self, + data: DataBlock, + _data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let uncompressed_data = data.as_fixed_width().unwrap(); + + let mut compressed_buf = Vec::with_capacity(uncompressed_data.data.len()); + self.compressor + .compress(&uncompressed_data.data, &mut compressed_buf)?; + + let compressed_data = DataBlock::Opaque(OpaqueBlock { + buffers: vec![compressed_buf.into()], + num_values: uncompressed_data.num_values, + block_info: BlockInfo::new(), + }); + + let comp_buf_index = *buffer_index; + *buffer_index += 1; + + let encoding = ProtobufUtils::flat_encoding( + uncompressed_data.bits_per_value, + comp_buf_index, + Some(CompressionConfig::new(CompressionScheme::Zstd, None)), + ); + + Ok(EncodedArray { + data: compressed_data, + encoding, + }) + } +} + +#[cfg(test)] +mod tests { + use crate::{buffer::LanceBuffer, data::FixedWidthDataBlock}; + + use super::*; + + #[test] + fn test_compressed_buffer_encoder() { + let encoder = CompressedBufferEncoder::default(); + let data = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::reinterpret_vec(vec![0, 1, 2, 3, 4, 5, 6, 7]), + num_values: 8, + block_info: BlockInfo::new(), + }); + + let mut buffer_index = 0; + let encoded_array_result = encoder.encode(data, &DataType::Int64, &mut buffer_index); + assert!(encoded_array_result.is_ok(), "{:?}", encoded_array_result); + let encoded_array = encoded_array_result.unwrap(); + assert_eq!(encoded_array.data.num_values(), 8); + let buffers = encoded_array.data.into_buffers(); + assert_eq!(buffers.len(), 1); + assert!(buffers[0].len() < 64 * 8); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/dictionary.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/dictionary.rs new file mode 100644 index 000000000..08c5e5d80 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/dictionary.rs @@ -0,0 +1,588 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; +use std::vec; + +use arrow_array::builder::{ArrayBuilder, StringBuilder}; +use arrow_array::cast::AsArray; +use arrow_array::types::UInt8Type; +use arrow_array::{ + Array, ArrayRef, DictionaryArray, StringArray, UInt8Array, make_array, new_null_array, +}; +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; +use lance_arrow::DataTypeExt; +use lance_core::{Error, Result}; +use std::collections::HashMap; + +use crate::array_encoding::logical::primitive::PrimitiveFieldDecoder; +use crate::buffer::LanceBuffer; +use crate::data::{ + BlockInfo, DataBlock, DictionaryDataBlock, FixedWidthDataBlock, NullableDataBlock, + VariableWidthBlock, +}; +use crate::decoder::LogicalPageDecoder; +use crate::format::ProtobufUtils; +use crate::{ + EncodingsIo, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, +}; + +#[derive(Debug)] +pub struct DictionaryPageScheduler { + indices_scheduler: Arc, + items_scheduler: Arc, + // The number of items in the dictionary + num_dictionary_items: u32, + // If true, decode the dictionary items. If false, leave them dictionary encoded (e.g. the + // output type is probably a dictionary type) + should_decode_dict: bool, +} + +impl DictionaryPageScheduler { + pub fn new( + indices_scheduler: Arc, + items_scheduler: Arc, + num_dictionary_items: u32, + should_decode_dict: bool, + ) -> Self { + Self { + indices_scheduler, + items_scheduler, + num_dictionary_items, + should_decode_dict, + } + } +} + +impl PageScheduler for DictionaryPageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + // We want to decode indices and items + // e.g. indices [0, 1, 2, 0, 1, 0] + // items (dictionary) ["abcd", "hello", "apple"] + // This will map to ["abcd", "hello", "apple", "abcd", "hello", "abcd"] + // We decode all the items during scheduling itself + // These are used to rebuild the string later + + // Schedule indices for decoding + let indices_page_decoder = + self.indices_scheduler + .schedule_ranges(ranges, scheduler, top_level_row); + + // Schedule items for decoding + let items_range = 0..(self.num_dictionary_items as u64); + let items_page_decoder = self.items_scheduler.schedule_ranges( + std::slice::from_ref(&items_range), + scheduler, + top_level_row, + ); + + let copy_size = self.num_dictionary_items as u64; + + if self.should_decode_dict { + tokio::spawn(async move { + let items_decoder: Arc = + Arc::from(items_page_decoder.await?); + + let mut primitive_wrapper = PrimitiveFieldDecoder::new_from_data( + items_decoder.clone(), + DataType::Utf8, + copy_size, + false, + ); + + // Decode all items + let drained_task = primitive_wrapper.drain(copy_size)?; + let items_decode_task = drained_task.task; + let (decoded_dict, _) = items_decode_task.decode()?; + + let indices_decoder: Box = indices_page_decoder.await?; + + Ok(Box::new(DictionaryPageDecoder { + decoded_dict, + indices_decoder, + }) as Box) + }) + .map(|join_handle| join_handle.unwrap()) + .boxed() + } else { + let num_dictionary_items = self.num_dictionary_items; + tokio::spawn(async move { + let items_decoder: Arc = + Arc::from(items_page_decoder.await?); + + let decoded_dict = items_decoder + .decode(0, num_dictionary_items as u64)? + .clone(); + + let indices_decoder = indices_page_decoder.await?; + + Ok(Box::new(DirectDictionaryPageDecoder { + decoded_dict, + indices_decoder, + }) as Box) + }) + .map(|join_handle| join_handle.unwrap()) + .boxed() + } + } +} + +struct DirectDictionaryPageDecoder { + decoded_dict: DataBlock, + indices_decoder: Box, +} + +impl PrimitivePageDecoder for DirectDictionaryPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let indices = self + .indices_decoder + .decode(rows_to_skip, num_rows)? + .as_fixed_width() + .unwrap(); + let dict = self.decoded_dict.clone(); + Ok(DataBlock::Dictionary(DictionaryDataBlock { + indices, + dictionary: Box::new(dict), + })) + } +} + +struct DictionaryPageDecoder { + decoded_dict: Arc, + indices_decoder: Box, +} + +impl PrimitivePageDecoder for DictionaryPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + // Decode the indices + let indices_data = self.indices_decoder.decode(rows_to_skip, num_rows)?; + + let indices_array = make_array(indices_data.into_arrow(DataType::UInt8, false)?); + let indices_array = indices_array.as_primitive::(); + + let dictionary = self.decoded_dict.clone(); + + let adjusted_indices: UInt8Array = indices_array + .iter() + .map(|x| match x { + Some(0) => None, + Some(x) => Some(x - 1), + None => None, + }) + .collect(); + + // Build dictionary array using indices and items + let dict_array = + DictionaryArray::::try_new(adjusted_indices, dictionary).unwrap(); + let string_array = arrow_cast::cast(&dict_array, &DataType::Utf8).unwrap(); + let string_array = string_array.as_any().downcast_ref::().unwrap(); + + let null_buffer = string_array.nulls().map(|n| n.buffer().clone()); + let offsets_buffer = string_array.offsets().inner().inner().clone(); + let bytes_buffer = string_array.values().clone(); + + let string_data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: 32, + data: LanceBuffer::from(bytes_buffer), + offsets: LanceBuffer::from(offsets_buffer), + num_values: num_rows, + block_info: BlockInfo::new(), + }); + if let Some(nulls) = null_buffer { + Ok(DataBlock::Nullable(NullableDataBlock { + data: Box::new(string_data), + nulls: LanceBuffer::from(nulls), + block_info: BlockInfo::new(), + })) + } else { + Ok(string_data) + } + } +} + +/// An encoder for data that is already dictionary encoded. Stores the +/// data as a dictionary encoding. +#[derive(Debug)] +pub struct AlreadyDictionaryEncoder { + indices_encoder: Box, + items_encoder: Box, +} + +impl AlreadyDictionaryEncoder { + pub fn new( + indices_encoder: Box, + items_encoder: Box, + ) -> Self { + Self { + indices_encoder, + items_encoder, + } + } +} + +impl ArrayEncoder for AlreadyDictionaryEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let DataType::Dictionary(key_type, value_type) = data_type else { + panic!("Expected dictionary type"); + }; + + let dict_data = match data { + DataBlock::Dictionary(dict_data) => dict_data, + DataBlock::AllNull(all_null) => { + // In 2.1 this won't happen, kind of annoying to materialize a bunch of nulls + let indices = UInt8Array::from(vec![0; all_null.num_values as usize]); + let indices = arrow_cast::cast(&indices, key_type.as_ref()).unwrap(); + let indices = indices.into_data(); + let values = new_null_array(value_type, 1); + DictionaryDataBlock { + indices: FixedWidthDataBlock { + bits_per_value: key_type.byte_width() as u64 * 8, + data: LanceBuffer::from(indices.buffers()[0].clone()), + num_values: all_null.num_values, + block_info: BlockInfo::new(), + }, + dictionary: Box::new(DataBlock::from_array(values)), + } + } + _ => panic!("Expected dictionary data"), + }; + let num_dictionary_items = dict_data.dictionary.num_values() as u32; + + let encoded_indices = self.indices_encoder.encode( + DataBlock::FixedWidth(dict_data.indices), + key_type, + buffer_index, + )?; + let encoded_items = + self.items_encoder + .encode(*dict_data.dictionary, value_type, buffer_index)?; + + let encoded = DataBlock::Dictionary(DictionaryDataBlock { + dictionary: Box::new(encoded_items.data), + indices: encoded_indices.data.as_fixed_width().unwrap(), + }); + + let encoding = ProtobufUtils::dict_encoding( + encoded_indices.encoding, + encoded_items.encoding, + num_dictionary_items, + ); + + Ok(EncodedArray { + data: encoded, + encoding, + }) + } +} + +#[derive(Debug)] +pub struct DictionaryEncoder { + indices_encoder: Box, + items_encoder: Box, +} + +impl DictionaryEncoder { + pub fn new( + indices_encoder: Box, + items_encoder: Box, + ) -> Self { + Self { + indices_encoder, + items_encoder, + } + } +} + +fn encode_dict_indices_and_items(string_array: &StringArray) -> (ArrayRef, ArrayRef) { + let mut arr_hashmap: HashMap<&str, u8> = HashMap::new(); + // We start with a dict index of 1 because the value 0 is reserved for nulls + // The dict indices are adjusted by subtracting 1 later during decode + let mut curr_dict_index = 1; + let total_capacity = string_array.len(); + + let mut dict_indices = Vec::with_capacity(total_capacity); + let mut dict_builder = StringBuilder::new(); + + for i in 0..string_array.len() { + if !string_array.is_valid(i) { + // null value + dict_indices.push(0); + continue; + } + + let st = string_array.value(i); + + let hashmap_entry = *arr_hashmap.entry(st).or_insert(curr_dict_index); + dict_indices.push(hashmap_entry); + + // if item didn't exist in the hashmap, add it to the dictionary + // and increment the dictionary index + if hashmap_entry == curr_dict_index { + dict_builder.append_value(st); + curr_dict_index += 1; + } + } + + let array_dict_indices = Arc::new(UInt8Array::from(dict_indices)) as ArrayRef; + + // If there is an empty dictionary: + // Either there is an array of nulls or an empty array altogether + // In this case create the dictionary with a single null element + // Because decoding [] is not currently supported by the binary decoder + if dict_builder.is_empty() { + dict_builder.append_option(Option::<&str>::None); + } + + let dict_elements = dict_builder.finish(); + let array_dict_elements = arrow_cast::cast(&dict_elements, &DataType::Utf8).unwrap(); + + (array_dict_indices, array_dict_elements) +} + +impl ArrayEncoder for DictionaryEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + if !matches!(data_type, DataType::Utf8) { + return Err(Error::invalid_input_source( + format!( + "DictionaryEncoder only supports string arrays but got {}", + data_type + ) + .into(), + )); + } + // We only support string arrays for now + let str_data = make_array(data.into_arrow(DataType::Utf8, false)?); + + let (index_array, items_array) = encode_dict_indices_and_items(str_data.as_string()); + let dict_size = items_array.len() as u32; + let index_data = DataBlock::from(index_array); + let items_data = DataBlock::from(items_array); + + let encoded_indices = + self.indices_encoder + .encode(index_data, &DataType::UInt8, buffer_index)?; + + let encoded_items = self + .items_encoder + .encode(items_data, &DataType::Utf8, buffer_index)?; + + let encoded_data = DataBlock::Dictionary(DictionaryDataBlock { + indices: encoded_indices.data.as_fixed_width().unwrap(), + dictionary: Box::new(encoded_items.data), + }); + + let encoding = ProtobufUtils::dict_encoding( + encoded_indices.encoding, + encoded_items.encoding, + dict_size, + ); + + Ok(EncodedArray { + data: encoded_data, + encoding, + }) + } +} + +#[cfg(test)] +mod tests { + + use arrow_array::{ + ArrayRef, DictionaryArray, StringArray, UInt8Array, + builder::{LargeStringBuilder, StringBuilder}, + }; + use arrow_schema::{DataType, Field}; + use std::{collections::HashMap, sync::Arc, vec}; + + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + use super::encode_dict_indices_and_items; + + // These tests cover the case where we opportunistically convert some (or all) pages of + // a string column into dictionaries (and decode on read) + + #[test] + fn test_encode_dict_nulls() { + // Null entries in string arrays should be adjusted + let string_array = Arc::new(StringArray::from(vec![ + None, + Some("foo"), + Some("bar"), + Some("bar"), + None, + Some("foo"), + None, + None, + ])); + let (dict_indices, dict_items) = encode_dict_indices_and_items(&string_array); + + let expected_indices = Arc::new(UInt8Array::from(vec![0, 1, 2, 2, 0, 1, 0, 0])) as ArrayRef; + let expected_items = Arc::new(StringArray::from(vec!["foo", "bar"])) as ArrayRef; + assert_eq!(&dict_indices, &expected_indices); + assert_eq!(&dict_items, &expected_items); + } + + #[test_log::test(tokio::test)] + async fn test_utf8() { + let field = Field::new("", DataType::Utf8, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_binary() { + let field = Field::new("", DataType::Binary, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_large_binary() { + let field = Field::new("", DataType::LargeBinary, true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_large_utf8() { + let field = Field::new("", DataType::LargeUtf8, true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_utf8() { + let string_array = StringArray::from(vec![Some("abc"), Some("de"), None, Some("fgh")]); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![1, 3]); + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_sliced_utf8() { + let string_array = StringArray::from(vec![Some("abc"), Some("de"), None, Some("fgh")]); + let string_array = string_array.slice(1, 3); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_range(0..2) + .with_range(1..2); + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_empty_strings() { + // Scenario 1: Some strings are empty + + let values = [Some("abc"), Some(""), None]; + // Test empty list at beginning, middle, and end + for order in [[0, 1, 2], [1, 0, 2], [2, 0, 1]] { + let mut string_builder = StringBuilder::new(); + for idx in order { + string_builder.append_option(values[idx]); + } + let string_array = Arc::new(string_builder.finish()); + let test_cases = TestCases::default() + .with_indices(vec![1]) + .with_indices(vec![0]) + .with_indices(vec![2]); + check_round_trip_encoding_of_data( + vec![string_array.clone()], + &test_cases, + HashMap::new(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()) + .await; + } + + // Scenario 2: All strings are empty + + // When encoding an array of empty strings there are no bytes to encode + // which is strange and we want to ensure we handle it + let string_array = Arc::new(StringArray::from(vec![Some(""), None, Some("")])); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data(vec![string_array.clone()], &test_cases, HashMap::new()) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + #[ignore] // This test is quite slow in debug mode + async fn test_jumbo_string() { + // This is an overflow test. We have a list of lists where each list + // has 1Mi items. We encode 5000 of these lists and so we have over 4Gi in the + // offsets range + let mut string_builder = LargeStringBuilder::new(); + // a 1 MiB string + let giant_string = String::from_iter((0..(1024 * 1024)).map(|_| '0')); + for _ in 0..5000 { + string_builder.append_option(Some(&giant_string)); + } + let giant_array = Arc::new(string_builder.finish()) as ArrayRef; + let arrs = vec![giant_array]; + + // // We can't validate because our validation relies on concatenating all input arrays + let test_cases = TestCases::default().without_validation(); + check_round_trip_encoding_of_data(arrs, &test_cases, HashMap::new()).await; + } + + // These tests cover the case where the input is already dictionary encoded + + #[test_log::test(tokio::test)] + async fn test_random_dictionary_input() { + let dict_field = Field::new( + "", + DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8)), + false, + ); + check_basic_random(dict_field).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_already_dictionary() { + let values = StringArray::from_iter_values(["a", "bb", "ccc"]); + let indices = UInt8Array::from(vec![0, 1, 2, 0, 1, 2, 0, 1, 2]); + let dict_array = DictionaryArray::new(indices, Arc::new(values)); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(1..3) + .with_range(2..4) + .with_indices(vec![1]) + .with_indices(vec![2]); + check_round_trip_encoding_of_data(vec![Arc::new(dict_array)], &test_cases, HashMap::new()) + .await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_binary.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_binary.rs new file mode 100644 index 000000000..cd5cfd706 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_binary.rs @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_buffer::ScalarBuffer; +use futures::{FutureExt, future::BoxFuture}; +use lance_core::Result; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, VariableWidthBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, +}; + +/// A scheduler for fixed size binary data +#[derive(Debug)] +pub struct FixedSizeBinaryPageScheduler { + bytes_scheduler: Box, + byte_width: u32, + bytes_per_offset: u32, +} + +impl FixedSizeBinaryPageScheduler { + pub fn new( + bytes_scheduler: Box, + byte_width: u32, + bytes_per_offset: u32, + ) -> Self { + Self { + bytes_scheduler, + byte_width, + bytes_per_offset, + } + } +} + +impl PageScheduler for FixedSizeBinaryPageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let expanded_ranges = ranges + .iter() + .map(|range| { + (range.start * self.byte_width as u64)..(range.end * self.byte_width as u64) + }) + .collect::>(); + + let bytes_page_decoder = + self.bytes_scheduler + .schedule_ranges(&expanded_ranges, scheduler, top_level_row); + + let byte_width = self.byte_width as u64; + let bytes_per_offset = self.bytes_per_offset; + + async move { + let bytes_decoder = bytes_page_decoder.await?; + Ok(Box::new(FixedSizeBinaryDecoder { + bytes_decoder, + byte_width, + bytes_per_offset, + }) as Box) + } + .boxed() + } +} + +pub struct FixedSizeBinaryDecoder { + bytes_decoder: Box, + byte_width: u64, + bytes_per_offset: u32, +} + +impl PrimitivePageDecoder for FixedSizeBinaryDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let rows_to_skip = rows_to_skip * self.byte_width; + let num_bytes = num_rows * self.byte_width; + let bytes = self.bytes_decoder.decode(rows_to_skip, num_bytes)?; + let bytes = bytes.as_fixed_width().unwrap(); + debug_assert_eq!(bytes.bits_per_value, self.byte_width * 8); + + let offsets_buffer = match self.bytes_per_offset { + 8 => { + let offsets_vec = (0..(num_rows + 1)) + .map(|i| i * self.byte_width) + .collect::>(); + + ScalarBuffer::from(offsets_vec).into_inner() + } + 4 => { + let offsets_vec = (0..(num_rows as u32 + 1)) + .map(|i| i * self.byte_width as u32) + .collect::>(); + + ScalarBuffer::from(offsets_vec).into_inner() + } + _ => panic!("Unsupported offsets type"), + }; + + let string_data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: (self.bytes_per_offset * 8) as u8, + data: bytes.data, + num_values: num_rows, + offsets: LanceBuffer::from(offsets_buffer), + block_info: BlockInfo::new(), + }); + + Ok(string_data) + } +} + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{ + Array, ArrayRef, FixedSizeBinaryArray, LargeStringArray, StringArray, + builder::LargeStringBuilder, + }; + use arrow_buffer::Buffer; + use arrow_data::ArrayData; + use arrow_schema::{DataType, Field}; + + use crate::array_encoding::physical::fixed_size_binary::FixedSizeBinaryDecoder; + use crate::data::{DataBlock, FixedWidthDataBlock}; + use crate::decoder::PrimitivePageDecoder; + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + #[test_log::test(tokio::test)] + async fn test_fixed_size_utf8_binary() { + let field = Field::new("", DataType::Utf8, false); + // This test only generates fixed size binary arrays anyway + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_fixed_size_binary() { + let field = Field::new("", DataType::Binary, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_fixed_size_large_binary() { + let field = Field::new("", DataType::LargeBinary, true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_fixed_size_large_utf8() { + let field = Field::new("", DataType::LargeUtf8, true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_fixed_size_utf8() { + let string_array = StringArray::from(vec![ + Some("abc"), + Some("def"), + Some("ghi"), + Some("jkl"), + Some("mno"), + ]); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![0, 1, 3, 4]); + + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_fixed_size_with_nulls_utf8() { + let string_array = + LargeStringArray::from(vec![Some("abc"), None, Some("ghi"), None, Some("mno")]); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![0, 1, 3, 4]); + + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_fixed_size_sliced_utf8() { + let string_array = StringArray::from(vec![Some("abc"), Some("def"), None, Some("fgh")]); + let string_array = string_array.slice(1, 3); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_range(0..2) + .with_range(1..2); + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_fixed_size_empty_strings() { + // All strings are empty + + // When encoding an array of empty strings there are no bytes to encode + // which is strange and we want to ensure we handle it + let string_array = Arc::new(StringArray::from(vec![Some(""), None, Some("")])); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data(vec![string_array.clone()], &test_cases, HashMap::new()) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + #[ignore] // This test is quite slow in debug mode + async fn test_jumbo_string() { + // This is an overflow test. We have a list of lists where each list + // has 1Mi items. We encode 5000 of these lists and so we have over 4Gi in the + // offsets range + let mut string_builder = LargeStringBuilder::new(); + // a 1 MiB string + let giant_string = String::from_iter((0..(1024 * 1024)).map(|_| '0')); + for _ in 0..5000 { + string_builder.append_option(Some(&giant_string)); + } + let giant_array = Arc::new(string_builder.finish()) as ArrayRef; + let arrs = vec![giant_array]; + + // // We can't validate because our validation relies on concatenating all input arrays + let test_cases = TestCases::default().without_validation(); + check_round_trip_encoding_of_data(arrs, &test_cases, HashMap::new()).await; + } + + struct FixedWidthCloningPageDecoder { + data_block: FixedWidthDataBlock, + } + + impl PrimitivePageDecoder for FixedWidthCloningPageDecoder { + // clone the given data block as decoded data block + fn decode( + &self, + _rows_to_skip: u64, + _num_rows: u64, + ) -> lance_core::error::Result { + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: self.data_block.data.deep_copy(), + bits_per_value: self.data_block.bits_per_value, + num_values: self.data_block.num_values, + block_info: self.data_block.block_info.clone(), + })) + } + } + + #[test] + fn test_fixed_size_binary_decoder() { + let values: [u8; 6] = *b"aaabbb"; + let num_values = 2u64; + let byte_width = 3; + let array_data = ArrayData::builder(DataType::FixedSizeBinary(byte_width)) + .len(num_values as usize) + .add_buffer(Buffer::from(&values[..])) + .build() + .unwrap(); + let fixed_size_binary_array = FixedSizeBinaryArray::from(array_data); + let arrays = vec![Arc::new(fixed_size_binary_array) as ArrayRef]; + let fixed_width_data_block = DataBlock::from_arrays(&arrays, num_values); + assert_eq!(fixed_width_data_block.name(), "FixedWidth"); + + let bytes_decoder = FixedWidthCloningPageDecoder { + data_block: fixed_width_data_block.as_fixed_width().unwrap(), + }; + let decoder = FixedSizeBinaryDecoder { + bytes_decoder: Box::new(bytes_decoder), + byte_width: byte_width as u64, + bytes_per_offset: 4, // 32-bits offset binary + }; + + let decoded_binary = decoder.decode(0, num_values).unwrap(); + let maybe_data = decoded_binary.into_arrow(DataType::Utf8, true); + assert!(maybe_data.is_ok()); + let data = maybe_data.unwrap(); + let string_array = StringArray::from(data); + assert_eq!(string_array.len(), num_values as usize); + assert_eq!(string_array.value(0), "aaa"); + assert_eq!(string_array.value(1), "bbb"); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_list.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_list.rs new file mode 100644 index 000000000..a0f596fd8 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fixed_size_list.rs @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; +use lance_core::Result; +use log::trace; + +use crate::{ + EncodingsIo, + data::{DataBlock, FixedSizeListBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, + format::ProtobufUtils, +}; + +/// A scheduler for fixed size lists of primitive values +/// +/// This scheduler is, itself, primitive +#[derive(Debug)] +pub struct FixedListScheduler { + items_scheduler: Box, + dimension: u32, +} + +impl FixedListScheduler { + pub fn new(items_scheduler: Box, dimension: u32) -> Self { + Self { + items_scheduler, + dimension, + } + } +} + +impl PageScheduler for FixedListScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let expanded_ranges = ranges + .iter() + .map(|range| (range.start * self.dimension as u64)..(range.end * self.dimension as u64)) + .collect::>(); + trace!( + "Expanding {} fsl ranges across {}..{} to item ranges across {}..{}", + ranges.len(), + ranges[0].start, + ranges[ranges.len() - 1].end, + expanded_ranges[0].start, + expanded_ranges[expanded_ranges.len() - 1].end + ); + let inner_page_decoder = + self.items_scheduler + .schedule_ranges(&expanded_ranges, scheduler, top_level_row); + let dimension = self.dimension; + async move { + let items_decoder = inner_page_decoder.await?; + Ok(Box::new(FixedListDecoder { + items_decoder, + dimension: dimension as u64, + }) as Box) + } + .boxed() + } +} + +pub struct FixedListDecoder { + items_decoder: Box, + dimension: u64, +} + +impl PrimitivePageDecoder for FixedListDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let rows_to_skip = rows_to_skip * self.dimension; + let num_child_rows = num_rows * self.dimension; + let child_data = self.items_decoder.decode(rows_to_skip, num_child_rows)?; + Ok(DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(child_data), + dimension: self.dimension, + })) + } +} + +#[derive(Debug)] +pub struct FslEncoder { + items_encoder: Box, + dimension: u32, +} + +impl FslEncoder { + pub fn new(items_encoder: Box, dimension: u32) -> Self { + Self { + items_encoder, + dimension, + } + } +} + +impl ArrayEncoder for FslEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let inner_type = match data_type { + DataType::FixedSizeList(inner_field, _) => inner_field.data_type().clone(), + _ => panic!("Expected fixed size list data type and got {}", data_type), + }; + let data = data.as_fixed_size_list().unwrap(); + let child = *data.child; + + let encoded_data = self + .items_encoder + .encode(child, &inner_type, buffer_index)?; + + let data = DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(encoded_data.data), + dimension: self.dimension as u64, + }); + + let encoding = + ProtobufUtils::fsl_encoding(self.dimension as u64, encoded_data.encoding, false); + Ok(EncodedArray { data, encoding }) + } +} + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{FixedSizeListArray, Int32Array, types::Int32Type}; + use arrow_buffer::{BooleanBuffer, NullBuffer}; + use arrow_schema::{DataType, Field}; + use lance_datagen::{ArrayGeneratorExt, RowCount, array, gen_array}; + + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + const PRIMITIVE_TYPES: &[DataType] = &[DataType::Int8, DataType::Float32, DataType::Float64]; + + #[test_log::test(tokio::test)] + async fn test_value_fsl_primitive() { + for data_type in PRIMITIVE_TYPES { + let inner_field = Field::new("item", data_type.clone(), true); + let data_type = DataType::FixedSizeList(Arc::new(inner_field), 16); + let field = Field::new("", data_type, false); + check_basic_random(field).await; + } + } + + #[test_log::test(tokio::test)] + async fn test_simple_fsl() { + // [0, NULL], NULL, [4, 5] + let items = Arc::new(Int32Array::from(vec![ + Some(0), + None, + Some(2), + Some(3), + Some(4), + Some(5), + ])); + let items_field = Arc::new(Field::new("item", DataType::Int32, true)); + let list_nulls = NullBuffer::new(BooleanBuffer::from(vec![true, false, true])); + let list = Arc::new(FixedSizeListArray::new( + items_field, + 2, + items, + Some(list_nulls), + )); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(0..2) + .with_range(1..3) + .with_indices(vec![0, 1, 2]) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![list], &test_cases, HashMap::default()).await; + } + + #[test_log::test(tokio::test)] + #[ignore] + async fn test_simple_wide_fsl() { + let items = gen_array(array::rand::().with_random_nulls(0.1)) + .into_array_rows(RowCount::from(4096)) + .unwrap(); + let items_field = Arc::new(Field::new("item", DataType::Int32, true)); + let list_nulls = NullBuffer::new(BooleanBuffer::from(vec![true, false, true, false])); + let list = Arc::new(FixedSizeListArray::new( + items_field, + 1024, + items, + Some(list_nulls), + )); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(0..2) + .with_range(1..3) + .with_indices(vec![0, 1, 2]) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![list], &test_cases, HashMap::default()).await; + } + + #[test_log::test(tokio::test)] + async fn test_nested_fsl() { + // [[0, 1], NULL], NULL, [[8, 9], [NULL, 11]] + let items = Arc::new(Int32Array::from(vec![ + Some(0), + Some(1), + None, + None, + None, + None, + None, + None, + Some(8), + Some(9), + None, + Some(11), + ])); + let items_field = Arc::new(Field::new("item", DataType::Int32, true)); + let inner_list_nulls = NullBuffer::new(BooleanBuffer::from(vec![ + true, false, false, false, true, true, + ])); + let inner_list = Arc::new(FixedSizeListArray::new( + items_field.clone(), + 2, + items, + Some(inner_list_nulls), + )); + let inner_list_field = Arc::new(Field::new( + "item", + DataType::FixedSizeList(items_field, 2), + true, + )); + let outer_list_nulls = NullBuffer::new(BooleanBuffer::from(vec![true, false, true])); + let outer_list = Arc::new(FixedSizeListArray::new( + inner_list_field, + 2, + inner_list, + Some(outer_list_nulls), + )); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(0..2) + .with_range(1..3) + .with_indices(vec![0, 1, 2]) + .with_indices(vec![2]) + .with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![outer_list], &test_cases, HashMap::default()).await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fsst.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fsst.rs new file mode 100644 index 000000000..fd1f11b86 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/fsst.rs @@ -0,0 +1,198 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ops::Range, sync::Arc}; + +use arrow_buffer::ScalarBuffer; +use arrow_schema::DataType; +use futures::{FutureExt, future::BoxFuture}; + +use lance_core::Result; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, NullableDataBlock, VariableWidthBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, + format::ProtobufUtils, +}; + +#[derive(Debug)] +pub struct FsstPageScheduler { + inner_scheduler: Box, + symbol_table: LanceBuffer, +} + +impl FsstPageScheduler { + pub fn new(inner_scheduler: Box, symbol_table: LanceBuffer) -> Self { + Self { + inner_scheduler, + symbol_table, + } + } +} + +impl PageScheduler for FsstPageScheduler { + fn schedule_ranges( + &self, + ranges: &[Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let inner_decoder = self + .inner_scheduler + .schedule_ranges(ranges, scheduler, top_level_row); + let symbol_table = self.symbol_table.clone(); + + async move { + let inner_decoder = inner_decoder.await?; + Ok(Box::new(FsstPageDecoder { + inner_decoder, + symbol_table, + }) as Box) + } + .boxed() + } +} + +struct FsstPageDecoder { + inner_decoder: Box, + symbol_table: LanceBuffer, +} + +impl PrimitivePageDecoder for FsstPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let compressed_data = self.inner_decoder.decode(rows_to_skip, num_rows)?; + let (string_data, nulls) = match compressed_data { + DataBlock::Nullable(nullable) => { + let data = nullable.data.as_variable_width().unwrap(); + Result::Ok((data, Some(nullable.nulls))) + } + DataBlock::VariableWidth(variable) => Ok((variable, None)), + _ => panic!("Received non-variable width data from inner decoder"), + }?; + + let offsets = ScalarBuffer::::from(string_data.offsets.into_buffer()); + let bytes = string_data.data.into_buffer(); + + let mut decompressed_offsets = vec![0_i32; offsets.len()]; + let mut decompressed_bytes = vec![0_u8; bytes.len() * 8]; + // Safety: Exposes uninitialized memory but we're about to clobber it + unsafe { + decompressed_bytes.set_len(decompressed_bytes.capacity()); + } + fsst::fsst::decompress( + &self.symbol_table, + &bytes, + &offsets, + &mut decompressed_bytes, + &mut decompressed_offsets, + )?; + + // TODO: Change PrimitivePageDecoder to use Vec instead of BytesMut + // since there is no way to get BytesMut from Vec but these copies should be avoidable + // This is not the first time this has happened + let mut offsets_as_bytes_mut = Vec::with_capacity(decompressed_offsets.len()); + let decompressed_offsets = ScalarBuffer::::from(decompressed_offsets); + offsets_as_bytes_mut.extend_from_slice(decompressed_offsets.inner().as_slice()); + + let mut bytes_as_bytes_mut = Vec::with_capacity(decompressed_bytes.len()); + bytes_as_bytes_mut.extend_from_slice(&decompressed_bytes); + + let new_string_data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: 32, + data: LanceBuffer::from(bytes_as_bytes_mut), + num_values: num_rows, + offsets: LanceBuffer::from(offsets_as_bytes_mut), + block_info: BlockInfo::new(), + }); + + if let Some(nulls) = nulls { + Ok(DataBlock::Nullable(NullableDataBlock { + data: Box::new(new_string_data), + nulls, + block_info: BlockInfo::new(), + })) + } else { + Ok(new_string_data) + } + } +} + +#[derive(Debug)] +pub struct FsstArrayEncoder { + inner_encoder: Box, +} + +impl FsstArrayEncoder { + pub fn new(inner_encoder: Box) -> Self { + Self { inner_encoder } + } +} + +impl ArrayEncoder for FsstArrayEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> lance_core::Result { + let (data, nulls) = match data { + DataBlock::Nullable(nullable) => { + let data = nullable.data.as_variable_width().unwrap(); + (data, Some(nullable.nulls)) + } + DataBlock::VariableWidth(variable) => (variable, None), + _ => panic!("Expected variable width data block"), + }; + assert_eq!(data.bits_per_offset, 32); + let num_values = data.num_values; + let offsets = data.offsets.borrow_to_typed_slice::(); + let offsets_slice = offsets.as_ref(); + let bytes_data = data.data.into_buffer(); + + let mut dest_offsets = vec![0_i32; offsets_slice.len() * 2]; + let mut dest_values = vec![0_u8; bytes_data.len() * 2]; + let mut symbol_table = vec![0_u8; fsst::fsst::FSST_SYMBOL_TABLE_SIZE]; + + fsst::fsst::compress( + &mut symbol_table, + bytes_data.as_slice(), + offsets_slice, + &mut dest_values, + &mut dest_offsets, + )?; + + let dest_offset = LanceBuffer::reinterpret_vec(dest_offsets); + let dest_values = LanceBuffer::from(dest_values); + let dest_data = DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: 32, + data: dest_values, + num_values, + offsets: dest_offset, + block_info: BlockInfo::new(), + }); + + let data_block = if let Some(nulls) = nulls { + DataBlock::Nullable(NullableDataBlock { + data: Box::new(dest_data), + nulls, + block_info: BlockInfo::new(), + }) + } else { + dest_data + }; + + let inner_encoded = self + .inner_encoder + .encode(data_block, data_type, buffer_index)?; + + let encoding = ProtobufUtils::fsst(inner_encoded.encoding, symbol_table); + + Ok(EncodedArray { + data: inner_encoded.data, + encoding, + }) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/packed_struct.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/packed_struct.rs new file mode 100644 index 000000000..b608071e9 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/packed_struct.rs @@ -0,0 +1,337 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use arrow_schema::{DataType, Fields}; +use bytes::Bytes; +use bytes::BytesMut; +use futures::{FutureExt, future::BoxFuture}; +use lance_arrow::DataTypeExt; +use lance_core::{Error, Result}; + +use crate::data::BlockInfo; +use crate::data::FixedSizeListBlock; +use crate::format::ProtobufUtils; +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{DataBlock, FixedWidthDataBlock, StructDataBlock}, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, +}; + +#[derive(Debug)] +pub struct PackedStructPageScheduler { + // We don't actually need these schedulers right now since we decode all the field bytes directly + // But they can be useful if we actually need to use the decoders for the inner fields later + // e.g. once bitpacking is added + _inner_schedulers: Vec>, + fields: Fields, + buffer_offset: u64, +} + +impl PackedStructPageScheduler { + pub fn new( + _inner_schedulers: Vec>, + struct_datatype: DataType, + buffer_offset: u64, + ) -> Self { + let DataType::Struct(fields) = struct_datatype else { + panic!("Struct datatype expected"); + }; + Self { + _inner_schedulers, + fields, + buffer_offset, + } + } +} + +impl PageScheduler for PackedStructPageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let mut total_bytes_per_row: u64 = 0; + + for field in &self.fields { + let bytes_per_field = field.data_type().byte_width() as u64; + total_bytes_per_row += bytes_per_field; + } + + // Parts of the arrays in a page may be encoded in different encoding tasks + // In that case decoding two different sets of rows can result in the same ranges parameter being passed in + // e.g. we may get ranges[0..2] and ranges[0..2] to decode 4 rows through 2 tasks + // So to get the correct byte ranges we need to know the position of the buffer in the page (i.e. the buffer offset) + // This is computed directly from the buffer stored in the protobuf + let byte_ranges = ranges + .iter() + .map(|range| { + let start = self.buffer_offset + (range.start * total_bytes_per_row); + let end = self.buffer_offset + (range.end * total_bytes_per_row); + start..end + }) + .collect::>(); + + // Directly creates a future to decode the bytes + let bytes = scheduler.submit_request(byte_ranges, top_level_row); + + let copy_struct_fields = self.fields.clone(); + + tokio::spawn(async move { + let bytes = bytes.await?; + + let mut combined_bytes = BytesMut::default(); + for byte_slice in bytes { + combined_bytes.extend_from_slice(&byte_slice); + } + + Ok(Box::new(PackedStructPageDecoder { + data: combined_bytes.freeze(), + fields: copy_struct_fields, + total_bytes_per_row: total_bytes_per_row as usize, + }) as Box) + }) + .map(|join_handle| join_handle.unwrap()) + .boxed() + } +} + +struct PackedStructPageDecoder { + data: Bytes, + fields: Fields, + total_bytes_per_row: usize, +} + +impl PrimitivePageDecoder for PackedStructPageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + // Decoding workflow: + // rows 0-2: {x: [1, 2, 3], y: [4, 5, 6], z: [7, 8, 9]} + // rows 3-5: {x: [10, 11, 12], y: [13, 14, 15], z: [16, 17, 18]} + // packed encoding: [ + // [1, 4, 7, 2, 5, 8, 3, 6, 9], + // [10, 13, 16, 11, 14, 17, 12, 15, 18] + // ] + // suppose bytes_per_field=1, 4, 8 for fields x, y, and z, respectively. + // Then total_bytes_per_row = 13 + // Suppose rows_to_skip=1 and num_rows=2. Then we will slice bytes 13 to 39. + // Now we have [2, 5, 8, 3, 6, 9] + // We rearrange this to get [BytesMut(2, 3), BytesMut(5, 6), BytesMut(8, 9)] as a Vec + // This is used to reconstruct the struct array later + + let bytes_to_skip = (rows_to_skip as usize) * self.total_bytes_per_row; + + let mut children = Vec::with_capacity(self.fields.len()); + + let mut start_index = 0; + + for field in &self.fields { + let bytes_per_field = field.data_type().byte_width(); + let mut field_bytes = Vec::with_capacity(bytes_per_field * num_rows as usize); + + let mut byte_index = start_index; + + for _ in 0..num_rows { + let start = bytes_to_skip + byte_index; + field_bytes.extend_from_slice(&self.data[start..(start + bytes_per_field)]); + byte_index += self.total_bytes_per_row; + } + + start_index += bytes_per_field; + let child_block = FixedWidthDataBlock { + data: LanceBuffer::from(field_bytes), + bits_per_value: bytes_per_field as u64 * 8, + num_values: num_rows, + block_info: BlockInfo::new(), + }; + let child_block = FixedSizeListBlock::from_flat(child_block, field.data_type()); + children.push(child_block); + } + Ok(DataBlock::Struct(StructDataBlock { + children, + block_info: BlockInfo::default(), + validity: None, + })) + } +} + +#[derive(Debug)] +pub struct PackedStructEncoder { + inner_encoders: Vec>, +} + +impl PackedStructEncoder { + pub fn new(inner_encoders: Vec>) -> Self { + Self { inner_encoders } + } +} + +impl ArrayEncoder for PackedStructEncoder { + fn encode( + &self, + data: DataBlock, + data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let struct_data = data.as_struct().unwrap(); + + let DataType::Struct(child_types) = data_type else { + panic!("Struct datatype expected"); + }; + + // Encode individual fields + let mut encoded_fields = Vec::with_capacity(struct_data.children.len()); + for ((child, encoder), child_type) in struct_data + .children + .into_iter() + .zip(&self.inner_encoders) + .zip(child_types) + { + encoded_fields.push(encoder.encode(child, child_type.data_type(), &mut 0)?); + } + + let (encoded_data_vec, child_encodings): (Vec<_>, Vec<_>) = encoded_fields + .into_iter() + .map(|field| (field.data, field.encoding)) + .unzip(); + + // Zip together encoded data + // + // We can currently encode both FixedWidth and FixedSizeList. In order + // to encode the latter we "flatten" it converting a FixedSizeList into + // a FixedWidth with very wide items. + let fixed_fields = encoded_data_vec + .into_iter() + .map(|child| match child { + DataBlock::FixedWidth(fixed) => Ok(fixed), + DataBlock::FixedSizeList(fixed_size_list) => { + let flattened = fixed_size_list.try_into_flat().ok_or_else(|| { + Error::invalid_input( + "Packed struct encoder cannot pack nullable fixed-width data blocks", + ) + })?; + Ok(flattened) + } + _ => Err(Error::invalid_input( + "Packed struct encoder currently only implemented for fixed-width data blocks", + )), + }) + .collect::>>()?; + let total_bits_per_value = fixed_fields.iter().map(|f| f.bits_per_value).sum::(); + + let num_values = fixed_fields[0].num_values; + debug_assert!( + fixed_fields + .iter() + .all(|field| field.num_values == num_values) + ); + + let zipped_input = fixed_fields + .into_iter() + .map(|field| (field.data, field.bits_per_value)) + .collect::>(); + let zipped = LanceBuffer::zip_into_one(zipped_input, num_values)?; + + // Create encoding protobuf + let index = *buffer_index; + *buffer_index += 1; + + let packed_data = DataBlock::FixedWidth(FixedWidthDataBlock { + data: zipped, + bits_per_value: total_bits_per_value, + num_values, + block_info: BlockInfo::new(), + }); + + let encoding = ProtobufUtils::packed_struct(child_encodings, index); + + Ok(EncodedArray { + data: packed_data, + encoding, + }) + } +} + +#[cfg(test)] +mod tests { + + use arrow_array::{ArrayRef, Int32Array, StructArray, UInt8Array, UInt64Array}; + use arrow_schema::{DataType, Field, Fields}; + use std::{collections::HashMap, sync::Arc, vec}; + + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + #[test_log::test(tokio::test)] + async fn test_random_packed_struct() { + let data_type = DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::UInt64, false), + Field::new("b", DataType::UInt32, false), + ])); + let mut metadata = HashMap::new(); + metadata.insert("packed".to_string(), "true".to_string()); + + let field = Field::new("", data_type, false).with_metadata(metadata); + + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_specific_packed_struct() { + let array1 = Arc::new(UInt64Array::from(vec![1, 2, 3, 4])); + let array2 = Arc::new(Int32Array::from(vec![5, 6, 7, 8])); + let array3 = Arc::new(UInt8Array::from(vec![9, 10, 11, 12])); + + let struct_array1 = Arc::new(StructArray::from(vec![ + ( + Arc::new(Field::new("x", DataType::UInt64, false)), + array1.clone() as ArrayRef, + ), + ( + Arc::new(Field::new("y", DataType::Int32, false)), + array2.clone() as ArrayRef, + ), + ( + Arc::new(Field::new("z", DataType::UInt8, false)), + array3.clone() as ArrayRef, + ), + ])); + + let array4 = Arc::new(UInt64Array::from(vec![13, 14, 15, 16])); + let array5 = Arc::new(Int32Array::from(vec![17, 18, 19, 20])); + let array6 = Arc::new(UInt8Array::from(vec![21, 22, 23, 24])); + + let struct_array2 = Arc::new(StructArray::from(vec![ + ( + Arc::new(Field::new("x", DataType::UInt64, false)), + array4.clone() as ArrayRef, + ), + ( + Arc::new(Field::new("y", DataType::Int32, false)), + array5.clone() as ArrayRef, + ), + ( + Arc::new(Field::new("z", DataType::UInt8, false)), + array6.clone() as ArrayRef, + ), + ])); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..6) + .with_range(1..4) + .with_indices(vec![1, 3, 7]); + + let mut metadata = HashMap::new(); + metadata.insert("packed".to_string(), "true".to_string()); + + check_round_trip_encoding_of_data( + vec![struct_array1, struct_array2], + &test_cases, + metadata, + ) + .await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/physical/value.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/value.rs new file mode 100644 index 000000000..9f594a002 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/physical/value.rs @@ -0,0 +1,243 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_schema::DataType; +use bytes::Bytes; +use futures::{FutureExt, future::BoxFuture}; +use log::trace; +use std::ops::Range; +use std::sync::{Arc, Mutex}; + +use crate::buffer::LanceBuffer; +use crate::data::{BlockInfo, DataBlock, FixedWidthDataBlock}; +use crate::encodings::physical::block::{ + CompressionConfig, CompressionScheme, GeneralBufferCompressor, +}; +use crate::encodings::physical::value::ValueEncoder; +use crate::format::ProtobufUtils; +use crate::{ + EncodingsIo, + decoder::{PageScheduler, PrimitivePageDecoder}, + encoder::{ArrayEncoder, EncodedArray}, +}; + +use lance_core::{Error, Result}; + +/// Scheduler for a simple encoding where buffers of fixed-size items are stored as-is on disk +#[derive(Debug, Clone, Copy)] +pub struct ValuePageScheduler { + // TODO: do we really support values greater than 2^32 bytes per value? + // I think we want to, in theory, but will need to test this case. + bytes_per_value: u64, + buffer_offset: u64, + buffer_size: u64, + compression_config: CompressionConfig, +} + +impl ValuePageScheduler { + pub fn new( + bytes_per_value: u64, + buffer_offset: u64, + buffer_size: u64, + compression_config: CompressionConfig, + ) -> Self { + Self { + bytes_per_value, + buffer_offset, + buffer_size, + compression_config, + } + } +} + +impl PageScheduler for ValuePageScheduler { + fn schedule_ranges( + &self, + ranges: &[std::ops::Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>> { + let (mut min, mut max) = (u64::MAX, 0); + let byte_ranges = if self.compression_config.scheme == CompressionScheme::None { + ranges + .iter() + .map(|range| { + let start = self.buffer_offset + (range.start * self.bytes_per_value); + let end = self.buffer_offset + (range.end * self.bytes_per_value); + min = min.min(start); + max = max.max(end); + start..end + }) + .collect::>() + } else { + min = self.buffer_offset; + max = self.buffer_offset + self.buffer_size; + // for compressed page, the ranges are always the entire page, + // and it is guaranteed that only one range is passed + vec![Range { + start: min, + end: max, + }] + }; + + trace!( + "Scheduling I/O for {} ranges spread across byte range {}..{}", + byte_ranges.len(), + min, + max + ); + let bytes = scheduler.submit_request(byte_ranges, top_level_row); + let bytes_per_value = self.bytes_per_value; + + let range_offsets = if self.compression_config.scheme != CompressionScheme::None { + ranges + .iter() + .map(|range| { + let start = (range.start * bytes_per_value) as usize; + let end = (range.end * bytes_per_value) as usize; + start..end + }) + .collect::>() + } else { + vec![] + }; + + let compression_config = self.compression_config; + async move { + let bytes = bytes.await?; + + Ok(Box::new(ValuePageDecoder { + bytes_per_value, + data: bytes, + uncompressed_data: Arc::new(Mutex::new(None)), + uncompressed_range_offsets: range_offsets, + compression_config, + }) as Box) + } + .boxed() + } +} + +struct ValuePageDecoder { + bytes_per_value: u64, + data: Vec, + uncompressed_data: Arc>>>, + uncompressed_range_offsets: Vec>, + compression_config: CompressionConfig, +} + +impl ValuePageDecoder { + fn decompress(&self) -> Result> { + // for compressed page, it is guaranteed that only one range is passed + let bytes_u8: Vec = self.data[0].to_vec(); + let buffer_compressor = GeneralBufferCompressor::get_compressor(self.compression_config)?; + let mut uncompressed_bytes: Vec = Vec::new(); + buffer_compressor.decompress(&bytes_u8, &mut uncompressed_bytes)?; + + let mut bytes_in_ranges: Vec = + Vec::with_capacity(self.uncompressed_range_offsets.len()); + for range in &self.uncompressed_range_offsets { + let start = range.start; + let end = range.end; + bytes_in_ranges.push(Bytes::from(uncompressed_bytes[start..end].to_vec())); + } + Ok(bytes_in_ranges) + } + + fn get_uncompressed_bytes(&self) -> Result>>>> { + let mut uncompressed_bytes = self.uncompressed_data.lock().unwrap(); + if uncompressed_bytes.is_none() { + *uncompressed_bytes = Some(self.decompress()?); + } + Ok(Arc::clone(&self.uncompressed_data)) + } + + fn is_compressed(&self) -> bool { + !self.uncompressed_range_offsets.is_empty() + } + + fn decode_buffers<'a>( + &'a self, + buffers: impl IntoIterator, + mut bytes_to_skip: u64, + mut bytes_to_take: u64, + ) -> LanceBuffer { + let mut dest: Option> = None; + + for buf in buffers.into_iter() { + let buf_len = buf.len() as u64; + if bytes_to_skip > buf_len { + bytes_to_skip -= buf_len; + } else { + let bytes_to_take_here = (buf_len - bytes_to_skip).min(bytes_to_take); + bytes_to_take -= bytes_to_take_here; + let start = bytes_to_skip as usize; + let end = start + bytes_to_take_here as usize; + let slice = buf.slice(start..end); + match (&mut dest, bytes_to_take) { + (None, 0) => { + // The entire request is contained in one buffer so we can maybe zero-copy + // if the slice is aligned properly + return LanceBuffer::from_bytes(slice, self.bytes_per_value); + } + (None, _) => { + dest.replace(Vec::with_capacity(bytes_to_take as usize)); + } + _ => {} + } + dest.as_mut().unwrap().extend_from_slice(&slice); + bytes_to_skip = 0; + } + } + LanceBuffer::from(dest.unwrap_or_default()) + } +} + +impl PrimitivePageDecoder for ValuePageDecoder { + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result { + let bytes_to_skip = rows_to_skip * self.bytes_per_value; + let bytes_to_take = num_rows * self.bytes_per_value; + + let data_buffer = if self.is_compressed() { + let decoding_data = self.get_uncompressed_bytes()?; + let buffers = decoding_data.lock().unwrap(); + self.decode_buffers(buffers.as_ref().unwrap(), bytes_to_skip, bytes_to_take) + } else { + self.decode_buffers(&self.data, bytes_to_skip, bytes_to_take) + }; + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bytes_per_value * 8, + data: data_buffer, + num_values: num_rows, + block_info: BlockInfo::new(), + })) + } +} + +impl ArrayEncoder for ValueEncoder { + fn encode( + &self, + data: DataBlock, + _data_type: &DataType, + buffer_index: &mut u32, + ) -> Result { + let index = *buffer_index; + *buffer_index += 1; + + let encoding = match &data { + DataBlock::FixedWidth(fixed_width) => Ok(ProtobufUtils::flat_encoding( + fixed_width.bits_per_value, + index, + None, + )), + _ => Err(Error::invalid_input_source( + format!( + "Cannot encode a data block of type {} with ValueEncoder", + data.name() + ) + .into(), + )), + }?; + Ok(EncodedArray { data, encoding }) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/array_encoding/strategy.rs b/lance-artifact/rust/lance-encoding/src/array_encoding/strategy.rs new file mode 100644 index 000000000..9700d7157 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/array_encoding/strategy.rs @@ -0,0 +1,653 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashMap, env, hash::RandomState, sync::Arc}; + +#[cfg(test)] +use arrow_array::cast::AsArray; +use arrow_array::{ArrayRef, UInt8Array}; +use arrow_schema::DataType; +use hyperloglogplus::{HyperLogLog, HyperLogLogPlus}; + +use crate::{ + array_encoding::{ + logical::{ + blob::BlobFieldEncoder, list::ListFieldEncoder, primitive::PrimitiveFieldEncoder, + }, + physical::{ + basic::BasicEncoder, + binary::BinaryEncoder, + dictionary::{AlreadyDictionaryEncoder, DictionaryEncoder}, + fixed_size_list::FslEncoder, + fsst::FsstArrayEncoder, + packed_struct::PackedStructEncoder, + }, + }, + constants::{ + COMPRESSION_LEVEL_META_KEY, COMPRESSION_META_KEY, PACKED_STRUCT_LEGACY_META_KEY, + PACKED_STRUCT_META_KEY, + }, + encoder::{ + ArrayEncoder, ArrayEncodingStrategy, ColumnIndexSequence, FieldEncoder, + FieldEncodingContext, FieldEncodingStrategy, + }, + encodings::{ + logical::r#struct::StructFieldEncoder, + physical::{ + block::{CompressionConfig, CompressionScheme}, + value::ValueEncoder, + }, + }, +}; + +use lance_arrow::BLOB_META_KEY; +use lance_core::datatypes::{BLOB_DESC_FIELD, Field}; +use lance_core::{Error, Result}; + +/// Field-to-column composition for the `pb::ArrayEncoding` grammar. +#[derive(Debug)] +pub struct ArrayFieldEncodingStrategy { + array_encoding_strategy: Arc, +} + +impl ArrayFieldEncodingStrategy { + /// Create the field strategy for the `pb::ArrayEncoding` grammar. + /// + /// ``` + /// use lance_encoding::encoder::ArrayFieldEncodingStrategy; + /// + /// let strategy = ArrayFieldEncodingStrategy::new(); + /// ``` + pub fn new() -> Self { + Self { + array_encoding_strategy: Arc::new(ArrayStrategy), + } + } + + fn is_primitive_type(data_type: &DataType) -> bool { + matches!( + data_type, + DataType::Boolean + | DataType::Date32 + | DataType::Date64 + | DataType::Decimal128(_, _) + | DataType::Decimal256(_, _) + | DataType::Duration(_) + | DataType::Float16 + | DataType::Float32 + | DataType::Float64 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Int8 + | DataType::Interval(_) + | DataType::Null + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::UInt16 + | DataType::UInt32 + | DataType::UInt64 + | DataType::UInt8 + | DataType::FixedSizeBinary(_) + | DataType::FixedSizeList(_, _) + | DataType::Binary + | DataType::LargeBinary + | DataType::Utf8 + | DataType::LargeUtf8, + ) + } +} + +impl Default for ArrayFieldEncodingStrategy { + fn default() -> Self { + Self::new() + } +} + +impl FieldEncodingStrategy for ArrayFieldEncodingStrategy { + fn create_field_encoder( + &self, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, + ) -> Result> { + let options = context.options; + let data_type = field.data_type(); + if Self::is_primitive_type(&data_type) { + let column_index = column_index.next_column_index(field.id as u32); + if field.metadata.contains_key(BLOB_META_KEY) { + let mut packed_meta = HashMap::new(); + packed_meta.insert(PACKED_STRUCT_META_KEY.to_string(), "true".to_string()); + let desc_field = + Field::try_from(BLOB_DESC_FIELD.clone().with_metadata(packed_meta)).unwrap(); + let desc_encoder = Box::new(PrimitiveFieldEncoder::try_new( + options, + self.array_encoding_strategy.clone(), + column_index, + desc_field, + )?); + Ok(Box::new(BlobFieldEncoder::new(desc_encoder))) + } else { + Ok(Box::new(PrimitiveFieldEncoder::try_new( + options, + self.array_encoding_strategy.clone(), + column_index, + field.clone(), + )?)) + } + } else { + match data_type { + DataType::List(_child) | DataType::LargeList(_child) => { + let list_idx = column_index.next_column_index(field.id as u32); + let inner_encoding = context.strategy.create_field_encoder( + &field.children[0], + column_index, + context, + )?; + let offsets_encoder = + Arc::new(BasicEncoder::new(Box::new(ValueEncoder::default()))); + Ok(Box::new(ListFieldEncoder::new( + inner_encoding, + offsets_encoder, + options.cache_bytes_per_column, + options.keep_original_array, + list_idx, + ))) + } + DataType::Struct(_) => { + let field_metadata = &field.metadata; + if field_metadata + .get(PACKED_STRUCT_LEGACY_META_KEY) + .map(|v| v == "true") + .unwrap_or(field_metadata.contains_key(PACKED_STRUCT_META_KEY)) + { + Ok(Box::new(PrimitiveFieldEncoder::try_new( + options, + self.array_encoding_strategy.clone(), + column_index.next_column_index(field.id as u32), + field.clone(), + )?)) + } else { + let header_idx = column_index.next_column_index(field.id as u32); + let children_encoders = field + .children + .iter() + .map(|field| { + context + .strategy + .create_field_encoder(field, column_index, context) + }) + .collect::>>()?; + Ok(Box::new(StructFieldEncoder::new( + children_encoders, + header_idx, + ))) + } + } + DataType::Dictionary(_, value_type) => { + // A dictionary of primitive is, itself, primitive + if Self::is_primitive_type(&value_type) { + Ok(Box::new(PrimitiveFieldEncoder::try_new( + options, + self.array_encoding_strategy.clone(), + column_index.next_column_index(field.id as u32), + field.clone(), + )?)) + } else { + // A dictionary of logical is, itself, logical and we don't support that today + // It could be possible (e.g. store indices in one column and values in remaining columns) + // but would be a significant amount of work + // + // An easier fallback implementation would be to decode-on-write and encode-on-read + Err(Error::not_supported_source(format!("cannot encode a dictionary column whose value type is a logical type ({})", value_type).into())) + } + } + _ => todo!("Implement encoding for field {}", field), + } + } + } +} + +/// Page-encoding selection for the `pb::ArrayEncoding` grammar. +#[derive(Debug)] +struct ArrayStrategy; + +impl ArrayStrategy { + fn get_field_compression(field_meta: &HashMap) -> Option { + let compression = field_meta.get(COMPRESSION_META_KEY)?; + let compression_scheme = compression.parse::(); + match compression_scheme { + Ok(compression_scheme) => Some(CompressionConfig::new( + compression_scheme, + field_meta + .get(COMPRESSION_LEVEL_META_KEY) + .and_then(|level| level.parse().ok()), + )), + Err(_) => None, + } + } + + fn default_binary_encoder( + arrays: &[ArrayRef], + field_meta: Option<&HashMap>, + data_size: u64, + ) -> Result> { + let bin_indices_encoder = + Self::choose_array_encoder(arrays, &DataType::UInt64, data_size, false, None)?; + + if let Some(compression) = field_meta.and_then(Self::get_field_compression) { + if compression.scheme() == CompressionScheme::Fsst { + // User requested FSST + let raw_encoder = Box::new(BinaryEncoder::try_new(bin_indices_encoder, None)?); + Ok(Box::new(FsstArrayEncoder::new(raw_encoder))) + } else { + // Generic compression + Ok(Box::new(BinaryEncoder::try_new( + bin_indices_encoder, + Some(compression), + )?)) + } + } else { + Ok(Box::new(BinaryEncoder::try_new(bin_indices_encoder, None)?)) + } + } + + fn choose_array_encoder( + arrays: &[ArrayRef], + data_type: &DataType, + data_size: u64, + use_dict_encoding: bool, + field_meta: Option<&HashMap>, + ) -> Result> { + match data_type { + DataType::FixedSizeList(inner, dimension) => { + Ok(Box::new(BasicEncoder::new(Box::new(FslEncoder::new( + Self::choose_array_encoder( + arrays, + inner.data_type(), + data_size, + use_dict_encoding, + None, + )?, + *dimension as u32, + ))))) + } + DataType::Dictionary(key_type, value_type) => { + let key_encoder = + Self::choose_array_encoder(arrays, key_type, data_size, false, None)?; + let value_encoder = + Self::choose_array_encoder(arrays, value_type, data_size, false, None)?; + + Ok(Box::new(AlreadyDictionaryEncoder::new( + key_encoder, + value_encoder, + ))) + } + DataType::Utf8 | DataType::LargeUtf8 | DataType::Binary | DataType::LargeBinary => { + if use_dict_encoding { + let dict_indices_encoder = Self::choose_array_encoder( + // We need to pass arrays to this method to figure out what kind of compression to + // use but we haven't actually calculated the indices yet. For now, we just assume + // worst case and use the full range. In the future maybe we can pass in statistics + // instead of the actual data + &[Arc::new(UInt8Array::from_iter_values(0_u8..255_u8))], + &DataType::UInt8, + data_size, + false, + None, + )?; + let dict_items_encoder = Self::choose_array_encoder( + arrays, + &DataType::Utf8, + data_size, + false, + None, + )?; + + Ok(Box::new(DictionaryEncoder::new( + dict_indices_encoder, + dict_items_encoder, + ))) + } else { + Self::default_binary_encoder(arrays, field_meta, data_size) + } + } + DataType::Struct(fields) => { + let num_fields = fields.len(); + let mut inner_encoders = Vec::new(); + + for i in 0..num_fields { + let inner_datatype = fields[i].data_type(); + let inner_encoder = Self::choose_array_encoder( + arrays, + inner_datatype, + data_size, + use_dict_encoding, + None, + )?; + inner_encoders.push(inner_encoder); + } + + Ok(Box::new(PackedStructEncoder::new(inner_encoders))) + } + DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => Ok( + Box::new(BasicEncoder::new(Box::new(ValueEncoder::default()))), + ), + + // TODO: for signed integers, I intend to make it a cascaded encoding, a sparse array for the negative values and very wide(bit-width) values, + // then a bitpacked array for the narrow(bit-width) values, I need `BitpackedForNeg` to be merged first, I am + // thinking about putting this sparse array in the metadata so bitpacking remain using one page buffer only. + DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => Ok(Box::new( + BasicEncoder::new(Box::new(ValueEncoder::default())), + )), + _ => Ok(Box::new(BasicEncoder::new(Box::new( + ValueEncoder::default(), + )))), + } + } +} + +fn get_dict_encoding_threshold() -> u64 { + env::var("LANCE_DICT_ENCODING_THRESHOLD") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(100) +} + +// check whether we want to use dictionary encoding or not +// by applying a threshold on cardinality +// returns true if cardinality < threshold but false if the total number of rows is less than the threshold +// The choice to use 100 is just a heuristic for now +// hyperloglog is used for cardinality estimation +// error rate = 1.04 / sqrt(2^p), where p is the precision +// and error rate is 1.04 / sqrt(2^12) = 1.56% +fn check_dict_encoding(arrays: &[ArrayRef], threshold: u64) -> bool { + let num_total_rows = arrays.iter().map(|arr| arr.len()).sum::(); + if num_total_rows < threshold as usize { + return false; + } + const PRECISION: u8 = 12; + + let mut hll: HyperLogLogPlus = + HyperLogLogPlus::new(PRECISION, RandomState::new()).unwrap(); + + for arr in arrays { + let string_array = arrow_array::cast::as_string_array(arr); + for value in string_array.iter().flatten() { + hll.insert(value); + let estimated_cardinality = hll.count() as u64; + if estimated_cardinality >= threshold { + return false; + } + } + } + + true +} + +#[cfg(test)] +fn check_fixed_size_encoding(arrays: &[ArrayRef]) -> Option { + if arrays.is_empty() { + return None; + } + + // make sure no array has an empty string + if !arrays.iter().all(|arr| { + if let Some(arr) = arr.as_string_opt::() { + arr.iter().flatten().all(|s| !s.is_empty()) + } else if let Some(arr) = arr.as_binary_opt::() { + arr.iter().flatten().all(|s| !s.is_empty()) + } else if let Some(arr) = arr.as_string_opt::() { + arr.iter().flatten().all(|s| !s.is_empty()) + } else if let Some(arr) = arr.as_binary_opt::() { + arr.iter().flatten().all(|s| !s.is_empty()) + } else { + panic!("wrong dtype"); + } + }) { + return None; + } + + let lengths = arrays + .iter() + .flat_map(|arr| { + if let Some(arr) = arr.as_string_opt::() { + let offsets = arr.offsets().inner(); + offsets + .windows(2) + .map(|w| (w[1] - w[0]) as u64) + .collect::>() + } else if let Some(arr) = arr.as_binary_opt::() { + let offsets = arr.offsets().inner(); + offsets + .windows(2) + .map(|w| (w[1] - w[0]) as u64) + .collect::>() + } else if let Some(arr) = arr.as_string_opt::() { + let offsets = arr.offsets().inner(); + offsets + .windows(2) + .map(|w| (w[1] - w[0]) as u64) + .collect::>() + } else if let Some(arr) = arr.as_binary_opt::() { + let offsets = arr.offsets().inner(); + offsets + .windows(2) + .map(|w| (w[1] - w[0]) as u64) + .collect::>() + } else { + panic!("wrong dtype"); + } + }) + .collect::>(); + + // find first non-zero value in lengths + let first_non_zero = lengths.iter().position(|&x| x != 0); + if let Some(first_non_zero) = first_non_zero { + // make sure all lengths are equal to first_non_zero length or zero + if !lengths + .iter() + .all(|&x| x == 0 || x == lengths[first_non_zero]) + { + return None; + } + + // set the byte width + Some(lengths[first_non_zero]) + } else { + None + } +} + +impl ArrayEncodingStrategy for ArrayStrategy { + fn create_array_encoder( + &self, + arrays: &[ArrayRef], + field: &Field, + ) -> Result> { + let data_size = arrays + .iter() + .map(|arr| arr.get_buffer_memory_size() as u64) + .sum::(); + let data_type = arrays[0].data_type(); + + let use_dict_encoding = data_type == &DataType::Utf8 + && check_dict_encoding(arrays, get_dict_encoding_threshold()); + + Self::choose_array_encoder( + arrays, + data_type, + data_size, + use_dict_encoding, + Some(&field.metadata), + ) + } +} + +#[cfg(test)] +mod tests { + use super::{ + ArrayEncodingStrategy, ArrayStrategy, check_dict_encoding, check_fixed_size_encoding, + }; + use crate::constants::{COMPRESSION_LEVEL_META_KEY, COMPRESSION_META_KEY}; + use arrow_array::{ArrayRef, StringArray}; + use arrow_schema::Field; + use std::collections::HashMap; + use std::sync::Arc; + + fn is_dict_encoding_applicable(arr: Vec>, threshold: u64) -> bool { + let arr = StringArray::from(arr); + let arr = Arc::new(arr) as ArrayRef; + check_dict_encoding(&[arr], threshold) + } + + #[test] + fn test_dict_encoding_should_be_applied_if_cardinality_less_than_threshold() { + assert!(is_dict_encoding_applicable( + vec![Some("a"), Some("b"), Some("a"), Some("b")], + 3, + )); + } + + #[test] + fn test_dict_encoding_should_not_be_applied_if_cardinality_larger_than_threshold() { + assert!(!is_dict_encoding_applicable( + vec![Some("a"), Some("b"), Some("c"), Some("d")], + 3, + )); + } + + #[test] + fn test_dict_encoding_should_not_be_applied_if_cardinality_equal_to_threshold() { + assert!(!is_dict_encoding_applicable( + vec![Some("a"), Some("b"), Some("c"), Some("a")], + 3, + )); + } + + #[test] + fn test_dict_encoding_should_not_be_applied_for_empty_arrays() { + assert!(!is_dict_encoding_applicable(vec![], 3)); + } + + #[test] + fn test_dict_encoding_should_not_be_applied_for_smaller_than_threshold_arrays() { + assert!(!is_dict_encoding_applicable(vec![Some("a"), Some("a")], 3)); + } + + fn is_fixed_size_encoding_applicable(arrays: Vec>>) -> bool { + let mut final_arrays = Vec::new(); + for arr in arrays { + let arr = StringArray::from(arr); + let arr = Arc::new(arr) as ArrayRef; + final_arrays.push(arr); + } + + check_fixed_size_encoding(&final_arrays).is_some() + } + + #[test] + fn test_fixed_size_binary_encoding_applicable() { + assert!(!is_fixed_size_encoding_applicable(vec![vec![]])); + + assert!(is_fixed_size_encoding_applicable(vec![vec![ + Some("a"), + Some("b") + ]])); + + assert!(!is_fixed_size_encoding_applicable(vec![vec![ + Some("abc"), + Some("de") + ]])); + + assert!(is_fixed_size_encoding_applicable(vec![vec![ + Some("pqr"), + None + ]])); + + assert!(!is_fixed_size_encoding_applicable(vec![vec![ + Some("pqr"), + Some("") + ]])); + + assert!(!is_fixed_size_encoding_applicable(vec![vec![ + Some(""), + Some("") + ]])); + } + + #[test] + fn test_fixed_size_binary_encoding_applicable_multiple_arrays() { + assert!(is_fixed_size_encoding_applicable(vec![ + vec![Some("a"), Some("b")], + vec![Some("c"), Some("d")] + ])); + + assert!(!is_fixed_size_encoding_applicable(vec![ + vec![Some("ab"), Some("bc")], + vec![Some("c"), Some("d")] + ])); + + assert!(!is_fixed_size_encoding_applicable(vec![ + vec![Some("ab"), None], + vec![None, Some("d")] + ])); + + assert!(is_fixed_size_encoding_applicable(vec![ + vec![Some("a"), None], + vec![None, Some("d")] + ])); + + assert!(!is_fixed_size_encoding_applicable(vec![ + vec![Some(""), None], + vec![None, Some("")] + ])); + + assert!(!is_fixed_size_encoding_applicable(vec![ + vec![None, None], + vec![None, None] + ])); + } + + fn verify_array_encoder( + array: ArrayRef, + field_meta: Option>, + expected_encoder: &str, + ) { + let encoding_strategy = ArrayStrategy; + let mut field = Field::new("test_field", array.data_type().clone(), true); + if let Some(field_meta) = field_meta { + field.set_metadata(field_meta); + } + let lance_field = lance_core::datatypes::Field::try_from(field).unwrap(); + let encoder_result = encoding_strategy.create_array_encoder(&[array], &lance_field); + assert!(encoder_result.is_ok()); + let encoder = encoder_result.unwrap(); + assert_eq!(format!("{:?}", encoder).as_str(), expected_encoder); + } + + #[test] + fn test_choose_encoder_for_zstd_compressed_string_field() { + verify_array_encoder( + Arc::new(StringArray::from(vec!["a", "bb", "ccc"])), + Some(HashMap::from([( + COMPRESSION_META_KEY.to_string(), + "zstd".to_string(), + )])), + "BinaryEncoder { indices_encoder: BasicEncoder { values_encoder: ValueEncoder }, compression_config: Some(CompressionConfig { scheme: Zstd, level: None }), buffer_compressor: Some(ZstdBufferCompressor { compression_level: 0 }) }", + ); + } + + #[test] + fn test_choose_encoder_for_zstd_compression_level() { + verify_array_encoder( + Arc::new(StringArray::from(vec!["a", "bb", "ccc"])), + Some(HashMap::from([ + (COMPRESSION_META_KEY.to_string(), "zstd".to_string()), + (COMPRESSION_LEVEL_META_KEY.to_string(), "22".to_string()), + ])), + "BinaryEncoder { indices_encoder: BasicEncoder { values_encoder: ValueEncoder }, compression_config: Some(CompressionConfig { scheme: Zstd, level: Some(22) }), buffer_compressor: Some(ZstdBufferCompressor { compression_level: 22 }) }", + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/buffer.rs b/lance-artifact/rust/lance-encoding/src/buffer.rs new file mode 100644 index 000000000..646f4f515 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/buffer.rs @@ -0,0 +1,503 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for byte arrays + +use std::{ops::Deref, panic::RefUnwindSafe, ptr::NonNull, sync::Arc}; + +use arrow_buffer::{ArrowNativeType, Buffer, MutableBuffer, ScalarBuffer}; +use lance_core::{Error, Result, utils::bit::is_pwr_two}; +use std::borrow::Cow; + +/// A copy-on-write byte buffer. +/// +/// It wraps arrow_buffer::Buffer which provides: +/// - Cheap cloning (reference counted) +/// - Zero-copy slicing +/// - Automatic memory alignment +/// +/// LanceBuffer is designed to be used in situations where you might need to +/// pass around byte buffers efficiently without worrying about ownership. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LanceBuffer(Buffer); + +impl LanceBuffer { + /// Convert into an Arrow buffer. Never copies data. + pub fn into_buffer(self) -> Buffer { + self.0 + } + + /// Returns a buffer of the given size with all bits set to 0 + pub fn all_unset(len: usize) -> Self { + Self(Buffer::from_vec(vec![0; len])) + } + + /// Returns a buffer of the given size with all bits set to 1 + pub fn all_set(len: usize) -> Self { + Self(Buffer::from_vec(vec![0xff; len])) + } + + /// Creates an empty buffer + pub fn empty() -> Self { + Self(Buffer::from_vec(Vec::::new())) + } + + /// Converts the buffer into a hex string + pub fn as_hex(&self) -> String { + hex::encode_upper(self) + } + + /// Combine multiple buffers into a single buffer + /// + /// This does involve a data copy (and allocation of a new buffer) + pub fn concat(buffers: &[Self]) -> Self { + let total_len = buffers.iter().map(|b| b.len()).sum(); + let mut data = Vec::with_capacity(total_len); + for buffer in buffers { + data.extend_from_slice(buffer.as_ref()); + } + Self(Buffer::from_vec(data)) + } + + /// Converts the buffer into a hex string, inserting a space + /// between words + pub fn as_spaced_hex(&self, bytes_per_word: u32) -> String { + let hex = self.as_hex(); + let chars_per_word = bytes_per_word as usize * 2; + let num_words = hex.len() / chars_per_word; + let mut spaced_hex = String::with_capacity(hex.len() + num_words); + for (i, c) in hex.chars().enumerate() { + if i % chars_per_word == 0 && i != 0 { + spaced_hex.push(' '); + } + spaced_hex.push(c); + } + spaced_hex + } + + /// Create a LanceBuffer from a bytes::Bytes object + /// + /// The alignment must be specified (as `bytes_per_value`) since we want to make + /// sure we can safely reinterpret the buffer. + /// + /// If the buffer is properly aligned this will be zero-copy. If not, a copy + /// will be made. + /// + /// If `bytes_per_value` is not a power of two, then we assume the buffer is + /// never going to be reinterpret into another type and we can safely + /// ignore the alignment. + /// + /// This is a zero-copy operation when the buffer is properly aligned. + pub fn from_bytes(bytes: bytes::Bytes, bytes_per_value: u64) -> Self { + if is_pwr_two(bytes_per_value) && bytes.as_ptr().align_offset(bytes_per_value as usize) != 0 + { + // The original buffer is not aligned, cannot zero-copy + let mut buf = Vec::with_capacity(bytes.len()); + buf.extend_from_slice(&bytes); + Self(Buffer::from_vec(buf)) + } else { + // The original buffer is aligned, can zero-copy + // SAFETY: the alignment is correct we can make this conversion + unsafe { + Self(Buffer::from_custom_allocation( + NonNull::new(bytes.as_ptr() as _).expect("should be a valid pointer"), + bytes.len(), + Arc::new(bytes), + )) + } + } + } + + /// Make an owned copy of the buffer (always does a copy of the data) + pub fn deep_copy(&self) -> Self { + Self(Buffer::from_vec(self.0.to_vec())) + } + + /// Reinterprets a `Vec` as a LanceBuffer + /// + /// This is a zero-copy operation. We can safely reinterpret `Vec` into `&[u8]` which is what happens here. + /// However, we cannot safely reinterpret a `Vec` into a `Vec` in rust due to alignment constraints + /// from [`Vec::from_raw_parts`]: + /// + /// > `T` needs to have the same alignment as what `ptr` was allocated with. + /// > (`T` having a less strict alignment is not sufficient, the alignment really + /// > needs to be equal to satisfy the `dealloc` requirement that memory must be + /// > allocated and deallocated with the same layout.) + pub fn reinterpret_vec(vec: Vec) -> Self { + Self(Buffer::from_vec(vec)) + } + + /// Reinterprets `Arc<[T]>` as a LanceBuffer + /// + /// This is similar to [`Self::reinterpret_vec`] but for `Arc<[T]>` instead of `Vec` + /// + /// The same alignment constraints apply + pub fn reinterpret_slice(arc: Arc<[T]>) -> Self { + let slice = arc.as_ref(); + let data = NonNull::new(slice.as_ptr() as _).unwrap_or(NonNull::dangling()); + let len = std::mem::size_of_val(slice); + // SAFETY: the ptr will be valid for len items if the Arc<[T]> is valid + let buffer = unsafe { Buffer::from_custom_allocation(data, len, Arc::new(arc)) }; + Self(buffer) + } + + /// Reinterprets a LanceBuffer into a `Vec` + /// + /// If the underlying buffer is not properly aligned, this will involve a copy of the data + /// + /// Note: doing this sort of re-interpretation generally makes assumptions about the endianness + /// of the data. Lance does not support big-endian machines so this is safe. However, if we end + /// up supporting big-endian machines in the future, then any use of this method will need to be + /// carefully reviewed. + pub fn borrow_to_typed_slice(&self) -> ScalarBuffer { + let align = std::mem::align_of::(); + let is_aligned = self.as_ptr().align_offset(align) == 0; + if !self.len().is_multiple_of(std::mem::size_of::()) { + panic!( + "attempt to borrow_to_typed_slice to data type of size {} but we have {} bytes which isn't evenly divisible", + std::mem::size_of::(), + self.len() + ); + } + + if is_aligned { + ScalarBuffer::::from(self.clone().into_buffer()) + } else { + let num_values = self.len() / std::mem::size_of::(); + let vec = Vec::::with_capacity(num_values); + let mut bytes = MutableBuffer::from(vec); + bytes.extend_from_slice(self); + ScalarBuffer::::from(Buffer::from(bytes)) + } + } + + /// Reinterprets a LanceBuffer into a `&[T]` + /// + /// Unlike [`Self::borrow_to_typed_slice`], this function returns a `Cow<'_, [T]>` instead of an owned + /// buffer. It saves the cost of Arc creation and destruction, which can be really helpful when + /// we borrow data and just drop it without reusing it. + /// + /// Caller should decide which way to use based on their own needs. + /// + /// If the underlying buffer is not properly aligned, this will involve a copy of the data + /// + /// Note: doing this sort of re-interpretation generally makes assumptions about the endianness + /// of the data. Lance does not support big-endian machines so this is safe. However, if we end + /// up supporting big-endian machines in the future, then any use of this method will need to be + /// carefully reviewed. + pub fn borrow_to_typed_view(&self) -> Cow<'_, [T]> { + let align = std::mem::align_of::(); + if !self.len().is_multiple_of(std::mem::size_of::()) { + panic!( + "attempt to view data type of size {} but we have {} bytes which isn't evenly divisible", + std::mem::size_of::(), + self.len() + ); + } + + if self.as_ptr().align_offset(align) == 0 { + Cow::Borrowed(bytemuck::cast_slice(&self.0)) + } else { + Cow::Owned(bytemuck::pod_collect_to_vec(self.0.as_slice())) + } + } + + /// Concatenates multiple buffers into a single buffer, consuming the input buffers + /// + /// If there is only one buffer, it will be returned as is + pub fn concat_into_one(buffers: Vec) -> Self { + if buffers.len() == 1 { + return buffers.into_iter().next().unwrap(); + } + + let mut total_len = 0; + for buffer in &buffers { + total_len += buffer.len(); + } + + let mut data = Vec::with_capacity(total_len); + for buffer in buffers { + data.extend_from_slice(buffer.as_ref()); + } + + Self(Buffer::from_vec(data)) + } + + /// Zips multiple buffers into a single buffer, consuming the input buffers + /// + /// Unlike concat_into_one this "zips" the buffers, interleaving the values + pub fn zip_into_one(buffers: Vec<(Self, u64)>, num_values: u64) -> Result { + let bytes_per_value = buffers.iter().map(|(_, bits_per_value)| { + if bits_per_value % 8 == 0 { + Ok(bits_per_value / 8) + } else { + Err(Error::invalid_input_source(format!("LanceBuffer::zip_into_one only supports full-byte buffers currently and received a buffer with {} bits per value", bits_per_value).into())) + } + }).collect::>>()?; + let total_bytes_per_value = bytes_per_value.iter().sum::(); + let total_bytes = (total_bytes_per_value * num_values) as usize; + + let mut zipped = vec![0_u8; total_bytes]; + let mut buffer_ptrs = buffers + .iter() + .zip(bytes_per_value) + .map(|((buffer, _), bytes_per_value)| (buffer.as_ptr(), bytes_per_value as usize)) + .collect::>(); + + let mut zipped_ptr = zipped.as_mut_ptr(); + unsafe { + let end = zipped_ptr.add(total_bytes); + while zipped_ptr < end { + for (buf, bytes_per_value) in buffer_ptrs.iter_mut() { + std::ptr::copy_nonoverlapping(*buf, zipped_ptr, *bytes_per_value); + zipped_ptr = zipped_ptr.add(*bytes_per_value); + *buf = buf.add(*bytes_per_value); + } + } + } + + Ok(Self(Buffer::from_vec(zipped))) + } + + /// Create a LanceBuffer from a slice + /// + /// This is NOT a zero-copy operation. We can't create a borrowed buffer because + /// we have no way of extending the lifetime of the slice. + pub fn copy_slice(slice: &[u8]) -> Self { + Self(Buffer::from_vec(slice.to_vec())) + } + + /// Create a LanceBuffer from an array (fixed-size slice) + /// + /// This is NOT a zero-copy operation. The slice memory could be on the stack and + /// thus we can't forget it. + pub fn copy_array(array: [u8; N]) -> Self { + Self(Buffer::from_vec(Vec::from(array))) + } + + #[allow(clippy::len_without_is_empty)] + pub fn len(&self) -> usize { + self.0.len() + } + + /// Returns a new [LanceBuffer] that is a slice of this buffer starting at `offset`, + /// with `length` bytes. + /// Doing so allows the same memory region to be shared between lance buffers. + /// # Panics + /// Panics if `(offset + length)` is larger than the existing length. + pub fn slice_with_length(&self, offset: usize, length: usize) -> Self { + let original_buffer_len = self.len(); + assert!( + offset.saturating_add(length) <= original_buffer_len, + "the offset + length of the sliced Buffer cannot exceed the existing length" + ); + Self(self.0.slice_with_length(offset, length)) + } + + /// Returns a new [LanceBuffer] that is a slice of this buffer starting at bit `offset` + /// with `length` bits. + /// + /// Unlike `slice_with_length`, this method allows for slicing at a bit level but always + /// requires a copy of the data (unless offset is byte-aligned) + /// + /// This method performs the bit slice using the Arrow convention of *bitwise* little-endian + /// + /// This means, given the bit buffer 0bABCDEFGH_HIJKLMNOP and the slice starting at bit 3 and + /// with length 8, the result will be 0bNOPABCDE + pub fn bit_slice_le_with_length(&self, offset: usize, length: usize) -> Self { + let sliced = self.0.bit_slice(offset, length); + Self(sliced) + } + + /// Get a pointer to the underlying data + pub fn as_ptr(&self) -> *const u8 { + self.0.as_ptr() + } +} + +impl AsRef<[u8]> for LanceBuffer { + fn as_ref(&self) -> &[u8] { + self.0.as_slice() + } +} + +impl Deref for LanceBuffer { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + self.as_ref() + } +} + +// All `From` implementations are zero-copy + +impl From> for LanceBuffer { + fn from(buffer: Vec) -> Self { + Self(Buffer::from_vec(buffer)) + } +} + +impl From for LanceBuffer { + fn from(buffer: Buffer) -> Self { + Self(buffer) + } +} + +// An iterator that keeps a clone of a borrowed LanceBuffer so we +// can have a 'static lifetime +pub struct LanceBufferIter { + buffer: Buffer, + index: usize, +} + +impl Iterator for LanceBufferIter { + type Item = u8; + + fn next(&mut self) -> Option { + if self.index >= self.buffer.len() { + None + } else { + // SAFETY: we just checked that index is in bounds + let byte = unsafe { self.buffer.get_unchecked(self.index) }; + self.index += 1; + Some(*byte) + } + } +} + +impl IntoIterator for LanceBuffer { + type Item = u8; + type IntoIter = LanceBufferIter; + + fn into_iter(self) -> Self::IntoIter { + LanceBufferIter { + buffer: self.0, + index: 0, + } + } +} + +#[cfg(test)] +mod tests { + use arrow_buffer::Buffer; + + use super::LanceBuffer; + + #[test] + fn test_eq() { + let buf = LanceBuffer::from(Buffer::from_vec(vec![1_u8, 2, 3])); + let buf2 = LanceBuffer::from(vec![1, 2, 3]); + assert_eq!(buf, buf2); + } + + #[test] + fn test_reinterpret_vec() { + let vec = vec![1_u32, 2, 3]; + let buf = LanceBuffer::reinterpret_vec(vec); + + let mut expected = Vec::with_capacity(12); + expected.extend_from_slice(&1_u32.to_ne_bytes()); + expected.extend_from_slice(&2_u32.to_ne_bytes()); + expected.extend_from_slice(&3_u32.to_ne_bytes()); + let expected = LanceBuffer::from(expected); + + assert_eq!(expected, buf); + assert_eq!(buf.borrow_to_typed_slice::().as_ref(), vec![1, 2, 3]); + } + + #[test] + fn test_concat() { + let buf1 = LanceBuffer::from(vec![1_u8, 2, 3]); + let buf2 = LanceBuffer::from(vec![4_u8, 5, 6]); + let buf3 = LanceBuffer::from(vec![7_u8, 8, 9]); + + let expected = LanceBuffer::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); + assert_eq!( + expected, + LanceBuffer::concat_into_one(vec![buf1, buf2, buf3]) + ); + + let empty = LanceBuffer::empty(); + assert_eq!( + LanceBuffer::empty(), + LanceBuffer::concat_into_one(vec![empty]) + ); + + let expected = LanceBuffer::from(vec![1, 2, 3]); + assert_eq!( + expected, + LanceBuffer::concat_into_one(vec![expected.deep_copy(), LanceBuffer::empty()]) + ); + } + + #[test] + fn test_zip() { + let buf1 = LanceBuffer::from(vec![1_u8, 2, 3]); + let buf2 = LanceBuffer::reinterpret_vec(vec![1_u16, 2, 3]); + let buf3 = LanceBuffer::reinterpret_vec(vec![1_u32, 2, 3]); + + let zipped = LanceBuffer::zip_into_one(vec![(buf1, 8), (buf2, 16), (buf3, 32)], 3).unwrap(); + + assert_eq!(zipped.len(), 21); + + let mut expected = Vec::with_capacity(21); + for i in 1..4 { + expected.push(i as u8); + expected.extend_from_slice(&(i as u16).to_ne_bytes()); + expected.extend_from_slice(&(i as u32).to_ne_bytes()); + } + let expected = LanceBuffer::from(expected); + + assert_eq!(expected, zipped); + } + + #[test] + fn test_hex() { + let buf = LanceBuffer::from(vec![1, 2, 15, 20]); + assert_eq!("01020F14", buf.as_hex()); + } + + #[test] + #[should_panic] + fn test_to_typed_slice_invalid() { + let buf = LanceBuffer::from(vec![0, 1, 2]); + buf.borrow_to_typed_slice::(); + } + + #[test] + fn test_to_typed_slice() { + // Buffer is aligned, no copy will be made, both calls + // should get same ptr + let buf = LanceBuffer::from(vec![0, 1]); + let borrow = buf.borrow_to_typed_slice::(); + let view_ptr = borrow.as_ref().as_ptr(); + let borrow2 = buf.borrow_to_typed_slice::(); + let view_ptr2 = borrow2.as_ref().as_ptr(); + + assert_eq!(view_ptr, view_ptr2); + + let bytes = bytes::Bytes::from(vec![0, 1, 2]); + let sliced = bytes.slice(1..3); + // Intentionally LYING about alignment here to trigger test + let buf = LanceBuffer::from_bytes(sliced, 1); + let borrow = buf.borrow_to_typed_slice::(); + let view_ptr = borrow.as_ref().as_ptr(); + let borrow2 = buf.borrow_to_typed_slice::(); + let view_ptr2 = borrow2.as_ref().as_ptr(); + + assert_ne!(view_ptr, view_ptr2); + } + + #[test] + fn test_bit_slice_le() { + let buf = LanceBuffer::from(vec![0x0F, 0x0B]); + + // Keep in mind that validity buffers are *bitwise* little-endian + assert_eq!(buf.bit_slice_le_with_length(0, 4).as_ref(), &[0x0F]); + assert_eq!(buf.bit_slice_le_with_length(4, 4).as_ref(), &[0x00]); + assert_eq!(buf.bit_slice_le_with_length(3, 8).as_ref(), &[0x61]); + assert_eq!(buf.bit_slice_le_with_length(0, 8).as_ref(), &[0x0F]); + assert_eq!(buf.bit_slice_le_with_length(4, 8).as_ref(), &[0xB0]); + assert_eq!(buf.bit_slice_le_with_length(4, 12).as_ref(), &[0xB0, 0x00]); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/compression.rs b/lance-artifact/rust/lance-encoding/src/compression.rs new file mode 100644 index 000000000..f20c8eb04 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/compression.rs @@ -0,0 +1,2703 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Compression traits and definitions for Lance 2.1 +//! +//! In 2.1 the first step of encoding is structural encoding, where we shred inputs into +//! leaf arrays and take care of the validity / offsets structure. Then we pick a structural +//! encoding (mini-block or full-zip) and then we compress the data. +//! +//! This module defines the traits for the compression step. Each structural encoding has its +//! own compression strategy. +//! +//! Miniblock compression is a block based approach for small data. Since we introduce some read +//! amplification and decompress entire blocks we are able to use opaque compression. +//! +//! Fullzip compression is a per-value approach where we require that values are transparently +//! compressed so that we can locate them later. + +#[cfg(feature = "bitpacking")] +use crate::encodings::physical::bitpacking::{InlineBitpacking, OutOfLineBitpacking}; +use crate::{ + buffer::LanceBuffer, + compression_config::{BssMode, CompressionFieldParams}, + constants::{ + BSS_META_KEY, COMPRESSION_LEVEL_META_KEY, COMPRESSION_META_KEY, RLE_THRESHOLD_META_KEY, + }, + data::{DataBlock, FixedWidthDataBlock, VariableWidthBlock}, + encodings::{ + logical::primitive::{ + fullzip::PerValueCompressor, + miniblock::{MAX_MINIBLOCK_VALUES, MiniBlockCompressor}, + }, + physical::{ + binary::{ + BinaryBlockDecompressor, BinaryMiniBlockDecompressor, BinaryMiniBlockEncoder, + VariableDecoder, VariableEncoder, + }, + block::{ + CompressedBufferEncoder, CompressionConfig, CompressionScheme, + GeneralBlockDecompressor, + }, + byte_stream_split::{ + ByteStreamSplitDecompressor, ByteStreamSplitEncoder, should_use_bss, + }, + constant::ConstantDecompressor, + fsst::{ + FsstMiniBlockDecompressor, FsstMiniBlockEncoder, FsstPerValueDecompressor, + FsstPerValueEncoder, + }, + general::{GeneralMiniBlockCompressor, GeneralMiniBlockDecompressor}, + packed::{ + PackedStructFixedWidthMiniBlockDecompressor, + PackedStructFixedWidthMiniBlockEncoder, PackedStructVariablePerValueDecompressor, + PackedStructVariablePerValueEncoder, VariablePackedStructFieldDecoder, + VariablePackedStructFieldKind, + }, + rle::{ + RleChildDecompressor, RleDecompressor, RleEncoder, RunLengthWidth, + rle_encoded_size, select_run_length_width, + }, + value::{ValueDecompressor, ValueEncoder}, + }, + }, + format::{ + ProtobufUtils21, + pb21::{CompressiveEncoding, compressive_encoding::Compression}, + }, + statistics::{GetStat, Stat}, +}; + +use arrow_array::{cast::AsArray, types::UInt64Type}; +use arrow_schema::DataType; +use fsst::fsst::{FSST_LEAST_INPUT_MAX_LENGTH, FSST_LEAST_INPUT_SIZE}; +use lance_core::{Error, Result, datatypes::Field, error::LanceOptionExt}; +use std::{str::FromStr, sync::Arc}; + +/// Default threshold for RLE compression selection when the user explicitly provides a threshold. +/// +/// If no threshold is provided, we use a size model instead of a fixed run ratio. +/// This preserves existing behavior for users relying on the default, while making +/// the default selection more type-aware. +const DEFAULT_RLE_COMPRESSION_THRESHOLD: f64 = 0.5; + +// Minimum block size (32kb) to trigger general block compression +const MIN_BLOCK_SIZE_FOR_GENERAL_COMPRESSION: u64 = 32 * 1024; +const RLE_BLOCK_HEADER_BYTES: u128 = std::mem::size_of::() as u128; + +/// Trait for compression algorithms that compress an entire block of data into one opaque +/// and self-described chunk. +/// +/// This is actually a _third_ compression strategy used in a few corner cases today (TODO: remove?) +/// +/// This is the most general type of compression. There are no constraints on the method +/// of compression it is assumed that the entire block of data will be present at decompression. +/// +/// This is the least appropriate strategy for random access because we must load the entire +/// block to access any single value. This should only be used for cases where random access is never +/// required (e.g. when encoding metadata buffers like a dictionary or for encoding rep/def +/// mini-block chunks) +pub trait BlockCompressor: std::fmt::Debug + Send + Sync { + /// Compress the data into a single buffer + /// + /// Also returns a description of the compression that can be used to decompress + /// when reading the data back + fn compress(&self, data: DataBlock) -> Result; +} + +/// A trait to pick which compression to use for given data +/// +/// There are several different kinds of compression. +/// +/// - Block compression is the most generic, but most difficult to use efficiently +/// - Per-value compression results in either a fixed width data block or a variable +/// width data block. In other words, there is some number of bits per value. +/// In addition, each value should be independently decompressible. +/// - Mini-block compression results in a small block of opaque data for chunks +/// of rows. Each block is somewhere between 0 and 16KiB in size. This is +/// used for narrow data types (both fixed and variable length) where we can +/// fit many values into an 16KiB block. +pub trait CompressionStrategy: Send + Sync + std::fmt::Debug { + /// Create a block compressor for the given data + fn create_block_compressor( + &self, + field: &Field, + data: &DataBlock, + ) -> Result<(Box, CompressiveEncoding)>; + + /// Create a per-value compressor for the given data + fn create_per_value( + &self, + field: &Field, + data: &DataBlock, + ) -> Result>; + + /// Create a mini-block compressor for the given data + fn create_miniblock_compressor( + &self, + field: &Field, + data: &DataBlock, + ) -> Result>; +} + +fn try_bss_for_mini_block( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, +) -> Option> { + // BSS requires general compression to be effective + // If compression is not set or explicitly disabled, skip BSS + if params.compression.is_none() || params.compression.as_deref() == Some("none") { + return None; + } + + let mode = params.bss.unwrap_or(BssMode::Auto); + // should_use_bss already checks for supported bit widths (32/64) + if should_use_bss(data, mode) { + return Some(Box::new(ByteStreamSplitEncoder::new( + data.bits_per_value as usize, + ))); + } + None +} + +fn rle_is_applicable(data: &FixedWidthDataBlock, params: &CompressionFieldParams) -> Option { + let bits = data.bits_per_value; + if !matches!(bits, 8 | 16 | 32 | 64) { + return None; + } + + let type_size = bits / 8; + let run_count = data.expect_single_stat::(Stat::RunCount); + let threshold = params + .rle_threshold + .unwrap_or(DEFAULT_RLE_COMPRESSION_THRESHOLD); + + // If the user explicitly provided a threshold then honor it as an additional guard. + // A lower threshold makes RLE harder to trigger and can be used to avoid CPU overhead. + let passes_threshold = match params.rle_threshold { + Some(_) => (run_count as f64) < (data.num_values as f64) * threshold, + None => true, + }; + + if !passes_threshold { + return None; + } + + Some((data.num_values as u128) * (type_size as u128)) +} + +fn rle_beats_raw_and_bitpacking( + data: &FixedWidthDataBlock, + encoded_bytes: u128, + raw_bytes: u128, +) -> bool { + if encoded_bytes >= raw_bytes { + return false; + } + + #[cfg(feature = "bitpacking")] + { + if let Some(bitpack_bytes) = estimate_inline_bitpacking_bytes(data).map(u128::from) + && bitpack_bytes < encoded_bytes + { + return false; + } + } + true +} + +fn try_fixed_u8_rle_for_mini_block( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, +) -> Option> { + let raw_bytes = rle_is_applicable(data, params)?; + let rle_bytes = estimate_rle_size_for_width_from_data( + data, + Some(*MAX_MINIBLOCK_VALUES), + RunLengthWidth::U8, + ) + .ok()?; + rle_beats_raw_and_bitpacking(data, rle_bytes, raw_bytes) + .then(|| Box::new(RleEncoder::with_run_length_width(RunLengthWidth::U8)) as _) +} + +fn try_child_rle_for_mini_block( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, +) -> Option> { + let raw_bytes = rle_is_applicable(data, params)?; + let (run_length_width, estimated_bytes) = + estimate_rle_width_and_size_from_data(data, Some(*MAX_MINIBLOCK_VALUES)).ok()?; + let child_compression = rle_child_compression_config(params); + let encoder = || { + RleEncoder::with_child_encoding( + run_length_width, + child_compression, + child_compression, + true, + ) + }; + + #[cfg(feature = "bitpacking")] + let bitpack_bytes = estimate_inline_bitpacking_bytes(data).map(u128::from); + #[cfg(not(feature = "bitpacking"))] + let bitpack_bytes = None::; + + let should_measure_children = (child_compression.is_some() || cfg!(feature = "bitpacking")) + && (estimated_bytes >= raw_bytes + || bitpack_bytes.is_some_and(|bytes| bytes < estimated_bytes)); + let selected_bytes = if should_measure_children { + encoder().selected_payload_size(data).ok()? + } else { + estimated_bytes + }; + + rle_beats_raw_and_bitpacking(data, selected_bytes, raw_bytes).then(|| Box::new(encoder()) as _) +} + +fn rle_child_compression_config(params: &CompressionFieldParams) -> Option { + let raw = params.compression.as_deref()?; + if matches!(raw, "none" | "fsst") { + return None; + } + let scheme = CompressionScheme::from_str(raw).ok()?; + Some(CompressionConfig::new(scheme, params.compression_level)) +} + +fn try_rle_for_block_with_width( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, + run_length_width: RunLengthWidth, + rle_payload_bytes: u128, +) -> Result, CompressiveEncoding)>> { + let bits = data.bits_per_value; + if !matches!(bits, 8 | 16 | 32 | 64) { + return Ok(None); + } + + let run_count = data.expect_single_stat::(Stat::RunCount); + let threshold = params + .rle_threshold + .unwrap_or(DEFAULT_RLE_COMPRESSION_THRESHOLD); + + let passes_threshold = match params.rle_threshold { + Some(_) => (run_count as f64) < (data.num_values as f64) * threshold, + None => true, + }; + + if !passes_threshold { + return Ok(None); + } + + let raw_bytes = (data.num_values as u128) * ((bits / 8) as u128); + let rle_bytes = rle_payload_bytes.saturating_add(RLE_BLOCK_HEADER_BYTES); + + if rle_bytes >= raw_bytes { + return Ok(None); + } + + #[cfg(feature = "bitpacking")] + { + if let Some(bitpack_bytes) = estimate_block_bitpacking_bytes(data) + && bitpack_bytes < rle_bytes + { + return Ok(None); + } + } + + let compressor = Box::new(RleEncoder::with_run_length_width(run_length_width)); + let encoding = ProtobufUtils21::rle( + ProtobufUtils21::flat(bits, None), + ProtobufUtils21::flat(run_length_width.bits_per_value(), None), + ); + Ok(Some((compressor, encoding))) +} + +fn try_fixed_u8_rle_for_block( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, +) -> Result, CompressiveEncoding)>> { + if !matches!(data.bits_per_value, 8 | 16 | 32 | 64) { + return Ok(None); + } + let encoded_bytes = estimate_rle_size_for_width_from_data(data, None, RunLengthWidth::U8)?; + try_rle_for_block_with_width(data, params, RunLengthWidth::U8, encoded_bytes) +} + +fn try_variable_rle_for_block( + data: &FixedWidthDataBlock, + params: &CompressionFieldParams, +) -> Result, CompressiveEncoding)>> { + if !matches!(data.bits_per_value, 8 | 16 | 32 | 64) { + return Ok(None); + } + let (width, encoded_bytes) = estimate_rle_width_and_size_from_data(data, None)?; + try_rle_for_block_with_width(data, params, width, encoded_bytes) +} + +fn estimate_rle_width_and_size_from_data( + data: &FixedWidthDataBlock, + max_segment_values: Option, +) -> Result<(RunLengthWidth, u128)> { + select_run_length_width( + &data.data, + data.num_values, + data.bits_per_value, + max_segment_values, + ) +} + +fn estimate_rle_size_for_width_from_data( + data: &FixedWidthDataBlock, + max_segment_values: Option, + run_length_width: RunLengthWidth, +) -> Result { + rle_encoded_size( + &data.data, + data.num_values, + data.bits_per_value, + max_segment_values, + run_length_width, + ) +} + +fn try_bitpack_for_mini_block(_data: &FixedWidthDataBlock) -> Option> { + #[cfg(feature = "bitpacking")] + { + let bits = _data.bits_per_value; + if estimate_inline_bitpacking_bytes(_data).is_some() { + return Some(Box::new(InlineBitpacking::new(bits))); + } + None + } + #[cfg(not(feature = "bitpacking"))] + { + None + } +} + +#[cfg(feature = "bitpacking")] +fn estimate_inline_bitpacking_bytes(data: &FixedWidthDataBlock) -> Option { + use arrow_array::cast::AsArray; + + let bits = data.bits_per_value; + if !matches!(bits, 8 | 16 | 32 | 64) { + return None; + } + if data.num_values == 0 { + return None; + } + + let bit_widths = data.expect_stat(Stat::BitWidth); + let widths = bit_widths.as_primitive::(); + + let words_per_chunk: u128 = 1; + let word_bytes: u128 = (bits / 8) as u128; + let mut total_words: u128 = 0; + for i in 0..widths.len() { + let bit_width = widths.value(i) as u128; + let packed_words = (1024u128 * bit_width) / (bits as u128); + total_words = total_words.saturating_add(words_per_chunk.saturating_add(packed_words)); + } + + let estimated_bytes = total_words.saturating_mul(word_bytes); + let raw_bytes = data.data_size() as u128; + + if estimated_bytes >= raw_bytes { + return None; + } + + u64::try_from(estimated_bytes).ok() +} + +fn try_bitpack_for_block( + data: &FixedWidthDataBlock, +) -> Option<(Box, CompressiveEncoding)> { + let bits = data.bits_per_value; + if !matches!(bits, 8 | 16 | 32 | 64) { + return None; + } + + let bit_widths = data.expect_stat(Stat::BitWidth); + let widths = bit_widths.as_primitive::(); + let max_bit_width = *widths.values().iter().max().unwrap(); + + let too_small = + widths.len() == 1 && InlineBitpacking::min_size_bytes(widths.value(0)) >= data.data_size(); + + if too_small { + return None; + } + + if data.num_values <= 1024 { + let compressor = Box::new(InlineBitpacking::new(bits)); + let encoding = ProtobufUtils21::inline_bitpacking(bits, None); + Some((compressor, encoding)) + } else { + let compressor = Box::new(OutOfLineBitpacking::new(max_bit_width, bits)); + let encoding = ProtobufUtils21::out_of_line_bitpacking( + bits, + ProtobufUtils21::flat(max_bit_width, None), + ); + Some((compressor, encoding)) + } +} + +#[cfg(feature = "bitpacking")] +fn estimate_block_bitpacking_bytes(data: &FixedWidthDataBlock) -> Option { + let bits = data.bits_per_value; + if !matches!(bits, 8 | 16 | 32 | 64) || data.num_values == 0 { + return None; + } + + let bit_widths = data.expect_stat(Stat::BitWidth); + let widths = bit_widths.as_primitive::(); + let max_bit_width = *widths.values().iter().max()?; + let word_bytes = (bits / 8) as u128; + + let bitpacked_words = if data.num_values <= 1024 { + 1 + (1024u128 * (max_bit_width as u128)) / (bits as u128) + } else { + estimate_out_of_line_bitpacking_words(data.num_values, max_bit_width, bits)? + }; + let bitpacked_bytes = bitpacked_words.saturating_mul(word_bytes); + if bitpacked_bytes >= data.data_size() as u128 { + return None; + } + + Some(bitpacked_bytes) +} + +#[cfg(feature = "bitpacking")] +fn estimate_out_of_line_bitpacking_words( + num_values: u64, + compressed_bits_per_value: u64, + bits_per_value: u64, +) -> Option { + let num_values = usize::try_from(num_values).ok()?; + let compressed_bits_per_value = usize::try_from(compressed_bits_per_value).ok()?; + let bits_per_value = usize::try_from(bits_per_value).ok()?; + if compressed_bits_per_value >= bits_per_value { + return None; + } + + let elems_per_chunk = 1024usize; + let num_chunks = num_values.div_ceil(elems_per_chunk); + let words_per_chunk = (elems_per_chunk * compressed_bits_per_value).div_ceil(bits_per_value); + let last_chunk_is_runt = !num_values.is_multiple_of(elems_per_chunk); + + if !last_chunk_is_runt { + return Some((num_chunks * words_per_chunk) as u128); + } + + let num_whole_chunks = num_chunks - 1; + let remaining_items = num_values - num_whole_chunks * elems_per_chunk; + let tail_bit_savings = bits_per_value - compressed_bits_per_value; + let padding_cost = compressed_bits_per_value * (elems_per_chunk - remaining_items); + let tail_pack_savings = tail_bit_savings * remaining_items; + let tail_words = if padding_cost < tail_pack_savings { + words_per_chunk + } else { + remaining_items + }; + + Some((num_whole_chunks * words_per_chunk + tail_words) as u128) +} + +fn maybe_wrap_general_for_mini_block( + inner: Box, + params: &CompressionFieldParams, +) -> Result> { + match params.compression.as_deref() { + None | Some("none") | Some("fsst") => Ok(inner), + Some(raw) => { + let scheme = CompressionScheme::from_str(raw) + .map_err(|_| Error::invalid_input(format!("Unknown compression scheme: {raw}")))?; + let cfg = CompressionConfig::new(scheme, params.compression_level); + Ok(Box::new(GeneralMiniBlockCompressor::new(inner, cfg))) + } + } +} + +fn try_general_compression( + field_params: &CompressionFieldParams, + data: &DataBlock, +) -> Result, CompressionConfig)>> { + // Explicitly disable general compression. + if field_params.compression.as_deref() == Some("none") { + return Ok(None); + } + + // User-requested compression (unused today but perhaps still used + // in the future someday) + if let Some(compression_scheme) = &field_params.compression { + let scheme: CompressionScheme = compression_scheme.parse()?; + let config = CompressionConfig::new(scheme, field_params.compression_level); + let compressor = Box::new(CompressedBufferEncoder::try_new(config)?); + return Ok(Some((compressor, config))); + } + + // Automatic compression for large blocks + if data.data_size() > MIN_BLOCK_SIZE_FOR_GENERAL_COMPRESSION { + let compressor = Box::new(CompressedBufferEncoder::default()); + let config = compressor.compressor.config(); + return Ok(Some((compressor, config))); + } + + Ok(None) +} + +/// Parse field-level compression metadata without applying format-specific constraints. +pub fn field_metadata_params(field: &Field) -> CompressionFieldParams { + let mut params = CompressionFieldParams::default(); + + if let Some(compression) = field.metadata.get(COMPRESSION_META_KEY) { + params.compression = Some(compression.clone()); + } + if let Some(level) = field.metadata.get(COMPRESSION_LEVEL_META_KEY) { + params.compression_level = level.parse().ok(); + } + if let Some(threshold) = field.metadata.get(RLE_THRESHOLD_META_KEY) { + params.rle_threshold = threshold.parse().ok(); + } + if let Some(bss_str) = field.metadata.get(BSS_META_KEY) { + match BssMode::parse(bss_str) { + Some(mode) => params.bss = Some(mode), + None => log::warn!("Invalid BSS mode '{}', using default", bss_str), + } + } + if let Some(minichunk_size_str) = field + .metadata + .get(super::constants::MINICHUNK_SIZE_META_KEY) + { + if let Ok(minichunk_size) = minichunk_size_str.parse::() { + params.minichunk_size = Some(minichunk_size); + } else { + log::warn!("Invalid minichunk_size '{}', skipping", minichunk_size_str); + } + } + + params +} + +/// Apply general-purpose compression requested for a fixed-width miniblock. +pub fn finalize_miniblock_compressor( + data: &DataBlock, + compressor: Box, + params: &CompressionFieldParams, +) -> Result> { + if matches!(data, DataBlock::FixedWidth(_)) { + maybe_wrap_general_for_mini_block(compressor, params) + } else { + Ok(compressor) + } +} + +/// Honor an explicit `compression = none` request for fixed-width miniblocks. +pub fn try_uncompressed_fixed_width_miniblock( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Option> { + (matches!(data, DataBlock::FixedWidth(_)) && params.compression.as_deref() == Some("none")) + .then(|| Box::new(ValueEncoder::default()) as _) +} + +/// Select byte-stream-split compression for an applicable fixed-width miniblock. +pub fn try_byte_stream_split_miniblock( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Option> { + let DataBlock::FixedWidth(data) = data else { + return None; + }; + try_bss_for_mini_block(data, params) +} + +/// Select the original fixed-u8 RLE miniblock grammar. +pub fn try_fixed_u8_rle_miniblock( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Option> { + let DataBlock::FixedWidth(data) = data else { + return None; + }; + try_fixed_u8_rle_for_mini_block(data, params) +} + +/// Select variable-width RLE with independently encoded children. +pub fn try_child_rle_miniblock( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Option> { + let DataBlock::FixedWidth(data) = data else { + return None; + }; + try_child_rle_for_mini_block(data, params) +} + +/// Select inline bitpacking for applicable fixed-width miniblocks. +pub fn try_bitpacking_miniblock(data: &DataBlock) -> Option> { + let DataBlock::FixedWidth(data) = data else { + return None; + }; + try_bitpack_for_mini_block(data) +} + +/// Store fixed-width miniblock values without a value codec. +pub fn try_raw_fixed_width_miniblock(data: &DataBlock) -> Option> { + matches!(data, DataBlock::FixedWidth(_)).then(|| Box::new(ValueEncoder::default()) as _) +} + +/// Encode variable-width miniblocks with binary or FSST encoding. +pub fn try_variable_width_miniblock( + field: &Field, + data: &DataBlock, + params: &CompressionFieldParams, +) -> Result>> { + let DataBlock::VariableWidth(data) = data else { + return Ok(None); + }; + if data.bits_per_offset != 32 && data.bits_per_offset != 64 { + return Err(Error::invalid_input(format!( + "Variable width compression not supported for {} bit offsets", + data.bits_per_offset + ))); + } + + let compression = params.compression.as_deref(); + let data_size = data.expect_single_stat::(Stat::DataSize); + let max_len = data.expect_single_stat::(Stat::MaxLength); + if compression == Some("none") { + return Ok(Some(Box::new(BinaryMiniBlockEncoder::new( + params.minichunk_size, + )))); + } + + let use_fsst = compression == Some("fsst") + || (compression.is_none() + && !matches!(field.data_type(), DataType::Binary | DataType::LargeBinary) + && max_len >= FSST_LEAST_INPUT_MAX_LENGTH + && data_size >= FSST_LEAST_INPUT_SIZE as u64); + let mut encoder: Box = if use_fsst { + Box::new(FsstMiniBlockEncoder::new(params.minichunk_size)) + } else { + Box::new(BinaryMiniBlockEncoder::new(params.minichunk_size)) + }; + if let Some(compression_scheme) = compression.filter(|scheme| *scheme != "fsst") { + let scheme: CompressionScheme = compression_scheme.parse()?; + let config = CompressionConfig::new(scheme, params.compression_level); + encoder = Box::new(GeneralMiniBlockCompressor::new(encoder, config)); + } + Ok(Some(encoder)) +} + +/// Encode fixed-width packed structs as miniblocks. +pub fn try_fixed_packed_struct_miniblock( + data: &DataBlock, +) -> Result>> { + let DataBlock::Struct(data) = data else { + return Ok(None); + }; + if data.has_variable_width_child() { + return Err(Error::invalid_input( + "Packed struct mini-block encoding supports only fixed-width children", + )); + } + Ok(Some(Box::new( + PackedStructFixedWidthMiniBlockEncoder::default(), + ))) +} + +/// Store fixed-size-list miniblocks without a value codec. +pub fn try_raw_fixed_size_list_miniblock(data: &DataBlock) -> Option> { + matches!(data, DataBlock::FixedSizeList(_)).then(|| Box::new(ValueEncoder::default()) as _) +} + +/// Store fixed-width and fixed-size-list values directly in full-zip pages. +pub fn try_raw_per_value(data: &DataBlock) -> Option> { + matches!(data, DataBlock::FixedWidth(_) | DataBlock::FixedSizeList(_)) + .then(|| Box::new(ValueEncoder::default()) as _) +} + +fn validate_packed_struct(field: &Field, data: &DataBlock) -> Result> { + let DataBlock::Struct(data) = data else { + return Ok(None); + }; + if field.children.len() != data.children.len() { + return Err(Error::invalid_input( + "Struct field metadata does not match data block children", + )); + } + Ok(Some(data.has_variable_width_child())) +} + +/// Reject variable-width packed structs while preserving the fixed-width error. +pub fn reject_packed_struct_per_value( + field: &Field, + data: &DataBlock, +) -> Result>> { + let Some(has_variable_child) = validate_packed_struct(field, data)? else { + return Ok(None); + }; + if has_variable_child { + return Err(Error::not_supported_source( + "Variable packed struct encoding is not enabled by the selected file format".into(), + )); + } + Err(Error::invalid_input( + "Packed struct per-value compression should not be used for fixed-width-only structs", + )) +} + +/// Encode variable-width packed structs with the exact strategy recursively. +pub fn try_variable_packed_struct_per_value( + strategy: Arc, + field: &Field, + data: &DataBlock, +) -> Result>> { + let Some(has_variable_child) = validate_packed_struct(field, data)? else { + return Ok(None); + }; + if !has_variable_child { + return Err(Error::invalid_input( + "Packed struct per-value compression should not be used for fixed-width-only structs", + )); + } + Ok(Some(Box::new(PackedStructVariablePerValueEncoder::new( + strategy, + field.children.clone(), + )))) +} + +/// Encode variable-width values directly, with FSST or per-value compression +/// when applicable. +pub fn try_variable_width_per_value( + field: &Field, + data: &DataBlock, + params: &CompressionFieldParams, +) -> Result>> { + let DataBlock::VariableWidth(data) = data else { + return Ok(None); + }; + let compression = params.compression.as_deref(); + if compression == Some("none") { + return Ok(Some(Box::new(VariableEncoder::default()))); + } + + let max_len = data.expect_single_stat::(Stat::MaxLength); + let data_size = data.expect_single_stat::(Stat::DataSize); + let per_value_requested = compression.is_some_and(|compression| compression != "fsst"); + if (max_len > 32 * 1024 || per_value_requested) && data_size >= FSST_LEAST_INPUT_SIZE as u64 { + if compression == Some("zstd") { + let config = CompressionConfig::new(CompressionScheme::Zstd, params.compression_level); + return Ok(Some(Box::new(CompressedBufferEncoder::try_new(config)?))); + } + return Ok(Some(Box::new(CompressedBufferEncoder::default()))); + } + + if data.bits_per_offset != 32 && data.bits_per_offset != 64 { + return Err(Error::invalid_input(format!( + "Per-value compression does not support variable-width data with {}-bit offsets", + data.bits_per_offset + ))); + } + let encoder = Box::new(VariableEncoder::default()); + let use_fsst = compression == Some("fsst") + || (compression.is_none() + && !matches!(field.data_type(), DataType::Binary | DataType::LargeBinary) + && max_len >= FSST_LEAST_INPUT_MAX_LENGTH + && data_size >= FSST_LEAST_INPUT_SIZE as u64); + Ok(Some(if use_fsst { + Box::new(FsstPerValueEncoder::new(encoder)) + } else { + encoder + })) +} + +/// Select fixed-u8 RLE for block compression. +pub fn try_fixed_u8_rle_block( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Result, CompressiveEncoding)>> { + let DataBlock::FixedWidth(data) = data else { + return Ok(None); + }; + try_fixed_u8_rle_for_block(data, params) +} + +/// Select variable-width RLE for block compression. +pub fn try_variable_rle_block( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Result, CompressiveEncoding)>> { + let DataBlock::FixedWidth(data) = data else { + return Ok(None); + }; + try_variable_rle_for_block(data, params) +} + +/// Select block bitpacking for applicable fixed-width values. +pub fn try_bitpacking_block( + data: &DataBlock, +) -> Option<(Box, CompressiveEncoding)> { + let DataBlock::FixedWidth(data) = data else { + return None; + }; + try_bitpack_for_block(data) +} + +/// Select explicitly requested or automatic general-purpose block compression. +pub fn try_general_block( + data: &DataBlock, + params: &CompressionFieldParams, +) -> Result, CompressiveEncoding)>> { + let Some((compressor, config)) = try_general_compression(params, data)? else { + return Ok(None); + }; + let inner = match data { + DataBlock::FixedWidth(data) => ProtobufUtils21::flat(data.bits_per_value, None), + DataBlock::VariableWidth(data) => ProtobufUtils21::variable( + ProtobufUtils21::flat(data.bits_per_offset as u64, None), + None, + ), + _ => return Ok(None), + }; + Ok(Some((compressor, ProtobufUtils21::wrapped(config, inner)?))) +} + +/// Store fixed- and variable-width block values without block compression. +pub fn try_raw_block(data: &DataBlock) -> Option<(Box, CompressiveEncoding)> { + match data { + DataBlock::FixedWidth(data) => Some(( + Box::new(ValueEncoder::default()) as Box, + ProtobufUtils21::flat(data.bits_per_value, None), + )), + DataBlock::VariableWidth(data) => Some(( + Box::new(VariableEncoder::default()) as Box, + ProtobufUtils21::variable( + ProtobufUtils21::flat(data.bits_per_offset as u64, None), + None, + ), + )), + _ => None, + } +} + +pub trait MiniBlockDecompressor: std::fmt::Debug + Send + Sync { + fn decompress(&self, data: Vec, num_values: u64) -> Result; + + /// Returns the exact aggregate decoded size when it is determined solely by the value count. + /// + /// Implementations should only return `Some` when this aggregate estimate can be used by + /// [`DataBlockBuilder`](crate::data::DataBlockBuilder) to preallocate the decoded output + /// exactly. Outputs with multiple buffers or whose layout-dependent allocation cannot be + /// represented by one aggregate estimate should return `None`. + fn decoded_size_bytes(&self, _num_values: u64) -> Option { + None + } +} + +pub trait FixedPerValueDecompressor: std::fmt::Debug + Send + Sync { + /// Decompress one or more values + fn decompress(&self, data: FixedWidthDataBlock, num_values: u64) -> Result; + /// The number of bits in each value + /// + /// Currently (and probably long term) this must be a multiple of 8 + fn bits_per_value(&self) -> u64; + + /// Returns the exact aggregate decoded size when it is determined solely by the value count. + /// + /// Implementations should only return `Some` when this aggregate estimate can be used by + /// [`DataBlockBuilder`](crate::data::DataBlockBuilder) to preallocate the decoded output + /// exactly. Outputs with multiple buffers or whose layout-dependent allocation cannot be + /// represented by one aggregate estimate should return `None`. + fn decoded_size_bytes(&self, _num_values: u64) -> Option { + None + } +} + +pub trait VariablePerValueDecompressor: std::fmt::Debug + Send + Sync { + /// Decompress one or more values + fn decompress(&self, data: VariableWidthBlock) -> Result; +} + +pub trait BlockDecompressor: std::fmt::Debug + Send + Sync { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result; +} + +pub trait DecompressionStrategy: std::fmt::Debug + Send + Sync { + fn create_miniblock_decompressor( + &self, + description: &CompressiveEncoding, + decompression_strategy: &dyn DecompressionStrategy, + ) -> Result>; + + fn create_fixed_per_value_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result>; + + fn create_variable_per_value_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result>; + + fn create_block_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result>; +} + +#[derive(Debug, Default)] +pub struct DefaultDecompressionStrategy {} + +impl DecompressionStrategy for DefaultDecompressionStrategy { + fn create_miniblock_decompressor( + &self, + description: &CompressiveEncoding, + decompression_strategy: &dyn DecompressionStrategy, + ) -> Result> { + match description.compression.as_ref().unwrap() { + Compression::Flat(flat) => Ok(Box::new(ValueDecompressor::from_flat(flat))), + #[cfg(feature = "bitpacking")] + Compression::InlineBitpacking(description) => { + Ok(Box::new(InlineBitpacking::from_description(description))) + } + #[cfg(not(feature = "bitpacking"))] + Compression::InlineBitpacking(_) => Err(Error::not_supported_source( + "this runtime was not built with bitpacking support".into(), + )), + Compression::Variable(variable) => { + let Compression::Flat(offsets) = variable + .offsets + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + else { + panic!("Variable compression only supports flat offsets") + }; + Ok(Box::new(BinaryMiniBlockDecompressor::new( + offsets.bits_per_value as u8, + ))) + } + Compression::Fsst(description) => { + let inner_decompressor = decompression_strategy.create_miniblock_decompressor( + description.values.as_ref().unwrap(), + decompression_strategy, + )?; + Ok(Box::new(FsstMiniBlockDecompressor::new( + description, + inner_decompressor, + ))) + } + Compression::PackedStruct(description) => Ok(Box::new( + PackedStructFixedWidthMiniBlockDecompressor::new(description), + )), + Compression::VariablePackedStruct(_) => Err(Error::not_supported_source( + "variable packed struct decoding is not yet implemented".into(), + )), + Compression::FixedSizeList(fsl) => { + // In the future, we might need to do something more complex here if FSL supports + // compression. + Ok(Box::new(ValueDecompressor::from_fsl(fsl))) + } + Compression::Rle(rle) => Ok(Box::new(create_rle_decompressor( + rle, + decompression_strategy, + )?)), + Compression::ByteStreamSplit(bss) => { + let Compression::Flat(values) = + bss.values.as_ref().unwrap().compression.as_ref().unwrap() + else { + panic!("ByteStreamSplit compression only supports flat values") + }; + Ok(Box::new(ByteStreamSplitDecompressor::new( + values.bits_per_value as usize, + ))) + } + Compression::General(general) => { + // Create inner decompressor + let inner_decompressor = self.create_miniblock_decompressor( + general.values.as_ref().ok_or_else(|| { + Error::invalid_input("GeneralMiniBlock missing inner encoding") + })?, + decompression_strategy, + )?; + + // Parse compression config + let compression = general.compression.as_ref().ok_or_else(|| { + Error::invalid_input("GeneralMiniBlock missing compression config") + })?; + + let scheme = compression.scheme().try_into()?; + + let compression_config = CompressionConfig::new(scheme, compression.level); + + Ok(Box::new(GeneralMiniBlockDecompressor::new( + inner_decompressor, + compression_config, + ))) + } + _ => todo!(), + } + } + + fn create_fixed_per_value_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result> { + match description.compression.as_ref().unwrap() { + Compression::Constant(constant) => Ok(Box::new(ConstantDecompressor::new( + constant + .value + .as_ref() + .map(|v| LanceBuffer::from_bytes(v.clone(), 1)), + ))), + Compression::Flat(flat) => Ok(Box::new(ValueDecompressor::from_flat(flat))), + Compression::FixedSizeList(fsl) => Ok(Box::new(ValueDecompressor::from_fsl(fsl))), + _ => todo!("fixed-per-value decompressor for {:?}", description), + } + } + + fn create_variable_per_value_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result> { + match description.compression.as_ref().unwrap() { + Compression::Variable(variable) => { + let Compression::Flat(offsets) = variable + .offsets + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + else { + panic!("Variable compression only supports flat offsets") + }; + assert!(offsets.bits_per_value < u8::MAX as u64); + Ok(Box::new(VariableDecoder::default())) + } + Compression::Fsst(fsst) => Ok(Box::new(FsstPerValueDecompressor::new( + LanceBuffer::from_bytes(fsst.symbol_table.clone(), 1), + Box::new(VariableDecoder::default()), + ))), + Compression::General(general) => Ok(Box::new(CompressedBufferEncoder::from_scheme( + general.compression.as_ref().expect_ok()?.scheme(), + )?)), + Compression::VariablePackedStruct(description) => { + let mut fields = Vec::with_capacity(description.fields.len()); + for field in &description.fields { + let value_encoding = field.value.as_ref().ok_or_else(|| { + Error::invalid_input("VariablePackedStruct field is missing value encoding") + })?; + let decoder = match field.layout.as_ref().ok_or_else(|| { + Error::invalid_input("VariablePackedStruct field is missing layout details") + })? { + crate::format::pb21::variable_packed_struct::field_encoding::Layout::BitsPerValue( + bits_per_value, + ) => { + let decompressor = + self.create_fixed_per_value_decompressor(value_encoding)?; + VariablePackedStructFieldDecoder { + kind: VariablePackedStructFieldKind::Fixed { + bits_per_value: *bits_per_value, + decompressor: Arc::from(decompressor), + }, + } + } + crate::format::pb21::variable_packed_struct::field_encoding::Layout::BitsPerLength( + bits_per_length, + ) => { + let decompressor = + self.create_variable_per_value_decompressor(value_encoding)?; + VariablePackedStructFieldDecoder { + kind: VariablePackedStructFieldKind::Variable { + bits_per_length: *bits_per_length, + decompressor: Arc::from(decompressor), + }, + } + } + }; + fields.push(decoder); + } + Ok(Box::new(PackedStructVariablePerValueDecompressor::new( + fields, + ))) + } + _ => todo!("variable-per-value decompressor for {:?}", description), + } + } + + fn create_block_decompressor( + &self, + description: &CompressiveEncoding, + ) -> Result> { + match description.compression.as_ref().unwrap() { + Compression::InlineBitpacking(inline_bitpacking) => Ok(Box::new( + InlineBitpacking::from_description(inline_bitpacking), + )), + Compression::Flat(flat) => Ok(Box::new(ValueDecompressor::from_flat(flat))), + Compression::Constant(constant) => { + let scalar = constant + .value + .as_ref() + .map(|v| LanceBuffer::from_bytes(v.clone(), 1)); + Ok(Box::new(ConstantDecompressor::new(scalar))) + } + Compression::Variable(_) => Ok(Box::new(BinaryBlockDecompressor::default())), + Compression::FixedSizeList(fsl) => { + Ok(Box::new(ValueDecompressor::from_fsl(fsl.as_ref()))) + } + Compression::OutOfLineBitpacking(out_of_line) => { + // Extract the compressed bit width from the values encoding + let compressed_bit_width = match out_of_line + .values + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + { + Compression::Flat(flat) => flat.bits_per_value, + _ => { + return Err(Error::invalid_input_source( + "OutOfLineBitpacking values must use Flat encoding".into(), + )); + } + }; + Ok(Box::new(OutOfLineBitpacking::new( + compressed_bit_width, + out_of_line.uncompressed_bits_per_value, + ))) + } + Compression::General(general) => { + let inner_desc = general + .values + .as_ref() + .ok_or_else(|| { + Error::invalid_input("General compression missing inner encoding") + })? + .as_ref(); + let inner_decompressor = self.create_block_decompressor(inner_desc)?; + + let compression = general.compression.as_ref().ok_or_else(|| { + Error::invalid_input("General compression missing compression config") + })?; + let scheme = compression.scheme().try_into()?; + let config = CompressionConfig::new(scheme, compression.level); + let general_decompressor = + GeneralBlockDecompressor::try_new(inner_decompressor, config)?; + + Ok(Box::new(general_decompressor)) + } + Compression::Rle(rle) => Ok(Box::new(create_rle_decompressor(rle, self)?)), + _ => todo!(), + } + } +} +pub(crate) fn create_rle_decompressor( + rle: &crate::format::pb21::Rle, + decompression_strategy: &dyn DecompressionStrategy, +) -> Result { + let values = rle + .values + .as_ref() + .ok_or_else(|| Error::invalid_input("RLE compression missing values encoding"))?; + let run_lengths = rle + .run_lengths + .as_ref() + .ok_or_else(|| Error::invalid_input("RLE compression missing run lengths encoding"))?; + + let values = create_rle_child_decompressor(values, "values", decompression_strategy)?; + let run_lengths = + create_rle_child_decompressor(run_lengths, "run lengths", decompression_strategy)?; + + if !matches!(values.bits_per_value(), 8 | 16 | 32 | 64) { + return Err(Error::invalid_input(format!( + "RLE compression only supports 8, 16, 32, or 64-bit values, got {}", + values.bits_per_value() + ))); + } + + let run_length_width = + RunLengthWidth::from_bits(run_lengths.bits_per_value()).ok_or_else(|| { + Error::invalid_input(format!( + "RLE compression only supports 8, 16, or 32-bit run lengths, got {}", + run_lengths.bits_per_value() + )) + })?; + + if values.requires_num_values() && run_lengths.requires_num_values() { + return Err(Error::invalid_input( + "RLE values and run lengths child encodings cannot both require the run count", + )); + } + + if values.is_identity() && run_lengths.is_identity() { + return Ok(RleDecompressor::with_run_length_width( + values.bits_per_value(), + run_length_width, + )); + } + + Ok(RleDecompressor::with_child_decompressors( + values.bits_per_value(), + run_length_width, + values, + run_lengths, + )) +} + +fn create_rle_child_decompressor( + encoding: &CompressiveEncoding, + role: &str, + decompression_strategy: &dyn DecompressionStrategy, +) -> Result { + let compression = encoding + .compression + .as_ref() + .ok_or_else(|| Error::invalid_input(format!("RLE {role} missing child compression")))?; + let (bits_per_value, requires_num_values, needs_decompressor) = + validate_rle_child_compression(compression, role)?; + + if needs_decompressor { + Ok(RleChildDecompressor::block( + bits_per_value, + decompression_strategy.create_block_decompressor(encoding)?, + requires_num_values, + )) + } else { + Ok(RleChildDecompressor::flat(bits_per_value)) + } +} + +fn validate_rle_child_compression( + compression: &Compression, + role: &str, +) -> Result<(u64, bool, bool)> { + match compression { + Compression::Flat(flat) => Ok((flat.bits_per_value, false, false)), + Compression::General(general) => { + general.compression.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} general child missing compression config" + )) + })?; + let values = general.values.as_ref().ok_or_else(|| { + Error::invalid_input(format!("RLE {role} general child missing inner encoding")) + })?; + let inner = values.compression.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} general child missing inner compression" + )) + })?; + let (bits_per_value, requires_num_values) = + validate_rle_block_child_inner(inner, role)?; + Ok((bits_per_value, requires_num_values, true)) + } + Compression::OutOfLineBitpacking(out_of_line) => { + let values = out_of_line.values.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} bitpacking child missing values encoding" + )) + })?; + let Compression::Flat(_) = values.compression.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} bitpacking child missing values compression" + )) + })? + else { + return Err(Error::invalid_input(format!( + "RLE {role} bitpacking child only supports flat values" + ))); + }; + Ok((out_of_line.uncompressed_bits_per_value, true, true)) + } + other => Err(Error::invalid_input(format!( + "RLE {role} only supports flat, general, or out-of-line bitpacking child encodings, got {}", + compression_name(other) + ))), + } +} + +fn validate_rle_block_child_inner(compression: &Compression, role: &str) -> Result<(u64, bool)> { + match compression { + Compression::Flat(flat) => Ok((flat.bits_per_value, false)), + Compression::OutOfLineBitpacking(out_of_line) => { + let values = out_of_line.values.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} bitpacking child missing values encoding" + )) + })?; + let Compression::Flat(_) = values.compression.as_ref().ok_or_else(|| { + Error::invalid_input(format!( + "RLE {role} bitpacking child missing values compression" + )) + })? + else { + return Err(Error::invalid_input(format!( + "RLE {role} bitpacking child only supports flat values" + ))); + }; + Ok((out_of_line.uncompressed_bits_per_value, true)) + } + other => Err(Error::invalid_input(format!( + "RLE {role} general child only supports flat or out-of-line bitpacking inner encodings, got {}", + compression_name(other) + ))), + } +} + +fn compression_name(compression: &Compression) -> &'static str { + match compression { + Compression::Flat(_) => "flat", + Compression::Variable(_) => "variable", + Compression::Fsst(_) => "fsst", + Compression::OutOfLineBitpacking(_) => "out-of-line bitpacking", + Compression::InlineBitpacking(_) => "inline bitpacking", + Compression::General(_) => "general", + Compression::Constant(_) => "constant", + Compression::Dictionary(_) => "dictionary", + Compression::ByteStreamSplit(_) => "byte stream split", + Compression::PackedStruct(_) => "packed struct", + Compression::FixedSizeList(_) => "fixed-size list", + Compression::VariablePackedStruct(_) => "variable packed struct", + Compression::Rle(_) => "rle", + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::buffer::LanceBuffer; + use crate::compression_config::CompressionParams; + use crate::data::{BlockInfo, DataBlock, FixedWidthDataBlock}; + use crate::encodings::logical::primitive::miniblock::MiniBlockCompressionContext; + use crate::statistics::ComputeStat; + use crate::testing::{TestEncoding, extract_array_encoding_chain, test_compression_strategy}; + use arrow_schema::{DataType, Field as ArrowField}; + use std::collections::HashMap; + + fn strategy(encoding: TestEncoding, params: CompressionParams) -> Arc { + test_compression_strategy(encoding, params) + } + + fn baseline_strategy(params: CompressionParams) -> Arc { + strategy(TestEncoding::StructuralU16, params) + } + + fn miniblock_context() -> MiniBlockCompressionContext { + MiniBlockCompressionContext::new(0, true, true) + } + + fn create_test_field(name: &str, data_type: DataType) -> Field { + let arrow_field = ArrowField::new(name, data_type, true); + let mut field = Field::try_from(&arrow_field).unwrap(); + field.id = -1; + field + } + + fn create_fixed_width_block_with_stats( + bits_per_value: u64, + num_values: u64, + run_count: u64, + ) -> DataBlock { + // Create varied data to avoid low entropy + let bytes_per_value = (bits_per_value / 8) as usize; + let total_bytes = bytes_per_value * num_values as usize; + let mut data = vec![0u8; total_bytes]; + + // Create data with specified run count + let values_per_run = (num_values / run_count).max(1); + let mut run_value = 0u8; + + for i in 0..num_values as usize { + if i % values_per_run as usize == 0 { + run_value = run_value.wrapping_add(17); // Use prime to get varied values + } + // Fill all bytes of the value to create high entropy + for j in 0..bytes_per_value { + let byte_offset = i * bytes_per_value + j; + if byte_offset < data.len() { + data[byte_offset] = run_value.wrapping_add(j as u8); + } + } + } + + let mut block = FixedWidthDataBlock { + bits_per_value, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }; + + // Compute all statistics including BytePositionEntropy + use crate::statistics::ComputeStat; + block.compute_stat(); + + DataBlock::FixedWidth(block) + } + + fn create_fixed_width_block(bits_per_value: u64, num_values: u64) -> DataBlock { + // Create data with some variety to avoid always triggering BSS + let bytes_per_value = (bits_per_value / 8) as usize; + let total_bytes = bytes_per_value * num_values as usize; + let mut data = vec![0u8; total_bytes]; + + // Add some variation to the data to make it more realistic + for i in 0..num_values as usize { + let byte_offset = i * bytes_per_value; + if byte_offset < data.len() { + data[byte_offset] = (i % 256) as u8; + } + } + + let mut block = FixedWidthDataBlock { + bits_per_value, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }; + + // Compute all statistics including BytePositionEntropy + use crate::statistics::ComputeStat; + block.compute_stat(); + + DataBlock::FixedWidth(block) + } + + fn rle_run_length_bits(encoding: &CompressiveEncoding) -> u64 { + let Compression::Rle(rle) = encoding.compression.as_ref().unwrap() else { + panic!("expected RLE encoding"); + }; + let Compression::Flat(run_lengths) = rle + .run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + else { + panic!("expected flat run lengths"); + }; + run_lengths.bits_per_value + } + + fn expect_rle_encoding(encoding: &CompressiveEncoding) -> &crate::format::pb21::Rle { + match encoding.compression.as_ref().unwrap() { + Compression::Rle(rle) => rle, + Compression::General(general) => { + let inner = general.values.as_ref().unwrap(); + let Compression::Rle(rle) = inner.compression.as_ref().unwrap() else { + panic!("expected wrapped RLE encoding"); + }; + rle + } + other => panic!("expected RLE encoding, got {}", compression_name(other)), + } + } + + fn create_variable_width_block( + bits_per_offset: u8, + num_values: u64, + avg_value_size: usize, + ) -> DataBlock { + use crate::statistics::ComputeStat; + + // Create offsets buffer (num_values + 1 offsets) + let mut offsets = Vec::with_capacity((num_values + 1) as usize); + let mut current_offset = 0i64; + offsets.push(current_offset); + + // Generate offsets with varying value sizes + for i in 0..num_values { + let value_size = if avg_value_size == 0 { + 1 + } else { + ((avg_value_size as i64 + (i as i64 % 8) - 4).max(1) as usize) + .min(avg_value_size * 2) + }; + current_offset += value_size as i64; + offsets.push(current_offset); + } + + // Create data buffer with realistic content + let total_data_size = current_offset as usize; + let mut data = vec![0u8; total_data_size]; + + // Fill data with varied content + for i in 0..num_values { + let start_offset = offsets[i as usize] as usize; + let end_offset = offsets[(i + 1) as usize] as usize; + + let content = (i % 256) as u8; + for j in 0..end_offset - start_offset { + data[start_offset + j] = content.wrapping_add(j as u8); + } + } + + // Convert offsets to appropriate lance buffer + let offsets_buffer = match bits_per_offset { + 32 => { + let offsets_32: Vec = offsets.iter().map(|&o| o as i32).collect(); + LanceBuffer::reinterpret_vec(offsets_32) + } + 64 => LanceBuffer::reinterpret_vec(offsets), + _ => panic!("Unsupported bits_per_offset: {}", bits_per_offset), + }; + + let mut block = VariableWidthBlock { + data: LanceBuffer::from(data), + offsets: offsets_buffer, + bits_per_offset, + num_values, + block_info: BlockInfo::default(), + }; + + block.compute_stat(); + DataBlock::VariableWidth(block) + } + + fn create_fsst_candidate_variable_width_block() -> DataBlock { + create_variable_width_block(32, 4096, FSST_LEAST_INPUT_MAX_LENGTH as usize + 16) + } + + #[test] + fn test_parameter_based_compression() { + let mut params = CompressionParams::new(); + + // Configure RLE for ID columns with BSS explicitly disabled + params.columns.insert( + "*_id".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.3), + compression: Some("lz4".to_string()), + compression_level: None, + bss: Some(BssMode::Off), // Explicitly disable BSS to test RLE + minichunk_size: None, + }, + ); + + let strategy = baseline_strategy(params); + let field = create_test_field("user_id", DataType::Int32); + + // Create data with low run count for RLE + // Use create_fixed_width_block_with_stats which properly sets run count + let data = create_fixed_width_block_with_stats(32, 1000, 100); // 100 runs out of 1000 values + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + // Should use RLE due to low threshold (0.3) and low run count (100/1000 = 0.1) + let debug_str = format!("{:?}", compressor); + + // The compressor should be RLE wrapped in general compression + assert!(debug_str.contains("GeneralMiniBlockCompressor")); + assert!(debug_str.contains("RleEncoder")); + } + + #[test] + fn test_type_level_parameters() { + let mut params = CompressionParams::new(); + + // Configure all Int32 to use specific settings + params.types.insert( + "Int32".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.1), // Very low threshold + compression: Some("zstd".to_string()), + compression_level: Some(3), + bss: Some(BssMode::Off), // Disable BSS to test RLE + minichunk_size: None, + }, + ); + + let strategy = baseline_strategy(params); + let field = create_test_field("some_column", DataType::Int32); + // Create data with very low run count (50 runs for 1000 values = 0.05 ratio) + let data = create_fixed_width_block_with_stats(32, 1000, 50); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + // Should use RLE due to very low threshold + assert!(format!("{:?}", compressor).contains("RleEncoder")); + } + + // Regression for #6626: an all-zero stat segment (e.g. rep/def for a long + // run of empty lists) used to disable block bitpacking entirely. + #[test] + #[cfg(feature = "bitpacking")] + fn test_block_bitpacks_with_zero_segment() { + let strategy = baseline_strategy(CompressionParams::default()); + let field = create_test_field("levels", DataType::UInt16); + + // First 1024 zeros, then 1024 ones; max bit width is 1. + let mut values: Vec = vec![0; 1024]; + values.extend(std::iter::repeat_n(1u16, 1024)); + let mut block = FixedWidthDataBlock { + bits_per_value: 16, + data: LanceBuffer::reinterpret_vec(values), + num_values: 2048, + block_info: BlockInfo::default(), + }; + block.compute_stat(); + let data = DataBlock::FixedWidth(block); + + let (compressor, _encoding) = strategy.create_block_compressor(&field, &data).unwrap(); + let debug_str = format!("{:?}", compressor); + assert!( + debug_str.contains("OutOfLineBitpacking"), + "expected OutOfLineBitpacking, got: {debug_str}" + ); + } + + #[test] + fn test_rle_block_accounts_for_header_before_selecting() { + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let field = create_test_field("small_constant", DataType::Int32); + let values = vec![42i32; 2]; + let mut block = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 2, + block_info: BlockInfo::default(), + }; + block.compute_stat(); + let data = DataBlock::FixedWidth(block); + + let (compressor, encoding) = strategy.create_block_compressor(&field, &data).unwrap(); + + assert!(format!("{compressor:?}").contains("ValueEncoder")); + assert!(matches!( + encoding.compression.as_ref(), + Some(Compression::Flat(_)) + )); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_block_prefers_bitpacking_when_smaller() { + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let field = create_test_field("levels", DataType::UInt16); + + let mut values = Vec::with_capacity(2048); + for run_idx in 0..1024 { + values.extend(std::iter::repeat_n((run_idx % 2) as u16, 2)); + } + let mut block = FixedWidthDataBlock { + bits_per_value: 16, + data: LanceBuffer::reinterpret_vec(values), + num_values: 2048, + block_info: BlockInfo::default(), + }; + block.compute_stat(); + let data = DataBlock::FixedWidth(block); + + let (compressor, encoding) = strategy.create_block_compressor(&field, &data).unwrap(); + let debug_str = format!("{compressor:?}"); + assert!( + debug_str.contains("OutOfLineBitpacking"), + "expected OutOfLineBitpacking, got: {debug_str}" + ); + assert!(matches!( + encoding.compression.as_ref(), + Some(Compression::OutOfLineBitpacking(_)) + )); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_low_cardinality_prefers_bitpacking_over_rle() { + let strategy = baseline_strategy(CompressionParams::default()); + let field = create_test_field("int_score", DataType::Int64); + + // Low cardinality values (3/4/5) but with moderate run count: + // RLE compresses vs raw, yet bitpacking should be smaller. + let mut values: Vec = Vec::with_capacity(256); + for run_idx in 0..64 { + let value = match run_idx % 3 { + 0 => 3u64, + 1 => 4u64, + _ => 5u64, + }; + values.extend(std::iter::repeat_n(value, 4)); + } + + let mut block = FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::reinterpret_vec(values), + num_values: 256, + block_info: BlockInfo::default(), + }; + + use crate::statistics::ComputeStat; + block.compute_stat(); + + let data = DataBlock::FixedWidth(block); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{:?}", compressor); + assert!( + debug_str.contains("InlineBitpacking"), + "expected InlineBitpacking, got: {debug_str}" + ); + assert!( + !debug_str.contains("RleEncoder"), + "expected RLE to be skipped when bitpacking is smaller, got: {debug_str}" + ); + } + + fn check_uncompressed_encoding(encoding: &CompressiveEncoding, variable: bool) { + let chain = extract_array_encoding_chain(encoding); + if variable { + assert_eq!(chain.len(), 2); + assert_eq!(chain.first().unwrap().as_str(), "variable"); + assert_eq!(chain.get(1).unwrap().as_str(), "flat"); + } else { + assert_eq!(chain.len(), 1); + assert_eq!(chain.first().unwrap().as_str(), "flat"); + } + } + + #[test] + fn test_none_compression() { + let mut params = CompressionParams::new(); + + // Disable compression for embeddings + params.columns.insert( + "embeddings".to_string(), + CompressionFieldParams { + compression: Some("none".to_string()), + ..Default::default() + }, + ); + + let strategy = baseline_strategy(params); + let field = create_test_field("embeddings", DataType::Float32); + let fixed_data = create_fixed_width_block(32, 1000); + let variable_data = create_variable_width_block(32, 10, 32 * 1024); + + // Test miniblock + let compressor = strategy + .create_miniblock_compressor(&field, &fixed_data) + .unwrap(); + let (_block, encoding) = compressor + .compress(miniblock_context(), fixed_data.clone()) + .unwrap(); + check_uncompressed_encoding(&encoding, false); + let compressor = strategy + .create_miniblock_compressor(&field, &variable_data) + .unwrap(); + let (_block, encoding) = compressor + .compress(miniblock_context(), variable_data.clone()) + .unwrap(); + check_uncompressed_encoding(&encoding, true); + + // Test pervalue + let compressor = strategy.create_per_value(&field, &fixed_data).unwrap(); + let (_block, encoding) = compressor.compress(fixed_data).unwrap(); + check_uncompressed_encoding(&encoding, false); + let compressor = strategy.create_per_value(&field, &variable_data).unwrap(); + let (_block, encoding) = compressor.compress(variable_data).unwrap(); + check_uncompressed_encoding(&encoding, true); + } + + #[test] + fn test_field_metadata_none_compression() { + // Prepare field with metadata for none compression + let mut arrow_field = ArrowField::new("simple_col", DataType::Binary, true); + let mut metadata = HashMap::new(); + metadata.insert(COMPRESSION_META_KEY.to_string(), "none".to_string()); + arrow_field = arrow_field.with_metadata(metadata); + let field = Field::try_from(&arrow_field).unwrap(); + + let strategy = baseline_strategy(CompressionParams::new()); + + // Test miniblock + let fixed_data = create_fixed_width_block(32, 1000); + let variable_data = create_variable_width_block(32, 10, 32 * 1024); + + let compressor = strategy + .create_miniblock_compressor(&field, &fixed_data) + .unwrap(); + let (_block, encoding) = compressor + .compress(miniblock_context(), fixed_data.clone()) + .unwrap(); + check_uncompressed_encoding(&encoding, false); + + let compressor = strategy + .create_miniblock_compressor(&field, &variable_data) + .unwrap(); + let (_block, encoding) = compressor + .compress(miniblock_context(), variable_data.clone()) + .unwrap(); + check_uncompressed_encoding(&encoding, true); + + // Test pervalue + let compressor = strategy.create_per_value(&field, &fixed_data).unwrap(); + let (_block, encoding) = compressor.compress(fixed_data).unwrap(); + check_uncompressed_encoding(&encoding, false); + + let compressor = strategy.create_per_value(&field, &variable_data).unwrap(); + let (_block, encoding) = compressor.compress(variable_data).unwrap(); + check_uncompressed_encoding(&encoding, true); + } + + #[test] + fn test_auto_fsst_disabled_for_binary_fields() { + let strategy = baseline_strategy(CompressionParams::default()); + let field = create_test_field("bytes", DataType::Binary); + let variable_data = create_fsst_candidate_variable_width_block(); + + let miniblock = strategy + .create_miniblock_compressor(&field, &variable_data) + .unwrap(); + let miniblock_debug = format!("{:?}", miniblock); + assert!( + miniblock_debug.contains("BinaryMiniBlockEncoder"), + "expected BinaryMiniBlockEncoder, got: {miniblock_debug}" + ); + assert!( + !miniblock_debug.contains("FsstMiniBlockEncoder"), + "did not expect FsstMiniBlockEncoder, got: {miniblock_debug}" + ); + + let per_value = strategy.create_per_value(&field, &variable_data).unwrap(); + let per_value_debug = format!("{:?}", per_value); + assert!( + per_value_debug.contains("VariableEncoder"), + "expected VariableEncoder, got: {per_value_debug}" + ); + assert!( + !per_value_debug.contains("FsstPerValueEncoder"), + "did not expect FsstPerValueEncoder, got: {per_value_debug}" + ); + } + + #[test] + fn test_auto_fsst_still_enabled_for_utf8_fields() { + let strategy = baseline_strategy(CompressionParams::default()); + let field = create_test_field("text", DataType::Utf8); + let variable_data = create_fsst_candidate_variable_width_block(); + + let miniblock = strategy + .create_miniblock_compressor(&field, &variable_data) + .unwrap(); + let miniblock_debug = format!("{:?}", miniblock); + assert!( + miniblock_debug.contains("FsstMiniBlockEncoder"), + "expected FsstMiniBlockEncoder, got: {miniblock_debug}" + ); + + let per_value = strategy.create_per_value(&field, &variable_data).unwrap(); + let per_value_debug = format!("{:?}", per_value); + assert!( + per_value_debug.contains("FsstPerValueEncoder"), + "expected FsstPerValueEncoder, got: {per_value_debug}" + ); + } + + #[test] + fn test_explicit_fsst_still_supported_for_binary_fields() { + let mut params = CompressionParams::new(); + params.columns.insert( + "bytes".to_string(), + CompressionFieldParams { + compression: Some("fsst".to_string()), + ..Default::default() + }, + ); + + let strategy = baseline_strategy(params); + let field = create_test_field("bytes", DataType::Binary); + let variable_data = create_fsst_candidate_variable_width_block(); + + let miniblock = strategy + .create_miniblock_compressor(&field, &variable_data) + .unwrap(); + let miniblock_debug = format!("{:?}", miniblock); + assert!( + miniblock_debug.contains("FsstMiniBlockEncoder"), + "expected FsstMiniBlockEncoder, got: {miniblock_debug}" + ); + + let per_value = strategy.create_per_value(&field, &variable_data).unwrap(); + let per_value_debug = format!("{:?}", per_value); + assert!( + per_value_debug.contains("FsstPerValueEncoder"), + "expected FsstPerValueEncoder, got: {per_value_debug}" + ); + } + + #[test] + #[cfg(feature = "zstd")] + fn test_compression_level_honored_for_large_per_value() { + let mut params = CompressionParams::new(); + params.columns.insert( + "html".to_string(), + CompressionFieldParams { + compression: Some("zstd".to_string()), + compression_level: Some(19), + ..Default::default() + }, + ); + let strategy = baseline_strategy(params); + let field = create_test_field("html", DataType::Utf8); + let large = create_variable_width_block(32, 64, 40 * 1024); + + let per_value = strategy.create_per_value(&field, &large).unwrap(); + let debug = format!("{per_value:?}"); + assert!( + debug.contains("ZstdBufferCompressor") && debug.contains("compression_level: 19"), + "expected zstd level 19 to reach the per-value compressor, got: {debug}" + ); + } + + #[test] + fn test_parameter_merge_priority() { + let mut params = CompressionParams::new(); + + // Set type-level + params.types.insert( + "Int32".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.5), + compression: Some("lz4".to_string()), + ..Default::default() + }, + ); + + // Set column-level (highest priority) + params.columns.insert( + "user_id".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.2), + compression: Some("zstd".to_string()), + compression_level: Some(6), + bss: None, + minichunk_size: None, + }, + ); + + // Get merged params + let merged = params.get_field_params("user_id", &DataType::Int32); + + // Column params should override type params + assert_eq!(merged.rle_threshold, Some(0.2)); + assert_eq!(merged.compression, Some("zstd".to_string())); + assert_eq!(merged.compression_level, Some(6)); + + // Test field with only type params + let merged = params.get_field_params("other_field", &DataType::Int32); + assert_eq!(merged.rle_threshold, Some(0.5)); + assert_eq!(merged.compression, Some("lz4".to_string())); + assert_eq!(merged.compression_level, None); + } + + #[test] + fn test_pattern_matching() { + let mut params = CompressionParams::new(); + + // Configure pattern for log files + params.columns.insert( + "log_*".to_string(), + CompressionFieldParams { + compression: Some("zstd".to_string()), + compression_level: Some(6), + ..Default::default() + }, + ); + + // Should match pattern + let merged = params.get_field_params("log_messages", &DataType::Utf8); + assert_eq!(merged.compression, Some("zstd".to_string())); + assert_eq!(merged.compression_level, Some(6)); + + // Should not match + let merged = params.get_field_params("messages_log", &DataType::Utf8); + assert_eq!(merged.compression, None); + } + + #[test] + fn test_legacy_metadata_support() { + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + // Test field with "none" compression metadata + let mut metadata = HashMap::new(); + metadata.insert(COMPRESSION_META_KEY.to_string(), "none".to_string()); + let mut field = create_test_field("some_column", DataType::Int32); + field.metadata = metadata; + + let data = create_fixed_width_block(32, 1000); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + + // Should respect metadata and use ValueEncoder + assert!(format!("{:?}", compressor).contains("ValueEncoder")); + } + + #[test] + fn test_default_behavior() { + // Empty params should fall back to default behavior + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + let field = create_test_field("random_column", DataType::Int32); + // Create data with high run count that won't trigger RLE (600 runs for 1000 values = 0.6 ratio) + let data = create_fixed_width_block_with_stats(32, 1000, 600); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + // Should use default strategy's decision + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("ValueEncoder") || debug_str.contains("InlineBitpacking")); + } + + #[test] + fn test_field_metadata_compression() { + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + // Test field with compression metadata + let mut metadata = HashMap::new(); + metadata.insert(COMPRESSION_META_KEY.to_string(), "zstd".to_string()); + metadata.insert(COMPRESSION_LEVEL_META_KEY.to_string(), "6".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let data = create_fixed_width_block(32, 1000); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + + // Should use zstd with level 6 + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("GeneralMiniBlockCompressor")); + } + + #[test] + fn test_field_metadata_rle_threshold() { + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + // Test field with RLE threshold metadata + let mut metadata = HashMap::new(); + metadata.insert(RLE_THRESHOLD_META_KEY.to_string(), "0.8".to_string()); + metadata.insert(BSS_META_KEY.to_string(), "off".to_string()); // Disable BSS to test RLE + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + // Create data with low run count (e.g., 100 runs for 1000 values = 0.1 ratio) + // This ensures run_count (100) < num_values * threshold (1000 * 0.8 = 800) + let data = create_fixed_width_block_with_stats(32, 1000, 100); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + + // Should use RLE because run_count (100) < num_values * threshold (800) + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("RleEncoder")); + } + + #[test] + fn test_rle_v2_miniblock_selects_u16_run_lengths() { + let mut metadata = HashMap::new(); + metadata.insert(RLE_THRESHOLD_META_KEY.to_string(), "1.0".to_string()); + metadata.insert(BSS_META_KEY.to_string(), "off".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let values = vec![7i32; 1000]; + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 1000, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 16); + } + + #[test] + fn test_rle_v2_miniblock_keeps_u8_run_lengths_before_v2_3() { + for version in [TestEncoding::StructuralU16, TestEncoding::StructuralU32] { + let mut metadata = HashMap::new(); + metadata.insert(RLE_THRESHOLD_META_KEY.to_string(), "1.0".to_string()); + metadata.insert(BSS_META_KEY.to_string(), "off".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let values = vec![7i32; 1000]; + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 1000, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(version, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 8, "version={version}"); + } + } + + #[test] + fn test_rle_v2_uses_selected_width_cost_before_bitpacking() { + let mut metadata = HashMap::new(); + metadata.insert(RLE_THRESHOLD_META_KEY.to_string(), "1.0".to_string()); + metadata.insert(BSS_META_KEY.to_string(), "off".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let values = vec![0i32; 4096]; + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 4096, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{compressor:?}"); + assert!(debug_str.contains("RleEncoder")); + + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 16); + } + + #[test] + fn test_rle_v2_sorted_dictionary_indices_select_u16_run_lengths() { + let field = create_test_field("dict_indices", DataType::Int32); + + let mut values = Vec::with_capacity(1_200); + for value in 0..4 { + values.extend(std::iter::repeat_n(value, 300)); + } + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 1_200, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 16); + } + + #[test] + fn test_rle_v2_short_runs_keep_u8_run_lengths() { + let field = create_test_field("dict_indices", DataType::Int32); + + let mut values = Vec::with_capacity(1_280); + for value in 0..10 { + values.extend(std::iter::repeat_n(value, 128)); + } + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 1_280, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 8); + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_rle_miniblock_released_versions_keep_flat_children_when_compression_requested() { + for version in [TestEncoding::StructuralU16, TestEncoding::StructuralU32] { + let mut params = CompressionParams::new(); + params.columns.insert( + "dict_indices".to_string(), + CompressionFieldParams { + compression: Some( + if cfg!(feature = "lz4") { "lz4" } else { "zstd" }.to_string(), + ), + rle_threshold: Some(1.0), + bss: Some(BssMode::Off), + ..Default::default() + }, + ); + let strategy = strategy(version, params); + let field = create_test_field("dict_indices", DataType::UInt32); + + let mut values = Vec::with_capacity(8192 * 4); + for value in 0..8192u32 { + values.extend(std::iter::repeat_n(value, 4)); + } + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 8192 * 4, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + let rle = expect_rle_encoding(&encoding); + + assert!( + matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + Compression::Flat(_) + ), + "version={version}" + ); + assert!( + matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + Compression::Flat(_) + ), + "version={version}" + ); + } + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_miniblock_strategy_bitpacks_child_values_when_smaller() { + let field = create_test_field("dict_indices", DataType::Int32); + + let mut values = Vec::with_capacity(8192 * 4); + for value in 0..8192 { + values.extend(std::iter::repeat_n(value, 4)); + } + let mut data = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 8192 * 4, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{compressor:?}"); + assert!(debug_str.contains("RleEncoder")); + + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + let Compression::Rle(rle) = encoding.compression.as_ref().unwrap() else { + panic!("expected RLE encoding"); + }; + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + Compression::OutOfLineBitpacking(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + Compression::Flat(_) + )); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_miniblock_keeps_child_bitpacked_rle_when_smaller_than_inline_bitpacking() { + let field = create_test_field("int_score", DataType::UInt64); + + let mut values = Vec::with_capacity(8192 * 8); + for run_idx in 0..8192 { + let value = match run_idx % 3 { + 0 => 3u64, + 1 => 4u64, + _ => 5u64, + }; + values.extend(std::iter::repeat_n(value, 8)); + } + let mut data = FixedWidthDataBlock { + bits_per_value: 64, + data: LanceBuffer::reinterpret_vec(values), + num_values: 8192 * 8, + block_info: BlockInfo::default(), + }; + data.compute_stat(); + let data = DataBlock::FixedWidth(data); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::default()); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{compressor:?}"); + assert!( + debug_str.contains("RleEncoder"), + "expected RLE to beat inline bitpacking after child selection, got: {debug_str}" + ); + + let (_compressed, encoding) = compressor.compress(miniblock_context(), data).unwrap(); + let rle = expect_rle_encoding(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + Compression::OutOfLineBitpacking(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + Compression::Flat(_) + )); + } + + #[test] + fn test_field_metadata_override_params() { + // Set up params with one configuration + let mut params = CompressionParams::new(); + params.columns.insert( + "test_column".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.3), + compression: Some("lz4".to_string()), + compression_level: None, + bss: None, + minichunk_size: None, + }, + ); + + let strategy = baseline_strategy(params); + + // Field metadata should override params + let mut metadata = HashMap::new(); + metadata.insert(COMPRESSION_META_KEY.to_string(), "none".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let data = create_fixed_width_block(32, 1000); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + + // Should use none compression (from metadata) instead of lz4 (from params) + assert!(format!("{:?}", compressor).contains("ValueEncoder")); + } + + #[test] + fn test_field_metadata_mixed_configuration() { + // Configure type-level params + let mut params = CompressionParams::new(); + params.types.insert( + "Int32".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.5), + compression: Some("lz4".to_string()), + ..Default::default() + }, + ); + + let strategy = baseline_strategy(params); + + // Field metadata provides partial override + let mut metadata = HashMap::new(); + metadata.insert(COMPRESSION_LEVEL_META_KEY.to_string(), "3".to_string()); + let mut field = create_test_field("test_column", DataType::Int32); + field.metadata = metadata; + + let data = create_fixed_width_block(32, 1000); + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + + // Should use lz4 (from type params) with level 3 (from metadata) + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("GeneralMiniBlockCompressor")); + } + + #[test] + fn test_bss_field_metadata() { + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + // Test BSS "on" mode with compression enabled (BSS requires compression to be effective) + let mut metadata = HashMap::new(); + metadata.insert(BSS_META_KEY.to_string(), "on".to_string()); + metadata.insert(COMPRESSION_META_KEY.to_string(), "lz4".to_string()); + let arrow_field = + ArrowField::new("temperature", DataType::Float32, false).with_metadata(metadata); + let field = Field::try_from(&arrow_field).unwrap(); + + // Create float data + let data = create_fixed_width_block(32, 100); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("ByteStreamSplitEncoder")); + } + + #[test] + fn test_bss_with_compression() { + let params = CompressionParams::new(); + let strategy = baseline_strategy(params); + + // Test BSS with LZ4 compression + let mut metadata = HashMap::new(); + metadata.insert(BSS_META_KEY.to_string(), "on".to_string()); + metadata.insert(COMPRESSION_META_KEY.to_string(), "lz4".to_string()); + let arrow_field = + ArrowField::new("sensor_data", DataType::Float64, false).with_metadata(metadata); + let field = Field::try_from(&arrow_field).unwrap(); + + // Create double data + let data = create_fixed_width_block(64, 100); + + let compressor = strategy.create_miniblock_compressor(&field, &data).unwrap(); + let debug_str = format!("{:?}", compressor); + // Should have BSS wrapped in general compression + assert!(debug_str.contains("GeneralMiniBlockCompressor")); + assert!(debug_str.contains("ByteStreamSplitEncoder")); + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_general_block_decompression_fixed_width_v2_2() { + // Request general compression via the write path (2.2 requirement) and ensure the read path mirrors it. + let mut params = CompressionParams::new(); + params.columns.insert( + "dict_values".to_string(), + CompressionFieldParams { + compression: Some(if cfg!(feature = "lz4") { "lz4" } else { "zstd" }.to_string()), + ..Default::default() + }, + ); + + let strategy = strategy(TestEncoding::StructuralU32, params); + + let field = create_test_field("dict_values", DataType::FixedSizeBinary(3)); + let data = create_fixed_width_block(24, 1024); + let DataBlock::FixedWidth(expected_block) = &data else { + panic!("expected fixed width block"); + }; + let expected_bits = expected_block.bits_per_value; + let expected_num_values = expected_block.num_values; + let num_values = expected_num_values; + + let (compressor, encoding) = strategy + .create_block_compressor(&field, &data) + .expect("general compression should be selected"); + match encoding.compression.as_ref() { + Some(Compression::General(_)) => {} + other => panic!("expected general compression, got {:?}", other), + } + + let compressed_buffer = compressor + .compress(data.clone()) + .expect("write path general compression should succeed"); + + let decompressor = DefaultDecompressionStrategy::default() + .create_block_decompressor(&encoding) + .expect("general block decompressor should be created"); + + let decoded = decompressor + .decompress(compressed_buffer, num_values) + .expect("decompression should succeed"); + + match decoded { + DataBlock::FixedWidth(block) => { + assert_eq!(block.bits_per_value, expected_bits); + assert_eq!(block.num_values, expected_num_values); + assert_eq!(block.data.as_ref(), expected_block.data.as_ref()); + } + _ => panic!("expected fixed width block"), + } + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_general_compression_not_selected_for_v2_1_even_if_requested() { + let mut params = CompressionParams::new(); + params.columns.insert( + "dict_values".to_string(), + CompressionFieldParams { + compression: Some(if cfg!(feature = "lz4") { "lz4" } else { "zstd" }.to_string()), + ..Default::default() + }, + ); + + let strategy = strategy(TestEncoding::StructuralU16, params); + let field = create_test_field("dict_values", DataType::FixedSizeBinary(3)); + let data = create_fixed_width_block(24, 1024); + + let (_compressor, encoding) = strategy + .create_block_compressor(&field, &data) + .expect("block compressor selection should succeed"); + + assert!( + !matches!(encoding.compression.as_ref(), Some(Compression::General(_))), + "general compression should not be selected for V2.1" + ); + } + + #[test] + fn test_none_compression_disables_auto_general_block_compression() { + let mut params = CompressionParams::new(); + params.columns.insert( + "dict_values".to_string(), + CompressionFieldParams { + compression: Some("none".to_string()), + ..Default::default() + }, + ); + + let strategy = strategy(TestEncoding::StructuralU32, params); + let field = create_test_field("dict_values", DataType::FixedSizeBinary(3)); + let data = create_fixed_width_block(24, 20_000); + + assert!( + data.data_size() > MIN_BLOCK_SIZE_FOR_GENERAL_COMPRESSION, + "test requires block size above automatic general compression threshold" + ); + + let (_compressor, encoding) = strategy + .create_block_compressor(&field, &data) + .expect("block compressor selection should succeed"); + + assert!( + !matches!(encoding.compression.as_ref(), Some(Compression::General(_))), + "compression=none should disable automatic block general compression" + ); + } + + #[test] + fn test_rle_v2_block_selects_u32_run_lengths() { + let field = create_test_field("dict_indices", DataType::Int32); + let expected_values = vec![42i32; 70_000]; + let mut block = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(expected_values.clone()), + num_values: expected_values.len() as u64, + block_info: BlockInfo::default(), + }; + block.compute_stat(); + let data = DataBlock::FixedWidth(block); + + let strategy = strategy(TestEncoding::StructuralSparse, CompressionParams::new()); + let (compressor, encoding) = strategy.create_block_compressor(&field, &data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 32); + + let compressed = compressor.compress(data).unwrap(); + let decompressor = DefaultDecompressionStrategy::default() + .create_block_decompressor(&encoding) + .unwrap(); + let decoded = decompressor + .decompress(compressed, expected_values.len() as u64) + .unwrap(); + + match decoded { + DataBlock::FixedWidth(block) => { + let values = block.data.borrow_to_typed_slice::(); + assert_eq!(values.as_ref(), expected_values); + } + _ => panic!("expected fixed-width block"), + } + } + + #[test] + fn test_rle_v2_block_keeps_u8_run_lengths_for_v2_2() { + let field = create_test_field("dict_indices", DataType::Int32); + let values = vec![42i32; 70_000]; + let mut block = FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::reinterpret_vec(values), + num_values: 70_000, + block_info: BlockInfo::default(), + }; + block.compute_stat(); + let data = DataBlock::FixedWidth(block); + + let strategy = strategy(TestEncoding::StructuralU32, CompressionParams::new()); + let (_compressor, encoding) = strategy.create_block_compressor(&field, &data).unwrap(); + assert_eq!(rle_run_length_bits(&encoding), 8); + } + + #[test] + fn test_rle_block_used_for_version_v2_2() { + let field = create_test_field("test_repdef", DataType::UInt16); + + // Create highly repetitive data + let num_values = 1000u64; + let mut data = Vec::with_capacity(num_values as usize); + for i in 0..10 { + for _ in 0..100 { + data.push(i as u16); + } + } + + let mut block = FixedWidthDataBlock { + bits_per_value: 16, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }; + + block.compute_stat(); + + let data_block = DataBlock::FixedWidth(block); + + let strategy = strategy(TestEncoding::StructuralU32, CompressionParams::new()); + + let (compressor, _) = strategy + .create_block_compressor(&field, &data_block) + .unwrap(); + + let debug_str = format!("{:?}", compressor); + assert!(debug_str.contains("RleEncoder")); + } + + #[test] + fn test_rle_block_not_used_for_version_v2_1() { + let field = create_test_field("test_repdef", DataType::UInt16); + + // Create highly repetitive data + let num_values = 1000u64; + let mut data = Vec::with_capacity(num_values as usize); + for i in 0..10 { + for _ in 0..100 { + data.push(i as u16); + } + } + + let mut block = FixedWidthDataBlock { + bits_per_value: 16, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }; + + block.compute_stat(); + + let data_block = DataBlock::FixedWidth(block); + + let strategy = strategy(TestEncoding::StructuralU16, CompressionParams::new()); + + let (compressor, _) = strategy + .create_block_compressor(&field, &data_block) + .unwrap(); + + let debug_str = format!("{:?}", compressor); + assert!( + !debug_str.contains("RleEncoder"), + "RLE should not be used for V2.1" + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/compression_config.rs b/lance-artifact/rust/lance-encoding/src/compression_config.rs new file mode 100644 index 000000000..4aee75b21 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/compression_config.rs @@ -0,0 +1,299 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Configuration for compression parameters +//! +//! This module provides types for configuring compression strategies +//! on a per-column or per-type basis using a parameter-driven approach. + +use std::collections::HashMap; + +use arrow_schema::DataType; + +/// Byte stream split encoding mode +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum BssMode { + /// Never use BSS + Off, + /// Always use BSS for floating point data + On, + /// Automatically decide based on data characteristics + Auto, +} + +impl BssMode { + /// Convert to internal sensitivity value + pub fn to_sensitivity(&self) -> f32 { + match self { + Self::Off => 0.0, + Self::On => 1.0, + Self::Auto => 0.5, // Default sensitivity for auto mode + } + } + + /// Parse from string + pub fn parse(s: &str) -> Option { + match s.to_lowercase().as_str() { + "off" => Some(Self::Off), + "on" => Some(Self::On), + "auto" => Some(Self::Auto), + _ => None, + } + } +} + +/// Compression parameter configuration +#[derive(Debug, Clone, PartialEq)] +pub struct CompressionParams { + /// Column-level parameters: column name/pattern -> parameters + pub columns: HashMap, + + /// Type-level parameters: data type name -> parameters + pub types: HashMap, +} + +/// Field-level compression parameters +#[derive(Debug, Clone, PartialEq, Default)] +pub struct CompressionFieldParams { + /// RLE threshold (0.0-1.0) + /// When run_count < num_values * threshold, RLE will be used + pub rle_threshold: Option, + + /// General compression scheme: "lz4", "zstd", "none" + pub compression: Option, + + /// Compression level (only for schemes that support it, e.g., zstd) + pub compression_level: Option, + + /// Byte stream split mode for floating point data + pub bss: Option, + + /// Minichunk size threshold for encoding + pub minichunk_size: Option, +} + +impl CompressionParams { + /// Create empty compression parameters + pub fn new() -> Self { + Self { + columns: HashMap::new(), + types: HashMap::new(), + } + } + + /// Get effective parameters for a field (merging type params and column params) + pub fn get_field_params( + &self, + field_name: &str, + data_type: &DataType, + ) -> CompressionFieldParams { + let mut params = CompressionFieldParams::default(); + + // Apply type-level parameters + let type_name = data_type.to_string(); + if let Some(type_params) = self.types.get(&type_name) { + params.merge(type_params); + } + + // Apply column-level parameters (highest priority) + // First check exact match + if let Some(col_params) = self.columns.get(field_name) { + params.merge(col_params); + } else { + // Check pattern matching + for (pattern, col_params) in &self.columns { + if matches_pattern(field_name, pattern) { + params.merge(col_params); + break; // Use first matching pattern + } + } + } + + params + } +} + +impl Default for CompressionParams { + fn default() -> Self { + Self::new() + } +} + +impl CompressionFieldParams { + /// Merge another CompressionFieldParams, non-None values will override + pub fn merge(&mut self, other: &Self) { + if other.rle_threshold.is_some() { + self.rle_threshold = other.rle_threshold; + } + if other.compression.is_some() { + self.compression = other.compression.clone(); + } + if other.compression_level.is_some() { + self.compression_level = other.compression_level; + } + if other.bss.is_some() { + self.bss = other.bss; + } + if other.minichunk_size.is_some() { + self.minichunk_size = other.minichunk_size; + } + } +} + +/// Check if a name matches a pattern (supports wildcards) +fn matches_pattern(name: &str, pattern: &str) -> bool { + if pattern == "*" { + return true; + } + + if let Some(prefix) = pattern.strip_suffix('*') { + return name.starts_with(prefix); + } + + if let Some(suffix) = pattern.strip_prefix('*') { + return name.ends_with(suffix); + } + + if pattern.contains('*') { + // Simple glob pattern matching (only supports single * in middle) + if let Some(pos) = pattern.find('*') { + let prefix = &pattern[..pos]; + let suffix = &pattern[pos + 1..]; + return name.starts_with(prefix) + && name.ends_with(suffix) + && name.len() >= pattern.len() - 1; + } + } + + name == pattern +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pattern_matching() { + assert!(matches_pattern("user_id", "*_id")); + assert!(matches_pattern("product_id", "*_id")); + assert!(!matches_pattern("identity", "*_id")); + + assert!(matches_pattern("log_message", "log_*")); + assert!(matches_pattern("log_level", "log_*")); + assert!(!matches_pattern("message_log", "log_*")); + + assert!(matches_pattern("test_field_name", "test_*_name")); + assert!(matches_pattern("test_column_name", "test_*_name")); + assert!(!matches_pattern("test_name", "test_*_name")); + + assert!(matches_pattern("anything", "*")); + assert!(matches_pattern("exact_match", "exact_match")); + } + + #[test] + fn test_field_params_merge() { + let mut params = CompressionFieldParams::default(); + assert_eq!(params.rle_threshold, None); + assert_eq!(params.compression, None); + assert_eq!(params.compression_level, None); + assert_eq!(params.bss, None); + + let other = CompressionFieldParams { + rle_threshold: Some(0.3), + compression: Some("lz4".to_string()), + compression_level: None, + bss: Some(BssMode::On), + minichunk_size: None, + }; + + params.merge(&other); + assert_eq!(params.rle_threshold, Some(0.3)); + assert_eq!(params.compression, Some("lz4".to_string())); + assert_eq!(params.compression_level, None); + assert_eq!(params.bss, Some(BssMode::On)); + + let another = CompressionFieldParams { + rle_threshold: None, + compression: Some("zstd".to_string()), + compression_level: Some(3), + bss: Some(BssMode::Auto), + minichunk_size: None, + }; + + params.merge(&another); + assert_eq!(params.rle_threshold, Some(0.3)); // Not overridden + assert_eq!(params.compression, Some("zstd".to_string())); // Overridden + assert_eq!(params.compression_level, Some(3)); // New value + assert_eq!(params.bss, Some(BssMode::Auto)); // Overridden + } + + #[test] + fn test_get_field_params() { + let mut params = CompressionParams::new(); + + // Set type-level params + params.types.insert( + "Int32".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.5), + compression: Some("lz4".to_string()), + ..Default::default() + }, + ); + + // Set column-level params + params.columns.insert( + "*_id".to_string(), + CompressionFieldParams { + rle_threshold: Some(0.3), + compression: Some("zstd".to_string()), + compression_level: Some(3), + bss: None, + minichunk_size: None, + }, + ); + + // Test no match (should get default) + let field_params = params.get_field_params("some_field", &DataType::Float32); + assert_eq!(field_params.compression, None); + assert_eq!(field_params.rle_threshold, None); + + // Test type match only + let field_params = params.get_field_params("some_field", &DataType::Int32); + assert_eq!(field_params.compression, Some("lz4".to_string())); // From type + assert_eq!(field_params.rle_threshold, Some(0.5)); // From type + + // Test column override (pattern match) + let field_params = params.get_field_params("user_id", &DataType::Int32); + assert_eq!(field_params.compression, Some("zstd".to_string())); // From column + assert_eq!(field_params.compression_level, Some(3)); // From column + assert_eq!(field_params.rle_threshold, Some(0.3)); // From column (overrides type) + } + + #[test] + fn test_exact_match_priority() { + let mut params = CompressionParams::new(); + + // Add pattern + params.columns.insert( + "*_id".to_string(), + CompressionFieldParams { + compression: Some("lz4".to_string()), + ..Default::default() + }, + ); + + // Add exact match + params.columns.insert( + "user_id".to_string(), + CompressionFieldParams { + compression: Some("zstd".to_string()), + ..Default::default() + }, + ); + + // Exact match should win + let field_params = params.get_field_params("user_id", &DataType::Int32); + assert_eq!(field_params.compression, Some("zstd".to_string())); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/constants.rs b/lance-artifact/rust/lance-encoding/src/constants.rs new file mode 100644 index 000000000..0bd31d676 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/constants.rs @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Constants for Lance encoding metadata keys +//! +//! These constants define the metadata keys used in Arrow field metadata +//! to configure various encoding behaviors in Lance. + +// Compression-related metadata keys +/// Metadata key for specifying compression scheme (e.g., "lz4", "zstd", "none") +pub const COMPRESSION_META_KEY: &str = "lance-encoding:compression"; +/// Metadata key for specifying compression level (applies to schemes that support levels) +pub const COMPRESSION_LEVEL_META_KEY: &str = "lance-encoding:compression-level"; +/// Metadata key for specifying RLE (Run-Length Encoding) threshold +pub const RLE_THRESHOLD_META_KEY: &str = "lance-encoding:rle-threshold"; +/// Metadata key for specifying minichunk size +pub const MINICHUNK_SIZE_META_KEY: &str = "lance-encoding:minichunk-size"; + +// Dictionary encoding metadata keys +/// Metadata key for specifying dictionary encoding threshold divisor +/// Set to a large value to discourage dictionary encoding +/// Set to a small value to encourage dictionary encoding +pub const DICT_DIVISOR_META_KEY: &str = "lance-encoding:dict-divisor"; +/// Metadata key for dictionary encoding size ratio threshold (0.0-1.0] +/// If estimated_dict_size/raw_size < ratio, use dictionary encoding. +/// Example: 0.8 means use dict if encoded size < 80% of raw size +/// Default: 0.8 +pub const DICT_SIZE_RATIO_META_KEY: &str = "lance-encoding:dict-size-ratio"; +/// Metadata key for selecting general compression scheme for dictionary values +/// Valid values: "lz4", "zstd", "none" +pub const DICT_VALUES_COMPRESSION_META_KEY: &str = "lance-encoding:dict-values-compression"; +/// Metadata key for selecting compression level for dictionary values +/// Applies to schemes that support levels (e.g. zstd) +pub const DICT_VALUES_COMPRESSION_LEVEL_META_KEY: &str = + "lance-encoding:dict-values-compression-level"; + +/// Environment variable for selecting general compression scheme for dictionary values +pub const DICT_VALUES_COMPRESSION_ENV_VAR: &str = "LANCE_ENCODING_DICT_VALUES_COMPRESSION"; +/// Environment variable for selecting compression level for dictionary values +pub const DICT_VALUES_COMPRESSION_LEVEL_ENV_VAR: &str = + "LANCE_ENCODING_DICT_VALUES_COMPRESSION_LEVEL"; + +// NOTE: BLOB_META_KEY is defined in lance-core to avoid circular dependency + +// Packed struct encoding metadata keys +/// Legacy metadata key for packed struct encoding (deprecated) +pub const PACKED_STRUCT_LEGACY_META_KEY: &str = "packed"; +/// Metadata key for packed struct encoding +pub const PACKED_STRUCT_META_KEY: &str = "lance-encoding:packed"; + +// Structural encoding metadata keys +/// Metadata key for specifying structural encoding type +pub const STRUCTURAL_ENCODING_META_KEY: &str = "lance-encoding:structural-encoding"; +/// Value for miniblock structural encoding +pub const STRUCTURAL_ENCODING_MINIBLOCK: &str = "miniblock"; +/// Value for fullzip structural encoding +pub const STRUCTURAL_ENCODING_FULLZIP: &str = "fullzip"; +/// Value for sparse structural encoding +pub const STRUCTURAL_ENCODING_SPARSE: &str = "sparse"; + +// Byte stream split metadata keys +/// Metadata key for byte stream split encoding configuration +pub const BSS_META_KEY: &str = "lance-encoding:bss"; +/// Default BSS mode +pub const DEFAULT_BSS_MODE: &str = "auto"; diff --git a/lance-artifact/rust/lance-encoding/src/data.rs b/lance-artifact/rust/lance-encoding/src/data.rs new file mode 100644 index 000000000..43a63c34e --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/data.rs @@ -0,0 +1,2704 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Data layouts to represent encoded data in a sub-Arrow format +//! +//! These [`DataBlock`] structures represent physical layouts. They fill a gap somewhere +//! between [`arrow_data::ArrayData`] (which, as a collection of buffers, is too +//! generic because it doesn't give us enough information about what those buffers represent) +//! and [`arrow_array::array::Array`] (which is too specific, because it cares about the +//! logical data type). +//! +//! In addition, the layouts represented here are slightly stricter than Arrow's layout rules. +//! For example, offset buffers MUST start with 0. These additional restrictions impose a +//! slight penalty on encode (to normalize arrow data) but make the development of encoders +//! and decoders easier (since they can rely on a normalized representation) + +use std::{ + ops::Range, + sync::{Arc, RwLock}, +}; + +use arrow_array::{ + Array, ArrayRef, OffsetSizeTrait, UInt64Array, + cast::AsArray, + new_empty_array, new_null_array, + types::{ArrowDictionaryKeyType, UInt8Type, UInt16Type, UInt32Type, UInt64Type}, +}; +use arrow_buffer::{ArrowNativeType, BooleanBuffer, BooleanBufferBuilder, NullBuffer}; +use arrow_data::{ArrayData, ArrayDataBuilder}; +use arrow_schema::DataType; +use lance_arrow::DataTypeExt; + +use lance_core::{Error, Result}; + +use crate::{ + buffer::LanceBuffer, + statistics::{ComputeStat, Stat}, +}; + +/// A data block with no buffers where everything is null +/// +/// Note: this data block should not be used for future work. It will be deprecated +/// in the 2.1 version of the format where nullability will be handled by the structural +/// encoders. +#[derive(Debug, Clone)] +pub struct AllNullDataBlock { + /// The number of values represented by this block + pub num_values: u64, +} + +impl AllNullDataBlock { + fn into_arrow(self, data_type: DataType, _validate: bool) -> Result { + Ok(ArrayData::new_null(&data_type, self.num_values as usize)) + } + + fn into_buffers(self) -> Vec { + vec![] + } +} + +use std::collections::HashMap; + +// `BlockInfo` stores the statistics of this `DataBlock`, such as `NullCount` for `NullableDataBlock`, +// `BitWidth` for `FixedWidthDataBlock`, `Cardinality` for all `DataBlock` +#[derive(Debug, Clone)] +pub struct BlockInfo(pub Arc>>>); + +impl Default for BlockInfo { + fn default() -> Self { + Self::new() + } +} + +impl BlockInfo { + pub fn new() -> Self { + Self(Arc::new(RwLock::new(HashMap::new()))) + } +} + +impl PartialEq for BlockInfo { + fn eq(&self, other: &Self) -> bool { + let self_info = self.0.read().unwrap(); + let other_info = other.0.read().unwrap(); + *self_info == *other_info + } +} + +/// Wraps a data block and adds nullability information to it +/// +/// Note: this data block should not be used for future work. It will be deprecated +/// in the 2.1 version of the format where nullability will be handled by the structural +/// encoders. +#[derive(Debug, Clone)] +pub struct NullableDataBlock { + /// The underlying data + pub data: Box, + /// A bitmap of validity for each value + pub nulls: LanceBuffer, + + pub block_info: BlockInfo, +} + +impl NullableDataBlock { + fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + let nulls = self.nulls.into_buffer(); + let data = self.data.into_arrow(data_type, validate)?.into_builder(); + let data = data.null_bit_buffer(Some(nulls)); + if validate { + Ok(data.build()?) + } else { + Ok(unsafe { data.build_unchecked() }) + } + } + + fn into_buffers(self) -> Vec { + let mut buffers = vec![self.nulls]; + buffers.extend(self.data.into_buffers()); + buffers + } + + pub fn data_size(&self) -> u64 { + self.data.data_size() + self.nulls.len() as u64 + } +} + +/// A block representing the same constant value repeated many times +#[derive(Debug, PartialEq, Clone)] +pub struct ConstantDataBlock { + /// Data buffer containing the value + pub data: LanceBuffer, + /// The number of values + pub num_values: u64, +} + +impl ConstantDataBlock { + fn into_buffers(self) -> Vec { + vec![self.data] + } + + fn into_arrow(self, _data_type: DataType, _validate: bool) -> Result { + // We don't need this yet but if we come up with some way of serializing + // scalars to/from bytes then we could implement it. + todo!() + } + + pub fn try_clone(&self) -> Result { + Ok(Self { + data: self.data.clone(), + num_values: self.num_values, + }) + } + + pub fn data_size(&self) -> u64 { + self.data.len() as u64 + } +} + +/// A data block for a single buffer of data where each element has a fixed number of bits +#[derive(Debug, PartialEq, Clone)] +pub struct FixedWidthDataBlock { + /// The data buffer + pub data: LanceBuffer, + /// The number of bits per value + pub bits_per_value: u64, + /// The number of values represented by this block + pub num_values: u64, + + pub block_info: BlockInfo, +} + +impl FixedWidthDataBlock { + fn do_into_arrow( + self, + data_type: DataType, + num_values: u64, + validate: bool, + ) -> Result { + // Booleans expanded for full-zip (bits_per_value==8, one byte each) need re-packing to + // Arrow's bit-packed format. + let data_buffer = if matches!(data_type, DataType::Boolean) && self.bits_per_value == 8 { + let mut builder = BooleanBufferBuilder::new(num_values as usize); + for &byte in self.data.as_ref().iter().take(num_values as usize) { + builder.append(byte != 0); + } + builder.finish().into_inner() + } else { + self.data.into_buffer() + }; + let builder = ArrayDataBuilder::new(data_type) + .add_buffer(data_buffer) + .len(num_values as usize) + .null_count(0); + if validate { + Ok(builder.build()?) + } else { + Ok(unsafe { builder.build_unchecked() }) + } + } + + pub fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + let root_num_values = self.num_values; + self.do_into_arrow(data_type, root_num_values, validate) + } + + pub fn into_buffers(self) -> Vec { + vec![self.data] + } + + pub fn try_clone(&self) -> Result { + Ok(Self { + data: self.data.clone(), + bits_per_value: self.bits_per_value, + num_values: self.num_values, + block_info: self.block_info.clone(), + }) + } + + pub fn data_size(&self) -> u64 { + self.data.len() as u64 + } +} + +#[derive(Debug)] +struct VariableWidthDataBlockBuilder { + offsets: Vec, + bytes: Vec, +} + +impl VariableWidthDataBlockBuilder { + fn new(estimated_size_bytes: u64) -> Self { + Self { + offsets: vec![T::from_usize(0).unwrap()], + bytes: Vec::with_capacity(estimated_size_bytes as usize), + } + } +} + +impl DataBlockBuilderImpl for VariableWidthDataBlockBuilder { + fn validate_append(&self, data_block: &DataBlock, selection: &Range) -> Result<()> { + let block = data_block.as_variable_width_ref().unwrap(); + block.validate_offsets_for_append::(selection) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let block = data_block.as_variable_width_ref().unwrap(); + debug_assert_eq!(block.bits_per_offset, T::get_byte_width() as u8 * 8); + let offsets = block.offsets.borrow_to_typed_view::(); + + let start_offset = offsets[selection.start as usize]; + let end_offset = offsets[selection.end as usize]; + let selected_data_len = end_offset.as_usize() - start_offset.as_usize(); + let new_data_len = self + .bytes + .len() + .checked_add(selected_data_len) + .ok_or_else(|| { + Error::not_supported_source( + "appending variable-width data would overflow usize".into(), + ) + })?; + if T::from_usize(new_data_len).is_none() { + return Err(Error::not_supported_source( + format!( + "appending variable-width data would require {} bytes, which exceeds the \ + capacity of {}-bit offsets", + new_data_len, + T::get_byte_width() * 8 + ) + .into(), + )); + } + let previous_len = self.bytes.len(); + + self.bytes + .extend_from_slice(&block.data[start_offset.as_usize()..end_offset.as_usize()]); + + self.offsets.extend( + offsets[selection.start as usize + 1..=selection.end as usize] + .iter() + .map(|&offset| { + let rebased_offset = + previous_len + (offset.as_usize() - start_offset.as_usize()); + T::from_usize(rebased_offset).unwrap() + }), + ); + Ok(()) + } + + fn finish(self: Box) -> DataBlock { + let num_values = (self.offsets.len() - 1) as u64; + DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(self.bytes), + offsets: LanceBuffer::reinterpret_vec(self.offsets), + bits_per_offset: T::get_byte_width() as u8 * 8, + num_values, + block_info: BlockInfo::new(), + }) + } +} + +#[derive(Debug)] +struct BitmapDataBlockBuilder { + values: BooleanBufferBuilder, +} + +impl BitmapDataBlockBuilder { + fn new(estimated_size_bytes: u64) -> Self { + Self { + values: BooleanBufferBuilder::new(estimated_size_bytes as usize * 8), + } + } +} + +impl DataBlockBuilderImpl for BitmapDataBlockBuilder { + fn validate_append(&self, _data_block: &DataBlock, _selection: &Range) -> Result<()> { + Ok(()) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let bitmap_blk = data_block.as_fixed_width_ref().unwrap(); + self.values.append_packed_range( + selection.start as usize..selection.end as usize, + &bitmap_blk.data, + ); + Ok(()) + } + + fn finish(mut self: Box) -> DataBlock { + let bool_buf = self.values.finish(); + let num_values = bool_buf.len() as u64; + let bits_buf = bool_buf.into_inner(); + DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(bits_buf), + bits_per_value: 1, + num_values, + block_info: BlockInfo::new(), + }) + } +} + +#[derive(Debug)] +struct FixedWidthDataBlockBuilder { + bits_per_value: u64, + bytes_per_value: u64, + values: Vec, +} + +impl FixedWidthDataBlockBuilder { + fn new(bits_per_value: u64, estimated_size_bytes: u64) -> Self { + assert!(bits_per_value.is_multiple_of(8)); + Self { + bits_per_value, + bytes_per_value: bits_per_value / 8, + values: Vec::with_capacity(estimated_size_bytes as usize), + } + } +} + +impl DataBlockBuilderImpl for FixedWidthDataBlockBuilder { + fn validate_append(&self, _data_block: &DataBlock, _selection: &Range) -> Result<()> { + Ok(()) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let block = data_block.as_fixed_width_ref().unwrap(); + assert_eq!(self.bits_per_value, block.bits_per_value); + let start = selection.start as usize * self.bytes_per_value as usize; + let end = selection.end as usize * self.bytes_per_value as usize; + self.values.extend_from_slice(&block.data[start..end]); + Ok(()) + } + + fn finish(self: Box) -> DataBlock { + let num_values = (self.values.len() / self.bytes_per_value as usize) as u64; + DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(self.values), + bits_per_value: self.bits_per_value, + num_values, + block_info: BlockInfo::new(), + }) + } +} + +#[derive(Debug)] +struct StructDataBlockBuilder { + children: Vec>, +} + +impl StructDataBlockBuilder { + fn new(children: Vec>) -> Self { + Self { children } + } +} + +impl DataBlockBuilderImpl for StructDataBlockBuilder { + fn validate_append(&self, data_block: &DataBlock, selection: &Range) -> Result<()> { + let data_block = data_block.as_struct_ref().unwrap(); + for i in 0..self.children.len() { + self.children[i].validate_append(&data_block.children[i], selection)?; + } + Ok(()) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let data_block = data_block.as_struct_ref().unwrap(); + for i in 0..self.children.len() { + self.children[i].append_validated(&data_block.children[i], selection.clone())?; + } + Ok(()) + } + + fn finish(self: Box) -> DataBlock { + let mut children_data_block = Vec::new(); + for child in self.children { + let child_data_block = child.finish(); + children_data_block.push(child_data_block); + } + DataBlock::Struct(StructDataBlock { + children: children_data_block, + block_info: BlockInfo::new(), + validity: None, + }) + } +} + +#[derive(Debug, Default)] +struct AllNullDataBlockBuilder { + num_values: u64, +} + +impl DataBlockBuilderImpl for AllNullDataBlockBuilder { + fn validate_append(&self, _data_block: &DataBlock, _selection: &Range) -> Result<()> { + Ok(()) + } + + fn append_validated(&mut self, _data_block: &DataBlock, selection: Range) -> Result<()> { + self.num_values += selection.end - selection.start; + Ok(()) + } + + fn finish(self: Box) -> DataBlock { + DataBlock::AllNull(AllNullDataBlock { + num_values: self.num_values, + }) + } +} + +/// A data block to represent a fixed size list +#[derive(Debug, Clone)] +pub struct FixedSizeListBlock { + /// The child data block + pub child: Box, + /// The number of items in each list + pub dimension: u64, +} + +impl FixedSizeListBlock { + pub fn num_values(&self) -> u64 { + self.child.num_values() / self.dimension + } + + /// Try to flatten a FixedSizeListBlock into a FixedWidthDataBlock + /// + /// Returns None if any children are nullable + pub fn try_into_flat(self) -> Option { + match *self.child { + // Cannot flatten a nullable child + DataBlock::Nullable(_) => None, + DataBlock::FixedSizeList(inner) => { + let mut flat = inner.try_into_flat()?; + flat.bits_per_value *= self.dimension; + flat.num_values /= self.dimension; + Some(flat) + } + DataBlock::FixedWidth(mut inner) => { + inner.bits_per_value *= self.dimension; + inner.num_values /= self.dimension; + Some(inner) + } + _ => panic!( + "Expected FixedSizeList or FixedWidth data block but found {:?}", + self + ), + } + } + + pub fn flatten_as_fixed(&mut self) -> FixedWidthDataBlock { + match self.child.as_mut() { + DataBlock::FixedSizeList(fsl) => fsl.flatten_as_fixed(), + DataBlock::FixedWidth(fw) => fw.clone(), + _ => panic!("Expected FixedSizeList or FixedWidth data block"), + } + } + + /// Convert a flattened values block into a FixedSizeListBlock + pub fn from_flat(data: FixedWidthDataBlock, data_type: &DataType) -> DataBlock { + match data_type { + DataType::FixedSizeList(child_field, dimension) => { + let mut data = data; + data.bits_per_value /= *dimension as u64; + data.num_values *= *dimension as u64; + let child_data = Self::from_flat(data, child_field.data_type()); + DataBlock::FixedSizeList(Self { + child: Box::new(child_data), + dimension: *dimension as u64, + }) + } + // Base case, we've hit a non-list type + _ => DataBlock::FixedWidth(data), + } + } + + fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + let num_values = self.num_values(); + let builder = match &data_type { + DataType::FixedSizeList(child_field, _) => { + let child_data = self + .child + .into_arrow(child_field.data_type().clone(), validate)?; + ArrayDataBuilder::new(data_type) + .add_child_data(child_data) + .len(num_values as usize) + .null_count(0) + } + _ => panic!("Expected FixedSizeList data type and got {:?}", data_type), + }; + if validate { + Ok(builder.build()?) + } else { + Ok(unsafe { builder.build_unchecked() }) + } + } + + fn into_buffers(self) -> Vec { + self.child.into_buffers() + } + + fn data_size(&self) -> u64 { + self.child.data_size() + } +} + +#[derive(Debug)] +struct FixedSizeListBlockBuilder { + inner: Box, + dimension: u64, +} + +impl FixedSizeListBlockBuilder { + fn new(inner: Box, dimension: u64) -> Self { + Self { inner, dimension } + } +} + +impl DataBlockBuilderImpl for FixedSizeListBlockBuilder { + fn validate_append(&self, data_block: &DataBlock, selection: &Range) -> Result<()> { + let selection = selection.start * self.dimension..selection.end * self.dimension; + let fsl = data_block.as_fixed_size_list_ref().unwrap(); + self.inner.validate_append(fsl.child.as_ref(), &selection) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let selection = selection.start * self.dimension..selection.end * self.dimension; + let fsl = data_block.as_fixed_size_list_ref().unwrap(); + self.inner.append_validated(fsl.child.as_ref(), selection) + } + + fn finish(self: Box) -> DataBlock { + let inner_block = self.inner.finish(); + DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(inner_block), + dimension: self.dimension, + }) + } +} + +#[derive(Debug)] +struct NullableDataBlockBuilder { + inner: Box, + validity: BooleanBufferBuilder, +} + +impl NullableDataBlockBuilder { + fn new(inner: Box, estimated_size_bytes: usize) -> Self { + Self { + inner, + validity: BooleanBufferBuilder::new(estimated_size_bytes * 8), + } + } +} + +impl DataBlockBuilderImpl for NullableDataBlockBuilder { + fn validate_append(&self, data_block: &DataBlock, selection: &Range) -> Result<()> { + let nullable = data_block.as_nullable_ref().unwrap(); + self.inner + .validate_append(nullable.data.as_ref(), selection) + } + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + let nullable = data_block.as_nullable_ref().unwrap(); + self.inner + .append_validated(nullable.data.as_ref(), selection.clone())?; + let bool_buf = BooleanBuffer::new( + nullable.nulls.clone().into_buffer(), + selection.start as usize, + (selection.end - selection.start) as usize, + ); + self.validity.append_buffer(&bool_buf); + Ok(()) + } + + fn finish(mut self: Box) -> DataBlock { + let inner_block = self.inner.finish(); + DataBlock::Nullable(NullableDataBlock { + data: Box::new(inner_block), + nulls: LanceBuffer::from(self.validity.finish().into_inner()), + block_info: BlockInfo::new(), + }) + } +} + +/// A data block with no regular structure. There is no available spot to attach +/// validity / repdef information and it cannot be converted to Arrow without being +/// decoded +#[derive(Debug, Clone)] +pub struct OpaqueBlock { + pub buffers: Vec, + pub num_values: u64, + pub block_info: BlockInfo, +} + +impl OpaqueBlock { + pub fn data_size(&self) -> u64 { + self.buffers.iter().map(|b| b.len() as u64).sum() + } +} + +/// A data block for variable-width data (e.g. strings, packed rows, etc.) +#[derive(Debug, Clone)] +pub struct VariableWidthBlock { + /// The data buffer + pub data: LanceBuffer, + /// The offsets buffer (contains num_values + 1 offsets) + /// + /// Offsets MUST start at 0 + pub offsets: LanceBuffer, + /// The number of bits per offset + pub bits_per_offset: u8, + /// The number of values represented by this block + pub num_values: u64, + + pub block_info: BlockInfo, +} + +/// Proof that a [`VariableWidthBlock`] satisfies the Arrow layout contract for +/// its target data type (offsets buffer long enough, offsets monotonic and +/// within the data buffer, values valid UTF-8 where required). +/// +/// Only [`VariableWidthBlock::validate_layout`] can construct it, which ties the +/// unchecked Arrow build below to an actual validation pass instead of a +/// caller-controlled flag. +struct ValidVariableWidthLayout; + +impl VariableWidthBlock { + fn append_error(&self, selection: &Range, detail: impl std::fmt::Display) -> Error { + Error::corrupt_file_named( + "variable width data block", + format!( + "cannot append offsets for selection {}..{}: {} (num_values: {}, \ + bits_per_offset: {}, offsets buffer size: {} bytes, data buffer size: {} bytes)", + selection.start, + selection.end, + detail, + self.num_values, + self.bits_per_offset, + self.offsets.len(), + self.data.len(), + ), + ) + } + + fn validate_offsets_for_append(&self, selection: &Range) -> Result<()> + where + T: OffsetSizeTrait + bytemuck::Pod, + { + let expected_bits_per_offset = T::get_byte_width() as u8 * 8; + if self.bits_per_offset != expected_bits_per_offset { + return Err(self.append_error( + selection, + format!( + "expected {}-bit offsets but found {}-bit offsets", + expected_bits_per_offset, self.bits_per_offset + ), + )); + } + let offset_size = std::mem::size_of::(); + if !self.offsets.len().is_multiple_of(offset_size) { + return Err(self.append_error( + selection, + format!( + "offsets buffer length {} is not a multiple of the {}-byte offset width", + self.offsets.len(), + offset_size + ), + )); + } + if selection.start > selection.end || selection.end > self.num_values { + return Err( + self.append_error(selection, "selection is outside the block's value range") + ); + } + let selection_start = usize::try_from(selection.start) + .map_err(|_| self.append_error(selection, "selection start does not fit in usize"))?; + let selection_end = usize::try_from(selection.end) + .map_err(|_| self.append_error(selection, "selection end does not fit in usize"))?; + let offsets = self.offsets.borrow_to_typed_view::(); + if selection_end >= offsets.len() { + return Err(self.append_error( + selection, + format!( + "selection requires offset {} but the buffer holds {} offsets", + selection_end, + offsets.len() + ), + )); + } + let selected_offsets = &offsets[selection_start..=selection_end]; + if let Some(detail) = + Self::offset_violation_detail(selected_offsets, self.data.len(), selection_start) + { + return Err(self.append_error(selection, detail)); + } + Ok(()) + } + + // The offsets buffer comes straight from file bytes, so an unchecked build would + // let a corrupt file smuggle out-of-bounds offsets into an Arrow array whose + // consumers then read (or crash on) memory outside the data buffer. This + // boundary therefore always validates the layout, ignoring the optional + // `validate` flag. Lance validates the common layouts itself (a branchless + // scan, measurably cheaper than Arrow's element-wise checked build) and only + // falls back to Arrow's checked build for the cold cases. + fn into_arrow(self, data_type: DataType, _validate: bool) -> Result { + let Some(expected_bits_per_offset) = Self::expected_bits_per_offset(&data_type) else { + // Not an [offsets, bytes] layout we know how to prove; let Arrow + // check it. + return self.into_arrow_checked(data_type); + }; + if self.bits_per_offset != expected_bits_per_offset { + return Err(self.layout_error( + &data_type, + format!( + "expected {}-bit offsets but got {}-bit offsets", + expected_bits_per_offset, self.bits_per_offset + ), + )); + } + if self.num_values == 0 { + // Cold path; Arrow handles the empty-offsets special cases. + return self.into_arrow_checked(data_type); + } + let proof = self.validate_layout(&data_type)?; + Ok(self.into_arrow_unchecked(data_type, proof)) + } + + /// The offset width Arrow mandates for `data_type`, or `None` if the type + /// does not use the `[offsets, bytes]` layout this block represents. + fn expected_bits_per_offset(data_type: &DataType) -> Option { + match data_type { + DataType::Binary | DataType::Utf8 => Some(32), + DataType::LargeBinary | DataType::LargeUtf8 => Some(64), + _ => None, + } + } + + fn layout_error(&self, data_type: &DataType, detail: impl std::fmt::Display) -> Error { + Self::format_layout_error( + data_type, + detail, + self.num_values, + self.bits_per_offset, + self.offsets.len(), + self.data.len(), + ) + } + + fn format_layout_error( + data_type: &DataType, + detail: impl std::fmt::Display, + num_values: u64, + bits_per_offset: u8, + offsets_size: usize, + data_size: usize, + ) -> Error { + Error::corrupt_file_named( + "variable width data block", + format!( + "invalid variable-width layout for {}: {} (num_values: {}, bits_per_offset: {}, \ + offsets buffer size: {} bytes, data buffer size: {} bytes)", + data_type, detail, num_values, bits_per_offset, offsets_size, data_size, + ), + ) + } + + fn validate_layout(&self, data_type: &DataType) -> Result { + let bytes_per_offset = (self.bits_per_offset / 8) as u64; + let required_bytes = self + .num_values + .checked_add(1) + .and_then(|num_offsets| num_offsets.checked_mul(bytes_per_offset)) + .ok_or_else(|| self.layout_error(data_type, "offsets buffer size overflows"))?; + if (self.offsets.len() as u64) < required_bytes { + return Err(self.layout_error( + data_type, + format!( + "offsets buffer must hold at least {} offsets ({} bytes)", + self.num_values + 1, + required_bytes + ), + )); + } + let validate_utf8 = matches!(data_type, DataType::Utf8 | DataType::LargeUtf8); + match self.bits_per_offset { + 32 => self.validate_offsets_and_values::(data_type, validate_utf8), + 64 => self.validate_offsets_and_values::(data_type, validate_utf8), + other => Err(self.layout_error( + data_type, + format!("unsupported offset width: {} bits", other), + )), + } + } + + fn validate_offsets_and_values( + &self, + data_type: &DataType, + validate_utf8: bool, + ) -> Result { + let num_offsets = self.num_values as usize + 1; + // Slice before borrowing: the buffer may carry padding that is not a + // multiple of the offset width. + let offsets = self + .offsets + .slice_with_length(0, num_offsets * std::mem::size_of::()); + let offsets = offsets.borrow_to_typed_slice::(); + let offsets: &[T] = offsets.as_ref(); + let data = self.data.as_ref(); + + if let Some(detail) = Self::offset_violation_detail(offsets, data.len(), 0) { + return Err(self.layout_error(data_type, detail)); + } + + if validate_utf8 { + let (first, last) = (offsets[0].as_usize(), offsets[num_offsets - 1].as_usize()); + let values = std::str::from_utf8(&data[first..last]) + .map_err(|utf8_err| self.layout_error(data_type, utf8_err))?; + let mut on_char_boundaries = true; + for &offset in offsets { + on_char_boundaries &= values.is_char_boundary(offset.as_usize() - first); + } + if !on_char_boundaries { + // Cold path: rescan to pinpoint the offending offset. + let position = offsets + .iter() + .position(|offset| !values.is_char_boundary(offset.as_usize() - first)) + .expect("the fast scan found a non-boundary offset"); + return Err(self.layout_error( + data_type, + format!("offset at position {position} splits a UTF-8 character"), + )); + } + } + + Ok(ValidVariableWidthLayout) + } + + fn offset_violation_detail( + offsets: &[T], + data_size: usize, + position_base: usize, + ) -> Option { + // A monotonic sequence with a non-negative first offset and an + // in-bounds last offset is entirely within [0, data_size]. Keep this + // valid path branchless so it vectorizes, and only rescan on failure. + let mut is_monotonic = true; + for window in offsets.windows(2) { + is_monotonic &= window[0] <= window[1]; + } + let first = offsets[0]; + let last = offsets[offsets.len() - 1]; + let bounds_ok = + first >= T::usize_as(0) && last.to_usize().is_some_and(|last| last <= data_size); + if is_monotonic && bounds_ok { + return None; + } + + for (relative_position, window) in offsets.windows(2).enumerate() { + if window[0] > window[1] { + let position = position_base + relative_position + 1; + return Some(format!( + "non-monotonic offset at position {}: {:?} decreases from {:?}", + position, window[1], window[0] + )); + } + } + for (relative_position, offset) in offsets.iter().enumerate() { + let position = position_base + relative_position; + match offset.to_usize() { + None => { + return Some(format!( + "offset at position {} is negative: {:?}", + position, offset + )); + } + Some(offset) if offset > data_size => { + return Some(format!( + "offset at position {} is out of bounds: {} > {}", + position, offset, data_size + )); + } + Some(_) => {} + } + } + Some("offsets failed validation".to_string()) + } + + fn into_arrow_checked(self, data_type: DataType) -> Result { + let num_values = self.num_values; + let bits_per_offset = self.bits_per_offset; + let offsets_size = self.offsets.len(); + let data_size = self.data.len(); + let builder = self.into_arrow_builder(data_type.clone()); + builder.build().map_err(|arrow_err| { + Self::format_layout_error( + &data_type, + arrow_err, + num_values, + bits_per_offset, + offsets_size, + data_size, + ) + }) + } + + fn into_arrow_unchecked( + self, + data_type: DataType, + _proof: ValidVariableWidthLayout, + ) -> ArrayData { + let builder = self.into_arrow_builder(data_type); + // SAFETY: `_proof` witnesses that `validate_layout` proved this block + // satisfies the Arrow layout contract for `data_type`. + unsafe { builder.build_unchecked() } + } + + fn into_arrow_builder(self, data_type: DataType) -> ArrayDataBuilder { + let num_values = self.num_values; + let data_buffer = self.data.into_buffer(); + let offsets_buffer = self.offsets.into_buffer(); + ArrayDataBuilder::new(data_type) + .add_buffer(offsets_buffer) + .add_buffer(data_buffer) + .len(num_values as usize) + .null_count(0) + } + + fn into_buffers(self) -> Vec { + vec![self.offsets, self.data] + } + + pub fn offsets_as_block(&mut self) -> DataBlock { + let offsets = self.offsets.clone(); + DataBlock::FixedWidth(FixedWidthDataBlock { + data: offsets, + bits_per_value: self.bits_per_offset as u64, + num_values: self.num_values + 1, + block_info: BlockInfo::new(), + }) + } + + pub fn data_size(&self) -> u64 { + (self.data.len() + self.offsets.len()) as u64 + } +} + +/// A data block representing a struct +#[derive(Debug, Clone)] +pub struct StructDataBlock { + /// The child arrays + pub children: Vec, + pub block_info: BlockInfo, + /// The validity bitmap for the struct (None means all valid) + pub validity: Option, +} + +impl StructDataBlock { + fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + if let DataType::Struct(fields) = &data_type { + let mut builder = ArrayDataBuilder::new(DataType::Struct(fields.clone())); + let mut num_rows = 0; + for (field, child) in fields.iter().zip(self.children) { + let child_data = child.into_arrow(field.data_type().clone(), validate)?; + num_rows = child_data.len(); + builder = builder.add_child_data(child_data); + } + + // Apply validity if present + let builder = if let Some(validity) = self.validity { + let null_count = validity.null_count(); + builder + .null_bit_buffer(Some(validity.into_inner().into_inner())) + .null_count(null_count) + } else { + builder.null_count(0) + }; + + let builder = builder.len(num_rows); + if validate { + Ok(builder.build()?) + } else { + Ok(unsafe { builder.build_unchecked() }) + } + } else { + Err(Error::internal(format!( + "Expected Struct, got {:?}", + data_type + ))) + } + } + + fn remove_outer_validity(self) -> Self { + Self { + children: self + .children + .into_iter() + .map(|c| c.remove_outer_validity()) + .collect(), + block_info: self.block_info, + validity: None, // Remove the validity + } + } + + fn into_buffers(self) -> Vec { + self.children + .into_iter() + .flat_map(|c| c.into_buffers()) + .collect() + } + + pub fn has_variable_width_child(&self) -> bool { + self.children + .iter() + .any(|child| !matches!(child, DataBlock::FixedWidth(_))) + } + + pub fn data_size(&self) -> u64 { + self.children + .iter() + .map(|data_block| data_block.data_size()) + .sum() + } +} + +/// A data block for dictionary encoded data +#[derive(Debug, Clone)] +pub struct DictionaryDataBlock { + /// The indices buffer + pub indices: FixedWidthDataBlock, + /// The dictionary itself + pub dictionary: Box, +} + +impl DictionaryDataBlock { + fn decode_helper(self) -> Result { + // Handle empty batch - this can happen when decoding a range that contains + // only empty/null lists, or when reading sparse data + if self.indices.num_values == 0 { + return Ok(DataBlock::AllNull(AllNullDataBlock { num_values: 0 })); + } + + // assume the indices are uniformly distributed. + let estimated_size_bytes = self.dictionary.data_size() + * (self.indices.num_values + self.dictionary.num_values() - 1) + / self.dictionary.num_values(); + let mut data_builder = DataBlockBuilder::with_capacity_estimate(estimated_size_bytes); + + let indices = self.indices.data.borrow_to_typed_slice::(); + let indices = indices.as_ref(); + + let selections = indices.iter().map(|idx| { + let idx = idx.to_usize().unwrap() as u64; + idx..idx + 1 + }); + data_builder.append_ranges(&self.dictionary, selections)?; + + Ok(data_builder.finish()) + } + + pub fn decode(self) -> Result { + match self.indices.bits_per_value { + 8 => self.decode_helper::(), + 16 => self.decode_helper::(), + 32 => self.decode_helper::(), + 64 => self.decode_helper::(), + _ => Err(lance_core::Error::internal(format!( + "Unsupported dictionary index bit width: {} bits", + self.indices.bits_per_value + ))), + } + } + + fn into_arrow_dict( + self, + key_type: Box, + value_type: Box, + validate: bool, + ) -> Result { + let indices = self.indices.into_arrow((*key_type).clone(), validate)?; + let dictionary = self + .dictionary + .into_arrow((*value_type).clone(), validate)?; + + let builder = indices + .into_builder() + .add_child_data(dictionary) + .data_type(DataType::Dictionary(key_type, value_type)); + + if validate { + Ok(builder.build()?) + } else { + Ok(unsafe { builder.build_unchecked() }) + } + } + + fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + if let DataType::Dictionary(key_type, value_type) = data_type { + self.into_arrow_dict(key_type, value_type, validate) + } else { + self.decode()?.into_arrow(data_type, validate) + } + } + + fn into_buffers(self) -> Vec { + let mut buffers = self.indices.into_buffers(); + buffers.extend(self.dictionary.into_buffers()); + buffers + } + + pub fn into_parts(self) -> (DataBlock, DataBlock) { + (DataBlock::FixedWidth(self.indices), *self.dictionary) + } + + pub fn from_parts(indices: FixedWidthDataBlock, dictionary: DataBlock) -> Self { + Self { + indices, + dictionary: Box::new(dictionary), + } + } +} + +/// A DataBlock is a collection of buffers that represents an "array" of data in very generic terms +/// +/// The output of each decoder is a DataBlock. Decoders can be chained together to transform +/// one DataBlock into a different kind of DataBlock. +/// +/// The DataBlock is somewhere in between Arrow's ArrayData and Array and represents a physical +/// layout of the data. +/// +/// A DataBlock can be converted into an Arrow ArrayData (and then Array) for a given array type. +/// For example, a FixedWidthDataBlock can be converted into any primitive type or a fixed size +/// list of a primitive type. This is a zero-copy operation. +/// +/// In addition, a DataBlock can be created from an Arrow array or arrays. This is not a zero-copy +/// operation as some normalization may be required. +#[derive(Debug, Clone)] +pub enum DataBlock { + Empty(), + Constant(ConstantDataBlock), + AllNull(AllNullDataBlock), + Nullable(NullableDataBlock), + FixedWidth(FixedWidthDataBlock), + FixedSizeList(FixedSizeListBlock), + VariableWidth(VariableWidthBlock), + Opaque(OpaqueBlock), + Struct(StructDataBlock), + Dictionary(DictionaryDataBlock), +} + +impl DataBlock { + /// Convert self into an Arrow ArrayData + pub fn into_arrow(self, data_type: DataType, validate: bool) -> Result { + match self { + Self::Empty() => Ok(new_empty_array(&data_type).to_data()), + Self::Constant(inner) => inner.into_arrow(data_type, validate), + Self::AllNull(inner) => inner.into_arrow(data_type, validate), + Self::Nullable(inner) => inner.into_arrow(data_type, validate), + Self::FixedWidth(inner) => inner.into_arrow(data_type, validate), + Self::FixedSizeList(inner) => inner.into_arrow(data_type, validate), + Self::VariableWidth(inner) => inner.into_arrow(data_type, validate), + Self::Struct(inner) => inner.into_arrow(data_type, validate), + Self::Dictionary(inner) => inner.into_arrow(data_type, validate), + Self::Opaque(_) => Err(Error::internal( + "Cannot convert OpaqueBlock to Arrow".to_string(), + )), + } + } + + /// Convert the data block into a collection of buffers for serialization + /// + /// The order matters and will be used to reconstruct the data block at read time. + pub fn into_buffers(self) -> Vec { + match self { + Self::Empty() => Vec::default(), + Self::Constant(inner) => inner.into_buffers(), + Self::AllNull(inner) => inner.into_buffers(), + Self::Nullable(inner) => inner.into_buffers(), + Self::FixedWidth(inner) => inner.into_buffers(), + Self::FixedSizeList(inner) => inner.into_buffers(), + Self::VariableWidth(inner) => inner.into_buffers(), + Self::Struct(inner) => inner.into_buffers(), + Self::Dictionary(inner) => inner.into_buffers(), + Self::Opaque(inner) => inner.buffers, + } + } + + /// Converts the data buffers into borrowed mode and clones the block + /// + /// This is a zero-copy operation but requires a mutable reference to self and, afterwards, + /// all buffers will be in Borrowed mode. + /// Try and clone the block + /// + /// This will fail if any buffers are in owned mode. You can call borrow_and_clone() to + /// ensure that all buffers are in borrowed mode before calling this method. + pub fn try_clone(&self) -> Result { + match self { + Self::Empty() => Ok(Self::Empty()), + Self::Constant(inner) => Ok(Self::Constant(inner.clone())), + Self::AllNull(inner) => Ok(Self::AllNull(inner.clone())), + Self::Nullable(inner) => Ok(Self::Nullable(inner.clone())), + Self::FixedWidth(inner) => Ok(Self::FixedWidth(inner.clone())), + Self::FixedSizeList(inner) => Ok(Self::FixedSizeList(inner.clone())), + Self::VariableWidth(inner) => Ok(Self::VariableWidth(inner.clone())), + Self::Struct(inner) => Ok(Self::Struct(inner.clone())), + Self::Dictionary(inner) => Ok(Self::Dictionary(inner.clone())), + Self::Opaque(inner) => Ok(Self::Opaque(inner.clone())), + } + } + + pub fn name(&self) -> &'static str { + match self { + Self::Constant(_) => "Constant", + Self::Empty() => "Empty", + Self::AllNull(_) => "AllNull", + Self::Nullable(_) => "Nullable", + Self::FixedWidth(_) => "FixedWidth", + Self::FixedSizeList(_) => "FixedSizeList", + Self::VariableWidth(_) => "VariableWidth", + Self::Struct(_) => "Struct", + Self::Dictionary(_) => "Dictionary", + Self::Opaque(_) => "Opaque", + } + } + + pub fn is_variable(&self) -> bool { + match self { + Self::Constant(_) => false, + Self::Empty() => false, + Self::AllNull(_) => false, + Self::Nullable(nullable) => nullable.data.is_variable(), + Self::FixedWidth(_) => false, + Self::FixedSizeList(fsl) => fsl.child.is_variable(), + Self::VariableWidth(_) => true, + Self::Struct(strct) => strct.children.iter().any(|c| c.is_variable()), + Self::Dictionary(_) => { + todo!("is_variable for DictionaryDataBlock is not implemented yet") + } + Self::Opaque(_) => panic!("Does not make sense to ask if an Opaque block is variable"), + } + } + + pub fn is_nullable(&self) -> bool { + match self { + Self::AllNull(_) => true, + Self::Nullable(_) => true, + Self::FixedSizeList(fsl) => fsl.child.is_nullable(), + Self::Struct(strct) => strct.children.iter().any(|c| c.is_nullable()), + Self::Dictionary(_) => { + todo!("is_nullable for DictionaryDataBlock is not implemented yet") + } + Self::Opaque(_) => panic!("Does not make sense to ask if an Opaque block is nullable"), + _ => false, + } + } + + /// The number of values in the block + /// + /// This function does not recurse into child blocks. If this is a FSL then it will + /// be the number of lists and not the number of items. + pub fn num_values(&self) -> u64 { + match self { + Self::Empty() => 0, + Self::Constant(inner) => inner.num_values, + Self::AllNull(inner) => inner.num_values, + Self::Nullable(inner) => inner.data.num_values(), + Self::FixedWidth(inner) => inner.num_values, + Self::FixedSizeList(inner) => inner.num_values(), + Self::VariableWidth(inner) => inner.num_values, + Self::Struct(inner) => inner.children[0].num_values(), + Self::Dictionary(inner) => inner.indices.num_values, + Self::Opaque(inner) => inner.num_values, + } + } + + /// The number of items in a single row + /// + /// This is always 1 unless there are layers of FSL + pub fn items_per_row(&self) -> u64 { + match self { + Self::Empty() => todo!(), // Leave undefined until needed + Self::Constant(_) => todo!(), // Leave undefined until needed + Self::AllNull(_) => todo!(), // Leave undefined until needed + Self::Nullable(nullable) => nullable.data.items_per_row(), + Self::FixedWidth(_) => 1, + Self::FixedSizeList(fsl) => fsl.dimension * fsl.child.items_per_row(), + Self::VariableWidth(_) => 1, + Self::Struct(_) => todo!(), // Leave undefined until needed + Self::Dictionary(_) => 1, + Self::Opaque(_) => 1, + } + } + + /// The number of bytes in the data block (including any child blocks) + pub fn data_size(&self) -> u64 { + match self { + Self::Empty() => 0, + Self::Constant(inner) => inner.data_size(), + Self::AllNull(_) => 0, + Self::Nullable(inner) => inner.data_size(), + Self::FixedWidth(inner) => inner.data_size(), + Self::FixedSizeList(inner) => inner.data_size(), + Self::VariableWidth(inner) => inner.data_size(), + Self::Struct(inner) => inner.children.iter().map(|child| child.data_size()).sum(), + Self::Dictionary(inner) => inner.indices.data_size() + inner.dictionary.data_size(), + Self::Opaque(inner) => inner.data_size(), + } + } + + /// Removes any validity information from the block + /// + /// This does not filter the block (e.g. remove rows). It only removes + /// the validity bitmaps (if present). Any garbage masked by null bits + /// will now appear as proper values. + /// + /// If `recurse` is true, then this will also remove validity from any child blocks. + pub fn remove_outer_validity(self) -> Self { + match self { + Self::AllNull(_) => panic!("Cannot remove validity on all-null data"), + Self::Nullable(inner) => *inner.data, + Self::Struct(inner) => Self::Struct(inner.remove_outer_validity()), + other => other, + } + } + + fn make_builder(&self, estimated_size_bytes: u64) -> Box { + match self { + Self::FixedWidth(inner) => { + if inner.bits_per_value == 1 { + Box::new(BitmapDataBlockBuilder::new(estimated_size_bytes)) + } else { + Box::new(FixedWidthDataBlockBuilder::new( + inner.bits_per_value, + estimated_size_bytes, + )) + } + } + Self::VariableWidth(inner) => { + if inner.bits_per_offset == 32 { + Box::new(VariableWidthDataBlockBuilder::::new( + estimated_size_bytes, + )) + } else if inner.bits_per_offset == 64 { + Box::new(VariableWidthDataBlockBuilder::::new( + estimated_size_bytes, + )) + } else { + todo!() + } + } + Self::FixedSizeList(inner) => { + let inner_builder = inner.child.make_builder(estimated_size_bytes); + Box::new(FixedSizeListBlockBuilder::new( + inner_builder, + inner.dimension, + )) + } + Self::Nullable(nullable) => { + // There's no easy way to know what percentage of the data is in the valiidty buffer + // but 1/16th seems like a reasonable guess. + let estimated_validity_size_bytes = estimated_size_bytes / 16; + let inner_builder = nullable + .data + .make_builder(estimated_size_bytes - estimated_validity_size_bytes); + Box::new(NullableDataBlockBuilder::new( + inner_builder, + estimated_validity_size_bytes as usize, + )) + } + Self::Struct(struct_data_block) => { + let num_children = struct_data_block.children.len(); + let per_child_estimate = if num_children == 0 { + 0 + } else { + estimated_size_bytes / num_children as u64 + }; + let child_builders = struct_data_block + .children + .iter() + .map(|child| child.make_builder(per_child_estimate)) + .collect(); + Box::new(StructDataBlockBuilder::new(child_builders)) + } + Self::AllNull(_) => Box::new(AllNullDataBlockBuilder::default()), + _ => todo!("make_builder for {:?}", self), + } + } +} + +macro_rules! as_type { + ($fn_name:ident, $inner:tt, $inner_type:ident) => { + pub fn $fn_name(self) -> Option<$inner_type> { + match self { + Self::$inner(inner) => Some(inner), + _ => None, + } + } + }; +} + +macro_rules! as_type_ref { + ($fn_name:ident, $inner:tt, $inner_type:ident) => { + pub fn $fn_name(&self) -> Option<&$inner_type> { + match self { + Self::$inner(inner) => Some(inner), + _ => None, + } + } + }; +} + +macro_rules! as_type_ref_mut { + ($fn_name:ident, $inner:tt, $inner_type:ident) => { + pub fn $fn_name(&mut self) -> Option<&mut $inner_type> { + match self { + Self::$inner(inner) => Some(inner), + _ => None, + } + } + }; +} + +// Cast implementations +impl DataBlock { + as_type!(as_all_null, AllNull, AllNullDataBlock); + as_type!(as_nullable, Nullable, NullableDataBlock); + as_type!(as_fixed_width, FixedWidth, FixedWidthDataBlock); + as_type!(as_fixed_size_list, FixedSizeList, FixedSizeListBlock); + as_type!(as_variable_width, VariableWidth, VariableWidthBlock); + as_type!(as_struct, Struct, StructDataBlock); + as_type!(as_dictionary, Dictionary, DictionaryDataBlock); + as_type_ref!(as_all_null_ref, AllNull, AllNullDataBlock); + as_type_ref!(as_nullable_ref, Nullable, NullableDataBlock); + as_type_ref!(as_fixed_width_ref, FixedWidth, FixedWidthDataBlock); + as_type_ref!(as_fixed_size_list_ref, FixedSizeList, FixedSizeListBlock); + as_type_ref!(as_variable_width_ref, VariableWidth, VariableWidthBlock); + as_type_ref!(as_struct_ref, Struct, StructDataBlock); + as_type_ref!(as_dictionary_ref, Dictionary, DictionaryDataBlock); + as_type_ref_mut!(as_all_null_ref_mut, AllNull, AllNullDataBlock); + as_type_ref_mut!(as_nullable_ref_mut, Nullable, NullableDataBlock); + as_type_ref_mut!(as_fixed_width_ref_mut, FixedWidth, FixedWidthDataBlock); + as_type_ref_mut!( + as_fixed_size_list_ref_mut, + FixedSizeList, + FixedSizeListBlock + ); + as_type_ref_mut!(as_variable_width_ref_mut, VariableWidth, VariableWidthBlock); + as_type_ref_mut!(as_struct_ref_mut, Struct, StructDataBlock); + as_type_ref_mut!(as_dictionary_ref_mut, Dictionary, DictionaryDataBlock); +} + +// Methods to convert from Arrow -> DataBlock + +fn get_byte_range(offsets: &mut LanceBuffer) -> Range { + let offsets = offsets.borrow_to_typed_slice::(); + if offsets.as_ref().is_empty() { + 0..0 + } else { + offsets.as_ref().first().unwrap().as_usize()..offsets.as_ref().last().unwrap().as_usize() + } +} + +// Given multiple offsets arrays [0, 5, 10], [0, 3, 7], etc. stitch +// them together to get [0, 5, 10, 13, 20, ...] +// +// Also returns the data range referenced by each offset array (may +// not be 0..len if there is slicing involved) +fn stitch_offsets + std::ops::Sub>( + offsets: Vec, +) -> (LanceBuffer, Vec>) { + if offsets.is_empty() { + return (LanceBuffer::empty(), Vec::default()); + } + let len = offsets.iter().map(|b| b.len()).sum::(); + // Note: we are making a copy here, even if there is only one input, because we want to + // normalize that input if it doesn't start with zero. This could be micro-optimized out + // if needed. + let mut dest = Vec::with_capacity(len); + let mut byte_ranges = Vec::with_capacity(offsets.len()); + + // We insert one leading 0 before processing any of the inputs + dest.push(T::from_usize(0).unwrap()); + + for mut o in offsets.into_iter() { + if !o.is_empty() { + let last_offset = *dest.last().unwrap(); + let o = o.borrow_to_typed_slice::(); + let start = *o.as_ref().first().unwrap(); + // First, we skip the first offset + // Then, we subtract that first offset from each remaining offset + // + // This gives us a 0-based offset array (minus the leading 0) + // + // Then we add the last offset from the previous array to each offset + // which shifts our offset array to the correct position + // + // For example, let's assume the last offset from the previous array + // was 10 and we are given [13, 17, 22]. This means we have two values with + // length 4 (17 - 13) and 5 (22 - 17). The output from this step will be + // [14, 19]. Combined with our last offset of 10, this gives us [10, 14, 19] + // which is our same two values of length 4 and 5. + dest.extend(o.as_ref()[1..].iter().map(|&x| x + last_offset - start)); + } + byte_ranges.push(get_byte_range::(&mut o)); + } + (LanceBuffer::reinterpret_vec(dest), byte_ranges) +} + +fn arrow_binary_to_data_block( + arrays: &[ArrayRef], + num_values: u64, + bits_per_offset: u8, +) -> DataBlock { + let data_vec = arrays.iter().map(|arr| arr.to_data()).collect::>(); + let bytes_per_offset = bits_per_offset as usize / 8; + let offsets = data_vec + .iter() + .map(|d| { + LanceBuffer::from( + d.buffers()[0].slice_with_length(d.offset(), (d.len() + 1) * bytes_per_offset), + ) + }) + .collect::>(); + let (offsets, data_ranges) = if bits_per_offset == 32 { + stitch_offsets::(offsets) + } else { + stitch_offsets::(offsets) + }; + let data = data_vec + .iter() + .zip(data_ranges) + .map(|(d, byte_range)| { + LanceBuffer::from( + d.buffers()[1] + .slice_with_length(byte_range.start, byte_range.end - byte_range.start), + ) + }) + .collect::>(); + let data = LanceBuffer::concat_into_one(data); + DataBlock::VariableWidth(VariableWidthBlock { + data, + offsets, + bits_per_offset, + num_values, + block_info: BlockInfo::new(), + }) +} + +fn encode_flat_data(arrays: &[ArrayRef], num_values: u64) -> LanceBuffer { + let bytes_per_value = arrays[0].data_type().byte_width(); + let mut buffer = Vec::with_capacity(num_values as usize * bytes_per_value); + for arr in arrays { + let data = arr.to_data(); + buffer.extend_from_slice(data.buffers()[0].as_slice()); + } + LanceBuffer::from(buffer) +} + +fn do_encode_bitmap_data(bitmaps: &[BooleanBuffer], num_values: u64) -> LanceBuffer { + let mut builder = BooleanBufferBuilder::new(num_values as usize); + + for buf in bitmaps { + builder.append_buffer(buf); + } + + let buffer = builder.finish().into_inner(); + LanceBuffer::from(buffer) +} + +fn encode_bitmap_data(arrays: &[ArrayRef], num_values: u64) -> LanceBuffer { + let bitmaps = arrays + .iter() + .map(|arr| arr.as_boolean().values().clone()) + .collect::>(); + do_encode_bitmap_data(&bitmaps, num_values) +} + +// Concatenate dictionary arrays. This is a bit tricky because we might overflow the +// index type. If we do, we need to upscale the indices to a larger type. +fn concat_dict_arrays(arrays: &[ArrayRef]) -> ArrayRef { + let value_type = arrays[0].as_any_dictionary().values().data_type(); + let array_refs = arrays.iter().map(|arr| arr.as_ref()).collect::>(); + match arrow_select::concat::concat(&array_refs) { + Ok(array) => array, + Err(arrow_schema::ArrowError::DictionaryKeyOverflowError) => { + // Slow, but hopefully a corner case. Optimize later + let upscaled = array_refs + .iter() + .map(|arr| { + match arrow_cast::cast( + *arr, + &DataType::Dictionary( + Box::new(DataType::UInt32), + Box::new(value_type.clone()), + ), + ) { + Ok(arr) => arr, + Err(arrow_schema::ArrowError::DictionaryKeyOverflowError) => { + // Technically I think this means the input type was u64 already + unimplemented!("Dictionary arrays with more than 2^32 unique values") + } + err => err.unwrap(), + } + }) + .collect::>(); + let array_refs = upscaled.iter().map(|arr| arr.as_ref()).collect::>(); + // Can still fail if concat pushes over u32 boundary + match arrow_select::concat::concat(&array_refs) { + Ok(array) => array, + Err(arrow_schema::ArrowError::DictionaryKeyOverflowError) => { + unimplemented!("Dictionary arrays with more than 2^32 unique values") + } + err => err.unwrap(), + } + } + // Shouldn't be any other possible errors in concat + err => err.unwrap(), + } +} + +fn max_index_val(index_type: &DataType) -> u64 { + match index_type { + DataType::Int8 => i8::MAX as u64, + DataType::Int16 => i16::MAX as u64, + DataType::Int32 => i32::MAX as u64, + DataType::Int64 => i64::MAX as u64, + DataType::UInt8 => u8::MAX as u64, + DataType::UInt16 => u16::MAX as u64, + DataType::UInt32 => u32::MAX as u64, + DataType::UInt64 => u64::MAX, + _ => panic!("Invalid dictionary index type"), + } +} + +// If we get multiple dictionary arrays and they don't all have the same dictionary +// then we need to normalize the indices. Otherwise we might have something like: +// +// First chunk ["hello", "foo"], [0, 0, 1, 1, 1] +// Second chunk ["bar", "world"], [0, 1, 0, 1, 1] +// +// If we simply encode as ["hello", "foo", "bar", "world"], [0, 0, 1, 1, 1, 0, 1, 0, 1, 1] +// then we will get the wrong answer because the dictionaries were not merged and the indices +// were not remapped. +// +// A simple way to do this today is to just concatenate all the arrays. This is because +// arrow's dictionary concatenation function already has the logic to merge dictionaries. +// +// TODO: We could be more efficient here by checking if the dictionaries are the same +// Also, if they aren't, we can possibly do something cheaper than concatenating +// +// In addition, we want to normalize the representation of nulls. The cheapest thing to +// do (space-wise) is to put the nulls in the dictionary. +fn arrow_dictionary_to_data_block(arrays: &[ArrayRef], validity: Option) -> DataBlock { + let array = concat_dict_arrays(arrays); + let array_dict = array.as_any_dictionary(); + let mut indices = array_dict.keys(); + let num_values = indices.len() as u64; + let mut values = array_dict.values().clone(); + // Placeholder, if we need to upcast, we will initialize this and set `indices` to refer to it + let mut upcast = None; + + // TODO: Should we just always normalize indices to u32? That would make logic simpler + // and we're going to bitpack them soon anyways + + let indices_block = if let Some(validity) = validity { + // If there is validity then we find the first invalid index in the dictionary values, inserting + // a new value if we need to. Then we change all indices to point to that value. This way we + // never need to store nullability of the indices. + let mut first_invalid_index = None; + if let Some(values_validity) = values.nulls() { + first_invalid_index = (!values_validity.inner()).set_indices().next(); + } + let first_invalid_index = first_invalid_index.unwrap_or_else(|| { + let null_arr = new_null_array(values.data_type(), 1); + values = arrow_select::concat::concat(&[values.as_ref(), null_arr.as_ref()]).unwrap(); + let null_index = values.len() - 1; + let max_index_val = max_index_val(indices.data_type()); + if null_index as u64 > max_index_val { + // Widen the index type + if max_index_val >= u32::MAX as u64 { + unimplemented!("Dictionary arrays with 2^32 unique value (or more) and a null") + } + upcast = Some(arrow_cast::cast(indices, &DataType::UInt32).unwrap()); + indices = upcast.as_ref().unwrap(); + } + null_index + }); + // This can't fail since we already checked for fit + let null_index_arr = arrow_cast::cast( + &UInt64Array::from(vec![first_invalid_index as u64]), + indices.data_type(), + ) + .unwrap(); + + let bytes_per_index = indices.data_type().byte_width(); + let bits_per_index = bytes_per_index as u64 * 8; + + let null_index_arr = null_index_arr.into_data(); + let null_index_bytes = &null_index_arr.buffers()[0]; + // Need to make a copy here since indices isn't mutable, could be avoided in theory + let mut indices_bytes = indices.to_data().buffers()[0].to_vec(); + for invalid_idx in (!validity.inner()).set_indices() { + indices_bytes[invalid_idx * bytes_per_index..(invalid_idx + 1) * bytes_per_index] + .copy_from_slice(null_index_bytes.as_slice()); + } + FixedWidthDataBlock { + data: LanceBuffer::from(indices_bytes), + bits_per_value: bits_per_index, + num_values, + block_info: BlockInfo::new(), + } + } else { + FixedWidthDataBlock { + data: LanceBuffer::from(indices.to_data().buffers()[0].clone()), + bits_per_value: indices.data_type().byte_width() as u64 * 8, + num_values, + block_info: BlockInfo::new(), + } + }; + + let items = DataBlock::from(values); + DataBlock::Dictionary(DictionaryDataBlock { + indices: indices_block, + dictionary: Box::new(items), + }) +} + +enum Nullability { + None, + All, + Some(NullBuffer), +} + +impl Nullability { + fn to_option(&self) -> Option { + match self { + Self::Some(nulls) => Some(nulls.clone()), + _ => None, + } + } +} + +fn extract_nulls(arrays: &[ArrayRef], num_values: u64) -> Nullability { + let mut has_nulls = false; + let nulls_and_lens = arrays + .iter() + .map(|arr| { + let nulls = arr.logical_nulls(); + has_nulls |= nulls.is_some(); + (nulls, arr.len()) + }) + .collect::>(); + if !has_nulls { + return Nullability::None; + } + let mut builder = BooleanBufferBuilder::new(num_values as usize); + let mut num_nulls = 0; + for (null, len) in nulls_and_lens { + if let Some(null) = null { + num_nulls += null.null_count(); + builder.append_buffer(&null.into_inner()); + } else { + builder.append_n(len, true); + } + } + if num_nulls == num_values as usize { + Nullability::All + } else { + Nullability::Some(NullBuffer::new(builder.finish())) + } +} + +impl DataBlock { + pub fn from_arrays(arrays: &[ArrayRef], num_values: u64) -> Self { + if arrays.is_empty() || num_values == 0 { + return Self::AllNull(AllNullDataBlock { num_values: 0 }); + } + + let data_type = arrays[0].data_type(); + let nulls = extract_nulls(arrays, num_values); + + if let Nullability::All = nulls { + return Self::AllNull(AllNullDataBlock { num_values }); + } + + let mut encoded = match data_type { + DataType::Binary | DataType::Utf8 => arrow_binary_to_data_block(arrays, num_values, 32), + // View types have no Lance disk representation; cast to the classic offset layout. + DataType::Utf8View => { + let casted: Vec = arrays + .iter() + .map(|a| { + arrow_cast::cast(a.as_ref(), &DataType::Utf8) + .expect("Utf8View to Utf8 cast is always valid") + }) + .collect(); + arrow_binary_to_data_block(&casted, num_values, 32) + } + DataType::BinaryView => { + let casted: Vec = arrays + .iter() + .map(|a| { + arrow_cast::cast(a.as_ref(), &DataType::Binary) + .expect("BinaryView to Binary cast is always valid") + }) + .collect(); + arrow_binary_to_data_block(&casted, num_values, 32) + } + DataType::LargeBinary | DataType::LargeUtf8 => { + arrow_binary_to_data_block(arrays, num_values, 64) + } + DataType::Boolean => { + let data = encode_bitmap_data(arrays, num_values); + Self::FixedWidth(FixedWidthDataBlock { + data, + bits_per_value: 1, + num_values, + block_info: BlockInfo::new(), + }) + } + DataType::Date32 + | DataType::Date64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Decimal256(_, _) + | DataType::Duration(_) + | DataType::FixedSizeBinary(_) + | DataType::Float16 + | DataType::Float32 + | DataType::Float64 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Int8 + | DataType::Interval(_) + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::UInt16 + | DataType::UInt32 + | DataType::UInt64 + | DataType::UInt8 => { + let data = encode_flat_data(arrays, num_values); + Self::FixedWidth(FixedWidthDataBlock { + data, + bits_per_value: data_type.byte_width() as u64 * 8, + num_values, + block_info: BlockInfo::new(), + }) + } + DataType::Null => Self::AllNull(AllNullDataBlock { num_values }), + DataType::Dictionary(_, _) => arrow_dictionary_to_data_block(arrays, nulls.to_option()), + DataType::Struct(fields) => { + let structs = arrays.iter().map(|arr| arr.as_struct()).collect::>(); + let mut children = Vec::with_capacity(fields.len()); + for child_idx in 0..fields.len() { + let child_vec = structs + .iter() + .map(|s| s.column(child_idx).clone()) + .collect::>(); + children.push(Self::from_arrays(&child_vec, num_values)); + } + + // Extract validity for the struct array + let validity = match &nulls { + Nullability::None => None, + Nullability::Some(null_buffer) => Some(null_buffer.clone()), + Nullability::All => unreachable!("Should have returned AllNull earlier"), + }; + + Self::Struct(StructDataBlock { + children, + block_info: BlockInfo::default(), + validity, + }) + } + DataType::FixedSizeList(_, dim) => { + let children = arrays + .iter() + .map(|arr| arr.as_fixed_size_list().values().clone()) + .collect::>(); + let child_block = Self::from_arrays(&children, num_values * *dim as u64); + Self::FixedSizeList(FixedSizeListBlock { + child: Box::new(child_block), + dimension: *dim as u64, + }) + } + DataType::LargeList(_) + | DataType::List(_) + | DataType::ListView(_) + | DataType::LargeListView(_) + | DataType::Map(_, _) + | DataType::RunEndEncoded(_, _) + | DataType::Union(_, _) => { + panic!( + "Field with data type {} cannot be converted to data block", + data_type + ) + } + }; + + // compute statistics + encoded.compute_stat(); + + if !matches!(data_type, DataType::Dictionary(_, _)) { + match nulls { + Nullability::None => encoded, + Nullability::Some(nulls) => Self::Nullable(NullableDataBlock { + data: Box::new(encoded), + nulls: LanceBuffer::from(nulls.into_inner().into_inner()), + block_info: BlockInfo::new(), + }), + _ => unreachable!(), + } + } else { + // Dictionaries already insert the nulls into the dictionary items + encoded + } + } + + pub fn from_array(array: T) -> Self { + let num_values = array.len(); + Self::from_arrays(&[Arc::new(array)], num_values as u64) + } +} + +impl From for DataBlock { + fn from(array: ArrayRef) -> Self { + let num_values = array.len() as u64; + Self::from_arrays(&[array], num_values) + } +} + +trait DataBlockBuilderImpl: std::fmt::Debug { + fn validate_append(&self, data_block: &DataBlock, selection: &Range) -> Result<()>; + + fn append_validated(&mut self, data_block: &DataBlock, selection: Range) -> Result<()>; + + fn append(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + self.validate_append(data_block, &selection)?; + self.append_validated(data_block, selection) + } + + fn finish(self: Box) -> DataBlock; +} + +#[derive(Debug)] +pub struct DataBlockBuilder { + estimated_size_bytes: u64, + builder: Option>, +} + +impl DataBlockBuilder { + pub fn with_capacity_estimate(estimated_size_bytes: u64) -> Self { + Self { + estimated_size_bytes, + builder: None, + } + } + + fn get_builder(&mut self, block: &DataBlock) -> &mut dyn DataBlockBuilderImpl { + if self.builder.is_none() { + self.builder = Some(block.make_builder(self.estimated_size_bytes)); + } + self.builder.as_mut().unwrap().as_mut() + } + + pub fn append(&mut self, data_block: &DataBlock, selection: Range) -> Result<()> { + self.get_builder(data_block).append(data_block, selection) + } + + fn append_ranges( + &mut self, + data_block: &DataBlock, + selections: impl IntoIterator>, + ) -> Result<()> { + let full_selection = 0..data_block.num_values(); + let builder = self.get_builder(data_block); + builder.validate_append(data_block, &full_selection)?; + for selection in selections { + if selection.start > selection.end || selection.end > full_selection.end { + return Err(Error::corrupt_file_named( + "data block", + format!( + "cannot append selection {}..{} from a block with {} values", + selection.start, selection.end, full_selection.end + ), + )); + } + builder.append_validated(data_block, selection)?; + } + Ok(()) + } + + pub fn finish(self) -> DataBlock { + let builder = self.builder.expect("DataBlockBuilder didn't see any data"); + builder.finish() + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{ + ArrayRef, BinaryArray, BinaryViewArray, DictionaryArray, Int8Array, LargeBinaryArray, + LargeStringArray, StringArray, StringViewArray, UInt8Array, UInt16Array, make_array, + new_null_array, + types::{Int8Type, Int32Type}, + }; + use arrow_buffer::{BooleanBuffer, NullBuffer}; + + use arrow_schema::{DataType, Field, Fields}; + use lance_core::Error; + use lance_datagen::{ArrayGeneratorExt, DEFAULT_SEED, RowCount, array}; + use rand::SeedableRng; + use rstest::rstest; + + use crate::buffer::LanceBuffer; + + use super::{ + AllNullDataBlock, BlockInfo, DataBlock, DataBlockBuilder, DictionaryDataBlock, + FixedWidthDataBlock, VariableWidthBlock, + }; + + use arrow_array::Array; + + #[test] + fn test_sliced_to_data_block() { + let ints = UInt16Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]); + let ints = ints.slice(2, 4); + let data = DataBlock::from_array(ints); + + let fixed_data = data.as_fixed_width().unwrap(); + assert_eq!(fixed_data.num_values, 4); + assert_eq!(fixed_data.data.len(), 8); + + let nullable_ints = + UInt16Array::from(vec![Some(0), None, Some(2), None, Some(4), None, Some(6)]); + let nullable_ints = nullable_ints.slice(1, 3); + let data = DataBlock::from_array(nullable_ints); + + let nullable = data.as_nullable().unwrap(); + assert_eq!(nullable.nulls, LanceBuffer::from(vec![0b00000010])); + } + + #[test] + fn test_string_to_data_block() { + // Converting string arrays that contain nulls to DataBlock + let strings1 = StringArray::from(vec![Some("hello"), None, Some("world")]); + let strings2 = StringArray::from(vec![Some("a"), Some("b")]); + let strings3 = StringArray::from(vec![Option::<&'static str>::None, None]); + + let arrays = &[strings1, strings2, strings3] + .iter() + .map(|arr| Arc::new(arr.clone()) as ArrayRef) + .collect::>(); + + let block = DataBlock::from_arrays(arrays, 7); + + assert_eq!(block.num_values(), 7); + let block = block.as_nullable().unwrap(); + + assert_eq!(block.nulls, LanceBuffer::from(vec![0b00011101])); + + let data = block.data.as_variable_width().unwrap(); + assert_eq!( + data.offsets, + LanceBuffer::reinterpret_vec(vec![0, 5, 5, 10, 11, 12, 12, 12]) + ); + + assert_eq!(data.data, LanceBuffer::copy_slice(b"helloworldab")); + + // Converting string arrays that do not contain nulls to DataBlock + let strings1 = StringArray::from(vec![Some("a"), Some("bc")]); + let strings2 = StringArray::from(vec![Some("def")]); + + let arrays = &[strings1, strings2] + .iter() + .map(|arr| Arc::new(arr.clone()) as ArrayRef) + .collect::>(); + + let block = DataBlock::from_arrays(arrays, 3); + + assert_eq!(block.num_values(), 3); + // Should be no nullable wrapper + let data = block.as_variable_width().unwrap(); + assert_eq!(data.offsets, LanceBuffer::reinterpret_vec(vec![0, 1, 3, 6])); + assert_eq!(data.data, LanceBuffer::copy_slice(b"abcdef")); + } + + #[test] + fn test_string_view_to_data_block() { + let views1 = StringViewArray::from(vec![Some("hello"), None, Some("world")]); + let views2 = StringViewArray::from(vec![Some("a"), Some("b")]); + let views3 = StringViewArray::from(vec![Option::<&'static str>::None, None]); + + let arrays = &[views1, views2, views3] + .iter() + .map(|arr| Arc::new(arr.clone()) as ArrayRef) + .collect::>(); + + let block = DataBlock::from_arrays(arrays, 7); + + assert_eq!(block.num_values(), 7); + let block = block.as_nullable().unwrap(); + assert_eq!(block.nulls, LanceBuffer::from(vec![0b00011101])); + let data = block.data.as_variable_width().unwrap(); + assert_eq!( + data.offsets, + LanceBuffer::reinterpret_vec(vec![0, 5, 5, 10, 11, 12, 12, 12]) + ); + assert_eq!(data.data, LanceBuffer::copy_slice(b"helloworldab")); + + let views1 = StringViewArray::from(vec![Some("a"), Some("bc")]); + let views2 = StringViewArray::from(vec![Some("def")]); + + let arrays = &[views1, views2] + .iter() + .map(|arr| Arc::new(arr.clone()) as ArrayRef) + .collect::>(); + + let block = DataBlock::from_arrays(arrays, 3); + + assert_eq!(block.num_values(), 3); + let data = block.as_variable_width().unwrap(); + assert_eq!(data.offsets, LanceBuffer::reinterpret_vec(vec![0, 1, 3, 6])); + assert_eq!(data.data, LanceBuffer::copy_slice(b"abcdef")); + } + + #[test] + fn test_binary_view_to_data_block() { + let arr: ArrayRef = Arc::new(BinaryViewArray::from(vec![ + Some(b"foo".as_slice()), + None, + Some(b"bar".as_slice()), + ])); + let block = DataBlock::from_arrays(&[arr], 3); + let block = block.as_nullable().unwrap(); + let data = block.data.as_variable_width().unwrap(); + assert_eq!(data.data, LanceBuffer::copy_slice(b"foobar")); + } + + #[test] + fn test_string_sliced() { + let check = |arr: Vec, expected_off: Vec, expected_data: &[u8]| { + let arrs = arr + .into_iter() + .map(|a| Arc::new(a) as ArrayRef) + .collect::>(); + let num_rows = arrs.iter().map(|a| a.len()).sum::() as u64; + let data = DataBlock::from_arrays(&arrs, num_rows); + + assert_eq!(data.num_values(), num_rows); + + let data = data.as_variable_width().unwrap(); + assert_eq!(data.offsets, LanceBuffer::reinterpret_vec(expected_off)); + assert_eq!(data.data, LanceBuffer::copy_slice(expected_data)); + }; + + let string = StringArray::from(vec![Some("hello"), Some("world")]); + check(vec![string.slice(1, 1)], vec![0, 5], b"world"); + check(vec![string.slice(0, 1)], vec![0, 5], b"hello"); + check( + vec![string.slice(0, 1), string.slice(1, 1)], + vec![0, 5, 10], + b"helloworld", + ); + + let string2 = StringArray::from(vec![Some("foo"), Some("bar")]); + check( + vec![string.slice(0, 1), string2.slice(0, 1)], + vec![0, 5, 8], + b"hellofoo", + ); + } + + #[test] + fn test_large() { + let arr = LargeBinaryArray::from_vec(vec![b"hello", b"world"]); + let data = DataBlock::from_array(arr); + + assert_eq!(data.num_values(), 2); + let data = data.as_variable_width().unwrap(); + assert_eq!(data.bits_per_offset, 64); + assert_eq!(data.num_values, 2); + assert_eq!(data.data, LanceBuffer::copy_slice(b"helloworld")); + assert_eq!( + data.offsets, + LanceBuffer::reinterpret_vec(vec![0_u64, 5, 10]) + ); + } + + #[test] + fn test_dictionary_indices_normalized() { + let arr1 = DictionaryArray::::from_iter([Some("a"), Some("a"), Some("b")]); + let arr2 = DictionaryArray::::from_iter([Some("b"), Some("c")]); + + let data = DataBlock::from_arrays(&[Arc::new(arr1), Arc::new(arr2)], 5); + + assert_eq!(data.num_values(), 5); + let data = data.as_dictionary().unwrap(); + let indices = data.indices; + assert_eq!(indices.bits_per_value, 8); + assert_eq!(indices.num_values, 5); + assert_eq!( + indices.data, + // You might expect 0, 0, 1, 1, 2 but it seems that arrow's dictionary concat does + // not actually collapse dictionaries. This is an arrow problem however, and we don't + // need to fix it here. + LanceBuffer::reinterpret_vec::(vec![0, 0, 1, 2, 3]) + ); + + let items = data.dictionary.as_variable_width().unwrap(); + assert_eq!(items.bits_per_offset, 32); + assert_eq!(items.num_values, 4); + assert_eq!(items.data, LanceBuffer::copy_slice(b"abbc")); + assert_eq!( + items.offsets, + LanceBuffer::reinterpret_vec(vec![0, 1, 2, 3, 4],) + ); + } + + #[test] + fn test_dictionary_nulls() { + // Test both ways of encoding nulls + + // By default, nulls get encoded into the indices + let arr1 = DictionaryArray::::from_iter([None, Some("a"), Some("b")]); + let arr2 = DictionaryArray::::from_iter([Some("c"), None]); + + let data = DataBlock::from_arrays(&[Arc::new(arr1), Arc::new(arr2)], 5); + + let check_common = |data: DataBlock| { + assert_eq!(data.num_values(), 5); + let dict = data.as_dictionary().unwrap(); + + let nullable_items = dict.dictionary.as_nullable().unwrap(); + assert_eq!(nullable_items.nulls, LanceBuffer::from(vec![0b00000111])); + assert_eq!(nullable_items.data.num_values(), 4); + + let items = nullable_items.data.as_variable_width().unwrap(); + assert_eq!(items.bits_per_offset, 32); + assert_eq!(items.num_values, 4); + assert_eq!(items.data, LanceBuffer::copy_slice(b"abc")); + assert_eq!( + items.offsets, + LanceBuffer::reinterpret_vec(vec![0, 1, 2, 3, 3],) + ); + + let indices = dict.indices; + assert_eq!(indices.bits_per_value, 8); + assert_eq!(indices.num_values, 5); + assert_eq!( + indices.data, + LanceBuffer::reinterpret_vec::(vec![3, 0, 1, 2, 3]) + ); + }; + check_common(data); + + // However, we can manually create a dictionary where nulls are in the dictionary + let items = StringArray::from(vec![Some("a"), Some("b"), Some("c"), None]); + let indices = Int8Array::from(vec![Some(3), Some(0), Some(1), Some(2), Some(3)]); + let dict = DictionaryArray::new(indices, Arc::new(items)); + + let data = DataBlock::from_array(dict); + + check_common(data); + } + + #[test] + fn test_dictionary_cannot_add_null() { + // 256 unique strings + let items = StringArray::from( + (0..256) + .map(|i| Some(String::from_utf8(vec![0; i]).unwrap())) + .collect::>(), + ); + // 257 indices, covering the whole range, plus one null + let indices = UInt8Array::from( + (0..=256) + .map(|i| if i == 256 { None } else { Some(i as u8) }) + .collect::>(), + ); + // We want to normalize this by pushing nulls into the dictionary, but we cannot because + // the dictionary is too large for the index type + let dict = DictionaryArray::new(indices, Arc::new(items)); + let data = DataBlock::from_array(dict); + + assert_eq!(data.num_values(), 257); + + let dict = data.as_dictionary().unwrap(); + + assert_eq!(dict.indices.bits_per_value, 32); + assert_eq!( + dict.indices.data, + LanceBuffer::reinterpret_vec((0_u32..257).collect::>()) + ); + + let nullable_items = dict.dictionary.as_nullable().unwrap(); + let null_buffer = NullBuffer::new(BooleanBuffer::new( + nullable_items.nulls.into_buffer(), + 0, + 257, + )); + for i in 0..256 { + assert!(!null_buffer.is_null(i)); + } + assert!(null_buffer.is_null(256)); + + assert_eq!( + nullable_items.data.as_variable_width().unwrap().data.len(), + 32640 + ); + } + + #[test] + fn test_all_null() { + for data_type in [ + DataType::UInt32, + DataType::FixedSizeBinary(2), + DataType::List(Arc::new(Field::new("item", DataType::UInt32, true))), + DataType::Struct(Fields::from(vec![Field::new("a", DataType::UInt32, true)])), + ] { + let block = DataBlock::AllNull(AllNullDataBlock { num_values: 10 }); + let arr = block.into_arrow(data_type.clone(), true).unwrap(); + let arr = make_array(arr); + let expected = new_null_array(&data_type, 10); + assert_eq!(&arr, &expected); + } + } + + #[test] + fn test_dictionary_cannot_concatenate() { + // 256 unique strings + let items = StringArray::from( + (0..256) + .map(|i| Some(String::from_utf8(vec![0; i]).unwrap())) + .collect::>(), + ); + // 256 different unique strings + let other_items = StringArray::from( + (0..256) + .map(|i| Some(String::from_utf8(vec![1; i + 1]).unwrap())) + .collect::>(), + ); + let indices = UInt8Array::from_iter_values(0..=255); + let dict1 = DictionaryArray::new(indices.clone(), Arc::new(items)); + let dict2 = DictionaryArray::new(indices, Arc::new(other_items)); + let data = DataBlock::from_arrays(&[Arc::new(dict1), Arc::new(dict2)], 512); + assert_eq!(data.num_values(), 512); + + let dict = data.as_dictionary().unwrap(); + + assert_eq!(dict.indices.bits_per_value, 32); + assert_eq!( + dict.indices.data, + LanceBuffer::reinterpret_vec::((0..512).collect::>()) + ); + // What fun: 0 + 1 + .. + 255 + 1 + 2 + .. + 256 = 2^16 + assert_eq!( + dict.dictionary.as_variable_width().unwrap().data.len(), + 65536 + ); + } + + #[test] + fn test_data_size() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + // test data_size() when input has no nulls + let mut genn = array::rand::().with_nulls(&[false, false, false]); + + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + assert!(block.data_size() == arr.get_buffer_memory_size() as u64); + + let arr = genn.generate(RowCount::from(400), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + assert!(block.data_size() == arr.get_buffer_memory_size() as u64); + + // test data_size() when input has nulls + let mut genn = array::rand::().with_nulls(&[false, true, false]); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + + let array_data = arr.to_data(); + let total_buffer_size: usize = array_data.buffers().iter().map(|buffer| buffer.len()).sum(); + // the NullBuffer.len() returns the length in bits so we divide_round_up by 8 + let array_nulls_size_in_bytes = arr.nulls().unwrap().len().div_ceil(8); + assert!(block.data_size() == (total_buffer_size + array_nulls_size_in_bytes) as u64); + + let arr = genn.generate(RowCount::from(400), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + + let array_data = arr.to_data(); + let total_buffer_size: usize = array_data.buffers().iter().map(|buffer| buffer.len()).sum(); + let array_nulls_size_in_bytes = arr.nulls().unwrap().len().div_ceil(8); + assert!(block.data_size() == (total_buffer_size + array_nulls_size_in_bytes) as u64); + + let mut genn = array::rand::().with_nulls(&[true, true, false]); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + + let array_data = arr.to_data(); + let total_buffer_size: usize = array_data.buffers().iter().map(|buffer| buffer.len()).sum(); + let array_nulls_size_in_bytes = arr.nulls().unwrap().len().div_ceil(8); + assert!(block.data_size() == (total_buffer_size + array_nulls_size_in_bytes) as u64); + + let arr = genn.generate(RowCount::from(400), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + + let array_data = arr.to_data(); + let total_buffer_size: usize = array_data.buffers().iter().map(|buffer| buffer.len()).sum(); + let array_nulls_size_in_bytes = arr.nulls().unwrap().len().div_ceil(8); + assert!(block.data_size() == (total_buffer_size + array_nulls_size_in_bytes) as u64); + + let mut genn = array::rand::().with_nulls(&[false, true, false]); + let arr1 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let arr2 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let arr3 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_arrays(&[arr1.clone(), arr2.clone(), arr3.clone()], 9); + + let concatenated_array = arrow_select::concat::concat(&[ + &*Arc::new(arr1.clone()) as &dyn Array, + &*Arc::new(arr2.clone()) as &dyn Array, + &*Arc::new(arr3.clone()) as &dyn Array, + ]) + .unwrap(); + let total_buffer_size: usize = concatenated_array + .to_data() + .buffers() + .iter() + .map(|buffer| buffer.len()) + .sum(); + + let total_nulls_size_in_bytes = concatenated_array.nulls().unwrap().len().div_ceil(8); + assert!(block.data_size() == (total_buffer_size + total_nulls_size_in_bytes) as u64); + } + + #[test] + fn variable_width_rejects_out_of_bounds_offsets_without_optional_validation() { + let block = VariableWidthBlock { + data: LanceBuffer::copy_slice(b"alphabetagamma"), + offsets: LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9, 100_000]), + bits_per_offset: 32, + num_values: 3, + block_info: BlockInfo::new(), + }; + + let error = block + .into_arrow(DataType::Binary, false) + .expect_err("out-of-bounds offsets must be rejected"); + assert!( + matches!(error, Error::CorruptFile { .. }), + "expected CorruptFile, got: {error}" + ); + let message = error.to_string(); + assert!( + message.contains("100000") && message.contains("data buffer size: 14 bytes"), + "error must report the offending offset and the data buffer size: {message}" + ); + } + + #[rstest] + #[case::i32_decreasing( + LanceBuffer::reinterpret_vec(vec![0_i32, 5, 2]), + 32, + 2, + "decreases" + )] + #[case::i64_decreasing( + LanceBuffer::reinterpret_vec(vec![0_i64, 5, 2]), + 64, + 2, + "decreases" + )] + #[case::i32_out_of_bounds( + LanceBuffer::reinterpret_vec(vec![0_i32, 6]), + 32, + 1, + "out of bounds" + )] + fn variable_width_builder_rejects_malformed_offsets( + #[case] offsets: LanceBuffer, + #[case] bits_per_offset: u8, + #[case] num_values: u64, + #[case] expected_message: &str, + ) { + let block = DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::copy_slice(b"abcde"), + offsets, + bits_per_offset, + num_values, + block_info: BlockInfo::new(), + }); + let mut builder = DataBlockBuilder::with_capacity_estimate(5); + + let error = builder + .append(&block, 0..num_values) + .expect_err("malformed offsets must fail concatenation"); + assert!( + matches!(error, Error::CorruptFile { .. }), + "expected CorruptFile, got: {error}" + ); + assert!( + error.to_string().contains(expected_message), + "unexpected message: {error}" + ); + } + + #[rstest] + #[case::binary_i32_tail_out_of_bounds( + DataType::Binary, + LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9, 100_000]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::utf8_i32_tail_out_of_bounds( + DataType::Utf8, + LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9, 100_000]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::large_binary_i64_tail_out_of_bounds( + DataType::LargeBinary, + LanceBuffer::reinterpret_vec(vec![0_i64, 5, 9, 100_000]), + 64, + 3, + b"alphabetagamma".as_slice() + )] + #[case::large_utf8_i64_tail_out_of_bounds( + DataType::LargeUtf8, + LanceBuffer::reinterpret_vec(vec![0_i64, 5, 9, 100_000]), + 64, + 3, + b"alphabetagamma".as_slice() + )] + #[case::binary_negative_offset( + DataType::Binary, + LanceBuffer::reinterpret_vec(vec![0_i32, -1, 9, 14]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::binary_non_monotonic_offsets( + DataType::Binary, + LanceBuffer::reinterpret_vec(vec![0_i32, 9, 5, 14]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::binary_interior_offset_out_of_bounds( + DataType::Binary, + LanceBuffer::reinterpret_vec(vec![0_i32, 100_000, 100_000, 14]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::binary_offsets_buffer_too_short( + DataType::Binary, + LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9]), + 32, + 3, + b"alphabetagamma".as_slice() + )] + #[case::utf8_invalid_byte_sequence( + DataType::Utf8, + LanceBuffer::reinterpret_vec(vec![0_i32, 1, 2, 3]), + 32, + 3, + &[b'a', 0xFF, b'b'] + )] + #[case::utf8_offset_splits_multibyte_char( + DataType::Utf8, + LanceBuffer::reinterpret_vec(vec![0_i32, 1, 2]), + 32, + 2, + "é".as_bytes() + )] + #[case::large_utf8_invalid_byte_sequence( + DataType::LargeUtf8, + LanceBuffer::reinterpret_vec(vec![0_i64, 1, 2, 3]), + 64, + 3, + &[b'a', 0xFF, b'b'] + )] + fn variable_width_rejects_malformed_layout( + #[case] data_type: DataType, + #[case] offsets: LanceBuffer, + #[case] bits_per_offset: u8, + #[case] num_values: u64, + #[case] data: &[u8], + ) { + let block = VariableWidthBlock { + data: LanceBuffer::copy_slice(data), + offsets, + bits_per_offset, + num_values, + block_info: BlockInfo::new(), + }; + + // The malformed layout must be rejected regardless of the optional + // `validate` flag: the flag selects extra validation, not the memory + // safety proof required to construct an Arrow array. + for validate in [false, true] { + let error = DataBlock::VariableWidth(block.clone()) + .into_arrow(data_type.clone(), validate) + .expect_err("malformed variable-width layout must be rejected"); + assert!( + matches!(error, Error::CorruptFile { .. }), + "expected CorruptFile with validate={validate}, got: {error}" + ); + } + } + + #[test] + fn dictionary_rejects_malformed_variable_width_values_without_optional_validation() { + let values = VariableWidthBlock { + data: LanceBuffer::copy_slice(b"alphabetagamma"), + offsets: LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9, 100_000]), + bits_per_offset: 32, + num_values: 3, + block_info: BlockInfo::new(), + }; + let dictionary = DataBlock::Dictionary(DictionaryDataBlock { + indices: FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(vec![0_i32, 1, 2]), + bits_per_value: 32, + num_values: 3, + block_info: BlockInfo::new(), + }, + dictionary: Box::new(DataBlock::VariableWidth(values)), + }); + + let data_type = DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Binary)); + let error = dictionary + .into_arrow(data_type, false) + .expect_err("dictionary with out-of-bounds value offsets must be rejected"); + assert!( + matches!(error, Error::CorruptFile { .. }), + "expected CorruptFile, got: {error}" + ); + } + + #[rstest] + #[case::binary(Arc::new(BinaryArray::from_vec(vec![b"alpha", b"", b"gamma"])) as ArrayRef)] + #[case::large_binary( + Arc::new(LargeBinaryArray::from_vec(vec![b"alpha", b"", b"gamma"])) as ArrayRef + )] + #[case::utf8(Arc::new(StringArray::from(vec!["héllo", "", "world"])) as ArrayRef)] + #[case::large_utf8(Arc::new(LargeStringArray::from(vec!["héllo", "", "world"])) as ArrayRef)] + fn variable_width_valid_data_survives_mandatory_validation(#[case] array: ArrayRef) { + let block = DataBlock::from_array(array.clone()); + for validate in [false, true] { + let round_tripped = make_array( + block + .clone() + .into_arrow(array.data_type().clone(), validate) + .unwrap(), + ); + assert_eq!(&round_tripped, &array); + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/decoder.rs b/lance-artifact/rust/lance-encoding/src/decoder.rs new file mode 100644 index 000000000..0d36e308e --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/decoder.rs @@ -0,0 +1,3491 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities and traits for scheduling & decoding data +//! +//! Reading data involves two steps: scheduling and decoding. The +//! scheduling step is responsible for figuring out what data is needed +//! and issuing the appropriate I/O requests. The decoding step is +//! responsible for taking the loaded data and turning it into Arrow +//! arrays. +//! +//! # Scheduling +//! +//! Scheduling is split into `FieldScheduler` and `PageScheduler`. +//! There is one field scheduler for each output field, which may map to many +//! columns of actual data. A field scheduler is responsible for figuring out +//! the order in which pages should be scheduled. Field schedulers then delegate +//! to page schedulers to figure out the I/O requests that need to be made for +//! the page. +//! +//! Page schedulers also create the decoders that will be used to decode the +//! scheduled data. +//! +//! # Decoding +//! +//! Decoders are split into `PhysicalPageDecoder` and +//! [`LogicalPageDecoder`]. Note that both physical and logical decoding +//! happens on a per-page basis. There is no concept of a "field decoder" or +//! "column decoder". +//! +//! The physical decoders handle lower level encodings. They have a few advantages: +//! +//! * They do not need to decode into an Arrow array and so they don't need +//! to be enveloped into the Arrow filesystem (e.g. Arrow doesn't have a +//! bit-packed type. We can use variable-length binary but that is kind +//! of awkward) +//! * They can decode into an existing allocation. This can allow for "page +//! bridging". If we are trying to decode into a batch of 1024 rows and +//! the rows 0..1024 are spread across two pages then we can avoid a memory +//! copy by allocating once and decoding each page into the outer allocation. +//! (note: page bridging is not actually implemented yet) +//! +//! However, there are some limitations for physical decoders: +//! +//! * They are constrained to a single column +//! * The API is more complex +//! +//! The logical decoders are designed to map one or more columns of Lance +//! data into an Arrow array. +//! +//! Typically, a "logical encoding" will have both a logical decoder and a field scheduler. +//! Meanwhile, a "physical encoding" will have a physical decoder but no corresponding field +//! scheduler. +//! +//! +//! # General notes +//! +//! Encodings are typically nested into each other to form a tree. The top of the tree is +//! the user requested schema. Each field in that schema is assigned to one top-level logical +//! encoding. That encoding can then contain other logical encodings or physical encodings. +//! Physical encodings can also contain other physical encodings. +//! +//! So, for example, a single field in the Arrow schema might have the type `List` +//! +//! The encoding tree could then be: +//! +//! root: List (logical encoding) +//! - indices: Primitive (logical encoding) +//! - column: Basic (physical encoding) +//! - validity: Bitmap (physical encoding) +//! - values: RLE (physical encoding) +//! - runs: Value (physical encoding) +//! - values: Value (physical encoding) +//! - items: Primitive (logical encoding) +//! - column: Basic (physical encoding) +//! - values: Value (physical encoding) +//! +//! Note that, in this example, root.items.column does not have a validity because there were +//! no nulls in the page. +//! +//! ## Multiple buffers or multiple columns? +//! +//! Note that there are many different ways we can write encodings. For example, we might +//! store primitive fields in a single column with two buffers (one for validity and one for +//! values) +//! +//! On the other hand, we could also store a primitive field as two different columns. One +//! that yields a non-nullable boolean array and one that yields a non-nullable array of items. +//! Then we could combine these two arrays into a single array where the boolean array is the +//! bitmap. There are a few subtle differences between the approaches: +//! +//! * Storing things as multiple buffers within the same column is generally more efficient and +//! easier to schedule. For example, in-batch coalescing is very easy but can only be done +//! on data that is in the same page. +//! * When things are stored in multiple columns you have to worry about their pages not being +//! in sync. In our previous validity / values example this means we might have to do some +//! memory copies to get the validity array and values arrays to be the same length as +//! decode. +//! * When things are stored in a single column, projection is impossible. For example, if we +//! tried to store all the struct fields in a single column with lots of buffers then we wouldn't +//! be able to read back individual fields of the struct. +//! +//! The fixed size list decoding is an interesting example because it is actually both a physical +//! encoding and a logical encoding. A fixed size list of a physical encoding is, itself, a physical +//! encoding (e.g. a fixed size list of doubles). However, a fixed size list of a logical encoding +//! is a logical encoding (e.g. a fixed size list of structs). +//! +//! # The scheduling loop +//! +//! Reading a Lance file involves both scheduling and decoding. Its generally expected that these +//! will run as two separate threads. +//! +//! ```text +//! +//! I/O PARALLELISM +//! Issues +//! Requests ┌─────────────────┐ +//! │ │ Wait for +//! ┌──────────► I/O Service ├─────► Enough I/O ◄─┐ +//! │ │ │ For batch │ +//! │ └─────────────────┘ │3 │ +//! │ │ │ +//! │ │ │2 +//! ┌─────────────────────┴─┐ ┌─────────▼───────┴┐ +//! │ │ │ │Poll +//! │ Batch Decode │ Decode tasks sent via channel│ Batch Decode │1 +//! │ Scheduler ├─────────────────────────────►│ Stream ◄───── +//! │ │ │ │ +//! └─────▲─────────────┬───┘ └─────────┬────────┘ +//! │ │ │4 +//! │ │ │ +//! └─────────────┘ ┌────────┴────────┐ +//! Caller of schedule_range Buffer polling │ │ +//! will be scheduler thread to achieve CPU │ Decode Batch ├────► +//! and schedule one decode parallelism │ Task │ +//! task (and all needed I/O) (thread per │ │ +//! per logical page batch) └─────────────────┘ +//! ``` +//! +//! The scheduling thread will work through the file from the +//! start to the end as quickly as possible. Data is scheduled one page at a time in a row-major +//! fashion. For example, imagine we have a file with the following page structure: +//! +//! ```text +//! Score (Float32) | C0P0 | +//! Id (16-byte UUID) | C1P0 | C1P1 | C1P2 | C1P3 | +//! Vector (4096 bytes) | C2P0 | C2P1 | C2P2 | C2P3 | .. | C2P1024 | +//! ``` +//! +//! This would be quite common as each of these pages has the same number of bytes. Let's pretend +//! each page is 1MiB and so there are 256Ki rows of data. Each page of `Score` has 256Ki rows. +//! Each page of `Id` has 64Ki rows. Each page of `Vector` has 256 rows. The scheduler would then +//! schedule in the following order: +//! +//! C0 P0 +//! C1 P0 +//! C2 P0 +//! C2 P1 +//! ... (254 pages omitted) +//! C2 P255 +//! C1 P1 +//! C2 P256 +//! ... (254 pages omitted) +//! C2 P511 +//! C1 P2 +//! C2 P512 +//! ... (254 pages omitted) +//! C2 P767 +//! C1 P3 +//! C2 P768 +//! ... (254 pages omitted) +//! C2 P1024 +//! +//! This is the ideal scheduling order because it means we can decode complete rows as quickly as possible. +//! Note that the scheduler thread does not need to wait for I/O to happen at any point. As soon as it starts +//! it will start scheduling one page of I/O after another until it has scheduled the entire file's worth of +//! I/O. This is slightly different than other file readers which have "row group parallelism" and will +//! typically only schedule X row groups worth of reads at a time. +//! +//! In the near future there will be a backpressure mechanism and so it may need to stop/pause if the compute +//! falls behind. +//! +//! ## Indirect I/O +//! +//! Regrettably, there are times where we cannot know exactly what data we need until we have partially decoded +//! the file. This happens when we have variable sized list data. In that case the scheduling task for that +//! page will only schedule the first part of the read (loading the list offsets). It will then immediately +//! spawn a new tokio task to wait for that I/O and decode the list offsets. That follow-up task is not part +//! of the scheduling loop or the decode loop. It is a free task. Once the list offsets are decoded we submit +//! a follow-up I/O task. This task is scheduled at a high priority because the decoder is going to need it soon. +//! +//! # The decode loop +//! +//! As soon as the scheduler starts we can start decoding. Each time we schedule a page we +//! push a decoder for that page's data into a channel. The decode loop +//! ([`BatchDecodeStream`]) reads from that channel. Each time it receives a decoder it +//! waits until the decoder has all of its data. Then it grabs the next decoder. Once it has +//! enough loaded decoders to complete a batch worth of rows it will spawn a "decode batch task". +//! +//! These batch decode tasks perform the actual CPU work of decoding the loaded data into Arrow +//! arrays. This may involve signifciant CPU processing like decompression or arithmetic in order +//! to restore the data to its correct in-memory representation. +//! +//! ## Batch size +//! +//! The `BatchDecodeStream` is configured with a batch size. This does not need to have any +//! relation to the page size(s) used to write the data. This keeps our compute work completely +//! independent of our I/O work. We suggest using small batch sizes: +//! +//! * Batches should fit in CPU cache (at least L3) +//! * More batches means more opportunity for parallelism +//! * The "batch overhead" is very small in Lance compared to other formats because it has no +//! relation to the way the data is stored. + +use std::collections::VecDeque; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{LazyLock, Once, OnceLock}; +use std::{ops::Range, sync::Arc}; + +use arrow_array::cast::AsArray; +use arrow_array::{ArrayRef, RecordBatch, RecordBatchIterator, RecordBatchReader}; +use arrow_schema::{ArrowError, DataType, Field as ArrowField, Fields, Schema as ArrowSchema}; +use bytes::Bytes; +use futures::future::{BoxFuture, MaybeDone, maybe_done}; +use futures::stream::{self, BoxStream}; +use futures::{FutureExt, StreamExt}; +use lance_arrow::DataTypeExt; +use lance_core::cache::{Context, DeepSizeOf, LanceCache}; +use lance_core::datatypes::{ + BLOB_DESC_LANCE_FIELD, Field, Schema, validate_fixed_size_list_dimensions, +}; +use lance_core::utils::futures::{FinallyStreamExt, StreamOnDropExt}; +use lance_core::utils::parse::parse_env_as_bool; +use log::{debug, trace, warn}; +use prost::Message; +use tokio::sync::mpsc::error::SendError; +use tokio::sync::mpsc::{self, unbounded_channel}; + +use lance_core::error::LanceOptionExt; +use lance_core::{ArrowResult, Error, Result}; +use tracing::instrument; + +use crate::array_encoding::logical::list::OffsetPageInfo; +use crate::array_encoding::logical::r#struct::{SimpleStructDecoder, SimpleStructScheduler}; +use crate::array_encoding::logical::{ + binary::BinaryFieldScheduler, blob::BlobFieldScheduler, list::ListFieldScheduler, + primitive::PrimitiveFieldScheduler, +}; +use crate::compression::{DecompressionStrategy, DefaultDecompressionStrategy}; +use crate::data::DataBlock; +use crate::encoder::EncodedBatch; +use crate::encodings::logical::fixed_size_list::StructuralFixedSizeListScheduler; +use crate::encodings::logical::list::StructuralListScheduler; +use crate::encodings::logical::map::StructuralMapScheduler; +use crate::encodings::logical::primitive::StructuralPrimitiveFieldScheduler; +use crate::encodings::logical::r#struct::{StructuralStructDecoder, StructuralStructScheduler}; +use crate::format::pb::{self, column_encoding}; +use crate::format::pb21; +use crate::repdef::{CompositeRepDefUnraveler, RepDefUnraveler}; +use crate::{BufferScheduler, EncodingsIo}; + +pub trait SchedulingJob: std::fmt::Debug { + fn schedule_next( + &mut self, + context: &mut SchedulerContext, + priority: &dyn PriorityRange, + ) -> Result; + + fn num_rows(&self) -> u64; +} + +/// Schedules the I/O needed to decode one field. +pub trait FieldScheduler: Send + Sync + std::fmt::Debug { + fn initialize<'a>( + &'a self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>>; + + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result>; + + fn num_rows(&self) -> u64; +} + +#[derive(Debug)] +pub struct DecoderReady { + pub decoder: Box, + pub path: VecDeque, +} + +/// Stateful decoder for one logical page. +pub trait LogicalPageDecoder: std::fmt::Debug + Send { + fn accept_child(&mut self, _child: DecoderReady) -> Result<()> { + Err(Error::internal(format!( + "The decoder {:?} does not expect children but received a child", + self + ))) + } + + fn wait_for_loaded(&'_ mut self, loaded_need: u64) -> BoxFuture<'_, Result<()>>; + + fn rows_loaded(&self) -> u64; + + fn rows_unloaded(&self) -> u64 { + self.num_rows() - self.rows_loaded() + } + + fn num_rows(&self) -> u64; + + fn rows_drained(&self) -> u64; + + fn rows_left(&self) -> u64 { + self.num_rows() - self.rows_drained() + } + + fn drain(&mut self, num_rows: u64) -> Result; + + fn data_type(&self) -> &DataType; +} + +// If users are getting batches over 10MiB large then it's time to reduce the batch size +const BATCH_SIZE_BYTES_WARNING: u64 = 10 * 1024 * 1024; +const ENV_LANCE_STRUCTURAL_BATCH_DECODE_SPAWN_MODE: &str = + "LANCE_STRUCTURAL_BATCH_DECODE_SPAWN_MODE"; +const ENV_LANCE_READ_CACHE_REPETITION_INDEX: &str = "LANCE_READ_CACHE_REPETITION_INDEX"; +const ENV_LANCE_INLINE_SCHEDULING_THRESHOLD: &str = "LANCE_INLINE_SCHEDULING_THRESHOLD"; + +// If a request is for at most this many rows we skip the scheduler-task spawn +// and run scheduling inline as part of the `schedule_and_decode` await. +const DEFAULT_INLINE_SCHEDULING_THRESHOLD: u64 = 16 * 1024; + +fn default_cache_repetition_index() -> bool { + static DEFAULT_CACHE_REPETITION_INDEX: OnceLock = OnceLock::new(); + *DEFAULT_CACHE_REPETITION_INDEX + .get_or_init(|| parse_env_as_bool(ENV_LANCE_READ_CACHE_REPETITION_INDEX, true)) +} + +fn inline_scheduling_threshold() -> u64 { + static THRESHOLD: OnceLock = OnceLock::new(); + *THRESHOLD.get_or_init(|| { + std::env::var(ENV_LANCE_INLINE_SCHEDULING_THRESHOLD) + .ok() + .and_then(|v| v.trim().parse::().ok()) + .unwrap_or(DEFAULT_INLINE_SCHEDULING_THRESHOLD) + }) +} + +/// Top-level encoding message for a page. Wraps both the v2.0 +/// [`pb::ArrayEncoding`] grammar and the structural [`pb21::PageLayout`] grammar. +/// +/// A file should only use one or the other and never both. +/// 2.0 decoders can always assume this is pb::ArrayEncoding +/// and 2.1+ decoders can always assume this is pb::PageLayout +#[derive(Debug, Clone)] +pub enum PageEncoding { + Legacy(pb::ArrayEncoding), + Structural(pb21::PageLayout), +} + +impl DeepSizeOf for PageEncoding { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + match self { + Self::Legacy(encoding) => encoding.encoded_len() * 4, + Self::Structural(encoding) => encoding.encoded_len() * 4, + } + } +} + +impl PageEncoding { + pub fn as_legacy(&self) -> &pb::ArrayEncoding { + match self { + Self::Legacy(enc) => enc, + Self::Structural(_) => panic!("Expected a legacy encoding"), + } + } + + pub fn as_structural(&self) -> &pb21::PageLayout { + match self { + Self::Structural(enc) => enc, + Self::Legacy(_) => panic!("Expected a structural encoding"), + } + } + + pub fn is_structural(&self) -> bool { + matches!(self, Self::Structural(_)) + } +} + +/// Metadata describing a page in a file +/// +/// This is typically created by reading the metadata section of a Lance file +#[derive(Debug)] +pub struct PageInfo { + /// The number of rows in the page + pub num_rows: u64, + /// The priority (top level row number) of the page + /// + /// This is only set in 2.1 files and will be 0 for 2.0 files + pub priority: u64, + /// The encoding that explains the buffers in the page + pub encoding: PageEncoding, + /// The offsets and sizes of the buffers in the file + pub buffer_offsets_and_sizes: Arc<[(u64, u64)]>, +} + +impl DeepSizeOf for PageInfo { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.encoding.deep_size_of_children(context) + + self.buffer_offsets_and_sizes.deep_size_of_children(context) + } +} + +/// Metadata describing a column in a file +/// +/// This is typically created by reading the metadata section of a Lance file +#[derive(Debug, Clone)] +pub struct ColumnInfo { + /// The index of the column in the file + pub index: u32, + /// The metadata for each page in the column + pub page_infos: Arc<[PageInfo]>, + /// File positions and their sizes of the column-level buffers + pub buffer_offsets_and_sizes: Arc<[(u64, u64)]>, + pub encoding: pb::ColumnEncoding, +} + +impl DeepSizeOf for ColumnInfo { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.page_infos.deep_size_of_children(context) + + self.buffer_offsets_and_sizes.deep_size_of_children(context) + + self.encoding.encoded_len() * 4 + } +} + +impl ColumnInfo { + /// Create a new instance + pub fn new( + index: u32, + page_infos: Arc<[PageInfo]>, + buffer_offsets_and_sizes: Vec<(u64, u64)>, + encoding: pb::ColumnEncoding, + ) -> Self { + Self { + index, + page_infos, + buffer_offsets_and_sizes: buffer_offsets_and_sizes.into_boxed_slice().into(), + encoding, + } + } + + pub fn is_structural(&self) -> bool { + self.page_infos + // Can just look at the first since all should be the same + .first() + .map(|page| page.encoding.is_structural()) + .unwrap_or(false) + } +} + +enum RootScheduler { + Structural(Box), + Array(Arc), +} + +impl RootScheduler { + fn as_array(&self) -> &Arc { + match self { + Self::Structural(_) => panic!("Expected an array scheduler"), + Self::Array(s) => s, + } + } + + fn as_structural(&self) -> &dyn StructuralFieldScheduler { + match self { + Self::Structural(s) => s.as_ref(), + Self::Array(_) => panic!("Expected a structural scheduler"), + } + } +} + +/// The scheduler for decoding batches +/// +/// Lance decoding is done in two steps, scheduling, and decoding. The +/// scheduling tends to be lightweight and should quickly figure what data +/// is needed from the disk issue the appropriate I/O requests. A decode task is +/// created to eventually decode the data (once it is loaded) and scheduling +/// moves on to scheduling the next page. +/// +/// Meanwhile, it's expected that a decode stream will be setup to run at the +/// same time. Decode tasks take the data that is loaded and turn it into +/// Arrow arrays. +/// +/// This approach allows us to keep our I/O parallelism and CPU parallelism +/// completely separate since those are often two very different values. +/// +/// Backpressure should be achieved via the I/O service. Requests that are +/// issued will pile up if the decode stream is not polling quickly enough. +/// The [`crate::EncodingsIo::submit_request`] function should return a pending +/// future once there are too many I/O requests in flight. +/// +/// TODO: Implement backpressure +pub struct DecodeBatchScheduler { + root_scheduler: RootScheduler, + pub root_fields: Fields, + cache: Arc, +} + +pub struct ColumnInfoIter<'a> { + column_infos: Vec>, + column_indices: &'a [u32], + column_info_pos: usize, + column_indices_pos: usize, +} + +impl<'a> ColumnInfoIter<'a> { + pub fn new(column_infos: Vec>, column_indices: &'a [u32]) -> Self { + let initial_pos = column_indices.first().copied().unwrap_or(0) as usize; + Self { + column_infos, + column_indices, + column_info_pos: initial_pos, + column_indices_pos: 0, + } + } + + pub fn peek(&self) -> &Arc { + &self.column_infos[self.column_info_pos] + } + + pub fn peek_transform(&mut self, transform: impl FnOnce(Arc) -> Arc) { + let column_info = self.column_infos[self.column_info_pos].clone(); + let transformed = transform(column_info); + self.column_infos[self.column_info_pos] = transformed; + } + + pub fn expect_next(&mut self) -> Result<&Arc> { + self.next().ok_or_else(|| { + Error::invalid_input( + "there were more fields in the schema than provided column indices / infos", + ) + }) + } + + fn next(&mut self) -> Option<&Arc> { + if self.column_info_pos < self.column_infos.len() { + let info = &self.column_infos[self.column_info_pos]; + self.column_info_pos += 1; + Some(info) + } else { + None + } + } + + pub(crate) fn next_top_level(&mut self) { + self.column_indices_pos += 1; + if self.column_indices_pos < self.column_indices.len() { + self.column_info_pos = self.column_indices[self.column_indices_pos] as usize; + } else { + self.column_info_pos = self.column_infos.len(); + } + } +} + +/// These contain the file buffers shared across the entire file +#[derive(Clone, Copy, Debug)] +pub struct FileBuffers<'a> { + pub positions_and_sizes: &'a [(u64, u64)], +} + +/// These contain the file buffers and also buffers specific to a column +#[derive(Clone, Copy, Debug)] +pub struct ColumnBuffers<'a, 'b> { + pub file_buffers: FileBuffers<'a>, + pub positions_and_sizes: &'b [(u64, u64)], +} + +/// These contain the file & column buffers and also buffers specific to a page +#[derive(Clone, Copy, Debug)] +pub struct PageBuffers<'a, 'b, 'c> { + pub column_buffers: ColumnBuffers<'a, 'b>, + pub positions_and_sizes: &'c [(u64, u64)], +} + +/// The core decoder strategy handles all the various Arrow types +#[derive(Debug)] +pub struct CoreFieldDecoderStrategy { + pub validate_data: bool, + pub decompressor_strategy: Arc, + pub cache_repetition_index: bool, +} + +impl Default for CoreFieldDecoderStrategy { + fn default() -> Self { + Self { + validate_data: false, + decompressor_strategy: Arc::new(DefaultDecompressionStrategy {}), + cache_repetition_index: false, + } + } +} + +impl CoreFieldDecoderStrategy { + /// Create a new strategy with cache_repetition_index enabled + pub fn with_cache_repetition_index(mut self, cache_repetition_index: bool) -> Self { + self.cache_repetition_index = cache_repetition_index; + self + } + + /// Create a new strategy from decoder config + pub fn from_decoder_config(config: &DecoderConfig) -> Self { + Self { + validate_data: config.validate_on_decode, + decompressor_strategy: Arc::new(DefaultDecompressionStrategy {}), + cache_repetition_index: config.cache_repetition_index, + } + } + + /// This is just a sanity check to ensure there is no "wrapped encodings" + /// that haven't been handled. + fn ensure_values_encoded(column_info: &ColumnInfo, field_name: &str) -> Result<()> { + let column_encoding = column_info + .encoding + .column_encoding + .as_ref() + .ok_or_else(|| { + Error::invalid_input(format!( + "the column at index {} was missing a ColumnEncoding", + column_info.index + )) + })?; + if matches!( + column_encoding, + pb::column_encoding::ColumnEncoding::Values(_) + ) { + Ok(()) + } else { + Err(Error::invalid_input(format!( + "the column at index {} mapping to the input field {} has column encoding {:?} and no decoder is registered to handle it", + column_info.index, field_name, column_encoding + ))) + } + } + + fn is_structural_primitive(data_type: &DataType) -> bool { + if data_type.is_primitive() { + true + } else { + match data_type { + // DataType::is_primitive doesn't consider these primitive but we do + DataType::Dictionary(_, value_type) => Self::is_structural_primitive(value_type), + DataType::Boolean + | DataType::Null + | DataType::FixedSizeBinary(_) + | DataType::Binary + | DataType::LargeBinary + | DataType::Utf8 + | DataType::LargeUtf8 => true, + DataType::FixedSizeList(inner, _) => { + Self::is_structural_primitive(inner.data_type()) + } + _ => false, + } + } + } + + fn is_array_primitive(data_type: &DataType) -> bool { + if data_type.is_primitive() { + true + } else { + match data_type { + // DataType::is_primitive doesn't consider these primitive but we do + DataType::Boolean | DataType::Null | DataType::FixedSizeBinary(_) => true, + DataType::FixedSizeList(inner, _) => Self::is_array_primitive(inner.data_type()), + _ => false, + } + } + } + + fn create_primitive_scheduler( + &self, + field: &Field, + column: &ColumnInfo, + buffers: FileBuffers, + ) -> Result> { + Self::ensure_values_encoded(column, &field.name)?; + // Primitive fields map to a single column + let column_buffers = ColumnBuffers { + file_buffers: buffers, + positions_and_sizes: &column.buffer_offsets_and_sizes, + }; + Ok(Box::new(PrimitiveFieldScheduler::new( + column.index, + field.data_type(), + column.page_infos.clone(), + column_buffers, + self.validate_data, + ))) + } + + /// Helper method to verify the page encoding of a struct header column + fn check_simple_struct(column_info: &ColumnInfo, field_name: &str) -> Result<()> { + Self::ensure_values_encoded(column_info, field_name)?; + if column_info.page_infos.len() != 1 { + return Err(Error::invalid_input_source(format!("Due to schema we expected a struct column but we received a column with {} pages and right now we only support struct columns with 1 page", column_info.page_infos.len()).into())); + } + let encoding = &column_info.page_infos[0].encoding; + match encoding.as_legacy().array_encoding.as_ref().unwrap() { + pb::array_encoding::ArrayEncoding::Struct(_) => Ok(()), + _ => Err(Error::invalid_input_source(format!("Expected a struct encoding because we have a struct field in the schema but got the encoding {:?}", encoding).into())), + } + } + + fn check_packed_struct(column_info: &ColumnInfo) -> bool { + let encoding = &column_info.page_infos[0].encoding; + matches!( + encoding.as_legacy().array_encoding.as_ref().unwrap(), + pb::array_encoding::ArrayEncoding::PackedStruct(_) + ) + } + + fn create_list_scheduler( + &self, + list_field: &Field, + column_infos: &mut ColumnInfoIter, + buffers: FileBuffers, + offsets_column: &ColumnInfo, + ) -> Result> { + Self::ensure_values_encoded(offsets_column, &list_field.name)?; + let offsets_column_buffers = ColumnBuffers { + file_buffers: buffers, + positions_and_sizes: &offsets_column.buffer_offsets_and_sizes, + }; + let items_scheduler = + self.create_array_field_scheduler(&list_field.children[0], column_infos, buffers)?; + + let (inner_infos, null_offset_adjustments): (Vec<_>, Vec<_>) = offsets_column + .page_infos + .iter() + .filter(|offsets_page| offsets_page.num_rows > 0) + .map(|offsets_page| { + if let Some(pb::array_encoding::ArrayEncoding::List(list_encoding)) = + &offsets_page.encoding.as_legacy().array_encoding + { + let inner = PageInfo { + buffer_offsets_and_sizes: offsets_page.buffer_offsets_and_sizes.clone(), + encoding: PageEncoding::Legacy( + list_encoding.offsets.as_ref().unwrap().as_ref().clone(), + ), + num_rows: offsets_page.num_rows, + priority: 0, + }; + ( + inner, + OffsetPageInfo { + offsets_in_page: offsets_page.num_rows, + null_offset_adjustment: list_encoding.null_offset_adjustment, + num_items_referenced_by_page: list_encoding.num_items, + }, + ) + } else { + // TODO: Should probably return Err here + panic!("Expected a list column"); + } + }) + .unzip(); + let inner = Arc::new(PrimitiveFieldScheduler::new( + offsets_column.index, + DataType::UInt64, + Arc::from(inner_infos.into_boxed_slice()), + offsets_column_buffers, + self.validate_data, + )) as Arc; + let items_field = match list_field.data_type() { + DataType::List(inner) => inner, + DataType::LargeList(inner) => inner, + _ => unreachable!(), + }; + let offset_type = if matches!(list_field.data_type(), DataType::List(_)) { + DataType::Int32 + } else { + DataType::Int64 + }; + Ok(Box::new(ListFieldScheduler::new( + inner, + items_scheduler.into(), + items_field, + offset_type, + null_offset_adjustments, + ))) + } + + fn unwrap_blob(column_info: &ColumnInfo) -> Option { + if let column_encoding::ColumnEncoding::Blob(blob) = + column_info.encoding.column_encoding.as_ref().unwrap() + { + let mut column_info = column_info.clone(); + column_info.encoding = blob.inner.as_ref().unwrap().as_ref().clone(); + Some(column_info) + } else { + None + } + } + + fn create_structural_field_scheduler( + &self, + field: &Field, + column_infos: &mut ColumnInfoIter, + ) -> Result> { + let data_type = field.data_type(); + validate_fixed_size_list_dimensions(&field.name, &data_type)?; + if Self::is_structural_primitive(&data_type) { + let column_info = column_infos.expect_next()?; + let scheduler = Box::new(StructuralPrimitiveFieldScheduler::try_new( + column_info.as_ref(), + self.decompressor_strategy.as_ref(), + self.cache_repetition_index, + field, + )?); + + // advance to the next top level column + column_infos.next_top_level(); + + return Ok(scheduler); + } + match &data_type { + DataType::Struct(fields) => { + if field.is_packed_struct() { + // Packed struct + let column_info = column_infos.expect_next()?; + let scheduler = Box::new(StructuralPrimitiveFieldScheduler::try_new( + column_info.as_ref(), + self.decompressor_strategy.as_ref(), + self.cache_repetition_index, + field, + )?); + + // advance to the next top level column + column_infos.next_top_level(); + + return Ok(scheduler); + } + // Maybe a blob descriptions struct? + if field.is_blob() { + let column_info = column_infos.peek(); + if column_info.page_infos.iter().any(|page| { + matches!( + page.encoding, + PageEncoding::Structural(pb21::PageLayout { + layout: Some(pb21::page_layout::Layout::BlobLayout(_)) + }) + ) + }) { + let column_info = column_infos.expect_next()?; + let scheduler = Box::new(StructuralPrimitiveFieldScheduler::try_new( + column_info.as_ref(), + self.decompressor_strategy.as_ref(), + self.cache_repetition_index, + field, + )?); + column_infos.next_top_level(); + return Ok(scheduler); + } + } + + let mut child_schedulers = Vec::with_capacity(field.children.len()); + for field in field.children.iter() { + let field_scheduler = + self.create_structural_field_scheduler(field, column_infos)?; + child_schedulers.push(field_scheduler); + } + + let fields = fields.clone(); + Ok( + Box::new(StructuralStructScheduler::new(child_schedulers, fields)) + as Box, + ) + } + DataType::List(_) | DataType::LargeList(_) => { + let child = field.children.first().expect_ok()?; + let child_scheduler = + self.create_structural_field_scheduler(child, column_infos)?; + Ok(Box::new(StructuralListScheduler::new(child_scheduler)) + as Box) + } + DataType::FixedSizeList(inner, dimension) + if matches!(inner.data_type(), DataType::Struct(_)) => + { + let child = field.children.first().expect_ok()?; + let child_scheduler = + self.create_structural_field_scheduler(child, column_infos)?; + Ok(Box::new(StructuralFixedSizeListScheduler::new( + child_scheduler, + *dimension, + )) as Box) + } + DataType::Map(_, keys_sorted) => { + // TODO: We only support keys_sorted=false for now, + // because converting a rust arrow map field to the python arrow field will + // lose the keys_sorted property. + if *keys_sorted { + return Err(Error::not_supported_source(format!("Map data type is not supported with keys_sorted=true now, current value is {}", *keys_sorted).into())); + } + let entries_child = field.children.first().expect_ok()?; + let child_scheduler = + self.create_structural_field_scheduler(entries_child, column_infos)?; + Ok(Box::new(StructuralMapScheduler::new(child_scheduler)) + as Box) + } + _ => todo!("create_structural_field_scheduler for {}", data_type), + } + } + + fn create_array_field_scheduler( + &self, + field: &Field, + column_infos: &mut ColumnInfoIter, + buffers: FileBuffers, + ) -> Result> { + let data_type = field.data_type(); + validate_fixed_size_list_dimensions(&field.name, &data_type)?; + if Self::is_array_primitive(&data_type) { + let column_info = column_infos.expect_next()?; + let scheduler = self.create_primitive_scheduler(field, column_info, buffers)?; + return Ok(scheduler); + } else if data_type.is_binary_like() { + let column_info = column_infos.expect_next()?.clone(); + // Column is blob and user is asking for binary data + if let Some(blob_col) = Self::unwrap_blob(column_info.as_ref()) { + let desc_scheduler = + self.create_primitive_scheduler(&BLOB_DESC_LANCE_FIELD, &blob_col, buffers)?; + let blob_scheduler = Box::new(BlobFieldScheduler::new(desc_scheduler.into())); + return Ok(blob_scheduler); + } + if let Some(page_info) = column_info.page_infos.first() { + if matches!( + page_info.encoding.as_legacy(), + pb::ArrayEncoding { + array_encoding: Some(pb::array_encoding::ArrayEncoding::List(..)) + } + ) { + let list_type = if matches!(data_type, DataType::Utf8 | DataType::Binary) { + DataType::List(Arc::new(ArrowField::new("item", DataType::UInt8, false))) + } else { + DataType::LargeList(Arc::new(ArrowField::new( + "item", + DataType::UInt8, + false, + ))) + }; + let list_field = Field::try_from(ArrowField::new( + field.name.clone(), + list_type, + field.nullable, + )) + .unwrap(); + let list_scheduler = self.create_list_scheduler( + &list_field, + column_infos, + buffers, + &column_info, + )?; + let binary_scheduler = Box::new(BinaryFieldScheduler::new( + list_scheduler.into(), + field.data_type(), + )); + return Ok(binary_scheduler); + } else { + let scheduler = + self.create_primitive_scheduler(field, &column_info, buffers)?; + return Ok(scheduler); + } + } else { + return self.create_primitive_scheduler(field, &column_info, buffers); + } + } + match &data_type { + DataType::FixedSizeList(inner, _dimension) => { + // A fixed size list column could either be a physical or a logical decoder + // depending on the child data type. + if Self::is_array_primitive(inner.data_type()) { + let primitive_col = column_infos.expect_next()?; + let scheduler = + self.create_primitive_scheduler(field, primitive_col, buffers)?; + Ok(scheduler) + } else { + todo!() + } + } + DataType::Dictionary(_key_type, value_type) => { + if Self::is_array_primitive(value_type) || value_type.is_binary_like() { + let primitive_col = column_infos.expect_next()?; + let scheduler = + self.create_primitive_scheduler(field, primitive_col, buffers)?; + Ok(scheduler) + } else { + Err(Error::not_supported_source( + format!( + "No way to decode into a dictionary field of type {}", + value_type + ) + .into(), + )) + } + } + DataType::List(_) | DataType::LargeList(_) => { + let offsets_column = column_infos.expect_next()?.clone(); + column_infos.next_top_level(); + self.create_list_scheduler(field, column_infos, buffers, &offsets_column) + } + DataType::Struct(fields) => { + let column_info = column_infos.expect_next()?; + + // Column is blob and user is asking for descriptions + if let Some(blob_col) = Self::unwrap_blob(column_info.as_ref()) { + // Can use primitive scheduler here since descriptions are always packed struct + return self.create_primitive_scheduler(field, &blob_col, buffers); + } + + if Self::check_packed_struct(column_info) { + // use packed struct encoding + self.create_primitive_scheduler(field, column_info, buffers) + } else { + // use default struct encoding + Self::check_simple_struct(column_info, &field.name).unwrap(); + let num_rows = column_info + .page_infos + .iter() + .map(|page| page.num_rows) + .sum(); + let mut child_schedulers = Vec::with_capacity(field.children.len()); + for field in &field.children { + column_infos.next_top_level(); + let field_scheduler = + self.create_array_field_scheduler(field, column_infos, buffers)?; + child_schedulers.push(Arc::from(field_scheduler)); + } + + let fields = fields.clone(); + Ok(Box::new(SimpleStructScheduler::new( + child_schedulers, + fields, + num_rows, + ))) + } + } + // TODO: Still need support for RLE + _ => todo!(), + } + } +} + +/// Create's a dummy ColumnInfo for the root column +fn root_column(num_rows: u64) -> ColumnInfo { + let num_root_pages = num_rows.div_ceil(u32::MAX as u64); + let final_page_num_rows = num_rows % (u32::MAX as u64); + let root_pages = (0..num_root_pages) + .map(|i| PageInfo { + num_rows: if i == num_root_pages - 1 { + final_page_num_rows + } else { + u64::MAX + }, + encoding: PageEncoding::Legacy(pb::ArrayEncoding { + array_encoding: Some(pb::array_encoding::ArrayEncoding::Struct( + pb::SimpleStruct {}, + )), + }), + priority: 0, // not used by the array scheduler + buffer_offsets_and_sizes: Arc::new([]), + }) + .collect::>(); + ColumnInfo { + buffer_offsets_and_sizes: Arc::new([]), + encoding: pb::ColumnEncoding { + column_encoding: Some(pb::column_encoding::ColumnEncoding::Values(())), + }, + index: u32::MAX, + page_infos: Arc::from(root_pages), + } +} + +pub enum RootDecoder { + Structural(StructuralStructDecoder), + Array(SimpleStructDecoder), +} + +impl RootDecoder { + pub fn into_structural(self) -> StructuralStructDecoder { + match self { + Self::Structural(decoder) => decoder, + Self::Array(_) => panic!("Expected a structural decoder"), + } + } + + pub fn into_array(self) -> SimpleStructDecoder { + match self { + Self::Array(decoder) => decoder, + Self::Structural(_) => panic!("Expected an array decoder"), + } + } +} + +impl DecodeBatchScheduler { + /// Creates a new decode scheduler with the expected schema and the column + /// metadata of the file. + #[allow(clippy::too_many_arguments)] + pub async fn try_new<'a>( + schema: &'a Schema, + column_indices: &[u32], + column_infos: &[Arc], + file_buffer_positions_and_sizes: &'a Vec<(u64, u64)>, + num_rows: u64, + _decoder_plugins: Arc, + io: Arc, + cache: Arc, + filter: &FilterExpression, + decoder_config: &DecoderConfig, + ) -> Result { + assert!(num_rows > 0); + let buffers = FileBuffers { + positions_and_sizes: file_buffer_positions_and_sizes, + }; + let arrow_schema = ArrowSchema::from(schema); + let root_fields = arrow_schema.fields().clone(); + let root_type = DataType::Struct(root_fields.clone()); + let mut root_field = Field::try_from(&ArrowField::new("root", root_type, false))?; + // root_field.children and schema.fields should be identical at this point but the latter + // has field ids and the former does not. This line restores that. + // TODO: Is there another way to create the root field without forcing a trip through arrow? + root_field.children.clone_from(&schema.fields); + root_field + .metadata + .insert("__lance_decoder_root".to_string(), "true".to_string()); + + if column_infos.is_empty() || column_infos[0].is_structural() { + let mut column_iter = ColumnInfoIter::new(column_infos.to_vec(), column_indices); + + let strategy = CoreFieldDecoderStrategy::from_decoder_config(decoder_config); + let mut root_scheduler = + strategy.create_structural_field_scheduler(&root_field, &mut column_iter)?; + + let context = SchedulerContext::new(io, cache.clone()); + root_scheduler.initialize(filter, &context).await?; + + Ok(Self { + root_scheduler: RootScheduler::Structural(root_scheduler), + root_fields, + cache, + }) + } else { + // The old encoding style expected a header column for structs and so we + // need a header column for the top-level struct + let mut columns = Vec::with_capacity(column_infos.len() + 1); + columns.push(Arc::new(root_column(num_rows))); + columns.extend(column_infos.iter().cloned()); + + let adjusted_column_indices = [0_u32] + .into_iter() + .chain(column_indices.iter().map(|i| i.saturating_add(1))) + .collect::>(); + let mut column_iter = ColumnInfoIter::new(columns, &adjusted_column_indices); + let strategy = CoreFieldDecoderStrategy::from_decoder_config(decoder_config); + let root_scheduler = + strategy.create_array_field_scheduler(&root_field, &mut column_iter, buffers)?; + + let context = SchedulerContext::new(io, cache.clone()); + root_scheduler.initialize(filter, &context).await?; + + Ok(Self { + root_scheduler: RootScheduler::Array(root_scheduler.into()), + root_fields, + cache, + }) + } + } + + #[deprecated(since = "0.29.1", note = "This is for v2.0 array-encoding paths")] + pub fn from_scheduler( + root_scheduler: Arc, + root_fields: Fields, + cache: Arc, + ) -> Self { + Self { + root_scheduler: RootScheduler::Array(root_scheduler), + root_fields, + cache, + } + } + + fn do_schedule_ranges_structural( + &mut self, + ranges: &[Range], + filter: &FilterExpression, + io: Arc, + mut schedule_action: impl FnMut(Result) -> bool, + ) { + let root_scheduler = self.root_scheduler.as_structural(); + let mut context = SchedulerContext::new(io, self.cache.clone()); + let maybe_root_job = root_scheduler.schedule_ranges(ranges, filter); + if let Err(schedule_ranges_err) = maybe_root_job { + schedule_action(Err(schedule_ranges_err)); + return; + } + let mut root_job = maybe_root_job.unwrap(); + let mut num_rows_scheduled = 0; + loop { + let maybe_next_scan_lines = root_job.schedule_next(&mut context); + if let Err(err) = maybe_next_scan_lines { + schedule_action(Err(err)); + return; + } + let next_scan_lines = maybe_next_scan_lines.unwrap(); + if next_scan_lines.is_empty() { + return; + } + for next_scan_line in next_scan_lines { + trace!( + "Scheduled scan line of {} rows and {} decoders", + next_scan_line.rows_scheduled, + next_scan_line.decoders.len() + ); + num_rows_scheduled += next_scan_line.rows_scheduled; + if !schedule_action(Ok(DecoderMessage { + scheduled_so_far: num_rows_scheduled, + decoders: next_scan_line.decoders, + })) { + // Decoder has disconnected + return; + } + } + } + } + + fn do_schedule_ranges_array( + &mut self, + ranges: &[Range], + filter: &FilterExpression, + io: Arc, + mut schedule_action: impl FnMut(Result) -> bool, + // If specified, this will be used as the top_level_row for all scheduling + // tasks. This is used by list scheduling to ensure all items scheduling + // tasks are scheduled at the same top level row. + priority: Option>, + ) { + let root_scheduler = self.root_scheduler.as_array(); + let rows_requested = ranges.iter().map(|r| r.end - r.start).sum::(); + trace!( + "Scheduling {} ranges across {}..{} ({} rows){}", + ranges.len(), + ranges.first().unwrap().start, + ranges.last().unwrap().end, + rows_requested, + priority + .as_ref() + .map(|p| format!(" (priority={:?})", p)) + .unwrap_or_default() + ); + + let mut context = SchedulerContext::new(io, self.cache.clone()); + let maybe_root_job = root_scheduler.schedule_ranges(ranges, filter); + if let Err(schedule_ranges_err) = maybe_root_job { + schedule_action(Err(schedule_ranges_err)); + return; + } + let mut root_job = maybe_root_job.unwrap(); + let mut num_rows_scheduled = 0; + let mut rows_to_schedule = root_job.num_rows(); + let mut priority = priority.unwrap_or(Box::new(SimplePriorityRange::new(0))); + trace!("Scheduled ranges refined to {} rows", rows_to_schedule); + while rows_to_schedule > 0 { + let maybe_next_scan_line = root_job.schedule_next(&mut context, priority.as_ref()); + if let Err(schedule_next_err) = maybe_next_scan_line { + schedule_action(Err(schedule_next_err)); + return; + } + let next_scan_line = maybe_next_scan_line.unwrap(); + priority.advance(next_scan_line.rows_scheduled); + num_rows_scheduled += next_scan_line.rows_scheduled; + rows_to_schedule -= next_scan_line.rows_scheduled; + trace!( + "Scheduled scan line of {} rows and {} decoders", + next_scan_line.rows_scheduled, + next_scan_line.decoders.len() + ); + if !schedule_action(Ok(DecoderMessage { + scheduled_so_far: num_rows_scheduled, + decoders: next_scan_line.decoders, + })) { + // Decoder has disconnected + return; + } + + trace!("Finished scheduling {} ranges", ranges.len()); + } + } + + fn do_schedule_ranges( + &mut self, + ranges: &[Range], + filter: &FilterExpression, + io: Arc, + schedule_action: impl FnMut(Result) -> bool, + // If specified, this will be used as the top_level_row for all scheduling + // tasks. This is used by list scheduling to ensure all items scheduling + // tasks are scheduled at the same top level row. + priority: Option>, + ) { + match &self.root_scheduler { + RootScheduler::Array(_) => { + self.do_schedule_ranges_array(ranges, filter, io, schedule_action, priority) + } + RootScheduler::Structural(_) => { + self.do_schedule_ranges_structural(ranges, filter, io, schedule_action) + } + } + } + + // This method is similar to schedule_ranges but instead of + // sending the decoders to a channel it collects them all into a vector + pub fn schedule_ranges_to_vec( + &mut self, + ranges: &[Range], + filter: &FilterExpression, + io: Arc, + priority: Option>, + ) -> Result> { + let mut decode_messages = Vec::new(); + self.do_schedule_ranges( + ranges, + filter, + io, + |msg| { + decode_messages.push(msg); + true + }, + priority, + ); + decode_messages.into_iter().collect::>>() + } + + /// Schedules the load of multiple ranges of rows + /// + /// Ranges must be non-overlapping and in sorted order + /// + /// # Arguments + /// + /// * `ranges` - The ranges of rows to load + /// * `sink` - A channel to send the decode tasks + /// * `scheduler` An I/O scheduler to issue I/O requests + #[instrument(level = "debug", skip_all)] + pub fn schedule_ranges( + &mut self, + ranges: &[Range], + filter: &FilterExpression, + sink: mpsc::UnboundedSender>, + scheduler: Arc, + ) { + self.do_schedule_ranges( + ranges, + filter, + scheduler, + |msg| { + match sink.send(msg) { + Ok(_) => true, + Err(SendError { .. }) => { + // The receiver has gone away. We can't do anything about it + // so just ignore the error. + debug!( + "schedule_ranges aborting early since decoder appears to have been dropped" + ); + false + } + } + }, + None, + ) + } + + /// Schedules the load of a range of rows + /// + /// # Arguments + /// + /// * `range` - The range of rows to load + /// * `sink` - A channel to send the decode tasks + /// * `scheduler` An I/O scheduler to issue I/O requests + #[instrument(level = "debug", skip_all)] + pub fn schedule_range( + &mut self, + range: Range, + filter: &FilterExpression, + sink: mpsc::UnboundedSender>, + scheduler: Arc, + ) { + self.schedule_ranges(&[range], filter, sink, scheduler) + } + + /// Schedules the load of selected rows + /// + /// # Arguments + /// + /// * `indices` - The row indices to load (these must be in ascending order!) + /// * `sink` - A channel to send the decode tasks + /// * `scheduler` An I/O scheduler to issue I/O requests + pub fn schedule_take( + &mut self, + indices: &[u64], + filter: &FilterExpression, + sink: mpsc::UnboundedSender>, + scheduler: Arc, + ) { + debug_assert!(indices.windows(2).all(|w| w[0] < w[1])); + if indices.is_empty() { + return; + } + trace!("Scheduling take of {} rows", indices.len()); + let ranges = Self::indices_to_ranges(indices); + self.schedule_ranges(&ranges, filter, sink, scheduler) + } + + // coalesce continuous indices if possible (the input indices must be sorted and non-empty) + fn indices_to_ranges(indices: &[u64]) -> Vec> { + let mut ranges = Vec::new(); + let mut start = indices[0]; + + for window in indices.windows(2) { + if window[1] != window[0] + 1 { + ranges.push(start..window[0] + 1); + start = window[1]; + } + } + + ranges.push(start..*indices.last().unwrap() + 1); + ranges + } +} + +pub struct ReadBatchTask { + pub task: BoxFuture<'static, Result>, + pub num_rows: u32, +} + +/// A stream that takes scheduled jobs and generates decode tasks from them. +pub struct BatchDecodeStream { + context: DecoderContext, + root_decoder: SimpleStructDecoder, + rows_remaining: u64, + rows_per_batch: u32, + rows_scheduled: u64, + rows_drained: u64, + scheduler_exhausted: bool, + emitted_batch_size_warning: Arc, +} + +impl BatchDecodeStream { + /// Create a new instance of a batch decode stream + /// + /// # Arguments + /// + /// * `scheduled` - an incoming stream of decode tasks from a `DecodeBatchScheduler` + /// * `schema` - the schema of the data to create + /// * `rows_per_batch` the number of rows to create before making a batch + /// * `num_rows` the total number of rows scheduled + /// * `num_columns` the total number of columns in the file + pub fn new( + scheduled: mpsc::UnboundedReceiver>, + rows_per_batch: u32, + num_rows: u64, + root_decoder: SimpleStructDecoder, + ) -> Self { + Self { + context: DecoderContext::new(scheduled), + root_decoder, + rows_remaining: num_rows, + rows_per_batch, + rows_scheduled: 0, + rows_drained: 0, + scheduler_exhausted: false, + emitted_batch_size_warning: Arc::new(Once::new()), + } + } + + fn accept_decoder(&mut self, decoder: DecoderReady) -> Result<()> { + if decoder.path.is_empty() { + // The root decoder we can ignore + Ok(()) + } else { + self.root_decoder.accept_child(decoder) + } + } + + #[instrument(level = "debug", skip_all)] + async fn wait_for_scheduled(&mut self, scheduled_need: u64) -> Result { + if self.scheduler_exhausted { + return Ok(self.rows_scheduled); + } + while self.rows_scheduled < scheduled_need { + let next_message = self.context.source.recv().await; + match next_message { + Some(scan_line) => { + let scan_line = scan_line?; + self.rows_scheduled = scan_line.scheduled_so_far; + for message in scan_line.decoders { + self.accept_decoder(message.into_array())?; + } + } + None => { + // Schedule ended before we got all the data we expected. This probably + // means some kind of pushdown filter was applied and we didn't load as + // much data as we thought we would. + self.scheduler_exhausted = true; + return Ok(self.rows_scheduled); + } + } + } + Ok(scheduled_need) + } + + #[instrument(level = "debug", skip_all)] + async fn next_batch_task(&mut self) -> Result> { + trace!( + "Draining batch task (rows_remaining={} rows_drained={} rows_scheduled={})", + self.rows_remaining, self.rows_drained, self.rows_scheduled, + ); + if self.rows_remaining == 0 { + return Ok(None); + } + + let mut to_take = self.rows_remaining.min(self.rows_per_batch as u64); + self.rows_remaining -= to_take; + + let scheduled_need = (self.rows_drained + to_take).saturating_sub(self.rows_scheduled); + trace!( + "scheduled_need = {} because rows_drained = {} and to_take = {} and rows_scheduled = {}", + scheduled_need, self.rows_drained, to_take, self.rows_scheduled + ); + if scheduled_need > 0 { + let desired_scheduled = scheduled_need + self.rows_scheduled; + trace!( + "Draining from scheduler (desire at least {} scheduled rows)", + desired_scheduled + ); + let actually_scheduled = self.wait_for_scheduled(desired_scheduled).await?; + if actually_scheduled < desired_scheduled { + let under_scheduled = desired_scheduled - actually_scheduled; + to_take -= under_scheduled; + } + } + + if to_take == 0 { + return Ok(None); + } + + // wait_for_loaded waits for *>* loaded_need (not >=) so we do a -1 here + let loaded_need = self.rows_drained + to_take - 1; + trace!( + "Waiting for I/O (desire at least {} fully loaded rows)", + loaded_need + ); + self.root_decoder.wait_for_loaded(loaded_need).await?; + + let next_task = self.root_decoder.drain(to_take)?; + self.rows_drained += to_take; + Ok(Some(next_task)) + } + + pub fn into_stream(self) -> BoxStream<'static, ReadBatchTask> { + let stream = futures::stream::unfold(self, |mut slf| async move { + let next_task = match slf.next_batch_task().await { + Ok(Some(next_task)) => next_task, + Ok(None) => return None, + Err(err) => { + slf.rows_remaining = 0; + return Some(( + ReadBatchTask { + task: async move { Err(err) }.boxed(), + num_rows: 0, + }, + slf, + )); + } + }; + let num_rows = next_task.num_rows; + let emitted_batch_size_warning = slf.emitted_batch_size_warning.clone(); + let task = async move { + // Real decode work happens inside into_batch, which can block the current + // thread for a long time. By spawning it as a new task, we allow Tokio's + // worker threads to keep making progress. + let (batch, _data_size) = + tokio::spawn(async move { next_task.into_batch(emitted_batch_size_warning) }) + .await + .map_err(|err| Error::wrapped(err.into()))??; + Ok(batch) + }; + // This should be true since batch size is u32 + debug_assert!(num_rows <= u32::MAX as u64); + Some(( + ReadBatchTask { + task: task.boxed(), + num_rows: num_rows as u32, + }, + slf, + )) + }); + stream.boxed() + } +} + +// Utility types to smooth out the differences between the 2.0 and 2.1 decoders so that +// we can have a single implementation of the batch decode iterator +enum RootDecoderMessage { + LoadedPage(LoadedPageShard), + ArrayPage(DecoderReady), +} +trait RootDecoderType { + fn accept_message(&mut self, message: RootDecoderMessage) -> Result<()>; + fn drain_batch(&mut self, num_rows: u64) -> Result; + fn wait(&mut self, loaded_need: u64, runtime: &tokio::runtime::Runtime) -> Result<()>; +} +impl RootDecoderType for StructuralStructDecoder { + fn accept_message(&mut self, message: RootDecoderMessage) -> Result<()> { + let RootDecoderMessage::LoadedPage(loaded_page) = message else { + unreachable!() + }; + self.accept_page(loaded_page) + } + fn drain_batch(&mut self, num_rows: u64) -> Result { + self.drain_batch_task(num_rows) + } + fn wait(&mut self, _: u64, _: &tokio::runtime::Runtime) -> Result<()> { + // Waiting happens elsewhere (not as part of the decoder) + Ok(()) + } +} +impl RootDecoderType for SimpleStructDecoder { + fn accept_message(&mut self, message: RootDecoderMessage) -> Result<()> { + let RootDecoderMessage::ArrayPage(array_page) = message else { + unreachable!() + }; + self.accept_child(array_page) + } + fn drain_batch(&mut self, num_rows: u64) -> Result { + self.drain(num_rows) + } + fn wait(&mut self, loaded_need: u64, runtime: &tokio::runtime::Runtime) -> Result<()> { + runtime.block_on(self.wait_for_loaded(loaded_need)) + } +} + +/// A blocking batch decoder that performs synchronous decoding +struct BatchDecodeIterator { + messages: VecDeque>, + root_decoder: T, + rows_remaining: u64, + rows_per_batch: u32, + rows_scheduled: u64, + rows_drained: u64, + emitted_batch_size_warning: Arc, + // Note: this is not the runtime on which I/O happens. + // That's always in the scheduler. This is just a runtime we use to + // sleep the current thread if I/O is unready + wait_for_io_runtime: tokio::runtime::Runtime, + schema: Arc, +} + +impl BatchDecodeIterator { + /// Create a new instance of a batch decode iterator + pub fn new( + messages: VecDeque>, + rows_per_batch: u32, + num_rows: u64, + root_decoder: T, + schema: Arc, + ) -> Self { + Self { + messages, + root_decoder, + rows_remaining: num_rows, + rows_per_batch, + rows_scheduled: 0, + rows_drained: 0, + wait_for_io_runtime: tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(), + emitted_batch_size_warning: Arc::new(Once::new()), + schema, + } + } + + /// Wait for a single page of data to finish loading + /// + /// If the data is not available this will perform a *blocking* wait (put + /// the current thread to sleep) + fn wait_for_page(&self, unloaded_page: UnloadedPageShard) -> Result { + match maybe_done(unloaded_page.0) { + // Fast path, avoid all runtime shenanigans if the data is ready + MaybeDone::Done(loaded_page) => loaded_page, + // Slow path, we need to wait on I/O, enter the runtime + MaybeDone::Future(fut) => self.wait_for_io_runtime.block_on(fut), + MaybeDone::Gone => unreachable!(), + } + } + + /// Waits for I/O until `scheduled_need` rows have been loaded + /// + /// Note that `scheduled_need` is cumulative. E.g. this method + /// should be called with 5, 10, 15 and not 5, 5, 5 + #[instrument(level = "debug", skip_all)] + fn wait_for_io(&mut self, scheduled_need: u64, to_take: u64) -> Result { + while self.rows_scheduled < scheduled_need && !self.messages.is_empty() { + let message = self.messages.pop_front().unwrap()?; + self.rows_scheduled = message.scheduled_so_far; + for decoder_message in message.decoders { + match decoder_message { + MessageType::UnloadedPage(unloaded_page) => { + let loaded_page = self.wait_for_page(unloaded_page)?; + self.root_decoder + .accept_message(RootDecoderMessage::LoadedPage(loaded_page))?; + } + MessageType::DecoderReady(decoder_ready) => { + // The root decoder we can ignore + if !decoder_ready.path.is_empty() { + self.root_decoder + .accept_message(RootDecoderMessage::ArrayPage(decoder_ready))?; + } + } + } + } + } + + let loaded_need = self.rows_drained + to_take.min(self.rows_per_batch as u64) - 1; + + self.root_decoder + .wait(loaded_need, &self.wait_for_io_runtime)?; + Ok(self.rows_scheduled) + } + + #[instrument(level = "debug", skip_all)] + fn next_batch_task(&mut self) -> Result> { + trace!( + "Draining batch task (rows_remaining={} rows_drained={} rows_scheduled={})", + self.rows_remaining, self.rows_drained, self.rows_scheduled, + ); + if self.rows_remaining == 0 { + return Ok(None); + } + + let mut to_take = self.rows_remaining.min(self.rows_per_batch as u64); + self.rows_remaining -= to_take; + + let scheduled_need = (self.rows_drained + to_take).saturating_sub(self.rows_scheduled); + trace!( + "scheduled_need = {} because rows_drained = {} and to_take = {} and rows_scheduled = {}", + scheduled_need, self.rows_drained, to_take, self.rows_scheduled + ); + if scheduled_need > 0 { + let desired_scheduled = scheduled_need + self.rows_scheduled; + trace!( + "Draining from scheduler (desire at least {} scheduled rows)", + desired_scheduled + ); + let actually_scheduled = self.wait_for_io(desired_scheduled, to_take)?; + if actually_scheduled < desired_scheduled { + let under_scheduled = desired_scheduled - actually_scheduled; + to_take -= under_scheduled; + } + } + + if to_take == 0 { + return Ok(None); + } + + let next_task = self.root_decoder.drain_batch(to_take)?; + + self.rows_drained += to_take; + + let (batch, _data_size) = next_task.into_batch(self.emitted_batch_size_warning.clone())?; + + Ok(Some(batch)) + } +} + +impl Iterator for BatchDecodeIterator { + type Item = ArrowResult; + + fn next(&mut self) -> Option { + self.next_batch_task() + .transpose() + .map(|r| r.map_err(ArrowError::from)) + } +} + +impl RecordBatchReader for BatchDecodeIterator { + fn schema(&self) -> Arc { + self.schema.clone() + } +} + +/// Estimate the number of bytes per row for a given Arrow data type. +/// +/// For fixed-width types this is exact. For variable-width types (strings, +/// binary, lists) a rough default is used. The estimate is used as a +/// starting point when `batch_size_bytes` is set; a post-decode feedback +/// loop corrects it after the first batch. +/// +/// This estimate ignores validity bitmaps at the moment. We can't infer +/// their presence simply from the data_type and their impact is probably +/// fairly negligible. +fn estimate_bytes_per_row(data_type: &DataType) -> f64 { + if let Some(w) = data_type.byte_width_opt() { + return w as f64; + } + match data_type { + DataType::Boolean => 1.0 / 8.0, + DataType::Utf8 | DataType::Binary | DataType::LargeUtf8 | DataType::LargeBinary => 64.0, + DataType::Struct(fields) => fields + .iter() + .map(|f| estimate_bytes_per_row(f.data_type())) + .sum(), + DataType::List(child) | DataType::LargeList(child) => { + 5.0 * estimate_bytes_per_row(child.data_type()) + } + DataType::FixedSizeList(child, dim) => { + *dim as f64 * estimate_bytes_per_row(child.data_type()) + } + DataType::Dictionary(_, value_type) => estimate_bytes_per_row(value_type), + DataType::Map(entries, _) => 5.0 * estimate_bytes_per_row(entries.data_type()), + _ => 64.0, + } +} + +/// A stream that takes scheduled jobs and generates decode tasks from them. +pub struct StructuralBatchDecodeStream { + context: DecoderContext, + root_decoder: StructuralStructDecoder, + rows_remaining: u64, + rows_per_batch: u32, + rows_scheduled: u64, + rows_drained: u64, + scheduler_exhausted: bool, + emitted_batch_size_warning: Arc, + // Decode scheduling policy selected at planning time. + // + // Performance tradeoff: + // - true: spawn `into_batch` onto Tokio, which improves scan throughput by allowing + // more decode parallelism. + // - false: run `into_batch` inline, which avoids Tokio scheduling overhead and is + // typically better for point lookups / small takes. + spawn_batch_decode_tasks: bool, + /// If set, target this many bytes per batch while retaining `rows_per_batch` + /// as an independent upper bound. + batch_size_bytes: Option, + /// Schema-based estimate of bytes per row, computed once at construction. + /// Only meaningful when `batch_size_bytes` is `Some`. + schema_bytes_per_row: f64, + /// Post-decode feedback: actual bytes-per-row measured from the most + /// recently decoded batch. Zero means no feedback yet (use schema estimate). + bytes_per_row_feedback: Arc, +} + +impl StructuralBatchDecodeStream { + /// Create a new instance of a batch decode stream + /// + /// # Arguments + /// + /// * `scheduled` - an incoming stream of decode tasks from a `DecodeBatchScheduler` + /// * `schema` - the schema of the data to create + /// * `rows_per_batch` the number of rows to create before making a batch + /// * `num_rows` the total number of rows scheduled + /// * `num_columns` the total number of columns in the file + pub fn new( + scheduled: mpsc::UnboundedReceiver>, + rows_per_batch: u32, + num_rows: u64, + root_decoder: StructuralStructDecoder, + spawn_batch_decode_tasks: bool, + batch_size_bytes: Option, + ) -> Self { + let schema_bytes_per_row = if batch_size_bytes.is_some() { + estimate_bytes_per_row(root_decoder.data_type()).max(1.0) + } else { + 0.0 + }; + Self { + context: DecoderContext::new(scheduled), + root_decoder, + rows_remaining: num_rows, + rows_per_batch, + rows_scheduled: 0, + rows_drained: 0, + scheduler_exhausted: false, + emitted_batch_size_warning: Arc::new(Once::new()), + spawn_batch_decode_tasks, + batch_size_bytes, + schema_bytes_per_row, + bytes_per_row_feedback: Arc::new(AtomicU64::new(0)), + } + } + + #[instrument(level = "debug", skip_all)] + async fn wait_for_scheduled(&mut self, scheduled_need: u64) -> Result { + if self.scheduler_exhausted { + return Ok(self.rows_scheduled); + } + while self.rows_scheduled < scheduled_need { + let next_message = self.context.source.recv().await; + match next_message { + Some(scan_line) => { + let scan_line = scan_line?; + self.rows_scheduled = scan_line.scheduled_so_far; + for message in scan_line.decoders { + let unloaded_page = message.into_structural(); + let loaded_page = unloaded_page.0.await?; + self.root_decoder.accept_page(loaded_page)?; + } + } + None => { + // Schedule ended before we got all the data we expected. This probably + // means some kind of pushdown filter was applied and we didn't load as + // much data as we thought we would. + self.scheduler_exhausted = true; + return Ok(self.rows_scheduled); + } + } + } + Ok(scheduled_need) + } + + #[instrument(level = "debug", skip_all)] + async fn next_batch_task(&mut self) -> Result> { + trace!( + "Draining batch task (rows_remaining={} rows_drained={} rows_scheduled={})", + self.rows_remaining, self.rows_drained, self.rows_scheduled, + ); + if self.rows_remaining == 0 { + return Ok(None); + } + + let row_limit = self.rows_remaining.min(self.rows_per_batch as u64); + let mut to_take = if let Some(batch_size_bytes) = self.batch_size_bytes { + let feedback = self.bytes_per_row_feedback.load(Ordering::Relaxed); + let bpr = if feedback > 0 { + feedback as f64 + } else { + self.schema_bytes_per_row + }; + let rows = (batch_size_bytes as f64 / bpr) as u64; + row_limit.min(rows.max(1)) + } else { + row_limit + }; + self.rows_remaining -= to_take; + + let scheduled_need = (self.rows_drained + to_take).saturating_sub(self.rows_scheduled); + trace!( + "scheduled_need = {} because rows_drained = {} and to_take = {} and rows_scheduled = {}", + scheduled_need, self.rows_drained, to_take, self.rows_scheduled + ); + if scheduled_need > 0 { + let desired_scheduled = scheduled_need + self.rows_scheduled; + trace!( + "Draining from scheduler (desire at least {} scheduled rows)", + desired_scheduled + ); + let actually_scheduled = self.wait_for_scheduled(desired_scheduled).await?; + if actually_scheduled < desired_scheduled { + let under_scheduled = desired_scheduled - actually_scheduled; + to_take -= under_scheduled; + } + } + + if to_take == 0 { + return Ok(None); + } + + let next_task = self.root_decoder.drain_batch_task(to_take)?; + self.rows_drained += to_take; + Ok(Some(next_task)) + } + + pub fn into_stream(self) -> BoxStream<'static, ReadBatchTask> { + let stream = futures::stream::unfold(self, |mut slf| async move { + let next_task = match slf.next_batch_task().await { + Ok(Some(next_task)) => next_task, + Ok(None) => return None, + Err(err) => { + slf.rows_remaining = 0; + return Some(( + ReadBatchTask { + task: async move { Err(err) }.boxed(), + num_rows: 0, + }, + slf, + )); + } + }; + let num_rows = next_task.num_rows; + let emitted_batch_size_warning = slf.emitted_batch_size_warning.clone(); + let bytes_per_row_feedback = slf.bytes_per_row_feedback.clone(); + // Capture the per-stream policy once so every emitted batch task follows the + // same throughput-vs-overhead choice made by the scheduler. + let spawn_batch_decode_tasks = slf.spawn_batch_decode_tasks; + let task = async move { + let (batch, data_size) = if spawn_batch_decode_tasks { + tokio::spawn(async move { next_task.into_batch(emitted_batch_size_warning) }) + .await + .map_err(|err| Error::wrapped(err.into()))?? + } else { + next_task.into_batch(emitted_batch_size_warning)? + }; + let num_rows = batch.num_rows() as u64; + if let Some(bpr) = data_size.checked_div(num_rows) { + let prev = bytes_per_row_feedback.load(Ordering::Relaxed); + let next = if prev == 0 || bpr >= prev { + // First batch or actual size is larger than estimate: + // adopt immediately to avoid OOM. + bpr + } else { + // Actual size is smaller: degrade gradually toward + // the true value to avoid over-correcting on a + // single anomalous batch. + (prev + bpr) / 2 + }; + bytes_per_row_feedback.store(next.max(1), Ordering::Relaxed); + } + Ok(batch) + }; + // This should be true since batch size is u32 + debug_assert!(num_rows <= u32::MAX as u64); + Some(( + ReadBatchTask { + task: task.boxed(), + num_rows: num_rows as u32, + }, + slf, + )) + }); + stream.boxed() + } +} + +#[derive(Debug)] +pub enum RequestedRows { + Ranges(Vec>), + Indices(Vec), +} + +impl RequestedRows { + pub fn num_rows(&self) -> u64 { + match self { + Self::Ranges(ranges) => ranges.iter().map(|r| r.end - r.start).sum(), + Self::Indices(indices) => indices.len() as u64, + } + } + + pub fn trim_empty_ranges(mut self) -> Self { + if let Self::Ranges(ranges) = &mut self { + ranges.retain(|r| !r.is_empty()); + } + self + } +} + +/// Configuration for decoder behavior +#[derive(Debug, Clone)] +pub struct DecoderConfig { + /// Whether to cache repetition indices for better performance. + /// + /// This defaults to the `LANCE_READ_CACHE_REPETITION_INDEX` environment variable + /// when present and is enabled by default. Set the env var to a non-truthy + /// value (for example `0` or `false`) to disable it. The env var is read + /// once per process. + pub cache_repetition_index: bool, + /// Whether to validate decoded data + pub validate_on_decode: bool, + /// Override the strategy used to dispatch the scheduling work in + /// [`schedule_and_decode`]. + /// + /// `schedule_and_decode` always awaits the scheduler's `initialize` (which + /// performs metadata I/O) before returning. This flag controls what + /// happens with the subsequent (synchronous) work of pushing decoder + /// messages into the channel that feeds the decode stream. + /// + /// * `None` - default behavior: the scheduling work runs inline (as part + /// of the `schedule_and_decode` await) when the request is small + /// (controlled by the `LANCE_INLINE_SCHEDULING_THRESHOLD` env var) and + /// is dispatched onto a spawned task otherwise. + /// * `Some(true)` - always run scheduling inline. The await of + /// `schedule_and_decode` does not return until every decoder message + /// has been queued. + /// * `Some(false)` - always spawn a task for scheduling so that it can + /// overlap with consumption of the decode stream. + pub inline_scheduling: Option, +} + +impl Default for DecoderConfig { + fn default() -> Self { + Self { + cache_repetition_index: default_cache_repetition_index(), + validate_on_decode: false, + inline_scheduling: None, + } + } +} + +#[derive(Debug, Clone)] +pub struct SchedulerDecoderConfig { + pub decoder_plugins: Arc, + pub batch_size: u32, + pub io: Arc, + pub cache: Arc, + /// Decoder configuration + pub decoder_config: DecoderConfig, + /// If set, target this many bytes per batch while retaining `batch_size` as + /// an independent row-count upper bound. + /// + /// Only supported for v2.1+ (structural) files. For v2.0 files this + /// option is ignored and a warning is logged. + pub batch_size_bytes: Option, +} + +fn check_scheduler_on_drop( + stream: BoxStream<'static, ReadBatchTask>, + scheduler_handle: tokio::task::JoinHandle<()>, +) -> BoxStream<'static, ReadBatchTask> { + // This is a bit weird but we create an "empty stream" that unwraps the scheduler handle (which + // will panic if the scheduler panicked). This let's us check if the scheduler panicked + // when the stream finishes. + let abort_handle = scheduler_handle.abort_handle(); + let mut scheduler_handle = Some(scheduler_handle); + let check_scheduler = stream::unfold((), move |_| { + let handle = scheduler_handle.take(); + async move { + if let Some(handle) = handle { + handle.await.unwrap(); + } + None + } + }); + stream + .chain(check_scheduler) + .on_drop(move || { + // Abort the scheduler task on early drop. The scheduler task holds + // a reference to the I/O scheduler (via config.io) which keeps the + // ScanScheduler alive. If the scheduler task is stuck waiting for + // initialization I/O (which is blocked on backpressure that will + // never drain because no one is consuming the stream), we need to + // abort it so it releases its I/O reference and allows the + // ScanScheduler to drop and cancel pending I/O. + abort_handle.abort(); + }) + .boxed() +} + +#[allow(clippy::too_many_arguments)] +pub fn create_decode_stream( + schema: &Schema, + num_rows: u64, + batch_size: u32, + is_structural: bool, + should_validate: bool, + spawn_structural_batch_decode_tasks: bool, + rx: mpsc::UnboundedReceiver>, + batch_size_bytes: Option, +) -> Result> { + if is_structural { + let arrow_schema = ArrowSchema::from(schema); + let structural_decoder = StructuralStructDecoder::new( + arrow_schema.fields, + should_validate, + /*is_root=*/ true, + )?; + Ok(StructuralBatchDecodeStream::new( + rx, + batch_size, + num_rows, + structural_decoder, + spawn_structural_batch_decode_tasks, + batch_size_bytes, + ) + .into_stream()) + } else { + if batch_size_bytes.is_some() { + warn!("batch_size_bytes is not supported for v2.0 files and will be ignored"); + } + let arrow_schema = ArrowSchema::from(schema); + let root_fields = arrow_schema.fields; + + let simple_struct_decoder = SimpleStructDecoder::new(root_fields, num_rows); + Ok(BatchDecodeStream::new(rx, batch_size, num_rows, simple_struct_decoder).into_stream()) + } +} + +/// Creates a iterator that decodes a set of messages in a blocking fashion +/// +/// See [`schedule_and_decode_blocking`] for more information. +pub fn create_decode_iterator( + schema: &Schema, + num_rows: u64, + batch_size: u32, + should_validate: bool, + is_structural: bool, + messages: VecDeque>, +) -> Result> { + let arrow_schema = Arc::new(ArrowSchema::from(schema)); + let root_fields = arrow_schema.fields.clone(); + if is_structural { + let simple_struct_decoder = + StructuralStructDecoder::new(root_fields, should_validate, /*is_root=*/ true)?; + Ok(Box::new(BatchDecodeIterator::new( + messages, + batch_size, + num_rows, + simple_struct_decoder, + arrow_schema, + ))) + } else { + let root_decoder = SimpleStructDecoder::new(root_fields, num_rows); + Ok(Box::new(BatchDecodeIterator::new( + messages, + batch_size, + num_rows, + root_decoder, + arrow_schema, + ))) + } +} + +async fn create_scheduler_decoder( + column_infos: Vec>, + requested_rows: RequestedRows, + filter: FilterExpression, + column_indices: Vec, + target_schema: Arc, + config: SchedulerDecoderConfig, +) -> Result> { + let num_rows = requested_rows.num_rows(); + + let is_structural = column_infos[0].is_structural(); + let mode = std::env::var(ENV_LANCE_STRUCTURAL_BATCH_DECODE_SPAWN_MODE); + let spawn_structural_batch_decode_tasks = match mode.ok().as_deref() { + Some("always") => true, + Some("never") => false, + _ => matches!(requested_rows, RequestedRows::Ranges(_)), + }; + + let (tx, rx) = mpsc::unbounded_channel(); + + let decode_stream = create_decode_stream( + &target_schema, + num_rows, + config.batch_size, + is_structural, + config.decoder_config.validate_on_decode, + spawn_structural_batch_decode_tasks, + rx, + config.batch_size_bytes, + )?; + + // The scheduler's `initialize` may perform I/O to load column metadata + // unless that metadata is already in the cache. This metadata loading + // happens as part of this call and should be parallelized if reading + // multiple files. + let mut decode_scheduler = DecodeBatchScheduler::try_new( + target_schema.as_ref(), + &column_indices, + &column_infos, + &vec![], + num_rows, + config.decoder_plugins, + config.io.clone(), + config.cache, + &filter, + &config.decoder_config, + ) + .await?; + + // For small requests the scheduling cost is dwarfed by the overhead of + // spawning a task, so we run scheduling inline (still as part of this + // await) before returning. The threshold is configurable via + // `LANCE_INLINE_SCHEDULING_THRESHOLD`, and callers can force either + // strategy via `DecoderConfig::inline_scheduling`. + let inline_scheduling = config + .decoder_config + .inline_scheduling + .unwrap_or_else(|| num_rows <= inline_scheduling_threshold()); + + if inline_scheduling { + match requested_rows { + RequestedRows::Ranges(ranges) => { + decode_scheduler.schedule_ranges(&ranges, &filter, tx, config.io) + } + RequestedRows::Indices(indices) => { + decode_scheduler.schedule_take(&indices, &filter, tx, config.io) + } + } + Ok(decode_stream) + } else { + // Spawn the (still synchronous) scheduling work so that decoder + // messages can stream into the channel while the consumer is + // already pulling from the decode stream. + let scheduling = async move { + match requested_rows { + RequestedRows::Ranges(ranges) => { + decode_scheduler.schedule_ranges(&ranges, &filter, tx, config.io) + } + RequestedRows::Indices(indices) => { + decode_scheduler.schedule_take(&indices, &filter, tx, config.io) + } + } + }; + let scheduler_handle = tokio::task::spawn(scheduling); + Ok(check_scheduler_on_drop(decode_stream, scheduler_handle)) + } +} + +/// Initializes the scheduler, schedules the requested rows, and returns a +/// stream of decode tasks for the resulting batches. +/// +/// This is a convenience function that creates both the scheduler and the +/// decoder, which can be a little tricky to get right. +/// +/// # Why is this async? +/// +/// Constructing the scheduler runs `initialize` which will perform I/O +/// unless the data required is already in the file metadata cache. +/// +/// When `DecoderConfig::inline_scheduling` resolves to `true`, the +/// subsequent (synchronous) scheduling work also runs before this function +/// returns, leaving a fully primed decode stream. +pub async fn schedule_and_decode( + column_infos: Vec>, + requested_rows: RequestedRows, + filter: FilterExpression, + column_indices: Vec, + target_schema: Arc, + config: SchedulerDecoderConfig, +) -> Result> { + if requested_rows.num_rows() == 0 { + return Ok(stream::empty().boxed()); + } + + // If the user requested any ranges that are empty, ignore them. They are pointless and + // trying to read them has caused bugs in the past. + let requested_rows = requested_rows.trim_empty_ranges(); + + let io = config.io.clone(); + + let stream = create_scheduler_decoder( + column_infos, + requested_rows, + filter, + column_indices, + target_schema, + config, + ) + .await?; + + // Keep the io alive until the stream is dropped or finishes. Otherwise the + // I/O drops as soon as the scheduling is finished and the I/O loop terminates. + Ok(stream.finally(move || drop(io)).boxed()) +} + +pub static WAITER_RT: LazyLock = LazyLock::new(|| { + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() +}); + +/// Schedules and decodes the requested data in a blocking fashion +/// +/// This function is a blocking version of [`schedule_and_decode`]. It schedules the requested data +/// and decodes it in the current thread. +/// +/// This can be useful when the disk is fast (or the data is in memory) and the amount +/// of data is relatively small. For example, when doing a take against NVMe or in-memory data. +/// +/// This should NOT be used for full scans. Even if the data is in memory this function will +/// not parallelize the decode and will be slower than the async version. Full scans typically +/// make relatively few IOPs and so the asynchronous overhead is much smaller. +/// +/// This method will first completely run the scheduling process. Then it will run the +/// decode process. +pub fn schedule_and_decode_blocking( + column_infos: Vec>, + requested_rows: RequestedRows, + filter: FilterExpression, + column_indices: Vec, + target_schema: Arc, + config: SchedulerDecoderConfig, +) -> Result> { + if requested_rows.num_rows() == 0 { + let arrow_schema = Arc::new(ArrowSchema::from(target_schema.as_ref())); + return Ok(Box::new(RecordBatchIterator::new(vec![], arrow_schema))); + } + + let num_rows = requested_rows.num_rows(); + let is_structural = column_infos[0].is_structural(); + + let (tx, mut rx) = mpsc::unbounded_channel(); + + // Initialize the scheduler. This is still "asynchronous" but we run it with a current-thread + // runtime. + let mut decode_scheduler = WAITER_RT.block_on(DecodeBatchScheduler::try_new( + target_schema.as_ref(), + &column_indices, + &column_infos, + &vec![], + num_rows, + config.decoder_plugins, + config.io.clone(), + config.cache, + &filter, + &config.decoder_config, + ))?; + + // Schedule the requested rows + match requested_rows { + RequestedRows::Ranges(ranges) => { + decode_scheduler.schedule_ranges(&ranges, &filter, tx, config.io) + } + RequestedRows::Indices(indices) => { + decode_scheduler.schedule_take(&indices, &filter, tx, config.io) + } + } + + // Drain the scheduler queue into a vec of decode messages + let mut messages = Vec::new(); + while rx + .recv_many(&mut messages, usize::MAX) + .now_or_never() + .unwrap() + != 0 + {} + + // Create a decoder to decode the messages + let decode_iterator = create_decode_iterator( + &target_schema, + num_rows, + config.batch_size, + config.decoder_config.validate_on_decode, + is_structural, + messages.into(), + )?; + + Ok(decode_iterator) +} + +/// A decoder for single-column encodings of primitive data (this includes fixed size +/// lists of primitive data) +/// +/// Physical decoders are able to decode into existing buffers for zero-copy operation. +/// +/// Instances should be stateless and `Send` / `Sync`. This is because multiple decode +/// tasks could reference the same page. For example, imagine a page covers rows 0-2000 +/// and the decoder stream has a batch size of 1024. The decoder will be needed by both +/// the decode task for batch 0 and the decode task for batch 1. +/// +/// See [`crate::decoder`] for more information +pub trait PrimitivePageDecoder: Send + Sync { + /// Decode data into buffers + /// + /// This may be a simple zero-copy from a disk buffer or could involve complex decoding + /// such as decompressing from some compressed representation. + /// + /// Capacity is stored as a tuple of (num_bytes: u64, is_needed: bool). The `is_needed` + /// portion only needs to be updated if the encoding has some concept of an "optional" + /// buffer. + /// + /// Encodings can have any number of input or output buffers. For example, a dictionary + /// decoding will convert two buffers (indices + dictionary) into a single buffer + /// + /// Binary decodings have two output buffers (one for values, one for offsets) + /// + /// Other decodings could even expand the # of output buffers. For example, we could decode + /// fixed size strings into variable length strings going from one input buffer to multiple output + /// buffers. + /// + /// Each Arrow data type typically has a fixed structure of buffers and the encoding chain will + /// generally end at one of these structures. However, intermediate structures may exist which + /// do not correspond to any Arrow type at all. For example, a bitpacking encoding will deal + /// with buffers that have bits-per-value that is not a multiple of 8. + /// + /// The `primitive_array_from_buffers` method has an expected buffer layout for each arrow + /// type (order matters) and encodings that aim to decode into arrow types should respect + /// this layout. + /// # Arguments + /// + /// * `rows_to_skip` - how many rows to skip (within the page) before decoding + /// * `num_rows` - how many rows to decode + /// * `all_null` - A mutable bool, set to true if a decoder determines all values are null + fn decode(&self, rows_to_skip: u64, num_rows: u64) -> Result; +} + +/// A scheduler for single-column encodings of primitive data +/// +/// The scheduler is responsible for calculating what I/O is needed for the requested rows +/// +/// Instances should be stateless and `Send` and `Sync`. This is because instances can +/// be shared in follow-up I/O tasks. +/// +/// See [`crate::decoder`] for more information +pub trait PageScheduler: Send + Sync + std::fmt::Debug { + /// Schedules a batch of I/O to load the data needed for the requested ranges + /// + /// Returns a future that will yield a decoder once the data has been loaded + /// + /// # Arguments + /// + /// * `range` - the range of row offsets (relative to start of page) requested + /// these must be ordered and must not overlap + /// * `scheduler` - a scheduler to submit the I/O request to + /// * `top_level_row` - the row offset of the top level field currently being + /// scheduled. This can be used to assign priority to I/O requests + fn schedule_ranges( + &self, + ranges: &[Range], + scheduler: &Arc, + top_level_row: u64, + ) -> BoxFuture<'static, Result>>; +} + +/// A trait to control the priority of I/O +pub trait PriorityRange: std::fmt::Debug + Send + Sync { + fn advance(&mut self, num_rows: u64); + fn current_priority(&self) -> u64; + fn box_clone(&self) -> Box; +} + +/// A simple priority scheme for top-level fields with no parent +/// repetition +#[derive(Debug)] +pub struct SimplePriorityRange { + priority: u64, +} + +impl SimplePriorityRange { + fn new(priority: u64) -> Self { + Self { priority } + } +} + +impl PriorityRange for SimplePriorityRange { + fn advance(&mut self, num_rows: u64) { + self.priority += num_rows; + } + + fn current_priority(&self) -> u64 { + self.priority + } + + fn box_clone(&self) -> Box { + Box::new(Self { + priority: self.priority, + }) + } +} + +/// Determining the priority of a list request is tricky. We want +/// the priority to be the top-level row. So if we have a +/// `list>` and each outer list has 10 rows and each inner +/// list has 5 rows then the priority of the 100th item is 1 because +/// it is the 5th item in the 10th item of the *second* row. +/// +/// This structure allows us to keep track of this complicated priority +/// relationship. +/// +/// There's a fair amount of bookkeeping involved here. +/// +/// A better approach (using repetition levels) is coming in the future. +pub struct ListPriorityRange { + base: Box, + offsets: Arc<[u64]>, + cur_index_into_offsets: usize, + cur_position: u64, +} + +impl ListPriorityRange { + pub(crate) fn new(base: Box, offsets: Arc<[u64]>) -> Self { + Self { + base, + offsets, + cur_index_into_offsets: 0, + cur_position: 0, + } + } +} + +impl std::fmt::Debug for ListPriorityRange { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ListPriorityRange") + .field("base", &self.base) + .field("offsets.len()", &self.offsets.len()) + .field("cur_index_into_offsets", &self.cur_index_into_offsets) + .field("cur_position", &self.cur_position) + .finish() + } +} + +impl PriorityRange for ListPriorityRange { + fn advance(&mut self, num_rows: u64) { + // We've scheduled X items. Now walk through the offsets to + // determine how many rows we've scheduled. + self.cur_position += num_rows; + let mut idx_into_offsets = self.cur_index_into_offsets; + while idx_into_offsets + 1 < self.offsets.len() + && self.offsets[idx_into_offsets + 1] <= self.cur_position + { + idx_into_offsets += 1; + } + let base_rows_advanced = idx_into_offsets - self.cur_index_into_offsets; + self.cur_index_into_offsets = idx_into_offsets; + self.base.advance(base_rows_advanced as u64); + } + + fn current_priority(&self) -> u64 { + self.base.current_priority() + } + + fn box_clone(&self) -> Box { + Box::new(Self { + base: self.base.box_clone(), + offsets: self.offsets.clone(), + cur_index_into_offsets: self.cur_index_into_offsets, + cur_position: self.cur_position, + }) + } +} + +/// Contains the context for a scheduler +pub struct SchedulerContext { + recv: Option>, + io: Arc, + cache: Arc, + name: String, + path: Vec, + path_names: Vec, +} + +pub struct ScopedSchedulerContext<'a> { + pub context: &'a mut SchedulerContext, +} + +impl<'a> ScopedSchedulerContext<'a> { + pub fn pop(self) -> &'a mut SchedulerContext { + self.context.pop(); + self.context + } +} + +impl SchedulerContext { + pub fn new(io: Arc, cache: Arc) -> Self { + Self { + io, + cache, + recv: None, + name: "".to_string(), + path: Vec::new(), + path_names: Vec::new(), + } + } + + pub fn io(&self) -> &Arc { + &self.io + } + + pub fn cache(&self) -> &Arc { + &self.cache + } + + pub fn push(&'_ mut self, name: &str, index: u32) -> ScopedSchedulerContext<'_> { + self.path.push(index); + self.path_names.push(name.to_string()); + ScopedSchedulerContext { context: self } + } + + pub fn pop(&mut self) { + self.path.pop(); + self.path_names.pop(); + } + + pub fn path_name(&self) -> String { + let path = self.path_names.join("/"); + if self.recv.is_some() { + format!("TEMP({}){}", self.name, path) + } else { + format!("ROOT{}", path) + } + } + + pub fn current_path(&self) -> VecDeque { + VecDeque::from_iter(self.path.iter().copied()) + } + + #[deprecated(since = "0.29.1", note = "This is for v2.0 array-encoding paths")] + pub fn locate_decoder(&mut self, decoder: Box) -> DecoderReady { + trace!( + "Scheduling decoder of type {:?} for {:?}", + decoder.data_type(), + self.path, + ); + DecoderReady { + decoder, + path: self.current_path(), + } + } +} + +pub struct UnloadedPageShard(pub BoxFuture<'static, Result>); + +impl std::fmt::Debug for UnloadedPageShard { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("UnloadedPage").finish() + } +} + +#[derive(Debug)] +pub struct ScheduledScanLine { + pub rows_scheduled: u64, + pub decoders: Vec, +} + +pub trait StructuralSchedulingJob: std::fmt::Debug { + /// Schedule the next batch of data + /// + /// Normally this equates to scheduling the next page of data into one task. Very large pages + /// might be split into multiple scan lines. Each scan line has one or more rows. + /// + /// If a scheduler ends early it may return an empty vector. + fn schedule_next(&mut self, context: &mut SchedulerContext) -> Result>; +} + +/// A filter expression to apply to the data +/// +/// The core decoders do not currently take advantage of filtering in +/// any way. In order to maintain the abstraction we represent filters +/// as an arbitrary byte sequence. +/// +/// We recommend that encodings use Substrait for filters. +pub struct FilterExpression(pub Bytes); + +impl FilterExpression { + /// Create a filter expression that does not filter any data + /// + /// This is currently represented by an empty byte array. Encoders + /// that are "filter aware" should make sure they handle this case. + pub fn no_filter() -> Self { + Self(Bytes::new()) + } + + /// Returns true if the filter is the same as the [`Self::no_filter`] filter + pub fn is_noop(&self) -> bool { + self.0.is_empty() + } +} + +pub trait StructuralFieldScheduler: Send + std::fmt::Debug { + fn initialize<'a>( + &'a mut self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>>; + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result>; +} + +/// A trait for tasks that decode data into an Arrow array +pub trait DecodeArrayTask: Send { + /// Decodes the data into an Arrow array and its data size in bytes + fn decode(self: Box) -> Result<(ArrayRef, u64)>; +} + +impl DecodeArrayTask for Box { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + let decoded_array = StructuralDecodeArrayTask::decode(*self)?; + decoded_array.repdef.ensure_exhausted()?; + Ok((decoded_array.array, decoded_array.data_size)) + } +} + +/// A task to decode data into an Arrow record batch +/// +/// It has a child `task` which decodes a struct array with no nulls. +/// This is then converted into a record batch. +pub struct NextDecodeTask { + /// The decode task itself + pub task: Box, + /// The number of rows that will be created + pub num_rows: u64, +} + +impl NextDecodeTask { + // Run the task and produce a record batch + // + // If the batch is very large this function will log a warning message + // suggesting the user try a smaller batch size. + #[instrument(name = "task_to_batch", level = "debug", skip_all)] + fn into_batch(self, emitted_batch_size_warning: Arc) -> Result<(RecordBatch, u64)> { + let (struct_arr, data_size) = self.task.decode()?; + let batch = RecordBatch::from(struct_arr.as_struct()); + if data_size > BATCH_SIZE_BYTES_WARNING { + emitted_batch_size_warning.call_once(|| { + let size_mb = data_size / 1024 / 1024; + debug!("Lance read in a single batch that contained more than {}MiB of data. You may want to consider reducing the batch size.", size_mb); + }); + } + Ok((batch, data_size)) + } +} + +// An envelope to wrap both 2.0 style messages and 2.1 style messages so we can +// share some code paths between the two. Decoders can safely unwrap into whatever +// style they expect since a file will be either all-2.0 or all-2.1 +#[derive(Debug)] +pub enum MessageType { + // The older v2.0 scheduler/decoder used a scheme where the message was the + // decoder itself. The messages were not sent in priority order and the decoder + // had to wait for I/O, figuring out the correct priority. This was a lot of + // complexity. + DecoderReady(DecoderReady), + // Starting in 2.1 we use a simpler scheme where the scheduling happens in priority + // order and the message is an unloaded decoder. These can be awaited, in order, and + // the decoder does not have to worry about waiting for I/O. + UnloadedPage(UnloadedPageShard), +} + +impl MessageType { + pub fn into_array(self) -> DecoderReady { + match self { + Self::DecoderReady(decoder) => decoder, + Self::UnloadedPage(_) => { + panic!("Expected DecoderReady but got UnloadedPage") + } + } + } + + pub fn into_structural(self) -> UnloadedPageShard { + match self { + Self::UnloadedPage(unloaded) => unloaded, + Self::DecoderReady(_) => { + panic!("Expected UnloadedPage but got DecoderReady") + } + } + } +} + +pub struct DecoderMessage { + pub scheduled_so_far: u64, + pub decoders: Vec, +} + +pub struct DecoderContext { + source: mpsc::UnboundedReceiver>, +} + +impl DecoderContext { + pub fn new(source: mpsc::UnboundedReceiver>) -> Self { + Self { source } + } +} + +pub struct DecodedPage { + pub data: DataBlock, + pub repdef: RepDefUnraveler, +} + +pub trait DecodePageTask: Send + std::fmt::Debug { + /// Decodes the data into an Arrow array + fn decode(self: Box) -> Result; +} + +pub trait StructuralPageDecoder: std::fmt::Debug + Send { + fn drain(&mut self, num_rows: u64) -> Result>; + fn num_rows(&self) -> u64; +} + +#[derive(Debug)] +pub struct LoadedPageShard { + // The decoder that is ready to be decoded + pub decoder: Box, + // The path to the decoder, the first value is the column index + // following values, if present, are nested child indices + // + // For example, a path of [1, 1, 0] would mean to grab the second + // column, then the second child, and then the first child. + // + // It could represent x in the following schema: + // + // score: float64 + // points: struct + // color: string + // location: struct + // x: float64 + // + // Currently, only struct decoders have "children" although other + // decoders may at some point as well. List children are only + // handled through indirect I/O at the moment and so they don't + // need to be represented (yet) + pub path: VecDeque, +} + +pub struct DecodedArray { + pub array: ArrayRef, + pub repdef: CompositeRepDefUnraveler, + /// The number of bytes of data in this array (excluding Arrow overhead). + pub data_size: u64, +} + +pub trait StructuralDecodeArrayTask: std::fmt::Debug + Send { + fn decode(self: Box) -> Result; +} + +pub trait StructuralFieldDecoder: std::fmt::Debug + Send { + /// Add a newly scheduled child decoder + /// + /// The default implementation does not expect children and returns + /// an error. + fn accept_page(&mut self, _child: LoadedPageShard) -> Result<()>; + /// Creates a task to decode `num_rows` of data into an array + fn drain(&mut self, num_rows: u64) -> Result>; + /// The data type of the decoded data + fn data_type(&self) -> &DataType; +} + +#[derive(Debug, Default)] +pub struct DecoderPlugins {} + +/// The top-level column layout used by an in-memory encoded batch. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum EncodedBatchLayout { + /// Array pages include structural columns. + Array, + /// Structural pages include only leaf columns. + Structural, +} + +/// Decodes a batch of data from an in-memory structure created by [`crate::encoder::encode_batch`] +pub async fn decode_batch( + batch: &EncodedBatch, + filter: &FilterExpression, + decoder_plugins: Arc, + should_validate: bool, + layout: EncodedBatchLayout, + cache: Option>, +) -> Result { + // The io is synchronous so it shouldn't be possible for any async stuff to still be in progress + // Still, if we just use now_or_never we hit misfires because some futures (channels) need to be + // polled twice. + + let io_scheduler = Arc::new(BufferScheduler::new(batch.data.clone())) as Arc; + let cache = if let Some(cache) = cache { + cache + } else { + Arc::new(lance_core::cache::LanceCache::with_capacity( + 128 * 1024 * 1024, + )) + }; + let mut decode_scheduler = DecodeBatchScheduler::try_new( + batch.schema.as_ref(), + &batch.top_level_columns, + &batch.page_table, + &vec![], + batch.num_rows, + decoder_plugins, + io_scheduler.clone(), + cache, + filter, + &DecoderConfig::default(), + ) + .await?; + let (tx, rx) = unbounded_channel(); + decode_scheduler.schedule_range(0..batch.num_rows, filter, tx, io_scheduler); + let is_structural = layout == EncodedBatchLayout::Structural; + let mode = std::env::var(ENV_LANCE_STRUCTURAL_BATCH_DECODE_SPAWN_MODE); + let spawn_structural_batch_decode_tasks = !matches!(mode.ok().as_deref(), Some("never")); + let mut decode_stream = create_decode_stream( + &batch.schema, + batch.num_rows, + batch.num_rows as u32, + is_structural, + should_validate, + spawn_structural_batch_decode_tasks, + rx, + None, + )?; + decode_stream.next().await.unwrap().task.await +} + +#[cfg(test)] +// test coalesce indices to ranges +mod tests { + use super::*; + use std::collections::VecDeque; + + #[derive(Debug)] + struct FailingPageDecoder { + page_data_type: DataType, + total_rows: u64, + load_error_message: &'static str, + } + + impl FailingPageDecoder { + fn new( + page_data_type: DataType, + total_rows: u64, + load_error_message: &'static str, + ) -> Self { + Self { + page_data_type, + total_rows, + load_error_message, + } + } + } + + impl LogicalPageDecoder for FailingPageDecoder { + fn wait_for_loaded(&'_ mut self, _rows_needed: u64) -> BoxFuture<'_, Result<()>> { + let load_error_message = self.load_error_message; + async move { Err(Error::io(load_error_message)) }.boxed() + } + + fn rows_loaded(&self) -> u64 { + 0 + } + + fn num_rows(&self) -> u64 { + self.total_rows + } + + fn rows_drained(&self) -> u64 { + 0 + } + + fn drain(&mut self, requested_rows: u64) -> Result { + Err(Error::internal(format!( + "failing page decoder should not be drained after load error \ + (requested_rows={})", + requested_rows + ))) + } + + fn data_type(&self) -> &DataType { + &self.page_data_type + } + } + + struct InvalidInputDecodeTask; + + impl DecodeArrayTask for InvalidInputDecodeTask { + fn decode(self: Box) -> Result<(ArrayRef, u64)> { + Err(Error::invalid_input_source("malformed sparse page".into())) + } + } + + #[test] + fn next_decode_task_preserves_invalid_input_errors() { + let err = NextDecodeTask { + task: Box::new(InvalidInputDecodeTask), + num_rows: 0, + } + .into_batch(Arc::new(Once::new())) + .unwrap_err(); + assert!(matches!(err, Error::InvalidInput { .. })); + } + + #[test] + fn test_read_zero_dimension_fsl_errors_instead_of_panicking() { + // Simulates reading a column whose stored schema declares a + // zero-dimension FixedSizeList, as old writers (before #5102) could + // persist. The read plan is built by the field-scheduler factories, + // which run the dimension guard before touching any column data, so + // an empty column iterator is sufficient to reach the guard. The read + // must surface a clean error rather than a divide-by-zero panic. + use arrow_schema::Field as ArrowField; + + let zero_dim = DataType::FixedSizeList( + Arc::new(ArrowField::new("item", DataType::Float32, true)), + 0, + ); + let field = Field::try_from(&ArrowField::new("vec", zero_dim, true)).unwrap(); + let strategy = CoreFieldDecoderStrategy::default(); + + let mut structural_columns = ColumnInfoIter::new(vec![], &[]); + let err = strategy + .create_structural_field_scheduler(&field, &mut structural_columns) + .unwrap_err(); + assert!( + err.to_string() + .contains("dimension must be a positive integer"), + "unexpected error: {}", + err + ); + + let mut array_columns = ColumnInfoIter::new(vec![], &[]); + let err = strategy + .create_array_field_scheduler( + &field, + &mut array_columns, + FileBuffers { + positions_and_sizes: &[], + }, + ) + .unwrap_err(); + assert!( + err.to_string() + .contains("dimension must be a positive integer"), + "unexpected error: {}", + err + ); + } + + #[tokio::test] + async fn test_array_stream_stops_on_load_error() { + use arrow_schema::Field as ArrowField; + + let rows_per_batch = 1; + let total_rows = 2; + let scheduled_rows = 1; + let page_rows = 1; + let batch_readahead = 2; + let load_error_message = "simulated page load failure"; + let fields = Fields::from(vec![ArrowField::new("vector", DataType::Float32, true)]); + let root_decoder = SimpleStructDecoder::new(fields, total_rows); + let (tx, rx) = unbounded_channel(); + + tx.send(Ok(DecoderMessage { + scheduled_so_far: scheduled_rows, + decoders: vec![MessageType::DecoderReady(DecoderReady { + decoder: Box::new(FailingPageDecoder::new( + DataType::Float32, + page_rows, + load_error_message, + )), + path: VecDeque::from([0]), + })], + })) + .unwrap(); + drop(tx); + + let stream = + BatchDecodeStream::new(rx, rows_per_batch, total_rows, root_decoder).into_stream(); + let mut batches = stream.map(|task| task.task).buffered(batch_readahead); + + let err = batches + .next() + .await + .expect("stream should emit the array page-load error") + .unwrap_err(); + assert!( + err.to_string().contains(load_error_message), + "unexpected error: {}", + err + ); + assert!( + batches.next().await.is_none(), + "stream should stop after the array page-load error" + ); + } + + #[tokio::test] + async fn test_structural_stream_stops_on_load_error() { + let rows_per_batch = 1; + let total_rows = 2; + let scheduled_rows = 1; + let batch_readahead = 2; + let load_error_message = "simulated page load failure"; + let fields = Fields::from(vec![ArrowField::new("vector", DataType::Float32, true)]); + let root_decoder = StructuralStructDecoder::new(fields, false, /*is_root=*/ true).unwrap(); + let (tx, rx) = unbounded_channel(); + let failed_page = async move { Err(Error::io(load_error_message)) }.boxed(); + + tx.send(Ok(DecoderMessage { + scheduled_so_far: scheduled_rows, + decoders: vec![MessageType::UnloadedPage(UnloadedPageShard(failed_page))], + })) + .unwrap(); + drop(tx); + + let stream = StructuralBatchDecodeStream::new( + rx, + rows_per_batch, + total_rows, + root_decoder, + /*spawn_batch_decode_tasks=*/ true, + None, + ) + .into_stream(); + let mut batches = stream.map(|task| task.task).buffered(batch_readahead); + + let err = batches + .next() + .await + .expect("stream should emit the page-load error") + .unwrap_err(); + assert!( + err.to_string().contains(load_error_message), + "unexpected error: {}", + err + ); + assert!( + batches.next().await.is_none(), + "stream should stop after the page-load error" + ); + } + + #[test] + fn test_coalesce_indices_to_ranges_with_single_index() { + let indices = vec![1]; + let ranges = DecodeBatchScheduler::indices_to_ranges(&indices); + assert_eq!(ranges, vec![1..2]); + } + + #[test] + fn test_coalesce_indices_to_ranges() { + let indices = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; + let ranges = DecodeBatchScheduler::indices_to_ranges(&indices); + assert_eq!(ranges, vec![1..10]); + } + + #[test] + fn test_coalesce_indices_to_ranges_with_gaps() { + let indices = vec![1, 2, 3, 5, 6, 7, 9]; + let ranges = DecodeBatchScheduler::indices_to_ranges(&indices); + assert_eq!(ranges, vec![1..4, 5..8, 9..10]); + } + + #[test] + fn test_estimate_bytes_per_row() { + assert_eq!(estimate_bytes_per_row(&DataType::Int32), 4.0); + assert_eq!(estimate_bytes_per_row(&DataType::Int64), 8.0); + assert_eq!(estimate_bytes_per_row(&DataType::Float32), 4.0); + assert_eq!(estimate_bytes_per_row(&DataType::Boolean), 1.0 / 8.0); + assert_eq!(estimate_bytes_per_row(&DataType::Utf8), 64.0); + assert_eq!(estimate_bytes_per_row(&DataType::Binary), 64.0); + // Struct of 4 x Int32 = 16 bytes + let struct_type = DataType::Struct(Fields::from(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new("b", DataType::Int32, false), + ArrowField::new("c", DataType::Int32, false), + ArrowField::new("d", DataType::Int32, false), + ])); + assert_eq!(estimate_bytes_per_row(&struct_type), 16.0); + } + + /// Helper: encode a batch, then decode it as a stream with optional + /// `batch_size_bytes`, collecting all output batches. + async fn decode_batches_with_byte_limit( + batch: &RecordBatch, + batch_size: u32, + batch_size_bytes: Option, + ) -> Vec { + use crate::{ + encoder::{EncodingOptions, encode_batch}, + testing::{TestEncoding, test_encoding_strategy}, + }; + + let version = TestEncoding::StructuralU16; + let options = EncodingOptions::default(); + let strategy = test_encoding_strategy(version); + let schema = Schema::try_from(batch.schema().as_ref()).unwrap(); + let encoded = encode_batch(batch, Arc::new(schema.clone()), strategy.as_ref(), &options) + .await + .unwrap(); + + let io_scheduler = + Arc::new(BufferScheduler::new(encoded.data.clone())) as Arc; + let cache = Arc::new(lance_core::cache::LanceCache::with_capacity( + 128 * 1024 * 1024, + )); + let decoder_plugins = Arc::new(DecoderPlugins::default()); + + let mut decode_scheduler = DecodeBatchScheduler::try_new( + encoded.schema.as_ref(), + &encoded.top_level_columns, + &encoded.page_table, + &vec![], + encoded.num_rows, + decoder_plugins, + io_scheduler.clone(), + cache, + &FilterExpression::no_filter(), + &DecoderConfig::default(), + ) + .await + .unwrap(); + + let (tx, rx) = unbounded_channel(); + decode_scheduler.schedule_range( + 0..encoded.num_rows, + &FilterExpression::no_filter(), + tx, + io_scheduler, + ); + + let mut decode_stream = create_decode_stream( + &encoded.schema, + encoded.num_rows, + batch_size, + /*is_structural=*/ true, + /*should_validate=*/ true, + /*spawn_structural_batch_decode_tasks=*/ true, + rx, + batch_size_bytes, + ) + .unwrap(); + + let mut batches = Vec::new(); + while let Some(task) = decode_stream.next().await { + batches.push(task.task.await.unwrap()); + } + batches + } + + #[tokio::test] + async fn test_byte_sized_batches_fixed_width() { + use arrow_array::Int32Array; + + // 1000 rows x 4 Int32 columns = 16 bytes/row + let num_rows: i32 = 1000; + let arrays: Vec> = (0..4) + .map(|col| { + Arc::new(Int32Array::from_iter_values( + (0..num_rows).map(move |row| row * 10 + col), + )) as _ + }) + .collect(); + + let schema = Arc::new(ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new("b", DataType::Int32, false), + ArrowField::new("c", DataType::Int32, false), + ArrowField::new("d", DataType::Int32, false), + ])); + let input_batch = RecordBatch::try_new(schema, arrays).unwrap(); + + // 16 bytes/row, batch_size_bytes=1600 => 100 rows/batch + let batches = + decode_batches_with_byte_limit(&input_batch, /*batch_size=*/ 1024, Some(1600)).await; + + // Should produce 10 batches of 100 rows each + assert_eq!(batches.len(), 10); + for (i, batch) in batches.iter().enumerate() { + assert_eq!( + batch.num_rows(), + 100, + "batch {i} should have 100 rows, got {}", + batch.num_rows() + ); + } + + // Verify roundtrip: concatenate and compare + let all_batches: Vec<&RecordBatch> = batches.iter().collect(); + let concatenated = + arrow_select::concat::concat_batches(&batches[0].schema(), all_batches.iter().copied()) + .unwrap(); + assert_eq!(concatenated.num_rows(), num_rows as usize); + for col in 0..4 { + assert_eq!( + concatenated.column(col).as_ref(), + input_batch.column(col).as_ref(), + "column {col} roundtrip mismatch" + ); + } + } + + #[tokio::test] + async fn test_byte_sized_batches_none_unchanged() { + use arrow_array::Int32Array; + + // Without batch_size_bytes, rows_per_batch controls batching + let num_rows: i32 = 1000; + let arrays: Vec> = (0..2) + .map(|col| { + Arc::new(Int32Array::from_iter_values( + (0..num_rows).map(move |row| row * 10 + col), + )) as _ + }) + .collect(); + + let schema = Arc::new(ArrowSchema::new(vec![ + ArrowField::new("x", DataType::Int32, false), + ArrowField::new("y", DataType::Int32, false), + ])); + let input_batch = RecordBatch::try_new(schema, arrays).unwrap(); + + // batch_size=250, batch_size_bytes=None => 4 batches of 250 rows + let batches = decode_batches_with_byte_limit(&input_batch, /*batch_size=*/ 250, None).await; + assert_eq!(batches.len(), 4); + for (i, batch) in batches.iter().enumerate() { + assert_eq!( + batch.num_rows(), + 250, + "batch {i} should have 250 rows, got {}", + batch.num_rows() + ); + } + } + + #[tokio::test] + async fn test_byte_sized_batches_respect_row_limit() { + use arrow_array::Int32Array; + + let num_rows: i32 = 1000; + let schema = Arc::new(ArrowSchema::new(vec![ArrowField::new( + "x", + DataType::Int32, + false, + )])); + let input_batch = RecordBatch::try_new( + schema, + vec![Arc::new(Int32Array::from_iter_values(0..num_rows))], + ) + .unwrap(); + + // The byte limit can hold every row, so the 100-row limit must win. + let batches = + decode_batches_with_byte_limit(&input_batch, /*batch_size=*/ 100, Some(10_000)).await; + assert_eq!(batches.len(), 10); + assert!(batches.iter().all(|batch| batch.num_rows() == 100)); + } + + #[tokio::test] + async fn test_byte_sized_batches_feedback_convergence() { + use arrow_array::StringArray; + + // Each row has a 100-byte string. Schema estimate = 64 bytes (default + // for Utf8), so the first batch will overshoot. The feedback loop + // should correct subsequent batches toward the target. + let num_rows = 500; + let value: String = "x".repeat(100); + let arrays: Vec> = vec![Arc::new(StringArray::from( + (0..num_rows).map(|_| value.as_str()).collect::>(), + ))]; + let schema = Arc::new(ArrowSchema::new(vec![ArrowField::new( + "s", + DataType::Utf8, + false, + )])); + let input_batch = RecordBatch::try_new(schema, arrays).unwrap(); + + // Target 5000 bytes/batch. At 100 bytes/row the ideal is 50 rows/batch. + // Schema estimate is 64 bytes/row → first batch ~78 rows (overshoot). + // After feedback kicks in, batches should converge to ~50 rows. + let target_bytes: u64 = 5000; + let batches = decode_batches_with_byte_limit( + &input_batch, + /*batch_size=*/ 1024, + Some(target_bytes), + ) + .await; + + // Verify all data round-trips correctly + let all_batches: Vec<&RecordBatch> = batches.iter().collect(); + let concatenated = + arrow_select::concat::concat_batches(&batches[0].schema(), all_batches.iter().copied()) + .unwrap(); + assert_eq!(concatenated.num_rows(), num_rows as usize); + assert_eq!( + concatenated.column(0).as_ref(), + input_batch.column(0).as_ref() + ); + + // After the first batch, subsequent batches should be closer to the + // target. The ideal is 50 rows/batch. + assert!( + batches.len() >= 2, + "need at least 2 batches to test convergence" + ); + // The first batch uses the schema estimate (64 bytes/row) → + // ~78 rows. After feedback the rows should settle near 50. + if batches.len() >= 3 { + let second_batch_rows = batches[1].num_rows(); + let third_batch_rows = batches[2].num_rows(); + // Both should be within 20% of the ideal (50 rows) + assert!( + (40..=60).contains(&second_batch_rows), + "second batch should be near 50 rows, got {second_batch_rows}" + ); + assert!( + (40..=60).contains(&third_batch_rows), + "third batch should be near 50 rows, got {third_batch_rows}" + ); + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encoder.rs b/lance-artifact/rust/lance-encoding/src/encoder.rs new file mode 100644 index 000000000..9d9f11945 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encoder.rs @@ -0,0 +1,524 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! The top-level encoding module for Lance files. +//! +//! Lance files are encoded using a [`FieldEncodingStrategy`] which choose +//! what encoder to use for each field. +//! +//! Structural strategies build a tree of encoders for each field from the +//! version-free builders in [`structural`]. Struct and list encoders collect +//! validity and offsets; primitive leaf encoders accumulate values and emit +//! miniblock or full-zip pages. + +use std::{collections::HashMap, sync::Arc}; + +use arrow_array::{Array, ArrayRef, RecordBatch}; +use bytes::{Bytes, BytesMut}; +use futures::future::BoxFuture; +use lance_core::datatypes::{Field, Schema}; +use lance_core::utils::bit::{is_pwr_two, pad_bytes_to}; +use lance_core::{Error, Result}; + +use crate::buffer::LanceBuffer; +use crate::data::DataBlock; +use crate::decoder::PageEncoding; +use crate::repdef::RepDefBuilder; +use crate::{ + decoder::{ColumnInfo, PageInfo}, + format::pb, +}; + +pub use crate::array_encoding::ArrayFieldEncodingStrategy; + +pub mod structural; + +/// The minimum alignment for a page buffer. Writers must respect this. +pub const MIN_PAGE_BUFFER_ALIGNMENT: u64 = 8; + +/// An array encoded with the `pb::ArrayEncoding` grammar. +#[derive(Debug)] +pub struct EncodedArray { + pub data: DataBlock, + pub encoding: pb::ArrayEncoding, +} + +impl EncodedArray { + pub fn new(data: DataBlock, encoding: pb::ArrayEncoding) -> Self { + Self { data, encoding } + } + + pub fn into_buffers(self) -> (Vec, pb::ArrayEncoding) { + (self.data.into_buffers(), self.encoding) + } +} + +/// Encodes one data block and describes it with `pb::ArrayEncoding`. +pub trait ArrayEncoder: std::fmt::Debug + Send + Sync { + fn encode( + &self, + data: DataBlock, + data_type: &arrow_schema::DataType, + buffer_index: &mut u32, + ) -> Result; +} + +/// Selects an `ArrayEncoder` for one page. +pub trait ArrayEncodingStrategy: Send + Sync + std::fmt::Debug { + fn create_array_encoder( + &self, + arrays: &[ArrayRef], + field: &Field, + ) -> Result>; +} + +/// An encoded page of data +/// +/// Maps to a top-level array +/// +/// For example, `FixedSizeList` will have two EncodedArray instances and one EncodedPage +#[derive(Debug)] +pub struct EncodedPage { + // The encoded page buffers + pub data: Vec, + // A description of the encoding used to encode the page + pub description: PageEncoding, + /// The number of rows in the encoded page + pub num_rows: u64, + /// The top-level row number of the first row in the page + /// + /// Generally the number of "top-level" rows and the number of rows are the same. However, + /// when there is repetition (list/fixed-size-list) there will be more or less items than rows. + /// + /// A top-level row can never be split across a page boundary. + pub row_number: u64, + /// The index of the column + pub column_idx: u32, +} + +pub struct EncodedColumn { + pub column_buffers: Vec, + pub encoding: pb::ColumnEncoding, + pub final_pages: Vec, +} + +impl Default for EncodedColumn { + fn default() -> Self { + Self { + column_buffers: Default::default(), + encoding: pb::ColumnEncoding { + column_encoding: Some(pb::column_encoding::ColumnEncoding::Values(())), + }, + final_pages: Default::default(), + } + } +} + +/// A tool to reserve space for buffers that are not in-line with the data +/// +/// In most cases, buffers are stored in the page and referred to in the encoding +/// metadata by their index in the page. This keeps all buffers within a page together. +/// As a result, most encoders should not need to use this structure. +/// +/// In some cases (currently only the large binary encoding) there is a need to access +/// buffers that are not in the page (because storing the position / offset of every page +/// in the page metadata would be too expensive). +/// +/// To do this you can add a buffer with `add_buffer` and then use the returned position +/// in some way (in the large binary encoding the returned position is stored in the page +/// data as a position / size array). +pub struct OutOfLineBuffers { + position: u64, + buffer_alignment: u64, + buffers: Vec, +} + +impl OutOfLineBuffers { + pub fn new(base_position: u64, buffer_alignment: u64) -> Self { + Self { + position: base_position, + buffer_alignment, + buffers: Vec::new(), + } + } + + pub fn add_buffer(&mut self, buffer: LanceBuffer) -> u64 { + let position = self.position; + self.position += buffer.len() as u64; + self.position += pad_bytes_to(buffer.len(), self.buffer_alignment as usize) as u64; + self.buffers.push(buffer); + position + } + + pub fn take_buffers(self) -> Vec { + self.buffers + } + + pub fn reset_position(&mut self, position: u64) { + self.position = position; + } +} + +/// A task to create a page of data +pub type EncodeTask = BoxFuture<'static, Result>; + +/// Top level encoding trait to code any Arrow array type into one or more pages. +/// +/// The field encoder implements buffering and encoding of a single input column +/// but it may map to multiple output columns. For example, a list array or struct +/// array will be encoded into multiple columns. +/// +/// Also, fields may be encoded at different speeds. For example, given a struct +/// column with three fields (a boolean field, an int32 field, and a 4096-dimension +/// tensor field) the tensor field is likely to emit encoded pages much more frequently +/// than the boolean field. +pub trait FieldEncoder: Send { + /// Buffer the data and, if there is enough data in the buffer to form a page, return + /// an encoding task to encode the data. + /// + /// This may return more than one task because a single column may be mapped to multiple + /// output columns. For example, if encoding a struct column with three children then + /// up to three tasks may be returned from each call to maybe_encode. + /// + /// It may also return multiple tasks for a single column if the input array is larger + /// than a single disk page. + /// + /// It could also return an empty Vec if there is not enough data yet to encode any pages. + /// + /// The `row_number` must be passed which is the top-level row number currently being encoded + /// This is stored in any pages produced by this call so that we can know the priority of the + /// page. + /// + /// The `num_rows` is the number of top level rows. It is initially the same as `array.len()` + /// however it is passed seprately because array will become flattened over time (if there is + /// repetition) and we need to know the original number of rows for various purposes. + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result>; + /// Flush any remaining data from the buffers into encoding tasks + /// + /// Each encode task produces a single page. The order of these pages will be maintained + /// in the file (we do not worry about order between columns but all pages in the same + /// column should maintain order) + /// + /// This may be called intermittently throughout encoding but will always be called + /// once at the end of encoding just before calling finish + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result>; + /// Finish encoding and return column metadata + /// + /// This is called only once, after all encode tasks have completed + /// + /// This returns a Vec because a single field may have created multiple columns + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>>; + + /// The number of output columns this encoding will create + fn num_columns(&self) -> u32; +} + +/// Keeps track of the current column index and makes a mapping +/// from field id to column index +#[derive(Debug, Default)] +pub struct ColumnIndexSequence { + current_index: u32, + mapping: Vec<(u32, u32)>, +} + +impl ColumnIndexSequence { + pub fn next_column_index(&mut self, field_id: u32) -> u32 { + let idx = self.current_index; + self.current_index += 1; + self.mapping.push((field_id, idx)); + idx + } + + pub fn skip(&mut self) { + self.current_index += 1; + } +} + +/// Options that control the encoding process +pub struct EncodingOptions { + /// How much data (in bytes) to cache in-memory before writing a page + /// + /// This cache is applied on a per-column basis + pub cache_bytes_per_column: u64, + /// The maximum size of a page in bytes, if a single array would create + /// a page larger than this then it will be split into multiple pages + pub max_page_bytes: u64, + /// If false (the default) then arrays will be copied (deeply) before + /// being cached. This ensures any data kept alive by the array can + /// be discarded safely and helps avoid writer accumulation. However, + /// there is an associated cost. + pub keep_original_array: bool, + /// The alignment that the writer is applying to buffers + /// + /// The encoder needs to know this so it figures the position of out-of-line + /// buffers correctly + pub buffer_alignment: u64, +} + +impl Default for EncodingOptions { + fn default() -> Self { + Self { + cache_bytes_per_column: 8 * 1024 * 1024, + max_page_bytes: 32 * 1024 * 1024, + keep_original_array: true, + buffer_alignment: 64, + } + } +} + +/// A trait to pick which kind of field encoding to use for a field +/// +/// Unlike the ArrayEncodingStrategy, the field encoding strategy is +/// chosen before any data is generated and the same field encoder is +/// used for all data in the field. +pub trait FieldEncodingStrategy: Send + Sync + std::fmt::Debug { + /// Choose and create an appropriate field encoder for the given + /// field. + /// + /// The field encoder can be chosen on the data type as well as + /// any metadata that is attached to the field. + /// + fn create_field_encoder( + &self, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, + ) -> Result>; +} + +/// Context shared while one top-level field and all of its children are mapped +/// to concrete field encoders. +pub struct FieldEncodingContext<'a> { + /// The complete strategy composition used for recursive child fields. + pub strategy: &'a dyn FieldEncodingStrategy, + /// Runtime-only writer options. + pub options: &'a EncodingOptions, + /// Metadata inherited from the top-level field. + pub root_field_metadata: &'a HashMap, +} + +/// A batch encoder that encodes RecordBatch objects by delegating +/// to field encoders for each top-level field in the batch. +pub struct BatchEncoder { + pub field_encoders: Vec>, + pub field_id_to_column_index: Vec<(u32, u32)>, +} + +impl BatchEncoder { + pub fn try_new( + schema: &Schema, + strategy: &dyn FieldEncodingStrategy, + options: &EncodingOptions, + ) -> Result { + let mut col_idx = 0; + let mut col_idx_sequence = ColumnIndexSequence::default(); + let field_encoders = schema + .fields + .iter() + .map(|field| { + let context = FieldEncodingContext { + strategy, + options, + root_field_metadata: &field.metadata, + }; + let encoder = + strategy.create_field_encoder(field, &mut col_idx_sequence, &context)?; + col_idx += encoder.as_ref().num_columns(); + Ok(encoder) + }) + .collect::>>()?; + Ok(Self { + field_encoders, + field_id_to_column_index: col_idx_sequence.mapping, + }) + } + + pub fn num_columns(&self) -> u32 { + self.field_encoders + .iter() + .map(|field_encoder| field_encoder.num_columns()) + .sum::() + } +} + +/// An encoded batch of data and a page table describing it +/// +/// This is returned by [`crate::encoder::encode_batch`] +#[derive(Debug)] +pub struct EncodedBatch { + pub data: Bytes, + pub page_table: Vec>, + pub schema: Arc, + pub top_level_columns: Vec, + pub num_rows: u64, +} + +fn write_page_to_data_buffer(page: EncodedPage, data_buffer: &mut BytesMut) -> PageInfo { + let buffers = page.data; + let mut buffer_offsets_and_sizes = Vec::with_capacity(buffers.len()); + for buffer in buffers { + let buffer_offset = data_buffer.len() as u64; + data_buffer.extend_from_slice(&buffer); + let size = data_buffer.len() as u64 - buffer_offset; + buffer_offsets_and_sizes.push((buffer_offset, size)); + } + + PageInfo { + buffer_offsets_and_sizes: Arc::from(buffer_offsets_and_sizes), + encoding: page.description, + num_rows: page.num_rows, + priority: page.row_number, + } +} + +/// Helper method to encode a batch of data into memory +/// +/// This is primarily for testing and benchmarking but could be useful in other +/// niche situations like IPC. +pub async fn encode_batch( + batch: &RecordBatch, + schema: Arc, + encoding_strategy: &dyn FieldEncodingStrategy, + options: &EncodingOptions, +) -> Result { + if !is_pwr_two(options.buffer_alignment) || options.buffer_alignment < MIN_PAGE_BUFFER_ALIGNMENT + { + return Err(Error::invalid_input_source( + format!( + "buffer_alignment must be a power of two and at least {}", + MIN_PAGE_BUFFER_ALIGNMENT + ) + .into(), + )); + } + + let mut data_buffer = BytesMut::new(); + let lance_schema = Schema::try_from(batch.schema().as_ref())?; + let options = EncodingOptions { + keep_original_array: true, + ..*options + }; + let batch_encoder = BatchEncoder::try_new(&lance_schema, encoding_strategy, &options)?; + let mut page_table = Vec::new(); + let mut col_idx_offset = 0; + for (arr, mut encoder) in batch.columns().iter().zip(batch_encoder.field_encoders) { + let mut external_buffers = + OutOfLineBuffers::new(data_buffer.len() as u64, options.buffer_alignment); + let repdef = RepDefBuilder::default(); + let encoder = encoder.as_mut(); + let num_rows = arr.len() as u64; + let mut tasks = + encoder.maybe_encode(arr.clone(), &mut external_buffers, repdef, 0, num_rows)?; + tasks.extend(encoder.flush(&mut external_buffers)?); + for buffer in external_buffers.take_buffers() { + data_buffer.extend_from_slice(&buffer); + } + let mut pages = HashMap::>::new(); + for task in tasks { + let encoded_page = task.await?; + // Write external buffers first + pages + .entry(encoded_page.column_idx) + .or_default() + .push(write_page_to_data_buffer(encoded_page, &mut data_buffer)); + } + let mut external_buffers = + OutOfLineBuffers::new(data_buffer.len() as u64, options.buffer_alignment); + let encoded_columns = encoder.finish(&mut external_buffers).await?; + for buffer in external_buffers.take_buffers() { + data_buffer.extend_from_slice(&buffer); + } + let num_columns = encoded_columns.len(); + for (col_idx, encoded_column) in encoded_columns.into_iter().enumerate() { + let col_idx = col_idx + col_idx_offset; + let mut col_buffer_offsets_and_sizes = Vec::new(); + for buffer in encoded_column.column_buffers { + let buffer_offset = data_buffer.len() as u64; + data_buffer.extend_from_slice(&buffer); + let size = data_buffer.len() as u64 - buffer_offset; + col_buffer_offsets_and_sizes.push((buffer_offset, size)); + } + for page in encoded_column.final_pages { + pages + .entry(page.column_idx) + .or_default() + .push(write_page_to_data_buffer(page, &mut data_buffer)); + } + let col_pages = std::mem::take(pages.entry(col_idx as u32).or_default()); + page_table.push(Arc::new(ColumnInfo { + index: col_idx as u32, + buffer_offsets_and_sizes: Arc::from( + col_buffer_offsets_and_sizes.into_boxed_slice(), + ), + page_infos: Arc::from(col_pages.into_boxed_slice()), + encoding: encoded_column.encoding, + })) + } + col_idx_offset += num_columns; + } + let top_level_columns = batch_encoder + .field_id_to_column_index + .iter() + .map(|(_, idx)| *idx) + .collect(); + Ok(EncodedBatch { + data: data_buffer.freeze(), + top_level_columns, + page_table, + schema, + num_rows: batch.num_rows() as u64, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testing::{TestEncoding, create_test_field_encoder, test_encoding_strategy}; + use arrow_schema::{DataType as ArrowDataType, Field as ArrowField, Fields as ArrowFields}; + + #[test] + fn test_fixed_size_list_struct_requires_v2_2() { + let list_item = ArrowField::new( + "item", + ArrowDataType::Struct(ArrowFields::from(vec![ArrowField::new( + "x", + ArrowDataType::Int32, + true, + )])), + true, + ); + let arrow_field = ArrowField::new( + "list_struct", + ArrowDataType::FixedSizeList(Arc::new(list_item), 2), + true, + ); + let field = Field::try_from(&arrow_field).unwrap(); + + let strategy = test_encoding_strategy(TestEncoding::StructuralU16); + let mut column_index = ColumnIndexSequence::default(); + let options = EncodingOptions::default(); + + let result = + create_test_field_encoder(strategy.as_ref(), &field, &mut column_index, &options); + assert!( + result.is_err(), + "FixedSizeList should be rejected for file version 2.1" + ); + let err = result.err().unwrap(); + + assert!( + err.to_string() + .contains("FixedSizeList is not enabled by the selected file format") + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encoder/structural.rs b/lance-artifact/rust/lance-encoding/src/encoder/structural.rs new file mode 100644 index 000000000..3d4e49f42 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encoder/structural.rs @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Version-free structural field encoder builders. + +use std::sync::Arc; + +use arrow_schema::DataType; +use lance_core::{Error, Result, datatypes::Field, error::LanceOptionExt}; + +pub use crate::encodings::logical::primitive::PrimitivePageEncoding; + +use crate::encodings::logical::{ + blob::{BlobStructuralEncoder, BlobV2StructuralEncoder}, + fixed_size_list::FixedSizeListStructuralEncoder, + list::ListStructuralEncoder, + map::MapStructuralEncoder, + primitive::PrimitiveStructuralEncoder, + r#struct::StructStructuralEncoder, +}; + +use super::{ColumnIndexSequence, FieldEncoder, FieldEncodingContext}; + +/// Encode primitive leaves, primitive fixed-size lists, dictionaries, and +/// packed or empty structs using one concrete primitive page grammar. +#[derive(Debug, Clone)] +pub struct PrimitiveFieldEncoding { + page_encodings: Arc<[PrimitivePageEncoding]>, +} + +impl PrimitiveFieldEncoding { + /// Create a primitive field mechanism from ordered executable page behaviors. + pub fn new(page_encodings: impl IntoIterator) -> Self { + Self { + page_encodings: page_encodings.into_iter().collect(), + } + } + + fn is_primitive_type(data_type: &DataType) -> bool { + match data_type { + DataType::FixedSizeList(inner, _) => Self::is_primitive_type(inner.data_type()), + _ => matches!( + data_type, + DataType::Boolean + | DataType::Date32 + | DataType::Date64 + | DataType::Decimal128(_, _) + | DataType::Decimal256(_, _) + | DataType::Duration(_) + | DataType::Float16 + | DataType::Float32 + | DataType::Float64 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Int8 + | DataType::Interval(_) + | DataType::Null + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::UInt16 + | DataType::UInt32 + | DataType::UInt64 + | DataType::UInt8 + | DataType::FixedSizeBinary(_) + | DataType::Binary + | DataType::LargeBinary + | DataType::Utf8 + | DataType::LargeUtf8, + ), + } + } + + fn create_at( + &self, + field: Field, + column_index: u32, + context: &FieldEncodingContext<'_>, + ) -> Result> { + Ok(Box::new(PrimitiveStructuralEncoder::try_new( + context.options, + self.page_encodings.clone(), + column_index, + field, + Arc::new(context.root_field_metadata.clone()), + )?)) + } + + /// Create a primitive field encoder when this mechanism recognizes `field`. + pub fn try_create( + &self, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, + ) -> Result>> { + if field.is_blob() { + return Ok(None); + } + + let data_type = field.data_type(); + let is_primitive = Self::is_primitive_type(&data_type); + let is_packed_or_empty_struct = matches!( + &data_type, + DataType::Struct(fields) if field.is_packed_struct() || fields.is_empty() + ); + let is_primitive_dictionary = matches!( + &data_type, + DataType::Dictionary(_, value_type) if Self::is_primitive_type(value_type) + ); + + if !is_primitive && !is_packed_or_empty_struct && !is_primitive_dictionary { + if let DataType::Dictionary(_, value_type) = data_type { + return Err(Error::not_supported_source( + format!( + "cannot encode a dictionary column whose value type is a logical type ({})", + value_type + ) + .into(), + )); + } + return Ok(None); + } + + Ok(Some(self.create_at( + field.clone(), + column_index.next_column_index(field.id as u32), + context, + )?)) + } +} + +/// Create the original binary blob descriptor when `field` matches. +pub fn try_create_binary_blob( + primitive: &PrimitiveFieldEncoding, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + if !field.is_blob() || !matches!(field.data_type(), DataType::Binary | DataType::LargeBinary) { + return Ok(None); + } + let descriptor_column_index = column_index.next_column_index(field.id as u32); + Ok(Some(Box::new(BlobStructuralEncoder::new( + field, + |descriptor_field| primitive.create_at(descriptor_field, descriptor_column_index, context), + )?))) +} + +/// Create the structural blob descriptor when `field` matches. +pub fn try_create_structural_blob( + primitive: &PrimitiveFieldEncoding, + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + if !field.is_blob() || !matches!(field.data_type(), DataType::Struct(_)) { + return Ok(None); + } + let descriptor_column_index = column_index.next_column_index(field.id as u32); + Ok(Some(Box::new(BlobV2StructuralEncoder::new( + field, + |descriptor_field| primitive.create_at(descriptor_field, descriptor_column_index, context), + )?))) +} + +/// Create a variable-size list encoder when `field` matches. +pub fn try_create_list( + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + if !matches!( + field.data_type(), + DataType::List(_) | DataType::LargeList(_) + ) { + return Ok(None); + } + let child = field.children.first().expect_ok()?; + let child_encoder = context + .strategy + .create_field_encoder(child, column_index, context)?; + Ok(Some(Box::new(ListStructuralEncoder::new( + context.options.keep_original_array, + child_encoder, + )))) +} + +/// Create a fixed-size-list encoder whose child is a struct when applicable. +pub fn try_create_structural_fixed_size_list( + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + if !matches!( + field.data_type(), + DataType::FixedSizeList(inner, _) if matches!(inner.data_type(), DataType::Struct(_)) + ) { + return Ok(None); + } + let child = field.children.first().expect_ok()?; + let child_encoder = context + .strategy + .create_field_encoder(child, column_index, context)?; + Ok(Some(Box::new(FixedSizeListStructuralEncoder::new( + context.options.keep_original_array, + child_encoder, + )))) +} + +/// Create an Arrow map encoder when `field` matches. +pub fn try_create_map( + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + let DataType::Map(_, keys_sorted) = field.data_type() else { + return Ok(None); + }; + if keys_sorted { + return Err(Error::not_supported_source( + format!( + "Map data type is not supported with keys_sorted=true now, current value is {}", + keys_sorted + ) + .into(), + )); + } + let entries_child = field + .children + .first() + .ok_or_else(|| Error::schema("Map should have an entries child".to_string()))?; + let DataType::Struct(struct_fields) = entries_child.data_type() else { + return Err(Error::schema( + "Map entries field must be a Struct".to_string(), + )); + }; + if struct_fields.len() < 2 { + return Err(Error::schema( + "Map entries struct must contain both key and value fields".to_string(), + )); + } + let key_field = &struct_fields[0]; + if key_field.is_nullable() { + return Err(Error::schema(format!( + "Map key field '{}' must be non-nullable according to Arrow Map specification", + key_field.name() + ))); + } + let child_encoder = + context + .strategy + .create_field_encoder(entries_child, column_index, context)?; + Ok(Some(Box::new(MapStructuralEncoder::new( + context.options.keep_original_array, + child_encoder, + )))) +} + +/// Create a non-packed, non-empty struct encoder when `field` matches. +pub fn try_create_struct( + field: &Field, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, +) -> Result>> { + let DataType::Struct(fields) = field.data_type() else { + return Ok(None); + }; + if field.is_blob() || field.is_packed_struct() || fields.is_empty() { + return Ok(None); + } + let children_encoders = field + .children + .iter() + .map(|child| { + context + .strategy + .create_field_encoder(child, column_index, context) + }) + .collect::>>()?; + Ok(Some(Box::new(StructStructuralEncoder::new( + context.options.keep_original_array, + children_encoders, + )))) +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings.rs b/lance-artifact/rust/lance-encoding/src/encodings.rs new file mode 100644 index 000000000..d3ae90286 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings.rs @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod logical; +pub mod physical; + +#[cfg(test)] +pub mod fuzz_tests; diff --git a/lance-artifact/rust/lance-encoding/src/encodings/fuzz_tests.rs b/lance-artifact/rust/lance-encoding/src/encodings/fuzz_tests.rs new file mode 100644 index 000000000..3521d633a --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/fuzz_tests.rs @@ -0,0 +1,545 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Comprehensive fuzz testing for Lance 2.1 encoding coverage +//! +//! This module implements property-based testing using proptest to ensure +//! correct behavior across 16 different encoding permutations as specified +//! in issue #3347. + +use std::collections::HashMap; +use std::sync::Arc; + +use arrow_array::builder::{Int32Builder, ListBuilder}; +use arrow_array::*; +use arrow_schema::{DataType, Field}; +use proptest::prelude::*; + +use crate::testing::{TestCases, check_round_trip_encoding_of_data}; +use lance_core::Result; +use lance_datagen::{ArrayGenerator, ByteCount, Dimension, RowCount, Seed, array, gen_batch}; + +/// Test configuration representing one of the 16 permutations +#[derive(Debug, Clone)] +struct EncodingTestConfig { + encoding_type: EncodingType, + data_structure: DataStructure, + data_width: DataWidth, + nullable: bool, +} + +#[derive(Debug, Clone, PartialEq)] +enum EncodingType { + Miniblock, + FullZip, +} + +#[derive(Debug, Clone, PartialEq)] +enum DataStructure { + Primitive, + List, + FixedSizeList(i32), +} + +#[derive(Debug, Clone)] +enum DataWidth { + Fixed(FixedWidthType), + Variable(VariableWidthType), +} + +#[derive(Debug, Clone)] +enum FixedWidthType { + Int8, + Int16, + Int32, + Int64, + UInt8, + UInt16, + UInt32, + UInt64, + Float16, + Float32, + Float64, + FixedBinary(i32), +} + +#[derive(Debug, Clone)] +enum VariableWidthType { + String, + Binary, + LargeString, + LargeBinary, +} + +impl EncodingTestConfig { + /// Create field based on configuration + fn to_field(&self, name: &str) -> Field { + let data_type = self.to_data_type(); + Field::new(name, data_type, self.nullable) + } + + /// Get the Arrow data type for this configuration + fn to_data_type(&self) -> DataType { + let base_type = match &self.data_width { + DataWidth::Fixed(fixed) => match fixed { + FixedWidthType::Int8 => DataType::Int8, + FixedWidthType::Int16 => DataType::Int16, + FixedWidthType::Int32 => DataType::Int32, + FixedWidthType::Int64 => DataType::Int64, + FixedWidthType::UInt8 => DataType::UInt8, + FixedWidthType::UInt16 => DataType::UInt16, + FixedWidthType::UInt32 => DataType::UInt32, + FixedWidthType::UInt64 => DataType::UInt64, + FixedWidthType::Float16 => DataType::Float16, + FixedWidthType::Float32 => DataType::Float32, + FixedWidthType::Float64 => DataType::Float64, + FixedWidthType::FixedBinary(size) => DataType::FixedSizeBinary(*size), + }, + DataWidth::Variable(var) => match var { + VariableWidthType::String => DataType::Utf8, + VariableWidthType::Binary => DataType::Binary, + VariableWidthType::LargeString => DataType::LargeUtf8, + VariableWidthType::LargeBinary => DataType::LargeBinary, + }, + }; + + match &self.data_structure { + DataStructure::Primitive => base_type, + DataStructure::List => DataType::List(Arc::new(Field::new("item", base_type, true))), + DataStructure::FixedSizeList(size) => { + DataType::FixedSizeList(Arc::new(Field::new("item", base_type, true)), *size) + } + } + } +} + +/// Generate all valid test configurations (excluding FSL+List combinations) +fn encoding_config_strategy() -> impl Strategy { + ( + prop_oneof![Just(EncodingType::Miniblock), Just(EncodingType::FullZip)], + prop_oneof![ + Just(DataStructure::Primitive), + Just(DataStructure::List), + (1..=100i32).prop_map(DataStructure::FixedSizeList) + ], + prop_oneof![ + fixed_width_strategy().prop_map(DataWidth::Fixed), + variable_width_strategy().prop_map(DataWidth::Variable) + ], + any::(), // nullable + ) + .prop_filter( + "Skip unsupported combinations", + |(_, structure, width, _)| { + // FSL with Variable width types is not supported + // This combination causes compute_stat to be called twice + !matches!( + (structure, width), + (DataStructure::FixedSizeList(_), DataWidth::Variable(_)) + ) + }, + ) + .prop_map( + |(encoding, structure, width, nullable)| EncodingTestConfig { + encoding_type: encoding, + data_structure: structure, + data_width: width, + nullable, + }, + ) +} + +fn fixed_width_strategy() -> impl Strategy { + prop_oneof![ + Just(FixedWidthType::Int8), + Just(FixedWidthType::Int16), + Just(FixedWidthType::Int32), + Just(FixedWidthType::Int64), + Just(FixedWidthType::UInt8), + Just(FixedWidthType::UInt16), + Just(FixedWidthType::UInt32), + Just(FixedWidthType::UInt64), + Just(FixedWidthType::Float16), + Just(FixedWidthType::Float32), + Just(FixedWidthType::Float64), + (1..=128i32).prop_map(FixedWidthType::FixedBinary) + ] +} + +fn variable_width_strategy() -> impl Strategy { + prop_oneof![ + Just(VariableWidthType::String), + Just(VariableWidthType::Binary), + Just(VariableWidthType::LargeString), + Just(VariableWidthType::LargeBinary) + ] +} + +/// Generate test data based on configuration +fn generate_test_data_for_config( + config: &EncodingTestConfig, + num_rows: usize, + seed: u64, +) -> Result> { + let mut batch_gen = gen_batch().with_seed(Seed::from(seed)); + + // Generate base values + let generator: Box = match &config.data_width { + DataWidth::Fixed(fixed) => match fixed { + FixedWidthType::Int8 => array::rand_type(&DataType::Int8), + FixedWidthType::Int16 => array::rand_type(&DataType::Int16), + FixedWidthType::Int32 => array::rand_type(&DataType::Int32), + FixedWidthType::Int64 => array::rand_type(&DataType::Int64), + FixedWidthType::UInt8 => array::rand_type(&DataType::UInt8), + FixedWidthType::UInt16 => array::rand_type(&DataType::UInt16), + FixedWidthType::UInt32 => array::rand_type(&DataType::UInt32), + FixedWidthType::UInt64 => array::rand_type(&DataType::UInt64), + FixedWidthType::Float16 => array::rand_type(&DataType::Float16), + FixedWidthType::Float32 => array::rand_type(&DataType::Float32), + FixedWidthType::Float64 => array::rand_type(&DataType::Float64), + FixedWidthType::FixedBinary(size) => { + array::rand_type(&DataType::FixedSizeBinary(*size)) + } + }, + DataWidth::Variable(var) => { + // Control string/binary size based on encoding type + let max_len = if config.encoding_type == EncodingType::Miniblock { + 10 // Small strings for miniblock + } else { + 1000 // Large strings for full-zip + }; + + match var { + VariableWidthType::String => { + array::rand_utf8(ByteCount::from(max_len as u64), false) + } + VariableWidthType::Binary => { + array::rand_varbin(ByteCount::from(1), ByteCount::from(max_len as u64)) + } + VariableWidthType::LargeString => { + array::rand_utf8(ByteCount::from(max_len as u64), true) + } + VariableWidthType::LargeBinary => { + // For large binary, just use regular binary with larger size + array::rand_varbin(ByteCount::from(1), ByteCount::from(max_len as u64)) + } + } + } + }; + + // Wrap in list structure if needed + let final_generator: Box = match &config.data_structure { + DataStructure::Primitive => generator, + DataStructure::List => { + // Use rand_list_any for wrapping any generator + array::rand_list_any(generator, false) + } + DataStructure::FixedSizeList(size) => { + // Use cycle_vec for fixed size lists + array::cycle_vec(generator, Dimension::from(*size as u32)) + } + }; + + // Add nulls if configured + if config.nullable { + batch_gen.with_random_nulls(0.1); // 10% null rate + } + + let batch = batch_gen + .anon_col(final_generator) + .into_batch_rows(RowCount::from(num_rows as u64))?; + + Ok(batch.column(0).clone()) +} + +// Main property test for encoding round-trip +proptest! { + #![proptest_config(ProptestConfig::with_cases(50))] + + #[test] + fn test_encoding_round_trip( + config in encoding_config_strategy(), + num_rows in 100..=5000usize, + seed in any::() + ) { + let rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(async { + // Generate test data + let test_data = generate_test_data_for_config(&config, num_rows, seed) + .expect("Failed to generate test data"); + + // Set up test cases + let _field = config.to_field("test"); + + let mut metadata = HashMap::new(); + // Force specific encoding through metadata hints if needed + if config.encoding_type == EncodingType::Miniblock { + metadata.insert("encoding_hint".to_string(), "miniblock".to_string()); + } + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_batch_size(100) + .with_range(0..num_rows.min(500) as u64) + .with_indices(vec![0, num_rows as u64 / 2, (num_rows - 1) as u64]); + + // Execute round-trip test + check_round_trip_encoding_of_data( + vec![test_data], + &test_cases, + metadata + ).await; + }); + } +} + +#[tokio::test] +async fn test_edge_cases_single_value() { + // Test single value arrays + let single_int32 = Arc::new(Int32Array::from(vec![42])) as Arc; + let single_string = Arc::new(StringArray::from(vec!["test"])) as Arc; + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![single_int32], &test_cases, HashMap::new()).await; + + check_round_trip_encoding_of_data(vec![single_string], &test_cases, HashMap::new()).await; +} + +#[tokio::test] +async fn test_edge_cases_all_nulls() { + // Test arrays with all null values + let all_nulls_int32 = + Arc::new(Int32Array::from(vec![None, None, None] as Vec>)) as Arc; + let all_nulls_string = Arc::new(StringArray::from( + vec![None, None, None] as Vec> + )) as Arc; + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![all_nulls_int32], &test_cases, HashMap::new()).await; + + check_round_trip_encoding_of_data(vec![all_nulls_string], &test_cases, HashMap::new()).await; +} + +// Test list with repetition and definition levels +proptest! { + #[test] + fn test_list_repdef_handling( + list_sizes in prop::collection::vec(1..=20usize, 10..=100), + _seed in any::() + ) { + let rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(async { + // Create a list array with varying sizes + let mut list_builder = ListBuilder::new(Int32Builder::new()); + + for size in &list_sizes { + for i in 0..*size { + list_builder.values().append_value(i as i32); + } + list_builder.append(true); + } + + let list_array = Arc::new(list_builder.finish()) as Arc; + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_range(0..list_sizes.len().min(50) as u64); + + check_round_trip_encoding_of_data( + vec![list_array], + &test_cases, + HashMap::new() + ).await; + }); + } +} + +// Test fixed size list encoding +proptest! { + #[test] + fn test_fixed_size_list_encoding( + list_size in 1..=100i32, + num_rows in 10..=1000usize, + seed in any::() + ) { + let rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(async { + let config = EncodingTestConfig { + encoding_type: EncodingType::Miniblock, + data_structure: DataStructure::FixedSizeList(list_size), + data_width: DataWidth::Fixed(FixedWidthType::Int32), + nullable: false, + }; + + let test_data = generate_test_data_for_config(&config, num_rows, seed) + .expect("Failed to generate test data"); + + let test_cases = TestCases::default() + .with_structural_encodings(); + + check_round_trip_encoding_of_data( + vec![test_data], + &test_cases, + HashMap::new() + ).await; + }); + } +} + +#[tokio::test] +async fn test_list_dict_empty_batch() { + use arrow_array::builder::BinaryBuilder; + use arrow_array::builder::ListBuilder; + + // Create a list with some values followed by empty/null lists + let mut list_builder = ListBuilder::new(BinaryBuilder::new()); + + // First 50 lists have values with LOW CARDINALITY to trigger dictionary encoding + // Only 5 unique values repeated many times (150 total values, 5 unique) + let values = [b"aaaaa", b"bbbbb", b"ccccc", b"ddddd", b"eeeee"]; + for i in 0..50 { + // Each list has 3 values, cycling through the 5 unique values + list_builder.append_value([ + Some(values[i % 5]), + Some(values[(i + 1) % 5]), + Some(values[(i + 2) % 5]), + ]); + } + + // Next 50 lists are empty or null (no values) + for i in 0..50 { + if i % 2 == 0 { + list_builder.append_value(Vec::>::new()); // empty list + } else { + list_builder.append_null(); // null list + } + } + + let list_array = Arc::new(list_builder.finish()); + + let test_cases = TestCases::default() + .with_structural_encodings() + // Read only the empty/null lists (rows 50-99) + // This batch will have 0 underlying values + .with_range(50..100); + + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; +} + +// Test all valid combinations systematically (14 combinations) +// Excludes FSL with Variable width types which are not supported +#[tokio::test] +async fn test_all_valid_combinations() { + let combinations = vec![ + // Basic primitive types (4 combinations) + ( + EncodingType::Miniblock, + DataStructure::Primitive, + DataWidth::Fixed(FixedWidthType::Int32), + false, + ), + ( + EncodingType::Miniblock, + DataStructure::Primitive, + DataWidth::Variable(VariableWidthType::String), + false, + ), + ( + EncodingType::FullZip, + DataStructure::Primitive, + DataWidth::Fixed(FixedWidthType::Float64), + false, + ), + ( + EncodingType::FullZip, + DataStructure::Primitive, + DataWidth::Variable(VariableWidthType::Binary), + false, + ), + // List types (4 combinations) + ( + EncodingType::Miniblock, + DataStructure::List, + DataWidth::Fixed(FixedWidthType::Int32), + false, + ), + ( + EncodingType::Miniblock, + DataStructure::List, + DataWidth::Variable(VariableWidthType::String), + false, + ), + ( + EncodingType::FullZip, + DataStructure::List, + DataWidth::Fixed(FixedWidthType::Float64), + false, + ), + ( + EncodingType::FullZip, + DataStructure::List, + DataWidth::Variable(VariableWidthType::Binary), + false, + ), + // Fixed size list types (2 combinations - only with Fixed width types) + // Note: FSL with Variable width types is not supported + ( + EncodingType::Miniblock, + DataStructure::FixedSizeList(10), + DataWidth::Fixed(FixedWidthType::Int32), + false, + ), + ( + EncodingType::FullZip, + DataStructure::FixedSizeList(10), + DataWidth::Fixed(FixedWidthType::Float64), + false, + ), + // Nullable variants (4 combinations) + ( + EncodingType::Miniblock, + DataStructure::Primitive, + DataWidth::Fixed(FixedWidthType::Int32), + true, + ), + ( + EncodingType::Miniblock, + DataStructure::Primitive, + DataWidth::Variable(VariableWidthType::String), + true, + ), + ( + EncodingType::FullZip, + DataStructure::Primitive, + DataWidth::Fixed(FixedWidthType::Float64), + true, + ), + ( + EncodingType::FullZip, + DataStructure::Primitive, + DataWidth::Variable(VariableWidthType::Binary), + true, + ), + ]; + + for (encoding, structure, width, nullable) in combinations { + let config = EncodingTestConfig { + encoding_type: encoding, + data_structure: structure, + data_width: width, + nullable, + }; + + let test_data = + generate_test_data_for_config(&config, 100, 42).expect("Failed to generate test data"); + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![test_data], &test_cases, HashMap::new()).await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical.rs new file mode 100644 index 000000000..199f470f5 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical.rs @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod blob; +pub mod fixed_size_list; +pub mod list; +pub mod map; +pub mod primitive; +pub mod r#struct; diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/blob.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/blob.rs new file mode 100644 index 000000000..a5432fdc5 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/blob.rs @@ -0,0 +1,904 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashMap, sync::Arc}; + +use arrow_array::{ + Array, ArrayRef, StructArray, UInt64Array, + builder::{PrimitiveBuilder, StringBuilder}, + cast::AsArray, + types::{UInt8Type, UInt32Type, UInt64Type}, +}; +use arrow_buffer::Buffer; +use arrow_schema::{DataType, Field as ArrowField, Fields}; +use futures::{FutureExt, future::BoxFuture}; +use lance_core::{ + Error, Result, + datatypes::{BLOB_V2_DESC_FIELDS, BlobV2Layout, Field}, + error::LanceOptionExt, +}; + +use crate::{ + buffer::LanceBuffer, + constants::PACKED_STRUCT_META_KEY, + decoder::PageEncoding, + encoder::{EncodeTask, EncodedColumn, EncodedPage, FieldEncoder, OutOfLineBuffers}, + format::ProtobufUtils21, + repdef::{DefinitionInterpretation, RepDefBuilder}, +}; +use lance_core::datatypes::BlobKind; + +/// Blob structural encoder - stores large binary data in external buffers +/// +/// This encoder takes large binary arrays and stores them outside the normal +/// page structure. It creates a descriptor (position, size) for each blob +/// that is stored inline in the page. +pub struct BlobStructuralEncoder { + // Encoder for the descriptors (position/size struct) + descriptor_encoder: Box, + // Set when we first see data + def_meaning: Option>, +} + +impl BlobStructuralEncoder { + pub fn new( + field: &Field, + make_descriptor_encoder: impl FnOnce(Field) -> Result>, + ) -> Result { + // Create descriptor field: struct + // Preserve the original field's metadata for packed struct + let mut descriptor_metadata = HashMap::with_capacity(1); + descriptor_metadata.insert(PACKED_STRUCT_META_KEY.to_string(), "true".to_string()); + + let descriptor_data_type = DataType::Struct(Fields::from(vec![ + ArrowField::new("position", DataType::UInt64, false), + ArrowField::new("size", DataType::UInt64, false), + ])); + + // Use the original field's name for the descriptor + let descriptor_field = Field::try_from( + ArrowField::new(&field.name, descriptor_data_type, field.nullable) + .with_metadata(descriptor_metadata), + )?; + + // Use PrimitiveStructuralEncoder to handle the descriptor + let descriptor_encoder = make_descriptor_encoder(descriptor_field)?; + + Ok(Self { + descriptor_encoder, + def_meaning: None, + }) + } + + fn wrap_tasks( + tasks: Vec, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Vec { + tasks + .into_iter() + .map(|task| { + let def_meaning = def_meaning.clone(); + task.then(|encoded_page| async move { + let encoded_page = encoded_page?; + + let PageEncoding::Structural(inner_layout) = encoded_page.description else { + return Err(Error::internal( + "Expected inner encoding to return structural layout".to_string(), + )); + }; + + let wrapped = ProtobufUtils21::blob_layout(inner_layout, &def_meaning); + Ok(EncodedPage { + column_idx: encoded_page.column_idx, + data: encoded_page.data, + description: PageEncoding::Structural(wrapped), + num_rows: encoded_page.num_rows, + row_number: encoded_page.row_number, + }) + }) + .boxed() + }) + .collect::>() + } +} + +impl FieldEncoder for BlobStructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + if let Some(validity) = array.nulls() { + repdef.add_validity_bitmap(validity.clone()); + } else { + repdef.add_no_null(array.len()); + } + + // Convert input array to LargeBinary + let binary_array = array.as_binary_opt::().ok_or_else(|| { + Error::invalid_input_source( + format!("Expected LargeBinary array, got {}", array.data_type()).into(), + ) + })?; + + let repdef = RepDefBuilder::serialize(vec![repdef]); + + let rep = repdef.repetition_levels.as_ref(); + let def = repdef.definition_levels.as_ref(); + let def_meaning: Arc<[DefinitionInterpretation]> = repdef.def_meaning.into(); + + // A blob page stores one definition interpretation for all of its rows. + // The descriptor encoder can buffer multiple input arrays, so finish the + // pending page before a later array changes from all-valid to nullable (or + // vice versa). + let mut encode_tasks = match self.def_meaning.as_ref() { + Some(existing) if existing != &def_meaning => { + let existing = existing.clone(); + Self::wrap_tasks(self.descriptor_encoder.flush(external_buffers)?, existing) + } + _ => Vec::new(), + }; + self.def_meaning = Some(def_meaning.clone()); + + // Collect positions and sizes + let mut positions = Vec::with_capacity(binary_array.len()); + let mut sizes = Vec::with_capacity(binary_array.len()); + + for i in 0..binary_array.len() { + if binary_array.is_null(i) { + // Null values are smuggled into the positions array + + // If we have null values we must have definition levels + let mut repdef = (def.expect_ok()?[i] as u64) << 16; + if let Some(rep) = rep { + repdef += rep[i] as u64; + } + + debug_assert_ne!(repdef, 0); + positions.push(repdef); + sizes.push(0); + } else { + let value = binary_array.value(i); + if value.is_empty() { + // Empty values + positions.push(0); + sizes.push(0); + } else { + // Add data to external buffers + let position = + external_buffers.add_buffer(LanceBuffer::from(Buffer::from(value))); + positions.push(position); + sizes.push(value.len() as u64); + } + } + } + + // Create descriptor array + let position_array = Arc::new(UInt64Array::from(positions)); + let size_array = Arc::new(UInt64Array::from(sizes)); + let descriptor_array = Arc::new(StructArray::new( + Fields::from(vec![ + ArrowField::new("position", DataType::UInt64, false), + ArrowField::new("size", DataType::UInt64, false), + ]), + vec![position_array as ArrayRef, size_array as ArrayRef], + None, // Descriptors are never null + )); + + // Delegate to descriptor encoder + let descriptor_tasks = self.descriptor_encoder.maybe_encode( + descriptor_array, + external_buffers, + RepDefBuilder::default(), + row_number, + num_rows, + )?; + encode_tasks.extend(Self::wrap_tasks(descriptor_tasks, def_meaning)); + + Ok(encode_tasks) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + let encode_tasks = self.descriptor_encoder.flush(external_buffers)?; + + // Use the cached def meaning. If we haven't seen any data yet then we can just use a dummy + // value (not clear there would be any encode tasks in that case) + let def_meaning = self + .def_meaning + .clone() + .unwrap_or_else(|| Arc::new([DefinitionInterpretation::AllValidItem])); + + Ok(Self::wrap_tasks(encode_tasks, def_meaning)) + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + self.descriptor_encoder.finish(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.descriptor_encoder.num_columns() + } +} + +/// Blob v2 structural encoder +pub struct BlobV2StructuralEncoder { + descriptor_encoder: Box, +} + +impl BlobV2StructuralEncoder { + pub fn new( + field: &Field, + make_descriptor_encoder: impl FnOnce(Field) -> Result>, + ) -> Result { + let mut descriptor_metadata = HashMap::with_capacity(1); + descriptor_metadata.insert(PACKED_STRUCT_META_KEY.to_string(), "true".to_string()); + + let descriptor_data_type = DataType::Struct(BLOB_V2_DESC_FIELDS.clone()); + + let descriptor_field = Field::try_from( + ArrowField::new(&field.name, descriptor_data_type, field.nullable) + .with_metadata(descriptor_metadata), + )?; + + let descriptor_encoder = make_descriptor_encoder(descriptor_field)?; + + Ok(Self { descriptor_encoder }) + } +} + +impl FieldEncoder for BlobV2StructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let struct_arr = array + .as_any() + .downcast_ref::() + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Blob v2 encoder expected StructArray, got {}", + array.data_type() + ) + .into(), + ) + })?; + if BlobV2Layout::classify(struct_arr.fields()) != Some(BlobV2Layout::Prepared) { + let actual = BlobV2Layout::classify(struct_arr.fields()) + .map(|layout| layout.to_string()) + .unwrap_or_else(|| format!("unrecognized ({:?})", struct_arr.fields())); + return Err(Error::invalid_input_source( + format!("Blob v2 encoder expected prepared array layout, got {actual} layout") + .into(), + )); + } + + let kind_col = struct_arr + .column_by_name("kind") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `kind` field".into()) + })? + .as_primitive::(); + let data_col = struct_arr + .column_by_name("data") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `data` field".into()) + })? + .as_binary::(); + let uri_col = struct_arr + .column_by_name("uri") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `uri` field".into()) + })? + .as_string::(); + let blob_id_col = struct_arr + .column_by_name("blob_id") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `blob_id` field".into()) + })? + .as_primitive::(); + let blob_size_col = struct_arr + .column_by_name("blob_size") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `blob_size` field".into()) + })? + .as_primitive::(); + let packed_position_col = struct_arr + .column_by_name("position") + .ok_or_else(|| { + Error::invalid_input_source("Blob v2 struct missing `position` field".into()) + })? + .as_primitive::(); + + let row_count = struct_arr.len(); + + let mut kind_builder = PrimitiveBuilder::::with_capacity(row_count); + let mut position_builder = PrimitiveBuilder::::with_capacity(row_count); + let mut size_builder = PrimitiveBuilder::::with_capacity(row_count); + let mut blob_id_builder = PrimitiveBuilder::::with_capacity(row_count); + let mut uri_builder = StringBuilder::with_capacity(row_count, row_count * 16); + + for i in 0..row_count { + let (kind_value, position_value, size_value, blob_id_value, uri_value) = + if struct_arr.is_null(i) || kind_col.is_null(i) { + (BlobKind::Inline as u8, 0, 0, 0, "".to_string()) + } else { + let kind_val = BlobKind::try_from(kind_col.value(i))?; + match kind_val { + BlobKind::Dedicated => ( + BlobKind::Dedicated as u8, + 0, + blob_size_col.value(i), + blob_id_col.value(i), + "".to_string(), + ), + BlobKind::External => { + let uri = uri_col.value(i).to_string(); + let position = if packed_position_col.is_null(i) { + 0 + } else { + packed_position_col.value(i) + }; + let size = if blob_size_col.is_null(i) { + 0 + } else { + blob_size_col.value(i) + }; + let external_base_id = if blob_id_col.is_null(i) { + 0 + } else { + blob_id_col.value(i) + }; + ( + BlobKind::External as u8, + position, + size, + external_base_id, + uri, + ) + } + BlobKind::Packed => ( + BlobKind::Packed as u8, + packed_position_col.value(i), + blob_size_col.value(i), + blob_id_col.value(i), + "".to_string(), + ), + BlobKind::Inline => { + let data_val = data_col.value(i); + let blob_len = data_val.len() as u64; + let position = external_buffers + .add_buffer(LanceBuffer::from(Buffer::from(data_val))); + + ( + BlobKind::Inline as u8, + position, + blob_len, + 0, + "".to_string(), + ) + } + } + }; + + kind_builder.append_value(kind_value); + position_builder.append_value(position_value); + size_builder.append_value(size_value); + blob_id_builder.append_value(blob_id_value); + uri_builder.append_value(uri_value); + } + let children: Vec = vec![ + Arc::new(kind_builder.finish()), + Arc::new(position_builder.finish()), + Arc::new(size_builder.finish()), + Arc::new(blob_id_builder.finish()), + Arc::new(uri_builder.finish()), + ]; + + let descriptor_array = Arc::new(StructArray::try_new( + BLOB_V2_DESC_FIELDS.clone(), + children, + struct_arr.nulls().cloned(), + )?) as ArrayRef; + + self.descriptor_encoder.maybe_encode( + descriptor_array, + external_buffers, + repdef, + row_number, + num_rows, + ) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.descriptor_encoder.flush(external_buffers) + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + self.descriptor_encoder.finish(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.descriptor_encoder.num_columns() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + encoder::{ColumnIndexSequence, EncodingOptions}, + testing::{ + TestCases, TestEncoding, check_round_trip_encoding_of_data, + check_round_trip_encoding_of_data_with_expected, create_test_field_encoder, + test_encoding_strategy, + }, + }; + use arrow_array::{ + ArrayRef, LargeBinaryArray, StringArray, StructArray, UInt8Array, UInt32Array, UInt64Array, + }; + use arrow_schema::{DataType, Field as ArrowField}; + use lance_core::datatypes::BLOB_V2_LOGICAL_MINIMAL_FIELDS; + + #[test] + fn test_blob_encoder_creation() { + let field = Field::try_from( + ArrowField::new("blob_field", DataType::LargeBinary, true).with_metadata( + HashMap::from([(lance_arrow::BLOB_META_KEY.to_string(), "true".to_string())]), + ), + ) + .unwrap(); + let mut column_index = ColumnIndexSequence::default(); + let options = EncodingOptions::default(); + let strategy = test_encoding_strategy(TestEncoding::StructuralU16); + + let encoder = + create_test_field_encoder(strategy.as_ref(), &field, &mut column_index, &options); + + assert!(encoder.is_ok()); + } + + #[test] + fn test_blob_v2_encoder_rejects_logical_array_layout() { + let field = Field::try_from( + ArrowField::new( + "blob_field", + DataType::Struct(BLOB_V2_LOGICAL_MINIMAL_FIELDS.clone()), + true, + ) + .with_metadata(HashMap::from([( + lance_arrow::ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::BLOB_V2_EXT_NAME.to_string(), + )])), + ) + .unwrap(); + let mut column_index = ColumnIndexSequence::default(); + let options = EncodingOptions::default(); + let strategy = test_encoding_strategy(TestEncoding::StructuralU32); + let mut encoder = + create_test_field_encoder(strategy.as_ref(), &field, &mut column_index, &options) + .unwrap(); + let array = Arc::new( + StructArray::try_new( + BLOB_V2_LOGICAL_MINIMAL_FIELDS.clone(), + vec![ + Arc::new(LargeBinaryArray::from(vec![Some(b"payload".as_ref())])) as ArrayRef, + Arc::new(StringArray::from(vec![None::<&str>])) as ArrayRef, + ], + None, + ) + .unwrap(), + ) as ArrayRef; + let mut external_buffers = OutOfLineBuffers::new(0, 8); + let Err(error) = + encoder.maybe_encode(array, &mut external_buffers, RepDefBuilder::default(), 0, 1) + else { + panic!("logical array layout unexpectedly reached the descriptor encoder"); + }; + assert!(matches!(error, Error::InvalidInput { .. })); + assert!( + error + .to_string() + .contains("expected prepared array layout, got logical layout") + ); + } + + #[tokio::test] + async fn test_blob_encoding_simple() { + let field = Field::try_from( + ArrowField::new("blob_field", DataType::LargeBinary, true).with_metadata( + HashMap::from([(lance_arrow::BLOB_META_KEY.to_string(), "true".to_string())]), + ), + ) + .unwrap(); + let mut column_index = ColumnIndexSequence::default(); + let options = EncodingOptions::default(); + let strategy = test_encoding_strategy(TestEncoding::StructuralU16); + + let mut encoder = + create_test_field_encoder(strategy.as_ref(), &field, &mut column_index, &options) + .unwrap(); + + // Create test data with larger blobs + let large_data = vec![0u8; 1024 * 100]; // 100KB blob + let data: Vec> = + vec![Some(b"hello world"), None, Some(&large_data), Some(b"")]; + let array = Arc::new(LargeBinaryArray::from(data)); + + // Test encoding + let mut external_buffers = OutOfLineBuffers::new(0, 8); + let repdef = RepDefBuilder::default(); + + let tasks = encoder + .maybe_encode(array, &mut external_buffers, repdef, 0, 4) + .unwrap(); + + // If no tasks yet, flush to force encoding + if tasks.is_empty() { + let _flush_tasks = encoder.flush(&mut external_buffers).unwrap(); + } + + // Should produce encode tasks for the descriptor (or we need more data) + // For now, just verify no errors occurred + assert!(encoder.num_columns() > 0); + + // Verify external buffers were used for large data + let buffers = external_buffers.take_buffers(); + assert!( + !buffers.is_empty(), + "Large blobs should be stored in external buffers" + ); + } + + #[tokio::test] + async fn test_blob_round_trip() { + // Test round-trip encoding with blob metadata + let blob_metadata = + HashMap::from([(lance_arrow::BLOB_META_KEY.to_string(), "true".to_string())]); + + // Create test data + let val1: &[u8] = &vec![1u8; 1024]; // 1KB + let val2: &[u8] = &vec![2u8; 10240]; // 10KB + let val3: &[u8] = &vec![3u8; 102400]; // 100KB + let array = Arc::new(LargeBinaryArray::from(vec![ + Some(val1), + None, + Some(val2), + Some(val3), + ])); + + // Use the standard test harness + check_round_trip_encoding_of_data( + vec![array], + &TestCases::default().with_array_and_u16_encodings(), + blob_metadata, + ) + .await; + } + + #[tokio::test] + async fn test_blob_round_trip_empty_values() { + // Empty values share size == 0 with nulls in the descriptor layout + // and schedule no read; each must decode to zero-length bytes without + // consuming the read result of a following non-empty blob. Empties + // are placed before payloads so a misassignment corrupts the output + // instead of only exhausting the read iterator. + let blob_metadata = + HashMap::from([(lance_arrow::BLOB_META_KEY.to_string(), "true".to_string())]); + + let val1: &[u8] = &vec![1u8; 1024]; + let val2: &[u8] = &vec![2u8; 10240]; + let empty: &[u8] = &[]; + let array = Arc::new(LargeBinaryArray::from(vec![ + Some(empty), + Some(val1), + None, + Some(empty), + Some(val2), + None, + Some(empty), + ])); + + check_round_trip_encoding_of_data(vec![array], &TestCases::default(), blob_metadata).await; + } + + #[tokio::test] + async fn test_blob_round_trip_varying_chunk_nullability() { + let blob_metadata = + HashMap::from([(lance_arrow::BLOB_META_KEY.to_string(), "true".to_string())]); + let all_valid = Arc::new(LargeBinaryArray::from(vec![Some(b"first".as_ref())])); + let with_null = Arc::new(LargeBinaryArray::from(vec![ + Some(b"second".as_ref()), + None, + Some(b"".as_ref()), + ])); + let all_valid_again = Arc::new(LargeBinaryArray::from(vec![Some(b"last".as_ref())])); + + check_round_trip_encoding_of_data( + vec![all_valid, with_null, all_valid_again], + &TestCases::default().with_encoding(TestEncoding::StructuralU16), + blob_metadata, + ) + .await; + } + + #[tokio::test] + async fn test_blob_v2_external_round_trip() { + let blob_metadata = HashMap::from([( + lance_arrow::ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::BLOB_V2_EXT_NAME.to_string(), + )]); + + let kind_field = Arc::new(ArrowField::new("kind", DataType::UInt8, true)); + let data_field = Arc::new(ArrowField::new("data", DataType::LargeBinary, true)); + let uri_field = Arc::new(ArrowField::new("uri", DataType::Utf8, true)); + let blob_id_field = Arc::new(ArrowField::new("blob_id", DataType::UInt32, true)); + let blob_size_field = Arc::new(ArrowField::new("blob_size", DataType::UInt64, true)); + let position_field = Arc::new(ArrowField::new("position", DataType::UInt64, true)); + + let kind_array = UInt8Array::from(vec![ + BlobKind::Inline as u8, + BlobKind::External as u8, + BlobKind::External as u8, + ]); + let data_array = LargeBinaryArray::from(vec![Some(b"inline".as_ref()), None, None]); + let uri_array = StringArray::from(vec![ + None, + Some("file:///tmp/external.bin"), + Some("s3://bucket/blob"), + ]); + let blob_id_array = UInt32Array::from(vec![0, 0, 0]); + let blob_size_array = UInt64Array::from(vec![0, 0, 0]); + let position_array = UInt64Array::from(vec![0, 0, 0]); + + let struct_array = StructArray::from(vec![ + (kind_field, Arc::new(kind_array) as ArrayRef), + (data_field, Arc::new(data_array) as ArrayRef), + (uri_field, Arc::new(uri_array) as ArrayRef), + (blob_id_field, Arc::new(blob_id_array) as ArrayRef), + (blob_size_field, Arc::new(blob_size_array) as ArrayRef), + (position_field, Arc::new(position_array) as ArrayRef), + ]); + + let expected_descriptor = StructArray::from(vec![ + ( + Arc::new(ArrowField::new("kind", DataType::UInt8, false)), + Arc::new(UInt8Array::from(vec![ + BlobKind::Inline as u8, + BlobKind::External as u8, + BlobKind::External as u8, + ])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("position", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![0, 0, 0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("size", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![6, 0, 0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_id", DataType::UInt32, false)), + Arc::new(UInt32Array::from(vec![0, 0, 0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_uri", DataType::Utf8, false)), + Arc::new(StringArray::from(vec![ + "", + "file:///tmp/external.bin", + "s3://bucket/blob", + ])) as ArrayRef, + ), + ]); + + check_round_trip_encoding_of_data_with_expected( + vec![Arc::new(struct_array)], + Some(Arc::new(expected_descriptor)), + &TestCases::default().with_u32_structural_encodings(), + blob_metadata, + ) + .await; + } + + #[tokio::test] + async fn test_blob_v2_dedicated_round_trip() { + let blob_metadata = HashMap::from([( + lance_arrow::ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::BLOB_V2_EXT_NAME.to_string(), + )]); + + let kind_field = Arc::new(ArrowField::new("kind", DataType::UInt8, true)); + let data_field = Arc::new(ArrowField::new("data", DataType::LargeBinary, true)); + let uri_field = Arc::new(ArrowField::new("uri", DataType::Utf8, true)); + let blob_id_field = Arc::new(ArrowField::new("blob_id", DataType::UInt32, true)); + let blob_size_field = Arc::new(ArrowField::new("blob_size", DataType::UInt64, true)); + let position_field = Arc::new(ArrowField::new("position", DataType::UInt64, true)); + + let kind_array = UInt8Array::from(vec![BlobKind::Dedicated as u8, BlobKind::Inline as u8]); + let data_array = LargeBinaryArray::from(vec![None, Some(b"abc".as_ref())]); + let uri_array = StringArray::from(vec![Option::<&str>::None, None]); + let blob_id_array = UInt32Array::from(vec![42, 0]); + let blob_size_array = UInt64Array::from(vec![12, 0]); + let position_array = UInt64Array::from(vec![0, 0]); + + let struct_array = StructArray::from(vec![ + (kind_field, Arc::new(kind_array) as ArrayRef), + (data_field, Arc::new(data_array) as ArrayRef), + (uri_field, Arc::new(uri_array) as ArrayRef), + (blob_id_field, Arc::new(blob_id_array) as ArrayRef), + (blob_size_field, Arc::new(blob_size_array) as ArrayRef), + (position_field, Arc::new(position_array) as ArrayRef), + ]); + + let expected_descriptor = StructArray::from(vec![ + ( + Arc::new(ArrowField::new("kind", DataType::UInt8, false)), + Arc::new(UInt8Array::from(vec![ + BlobKind::Dedicated as u8, + BlobKind::Inline as u8, + ])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("position", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![0, 0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("size", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![12, 3])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_id", DataType::UInt32, false)), + Arc::new(UInt32Array::from(vec![42, 0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_uri", DataType::Utf8, false)), + Arc::new(StringArray::from(vec!["", ""])) as ArrayRef, + ), + ]); + + check_round_trip_encoding_of_data_with_expected( + vec![Arc::new(struct_array)], + Some(Arc::new(expected_descriptor)), + &TestCases::default().with_u32_structural_encodings(), + blob_metadata, + ) + .await; + } + + #[tokio::test] + async fn test_blob_v2_external_with_range_round_trip() { + let blob_metadata = HashMap::from([( + lance_arrow::ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::BLOB_V2_EXT_NAME.to_string(), + )]); + + let kind_field = Arc::new(ArrowField::new("kind", DataType::UInt8, true)); + let data_field = Arc::new(ArrowField::new("data", DataType::LargeBinary, true)); + let uri_field = Arc::new(ArrowField::new("uri", DataType::Utf8, true)); + let blob_id_field = Arc::new(ArrowField::new("blob_id", DataType::UInt32, true)); + let blob_size_field = Arc::new(ArrowField::new("blob_size", DataType::UInt64, true)); + let position_field = Arc::new(ArrowField::new("position", DataType::UInt64, true)); + + let kind_array = UInt8Array::from(vec![BlobKind::External as u8]); + let data_array = LargeBinaryArray::from(vec![None::<&[u8]>]); + let uri_array = StringArray::from(vec![Some("memory://container.pack")]); + let blob_id_array = UInt32Array::from(vec![0]); + let blob_size_array = UInt64Array::from(vec![42]); + let position_array = UInt64Array::from(vec![7]); + + let struct_array = StructArray::from(vec![ + (kind_field, Arc::new(kind_array) as ArrayRef), + (data_field, Arc::new(data_array) as ArrayRef), + (uri_field, Arc::new(uri_array) as ArrayRef), + (blob_id_field, Arc::new(blob_id_array) as ArrayRef), + (blob_size_field, Arc::new(blob_size_array) as ArrayRef), + (position_field, Arc::new(position_array) as ArrayRef), + ]); + + let expected_descriptor = StructArray::from(vec![ + ( + Arc::new(ArrowField::new("kind", DataType::UInt8, false)), + Arc::new(UInt8Array::from(vec![BlobKind::External as u8])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("position", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![7])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("size", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![42])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_id", DataType::UInt32, false)), + Arc::new(UInt32Array::from(vec![0])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_uri", DataType::Utf8, false)), + Arc::new(StringArray::from(vec!["memory://container.pack"])) as ArrayRef, + ), + ]); + + check_round_trip_encoding_of_data_with_expected( + vec![Arc::new(struct_array)], + Some(Arc::new(expected_descriptor)), + &TestCases::default().with_u32_structural_encodings(), + blob_metadata, + ) + .await; + } + + #[tokio::test] + async fn test_blob_v2_packed_round_trip() { + let blob_metadata = HashMap::from([( + lance_arrow::ARROW_EXT_NAME_KEY.to_string(), + lance_arrow::BLOB_V2_EXT_NAME.to_string(), + )]); + + let kind_field = Arc::new(ArrowField::new("kind", DataType::UInt8, true)); + let data_field = Arc::new(ArrowField::new("data", DataType::LargeBinary, true)); + let uri_field = Arc::new(ArrowField::new("uri", DataType::Utf8, true)); + let blob_id_field = Arc::new(ArrowField::new("blob_id", DataType::UInt32, true)); + let blob_size_field = Arc::new(ArrowField::new("blob_size", DataType::UInt64, true)); + let position_field = Arc::new(ArrowField::new("position", DataType::UInt64, true)); + + let kind_array = UInt8Array::from(vec![BlobKind::Packed as u8]); + let data_array = LargeBinaryArray::from(vec![None::<&[u8]>]); + let uri_array = StringArray::from(vec![None::<&str>]); + let blob_id_array = UInt32Array::from(vec![7]); + let blob_size_array = UInt64Array::from(vec![5]); + let position_array = UInt64Array::from(vec![10]); + + let struct_array = StructArray::from(vec![ + (kind_field, Arc::new(kind_array) as ArrayRef), + (data_field, Arc::new(data_array) as ArrayRef), + (uri_field, Arc::new(uri_array) as ArrayRef), + (blob_id_field, Arc::new(blob_id_array) as ArrayRef), + (blob_size_field, Arc::new(blob_size_array) as ArrayRef), + (position_field, Arc::new(position_array) as ArrayRef), + ]); + + let expected_descriptor = StructArray::from(vec![ + ( + Arc::new(ArrowField::new("kind", DataType::UInt8, false)), + Arc::new(UInt8Array::from(vec![BlobKind::Packed as u8])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("position", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![10])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("size", DataType::UInt64, false)), + Arc::new(UInt64Array::from(vec![5])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_id", DataType::UInt32, false)), + Arc::new(UInt32Array::from(vec![7])) as ArrayRef, + ), + ( + Arc::new(ArrowField::new("blob_uri", DataType::Utf8, false)), + Arc::new(StringArray::from(vec![""])) as ArrayRef, + ), + ]); + + check_round_trip_encoding_of_data_with_expected( + vec![Arc::new(struct_array)], + Some(Arc::new(expected_descriptor)), + &TestCases::default().with_u32_structural_encodings(), + blob_metadata, + ) + .await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/fixed_size_list.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/fixed_size_list.rs new file mode 100644 index 000000000..94d3702fd --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/fixed_size_list.rs @@ -0,0 +1,766 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Encoding support for complex FixedSizeList types (FSL with non-primitive children). +//! +//! Primitive FSL (e.g., `FixedSizeList`) is handled in the physical encoding layer. +//! This module handles FSL with complex children (Struct, Map, List) which require +//! structural encoding. + +use std::{ops::Range, sync::Arc}; + +use arrow_array::{Array, ArrayRef, GenericListArray, OffsetSizeTrait, StructArray, cast::AsArray}; +use arrow_buffer::{BooleanBufferBuilder, NullBuffer, OffsetBuffer, ScalarBuffer}; +use arrow_schema::DataType; +use futures::future::BoxFuture; +use lance_arrow::deepcopy::deep_copy_nulls; +use lance_core::{Error, Result}; + +use crate::{ + decoder::{ + DecodedArray, FilterExpression, ScheduledScanLine, SchedulerContext, + StructuralDecodeArrayTask, StructuralFieldDecoder, StructuralFieldScheduler, + StructuralSchedulingJob, + }, + encoder::{EncodeTask, FieldEncoder, OutOfLineBuffers}, + repdef::RepDefBuilder, +}; + +/// A structural encoder for complex fixed-size list fields +/// +/// The FSL's validity is added to the rep/def builder along with the dimension +/// and the FSL array's values are passed to the child encoder. +pub struct FixedSizeListStructuralEncoder { + keep_original_array: bool, + child: Box, +} + +impl FixedSizeListStructuralEncoder { + pub fn new(keep_original_array: bool, child: Box) -> Self { + Self { + keep_original_array, + child, + } + } +} + +impl FieldEncoder for FixedSizeListStructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let fsl_arr = array.as_fixed_size_list_opt().ok_or_else(|| { + Error::internal("FixedSizeList encoder used for non-fixed-size-list data".to_string()) + })?; + + let dimension = fsl_arr.value_length() as usize; + let values = fsl_arr.values().clone(); + + let validity = if self.keep_original_array { + array.nulls().cloned() + } else { + deep_copy_nulls(array.nulls()) + }; + repdef.add_fsl(validity.clone(), dimension, fsl_arr.len()); + + // FSL forces child elements to exist even under null rows. Normalize any + // nested lists under null FSL rows to null empty lists. + let values = if let Some(ref fsl_validity) = validity { + if needs_garbage_filtering(values.data_type()) { + let is_garbage = + expand_garbage_mask(&fsl_validity_to_garbage_mask(fsl_validity), dimension); + filter_fsl_child_garbage(values, &is_garbage) + } else { + values + } + } else { + values + }; + + self.child.maybe_encode( + values, + external_buffers, + repdef, + row_number, + num_rows * dimension as u64, + ) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.child.flush(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.child.num_columns() + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + self.child.finish(external_buffers) + } +} + +/// A scheduler for complex fixed-size list fields +/// +/// Scales row ranges by the FSL dimension when scheduling child rows, +/// and scales scheduled rows back when reporting to the parent. +#[derive(Debug)] +pub struct StructuralFixedSizeListScheduler { + child: Box, + dimension: u64, +} + +impl StructuralFixedSizeListScheduler { + pub fn new(child: Box, dimension: i32) -> Self { + Self { + child, + dimension: dimension as u64, + } + } +} + +impl StructuralFieldScheduler for StructuralFixedSizeListScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + // Scale ranges by dimension for the child - each FSL row becomes `dimension` child rows + let child_ranges: Vec> = ranges + .iter() + .map(|r| (r.start * self.dimension)..(r.end * self.dimension)) + .collect(); + let child = self.child.schedule_ranges(&child_ranges, filter)?; + Ok(Box::new(StructuralFixedSizeListSchedulingJob::new( + child, + self.dimension, + ))) + } + + fn initialize<'a>( + &'a mut self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + self.child.initialize(filter, context) + } +} + +#[derive(Debug)] +struct StructuralFixedSizeListSchedulingJob<'a> { + child: Box, + dimension: u64, +} + +impl<'a> StructuralFixedSizeListSchedulingJob<'a> { + fn new(child: Box, dimension: u64) -> Self { + Self { child, dimension } + } +} + +impl StructuralSchedulingJob for StructuralFixedSizeListSchedulingJob<'_> { + fn schedule_next(&mut self, context: &mut SchedulerContext) -> Result> { + // Get the child's scan lines (scheduled in terms of child struct rows) + let child_scan_lines = self.child.schedule_next(context)?; + + // Scale down rows_scheduled by dimension to convert from child rows to FSL rows + Ok(child_scan_lines + .into_iter() + .map(|scan_line| ScheduledScanLine { + decoders: scan_line.decoders, + rows_scheduled: scan_line.rows_scheduled / self.dimension, + }) + .collect()) + } +} + +/// A decoder for complex fixed-size list fields +/// +/// Drains `num_rows * dimension` from the child decoder and reconstructs +/// the FSL array with validity from the rep/def information. +#[derive(Debug)] +pub struct StructuralFixedSizeListDecoder { + child: Box, + data_type: DataType, +} + +impl StructuralFixedSizeListDecoder { + pub fn new(child: Box, data_type: DataType) -> Self { + Self { child, data_type } + } +} + +impl StructuralFieldDecoder for StructuralFixedSizeListDecoder { + fn accept_page(&mut self, child: crate::decoder::LoadedPageShard) -> Result<()> { + self.child.accept_page(child) + } + + fn drain(&mut self, num_rows: u64) -> Result> { + // For FixedSizeList, we need to drain num_rows * dimension from the child + let dimension = match &self.data_type { + DataType::FixedSizeList(_, d) => *d as u64, + _ => { + return Err(Error::internal( + "FixedSizeListDecoder has non-FSL data type".to_string(), + )); + } + }; + let child_task = self.child.drain(num_rows * dimension)?; + Ok(Box::new(StructuralFixedSizeListDecodeTask::new( + child_task, + self.data_type.clone(), + num_rows, + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +#[derive(Debug)] +struct StructuralFixedSizeListDecodeTask { + child_task: Box, + data_type: DataType, + num_rows: u64, +} + +impl StructuralFixedSizeListDecodeTask { + fn new( + child_task: Box, + data_type: DataType, + num_rows: u64, + ) -> Self { + Self { + child_task, + data_type, + num_rows, + } + } +} + +impl StructuralDecodeArrayTask for StructuralFixedSizeListDecodeTask { + fn decode(self: Box) -> Result { + let DecodedArray { + array, + mut repdef, + data_size, + } = self.child_task.decode()?; + match &self.data_type { + DataType::FixedSizeList(child_field, dimension) => { + let num_rows = self.num_rows as usize; + let validity = repdef.unravel_fsl_validity(num_rows, *dimension as usize)?; + let fsl_array = arrow_array::FixedSizeListArray::try_new( + child_field.clone(), + *dimension, + array, + validity, + )?; + Ok(DecodedArray { + array: Arc::new(fsl_array), + repdef, + data_size, + }) + } + _ => Err(Error::internal( + "FixedSizeList decoder did not have a fixed-size list field".to_string(), + )), + } + } +} + +// ======================= +// Garbage filtering +// ======================= + +/// Returns true if the data type contains any variable-length list-like types +/// (List, LargeList, ListView, LargeListView, Map) that need garbage filtering. +fn needs_garbage_filtering(data_type: &DataType) -> bool { + match data_type { + DataType::List(_) + | DataType::LargeList(_) + | DataType::ListView(_) + | DataType::LargeListView(_) + | DataType::Map(_, _) => true, + DataType::Struct(fields) => fields + .iter() + .any(|f| needs_garbage_filtering(f.data_type())), + DataType::FixedSizeList(field, _) => needs_garbage_filtering(field.data_type()), + _ => false, + } +} + +/// Filters garbage (undefined data under null FSL rows) from nested list-like types. +/// Unlike variable-length lists which can remove null children entirely, FSL children +/// always exist, so we must clean any nested lists before encoding. +/// +/// NB: Nested FSL is currently precluded at a higher level in our system. However, this code +/// supports and tests it. +fn filter_fsl_child_garbage(array: ArrayRef, is_garbage: &[bool]) -> ArrayRef { + debug_assert_eq!(array.len(), is_garbage.len()); + + match array.data_type() { + DataType::List(_) => filter_list_garbage(array.as_list::(), is_garbage), + DataType::LargeList(_) => filter_list_garbage(array.as_list::(), is_garbage), + DataType::ListView(_) | DataType::LargeListView(_) => { + unimplemented!("ListView inside complex FSL is not yet supported") + } + DataType::Map(_, _) => filter_map_garbage(array.as_map(), is_garbage), + DataType::FixedSizeList(_, dim) => { + filter_nested_fsl_garbage(array.as_fixed_size_list(), is_garbage, *dim as usize) + } + DataType::Struct(_) => filter_struct_garbage(array.as_struct(), is_garbage), + _ => array, + } +} + +fn filter_struct_garbage(struct_arr: &StructArray, is_garbage: &[bool]) -> ArrayRef { + let needs_filtering = struct_arr + .fields() + .iter() + .any(|f| needs_garbage_filtering(f.data_type())); + + if !needs_filtering { + return Arc::new(struct_arr.clone()); + } + + let new_columns: Vec = struct_arr + .columns() + .iter() + .zip(struct_arr.fields().iter()) + .map(|(col, field)| { + if needs_garbage_filtering(field.data_type()) { + filter_fsl_child_garbage(col.clone(), is_garbage) + } else { + col.clone() + } + }) + .collect(); + + Arc::new(StructArray::new( + struct_arr.fields().clone(), + new_columns, + struct_arr.nulls().cloned(), + )) +} + +fn expand_garbage_mask(is_garbage: &[bool], dimension: usize) -> Vec { + let mut expanded = Vec::with_capacity(is_garbage.len() * dimension); + for &garbage in is_garbage { + for _ in 0..dimension { + expanded.push(garbage); + } + } + expanded +} + +fn fsl_validity_to_garbage_mask(fsl_validity: &NullBuffer) -> Vec { + fsl_validity.iter().map(|valid| !valid).collect() +} + +fn filter_list_garbage( + list_arr: &GenericListArray, + is_garbage: &[bool], +) -> ArrayRef { + debug_assert_eq!( + list_arr.len(), + is_garbage.len(), + "list length must match garbage mask length" + ); + + let old_offsets = list_arr.offsets(); + let value_field = match list_arr.data_type() { + DataType::List(f) | DataType::LargeList(f) => f.clone(), + _ => unreachable!(), + }; + + let mut new_offsets: Vec = Vec::with_capacity(list_arr.len() + 1); + let mut values_to_keep: Vec = Vec::new(); + let mut validity_builder = BooleanBufferBuilder::new(list_arr.len()); + let mut current_offset = O::usize_as(0); + new_offsets.push(current_offset); + let old_validity = list_arr.nulls(); + + for (i, &garbage) in is_garbage.iter().enumerate() { + if garbage { + new_offsets.push(current_offset); + validity_builder.append(false); + } else { + let start = old_offsets[i].as_usize(); + let end = old_offsets[i + 1].as_usize(); + values_to_keep.extend(start..end); + current_offset += O::usize_as(end - start); + new_offsets.push(current_offset); + validity_builder.append(old_validity.map(|v| v.is_valid(i)).unwrap_or(true)); + } + } + + let new_values = if values_to_keep.is_empty() { + list_arr.values().slice(0, 0) + } else { + let indices = + arrow_array::UInt64Array::from_iter_values(values_to_keep.iter().map(|&i| i as u64)); + arrow_select::take::take(list_arr.values().as_ref(), &indices, None) + .expect("take should succeed") + }; + + let new_values = if needs_garbage_filtering(value_field.data_type()) && !new_values.is_empty() { + let len = new_values.len(); + filter_fsl_child_garbage(new_values, &vec![false; len]) + } else { + new_values + }; + + let new_validity = NullBuffer::new(validity_builder.finish()); + Arc::new(GenericListArray::new( + value_field, + OffsetBuffer::new(ScalarBuffer::from(new_offsets)), + new_values, + Some(new_validity), + )) +} + +fn filter_map_garbage(map_arr: &arrow_array::MapArray, is_garbage: &[bool]) -> ArrayRef { + debug_assert_eq!(map_arr.len(), is_garbage.len()); + + let old_offsets = map_arr.offsets(); + let entries_field = match map_arr.data_type() { + DataType::Map(field, _) => field.clone(), + _ => unreachable!(), + }; + + let mut new_offsets: Vec = Vec::with_capacity(map_arr.len() + 1); + let mut values_to_keep: Vec = Vec::new(); + let mut validity_builder = BooleanBufferBuilder::new(map_arr.len()); + let mut current_offset: i32 = 0; + new_offsets.push(current_offset); + let old_validity = map_arr.nulls(); + + for (i, &garbage) in is_garbage.iter().enumerate() { + if garbage { + new_offsets.push(current_offset); + validity_builder.append(false); + } else { + let start = old_offsets[i] as usize; + let end = old_offsets[i + 1] as usize; + values_to_keep.extend(start..end); + current_offset += (end - start) as i32; + new_offsets.push(current_offset); + validity_builder.append(old_validity.map(|v| v.is_valid(i)).unwrap_or(true)); + } + } + + let new_entries: ArrayRef = if values_to_keep.is_empty() { + Arc::new(map_arr.entries().slice(0, 0)) + } else { + let indices = + arrow_array::UInt64Array::from_iter_values(values_to_keep.iter().map(|&i| i as u64)); + arrow_select::take::take(map_arr.entries(), &indices, None).expect("take should succeed") + }; + + let new_entries = + if needs_garbage_filtering(entries_field.data_type()) && !new_entries.is_empty() { + let len = new_entries.len(); + filter_fsl_child_garbage(new_entries, &vec![false; len]) + } else { + new_entries + }; + + let new_validity = NullBuffer::new(validity_builder.finish()); + let keys_sorted = matches!(map_arr.data_type(), DataType::Map(_, true)); + + Arc::new( + arrow_array::MapArray::try_new( + entries_field, + OffsetBuffer::new(ScalarBuffer::from(new_offsets)), + new_entries.as_struct().clone(), + Some(new_validity), + keys_sorted, + ) + .expect("MapArray construction should succeed"), + ) +} + +/// Filters garbage from nested FSL arrays that contain list-like children. +fn filter_nested_fsl_garbage( + fsl_arr: &arrow_array::FixedSizeListArray, + is_garbage: &[bool], + dimension: usize, +) -> ArrayRef { + debug_assert_eq!(fsl_arr.len(), is_garbage.len()); + + let child_field = match fsl_arr.data_type() { + DataType::FixedSizeList(field, _) => field.clone(), + _ => unreachable!(), + }; + + if !needs_garbage_filtering(child_field.data_type()) { + return Arc::new(fsl_arr.clone()); + } + + let child_garbage = expand_garbage_mask(is_garbage, dimension); + let new_values = filter_fsl_child_garbage(fsl_arr.values().clone(), &child_garbage); + + Arc::new(arrow_array::FixedSizeListArray::new( + child_field, + dimension as i32, + new_values, + fsl_arr.nulls().cloned(), + )) +} + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{ + Array, FixedSizeListArray, + builder::{Int32Builder, ListBuilder}, + cast::AsArray, + }; + use arrow_schema::{DataType, Field, Fields}; + use rstest::rstest; + + use super::filter_nested_fsl_garbage; + use crate::{ + constants::{ + STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_META_KEY, + STRUCTURAL_ENCODING_MINIBLOCK, + }, + testing::{TestCases, check_specific_random}, + }; + + fn make_fsl_struct_type(struct_fields: Fields, dimension: i32) -> DataType { + DataType::FixedSizeList( + Arc::new(Field::new("item", DataType::Struct(struct_fields), true)), + dimension, + ) + } + + fn simple_struct_fields() -> Fields { + Fields::from(vec![ + Field::new("x", DataType::Float64, false), + Field::new("y", DataType::Float64, false), + ]) + } + + fn nested_struct_fields() -> Fields { + let inner = Fields::from(vec![ + Field::new("a", DataType::Int32, false), + Field::new("b", DataType::Int32, false), + ]); + Fields::from(vec![ + Field::new("outer_val", DataType::Float64, false), + Field::new("inner", DataType::Struct(inner), true), + ]) + } + + fn nested_struct_with_list_fields() -> Fields { + let inner = Fields::from(vec![Field::new( + "values", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + )]); + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new("inner", DataType::Struct(inner), true), + ]) + } + + fn struct_with_list_fields() -> Fields { + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "values", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + ), + ]) + } + + fn struct_with_large_list_fields() -> Fields { + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "values", + DataType::LargeList(Arc::new(Field::new("item", DataType::Int64, true))), + true, + ), + ]) + } + + fn struct_with_nested_fsl_fields() -> Fields { + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "vectors", + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float32, true)), 4), + true, + ), + ]) + } + + fn struct_with_map_fields() -> Fields { + let entries_field = Arc::new(Field::new( + "entries", + DataType::Struct(Fields::from(vec![ + Field::new("keys", DataType::Utf8, false), + Field::new("values", DataType::Int32, true), + ])), + false, + )); + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new("props", DataType::Map(entries_field, false), true), + ]) + } + + fn make_fsl_of_list() -> DataType { + DataType::FixedSizeList( + Arc::new(Field::new( + "item", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + )), + 2, + ) + } + + fn make_fsl_of_large_list() -> DataType { + DataType::FixedSizeList( + Arc::new(Field::new( + "item", + DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true))), + true, + )), + 2, + ) + } + + fn make_fsl_of_map() -> DataType { + DataType::FixedSizeList( + Arc::new(Field::new( + "item", + DataType::Map( + Arc::new(Field::new( + "entries", + DataType::Struct(Fields::from(vec![ + Field::new("key", DataType::Utf8, false), + Field::new("value", DataType::Int32, true), + ])), + false, + )), + false, + ), + true, + )), + 2, + ) + } + + fn make_fsl_of_nested_fsl_struct() -> DataType { + DataType::FixedSizeList( + Arc::new(Field::new( + "item", + DataType::FixedSizeList( + Arc::new(Field::new( + "item", + DataType::Struct(Fields::from(vec![Field::new( + "x", + DataType::Int32, + true, + )])), + true, + )), + 4, + ), + true, + )), + 2, + ) + } + + #[rstest] + #[case::simple(simple_struct_fields(), 2)] + #[case::nested_struct(nested_struct_fields(), 2)] + #[case::struct_with_list(struct_with_list_fields(), 2)] + #[case::struct_with_large_list(struct_with_large_list_fields(), 2)] + #[case::nested_struct_with_list(nested_struct_with_list_fields(), 2)] + #[case::struct_with_nested_fsl(struct_with_nested_fsl_fields(), 2)] + #[case::struct_with_map(struct_with_map_fields(), 2)] + #[test_log::test(tokio::test)] + async fn test_fsl_struct_random( + #[case] struct_fields: Fields, + #[case] dimension: i32, + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let data_type = make_fsl_struct_type(struct_fields, dimension); + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + let field = Field::new("", data_type, true).with_metadata(field_metadata); + let test_cases = TestCases::basic().with_u32_structural_encodings(); + check_specific_random(field, test_cases).await; + } + + #[rstest] + #[case::list(make_fsl_of_list())] + #[case::large_list(make_fsl_of_large_list())] + #[case::map(make_fsl_of_map())] + #[case::nested_fsl_struct(make_fsl_of_nested_fsl_struct())] + fn test_unsupported_fsl_child_types_return_error(#[case] data_type: DataType) { + let arrow_field = Field::new("test", data_type, true); + let err = lance_core::datatypes::Field::try_from(&arrow_field).unwrap_err(); + assert!(err.to_string().contains("Unsupported data type")); + } + + #[test] + fn test_filter_nested_fsl_garbage() { + // Create FSL> with dimension 2: [[[1], [2]], [[3], [4]], [[5], [6]]] + let mut list_builder = ListBuilder::new(Int32Builder::new()); + for i in 1..=6 { + list_builder.values().append_value(i); + list_builder.append(true); + } + let list_arr = list_builder.finish(); + + let fsl_field = Arc::new(Field::new( + "item", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + )); + let fsl = FixedSizeListArray::new(fsl_field, 2, Arc::new(list_arr), None); + + // Mark second FSL row as garbage + let result = filter_nested_fsl_garbage(&fsl, &[false, true, false], 2); + let result = result.as_fixed_size_list(); + + // Child lists at positions 2,3 (garbage row 1) should be filtered to null + let child_list = result.values().as_list::(); + assert_eq!( + (0..6).map(|i| child_list.is_valid(i)).collect::>(), + vec![true, true, false, false, true, true] + ); + } + + #[test] + fn test_filter_nested_fsl_no_list_child() { + // FSL - no list child, should return unchanged + let fsl_field = Arc::new(Field::new("item", DataType::Int32, true)); + let values = arrow_array::Int32Array::from(vec![1, 2, 3, 4, 5, 6]); + let fsl = FixedSizeListArray::new(fsl_field, 2, Arc::new(values), None); + + let result = filter_nested_fsl_garbage(&fsl, &[false, true, false], 2); + // Should return the same array unchanged + assert_eq!(result.len(), 3); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/list.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/list.rs new file mode 100644 index 000000000..b79b651e6 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/list.rs @@ -0,0 +1,1444 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ops::Range, sync::Arc}; + +use arrow_array::{Array, ArrayRef, LargeListArray, ListArray, cast::AsArray, make_array}; +use arrow_schema::DataType; +use futures::future::BoxFuture; +use lance_arrow::deepcopy::deep_copy_nulls; +use lance_arrow::list::ListArrayExt; +use lance_core::Result; + +use crate::{ + decoder::{ + DecodedArray, FilterExpression, ScheduledScanLine, SchedulerContext, + StructuralDecodeArrayTask, StructuralFieldDecoder, StructuralFieldScheduler, + StructuralSchedulingJob, + }, + encoder::{EncodeTask, FieldEncoder, OutOfLineBuffers}, + repdef::RepDefBuilder, +}; + +/// A structural encoder for list fields +/// +/// The list's offsets are added to the rep/def builder +/// and the list array's values are passed to the child encoder +/// +/// The values will have any garbage values removed and will be trimmed +/// to only include the values that are actually used. +pub struct ListStructuralEncoder { + keep_original_array: bool, + child: Box, +} + +impl ListStructuralEncoder { + pub fn new(keep_original_array: bool, child: Box) -> Self { + Self { + keep_original_array, + child, + } + } +} + +impl FieldEncoder for ListStructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let values = if let Some(list_arr) = array.as_list_opt::() { + let has_garbage_values = if self.keep_original_array { + repdef.add_offsets(list_arr.offsets().clone(), array.nulls().cloned()) + } else { + // there is no need to deep copy offsets, because offset buffers will be cast to a common type (i64). + repdef.add_offsets(list_arr.offsets().clone(), deep_copy_nulls(array.nulls())) + }; + if has_garbage_values { + list_arr.filter_garbage_nulls().trimmed_values() + } else { + list_arr.trimmed_values() + } + } else if let Some(list_arr) = array.as_list_opt::() { + let has_garbage_values = if self.keep_original_array { + repdef.add_offsets(list_arr.offsets().clone(), array.nulls().cloned()) + } else { + repdef.add_offsets(list_arr.offsets().clone(), deep_copy_nulls(array.nulls())) + }; + if has_garbage_values { + list_arr.filter_garbage_nulls().trimmed_values() + } else { + list_arr.trimmed_values() + } + } else { + panic!("List encoder used for non-list data") + }; + self.child + .maybe_encode(values, external_buffers, repdef, row_number, num_rows) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.child.flush(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.child.num_columns() + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + self.child.finish(external_buffers) + } +} + +#[derive(Debug)] +pub struct StructuralListScheduler { + child: Box, +} + +impl StructuralListScheduler { + pub fn new(child: Box) -> Self { + Self { child } + } +} + +impl StructuralFieldScheduler for StructuralListScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + let child = self.child.schedule_ranges(ranges, filter)?; + + Ok(Box::new(StructuralListSchedulingJob::new(child))) + } + + fn initialize<'a>( + &'a mut self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + self.child.initialize(filter, context) + } +} + +/// Scheduling job for list data +/// +/// Scheduling is handled by the primitive encoder and nothing special +/// happens here. +#[derive(Debug)] +struct StructuralListSchedulingJob<'a> { + child: Box, +} + +impl<'a> StructuralListSchedulingJob<'a> { + fn new(child: Box) -> Self { + Self { child } + } +} + +impl StructuralSchedulingJob for StructuralListSchedulingJob<'_> { + fn schedule_next(&mut self, context: &mut SchedulerContext) -> Result> { + self.child.schedule_next(context) + } +} + +#[derive(Debug)] +pub struct StructuralListDecoder { + child: Box, + data_type: DataType, +} + +impl StructuralListDecoder { + pub fn new(child: Box, data_type: DataType) -> Self { + Self { child, data_type } + } +} + +impl StructuralFieldDecoder for StructuralListDecoder { + fn accept_page(&mut self, child: crate::decoder::LoadedPageShard) -> Result<()> { + self.child.accept_page(child) + } + + fn drain(&mut self, num_rows: u64) -> Result> { + let child_task = self.child.drain(num_rows)?; + Ok(Box::new(StructuralListDecodeTask::new( + child_task, + self.data_type.clone(), + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +#[derive(Debug)] +struct StructuralListDecodeTask { + child_task: Box, + data_type: DataType, +} + +impl StructuralListDecodeTask { + fn new(child_task: Box, data_type: DataType) -> Self { + Self { + child_task, + data_type, + } + } +} + +impl StructuralDecodeArrayTask for StructuralListDecodeTask { + fn decode(self: Box) -> Result { + let DecodedArray { + array, + mut repdef, + data_size, + } = self.child_task.decode()?; + match &self.data_type { + DataType::List(child_field) => { + let (offsets, validity) = repdef.unravel_offsets::()?; + let array = if !child_field.is_nullable() && array.null_count() == array.len() { + make_array(array.into_data().into_builder().nulls(None).build()?) + } else { + array + }; + let list_array = ListArray::try_new(child_field.clone(), offsets, array, validity)?; + + Ok(DecodedArray { + array: Arc::new(list_array), + repdef, + data_size, + }) + } + DataType::LargeList(child_field) => { + let (offsets, validity) = repdef.unravel_offsets::()?; + let list_array = + LargeListArray::try_new(child_field.clone(), offsets, array, validity)?; + Ok(DecodedArray { + array: Arc::new(list_array), + repdef, + data_size, + }) + } + _ => panic!("List decoder did not have a list field"), + } + } +} + +#[cfg(test)] +mod tests { + + use std::{collections::HashMap, sync::Arc}; + + use crate::constants::{ + STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_META_KEY, STRUCTURAL_ENCODING_MINIBLOCK, + }; + use arrow_array::{ + Array, ArrayRef, BooleanArray, DictionaryArray, LargeStringArray, ListArray, StructArray, + UInt8Array, UInt64Array, + builder::{ + Int32Builder, Int64Builder, LargeListBuilder, ListBuilder, StringBuilder, UInt32Builder, + }, + }; + + use arrow_buffer::{BooleanBuffer, NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field, Fields}; + use rstest::rstest; + + use crate::testing::{ + TestCases, TestEncoding, check_basic_random, check_round_trip_encoding_of_data, + create_test_field_encoder, test_encoding_strategy, + }; + + fn make_list_type(inner_type: DataType) -> DataType { + DataType::List(Arc::new(Field::new("item", inner_type, true))) + } + + fn make_large_list_type(inner_type: DataType) -> DataType { + DataType::LargeList(Arc::new(Field::new("item", inner_type, true))) + } + + async fn try_encode_v22_pages( + array: ArrayRef, + ) -> lance_core::Result> { + try_encode_v22_pages_with_metadata(array, HashMap::new()).await + } + + async fn try_encode_v22_pages_with_metadata( + array: ArrayRef, + field_metadata: HashMap, + ) -> lance_core::Result> { + let arrow_field = + Field::new("", array.data_type().clone(), true).with_metadata(field_metadata); + let lance_field = lance_core::datatypes::Field::try_from(&arrow_field).unwrap(); + let encoding_strategy = test_encoding_strategy(TestEncoding::StructuralU32); + let mut column_index_seq = crate::encoder::ColumnIndexSequence::default(); + let encoding_options = crate::encoder::EncodingOptions::default(); + let mut encoder = create_test_field_encoder( + encoding_strategy.as_ref(), + &lance_field, + &mut column_index_seq, + &encoding_options, + ) + .unwrap(); + let mut external_buffers = + crate::encoder::OutOfLineBuffers::new(0, crate::encoder::MIN_PAGE_BUFFER_ALIGNMENT); + let num_rows = array.len() as u64; + let mut pages = Vec::new(); + for task in encoder + .maybe_encode( + array, + &mut external_buffers, + crate::repdef::RepDefBuilder::default(), + 0, + num_rows, + ) + .unwrap() + { + pages.push(task.await?); + } + for task in encoder.flush(&mut external_buffers).unwrap() { + pages.push(task.await?); + } + Ok(pages) + } + + async fn encode_v22_pages(array: ArrayRef) -> Vec { + try_encode_v22_pages(array).await.unwrap() + } + + fn assert_split_miniblock_layout( + pages: &[crate::encoder::EncodedPage], + expect_structural_only_page: bool, + ) { + let mut miniblock_pages = 0; + let mut fullzip_pages = 0; + let mut structural_only_pages = 0; + + for page in pages { + let crate::decoder::PageEncoding::Structural(layout) = &page.description else { + continue; + }; + match layout.layout.as_ref().unwrap() { + crate::format::pb21::page_layout::Layout::MiniBlockLayout(_) => { + miniblock_pages += 1; + } + crate::format::pb21::page_layout::Layout::FullZipLayout(_) => { + fullzip_pages += 1; + } + crate::format::pb21::page_layout::Layout::ConstantLayout(layout) => { + if layout.inline_value.is_none() + && (layout.num_rep_values > 0 || layout.num_def_values > 0) + { + structural_only_pages += 1; + } + } + crate::format::pb21::page_layout::Layout::BlobLayout(_) => {} + crate::format::pb21::page_layout::Layout::SparseLayout(_) => {} + } + } + + assert!( + miniblock_pages > 0, + "expected leaf values to remain on mini-block pages" + ); + assert_eq!( + fullzip_pages, 0, + "split list pages should not fall back to full-zip" + ); + if expect_structural_only_page { + assert!( + structural_only_pages > 0, + "expected at least one structural-only page" + ); + } + } + + fn assert_has_fullzip_layout(pages: &[crate::encoder::EncodedPage]) { + let has_fullzip = pages.iter().any(|page| { + let crate::decoder::PageEncoding::Structural(layout) = &page.description else { + return false; + }; + matches!( + layout.layout.as_ref().unwrap(), + crate::format::pb21::page_layout::Layout::FullZipLayout(_) + ) + }); + assert!(has_fullzip, "expected at least one full-zip page"); + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + let field = + Field::new("", make_list_type(DataType::Int32), true).with_metadata(field_metadata); + check_basic_random(field).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_deeply_nested_lists( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + let field = Field::new("item", DataType::Int32, true).with_metadata(field_metadata); + for _ in 0..5 { + let field = Field::new("", make_list_type(field.data_type().clone()), true); + check_basic_random(field).await; + } + } + + #[test_log::test(tokio::test)] + async fn test_large_list() { + let field = Field::new("", make_large_list_type(DataType::Int32), true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_nested_strings() { + let field = Field::new("", make_list_type(DataType::Utf8), true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_nested_list() { + let field = Field::new("", make_list_type(make_list_type(DataType::Int32)), true); + check_basic_random(field).await; + } + + /// Regression test: a `List>` column written as MULTIPLE + /// batches (chunks) whose flattened leaf values cross a value-page boundary + /// fails to decode with "Max offset N exceeds length of values M" (Arrow + /// error raised by `ListArray::try_new` in `StructuralListDecodeTask::decode`). + /// + /// The trigger (verified against the production file and pylance 7.0.0b12 / + /// 7.0.0 / 9.0.0-beta.10) requires ALL of: + /// 1. >= 2 list layers (`List>`), + /// 2. a leaf large enough to be chunked into multiple value pages, + /// 3. the column written as more than one batch. + /// A single batch of the identical data round-trips fine — which is why the + /// earlier single-chunk version of this test (and the small `test_nested_list` + /// cases) did not catch it. Found in production on the gaming TransNet + /// `dino_embedding_per_frame` column (rectangular 3 x 768 float per row). + /// + /// Each element of the `vec![..]` passed to `check_round_trip_encoding_of_data` + /// is encoded as a separate batch (its own `RepDefBuilder`), so we split the + /// rows into two chunks to exercise the multi-batch repdef accumulation path. + #[rstest] + #[test_log::test(tokio::test)] + async fn test_multipage_nested_float_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + use arrow_array::Float32Array; + + // Production shape: 3 inner lists per row, 768 floats each. + let inner_per_row: usize = 3; + let inner_len: usize = 768; + // Two chunks (batches) -> two pages; a read batch that spans the page + // boundary is where the multi-page outer-offset bug triggered. A single + // [2731] chunk (one page) decodes fine, which is why this needs >= 2. + let chunk_rows: &[usize] = &[1366, 1365]; + + let make_chunk = |start_row: usize, num_rows: usize| -> Arc { + let total_inner = num_rows * inner_per_row; + let total_values = total_inner * inner_len; + let values = Float32Array::from( + (0..total_values) + .map(|i| (start_row + i) as f32) + .collect::>(), + ); + let inner_offsets = ScalarBuffer::::from( + (0..=total_inner) + .map(|i| (i * inner_len) as i32) + .collect::>(), + ); + let inner_list = ListArray::new( + Arc::new(Field::new("item", DataType::Float32, true)), + OffsetBuffer::new(inner_offsets), + Arc::new(values), + None, + ); + let outer_offsets = ScalarBuffer::::from( + (0..=num_rows) + .map(|i| (i * inner_per_row) as i32) + .collect::>(), + ); + Arc::new(ListArray::new( + Arc::new(Field::new( + "item", + DataType::List(Arc::new(Field::new("item", DataType::Float32, true))), + true, + )), + OffsetBuffer::new(outer_offsets), + Arc::new(inner_list), + None, + )) + }; + + let mut start = 0; + let chunks: Vec> = chunk_rows + .iter() + .map(|&n| { + let c = make_chunk(start, n); + start += n; + c + }) + .collect(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default().with_structural_encodings(); + check_round_trip_encoding_of_data(chunks, &test_cases, field_metadata).await; + } + + #[test_log::test(tokio::test)] + async fn test_list_struct_list() { + let struct_type = DataType::Struct(Fields::from(vec![Field::new( + "inner_str", + DataType::Utf8, + false, + )])); + + let field = Field::new("", make_list_type(struct_type), true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_list_struct_empty() { + let fields = Fields::from(vec![Field::new("inner", DataType::UInt64, true)]); + let items = UInt64Array::from(Vec::::new()); + let structs = StructArray::new(fields, vec![Arc::new(items)], None); + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0; 2 * 1024 * 1024 + 1])); + let lists = ListArray::new( + Arc::new(Field::new("item", structs.data_type().clone(), true)), + offsets, + Arc::new(structs), + None, + ); + + check_round_trip_encoding_of_data( + vec![Arc::new(lists)], + &TestCases::default(), + HashMap::new(), + ) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_value([Some(1), Some(2), Some(3)]); + list_builder.append_value([Some(4), Some(5)]); + list_builder.append_null(); + list_builder.append_value([Some(6), Some(7), Some(8)]); + let list_array = list_builder.finish(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![1, 3]) + .with_indices(vec![2]); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, field_metadata) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_nested_list_ends_with_null( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + use arrow_array::Int32Array; + + let values = Int32Array::from(vec![1, 2, 3, 4, 5]); + let inner_offsets = ScalarBuffer::::from(vec![0, 1, 2, 3, 4, 5, 5]); + let inner_validity = BooleanBuffer::from(vec![true, true, true, true, true, false]); + let outer_offsets = ScalarBuffer::::from(vec![0, 1, 2, 3, 4, 5, 6, 6]); + let outer_validity = BooleanBuffer::from(vec![true, true, true, true, true, true, false]); + + let inner_list = ListArray::new( + Arc::new(Field::new("item", DataType::Int32, true)), + OffsetBuffer::new(inner_offsets), + Arc::new(values), + Some(NullBuffer::new(inner_validity)), + ); + let outer_list = ListArray::new( + Arc::new(Field::new( + "item", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + )), + OffsetBuffer::new(outer_offsets), + Arc::new(inner_list), + Some(NullBuffer::new(outer_validity)), + ); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(5..7) + .with_indices(vec![1, 6]) + .with_indices(vec![6]) + .with_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(outer_list)], &test_cases, field_metadata) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_string_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let items_builder = StringBuilder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_value([Some("a"), Some("bc"), Some("def")]); + list_builder.append_value([Some("gh"), None]); + list_builder.append_null(); + list_builder.append_value([Some("ijk"), Some("lmnop"), Some("qrs")]); + let list_array = list_builder.finish(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![1, 3]) + .with_indices(vec![2]) + .with_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, field_metadata) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_string_list_no_null( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let items_builder = StringBuilder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_value([Some("a"), Some("bc"), Some("def")]); + list_builder.append_value([Some("gh"), Some("zxy")]); + list_builder.append_value([Some("gh"), Some("z")]); + list_builder.append_value([Some("ijk"), Some("lmnop"), Some("qrs")]); + let list_array = list_builder.finish(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![1, 3]) + .with_indices(vec![2]) + .with_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, field_metadata) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_sliced_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_value([Some(1), Some(2), Some(3)]); + list_builder.append_value([Some(4), Some(5)]); + list_builder.append_null(); + list_builder.append_value([Some(6), Some(7), Some(8)]); + let list_array = list_builder.finish(); + + let list_array = list_array.slice(1, 2); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(1..2) + .with_indices(vec![0]) + .with_indices(vec![1]) + .with_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, field_metadata) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_list_dict() { + let values = LargeStringArray::from_iter_values(["a", "bb", "ccc"]); + let indices = UInt8Array::from(vec![0, 1, 2, 0, 1, 2, 0, 1, 2]); + let dict_array = DictionaryArray::new(indices, Arc::new(values)); + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 3, 5, 6, 9])); + let list_array = ListArray::new( + Arc::new(Field::new("item", dict_array.data_type().clone(), true)), + offsets, + Arc::new(dict_array), + None, + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(1..3) + .with_range(2..4) + .with_indices(vec![1]) + .with_indices(vec![2]); + check_round_trip_encoding_of_data( + vec![Arc::new(list_array)], + &test_cases, + HashMap::default(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_list_all_null() { + let items = UInt64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + let offsets = ScalarBuffer::::from(vec![0, 5, 8, 10]); + let offsets = OffsetBuffer::new(offsets); + let list_validity = NullBuffer::new(BooleanBuffer::from(vec![false, false, false])); + + // The list array is nullable but the items are not. Then, all lists are null. + let list_arr = ListArray::new( + Arc::new(Field::new("item", DataType::UInt64, false)), + offsets, + Arc::new(items), + Some(list_validity), + ); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(1..2) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_structural_encodings(); + check_round_trip_encoding_of_data( + vec![Arc::new(list_arr)], + &test_cases, + HashMap::default(), + ) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_list_with_garbage_nulls( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + // In Arrow, list nulls are allowed to be non-empty, with masked garbage values + // Here we make a list with a null row in the middle with 3 garbage values + let items = UInt64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + let offsets = ScalarBuffer::::from(vec![0, 5, 8, 10]); + let offsets = OffsetBuffer::new(offsets); + let list_validity = NullBuffer::new(BooleanBuffer::from(vec![true, false, true])); + let list_arr = ListArray::new( + Arc::new(Field::new("item", DataType::UInt64, true)), + offsets, + Arc::new(items), + Some(list_validity), + ); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(1..2) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_arr)], &test_cases, field_metadata) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_two_page_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + // This is a simple pre-defined list that spans two pages. This test is useful for + // debugging the repetition index + + let items_builder = Int64Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + for i in 0..512 { + list_builder.append_value([Some(i), Some(i * 2)]); + } + let list_array_1 = list_builder.finish(); + + let items_builder = Int64Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + for i in 0..512 { + let i = i + 512; + list_builder.append_value([Some(i), Some(i * 2)]); + } + let list_array_2 = list_builder.finish(); + + let mut metadata = HashMap::new(); + metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_page_sizes(vec![100]) + .with_range(800..900); + check_round_trip_encoding_of_data( + vec![Arc::new(list_array_1), Arc::new(list_array_2)], + &test_cases, + metadata, + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_large_list() { + let items_builder = Int32Builder::new(); + let mut list_builder = LargeListBuilder::new(items_builder); + list_builder.append_value([Some(1), Some(2), Some(3)]); + list_builder.append_value([Some(4), Some(5)]); + list_builder.append_null(); + list_builder.append_value([Some(6), Some(7), Some(8)]); + let list_array = list_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![1, 3]); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, HashMap::new()) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_empty_lists( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + // Scenario 1: Some lists are empty + + let values = [vec![Some(1), Some(2), Some(3)], vec![], vec![None]]; + // Test empty list at beginning, middle, and end + for order in [[0, 1, 2], [1, 0, 2], [2, 0, 1]] { + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + for idx in order { + list_builder.append_value(values[idx].clone()); + } + let list_array = Arc::new(list_builder.finish()); + let test_cases = TestCases::default() + .with_indices(vec![1]) + .with_indices(vec![0]) + .with_indices(vec![2]) + .with_indices(vec![0, 1]); + check_round_trip_encoding_of_data( + vec![list_array.clone()], + &test_cases, + field_metadata.clone(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data( + vec![list_array], + &test_cases, + field_metadata.clone(), + ) + .await; + } + + // Scenario 2: All lists are empty + + // When encoding a list of empty lists there are no items to encode + // which is strange and we want to ensure we handle it + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append(true); + list_builder.append_null(); + list_builder.append(true); + let list_array = Arc::new(list_builder.finish()); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data( + vec![list_array.clone()], + &test_cases, + field_metadata.clone(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, field_metadata.clone()) + .await; + + // Scenario 2B: All lists are empty (but now with strings) + + // When encoding a list of empty lists there are no items to encode + // which is strange and we want to ensure we handle it + let items_builder = StringBuilder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append(true); + list_builder.append_null(); + list_builder.append(true); + let list_array = Arc::new(list_builder.finish()); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data( + vec![list_array.clone()], + &test_cases, + field_metadata.clone(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, field_metadata.clone()) + .await; + + // Scenario 3: All lists are null + + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_null(); + list_builder.append_null(); + list_builder.append_null(); + let list_array = Arc::new(list_builder.finish()); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data( + vec![list_array.clone()], + &test_cases, + field_metadata.clone(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, field_metadata.clone()) + .await; + + // Scenario 4: All lists are null and inside a struct (only valid for 2.1 since 2.0 doesn't + // support null structs) + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + list_builder.append_null(); + list_builder.append_null(); + list_builder.append_null(); + let list_array = Arc::new(list_builder.finish()); + + let struct_validity = NullBuffer::new(BooleanBuffer::from(vec![true, false, true])); + let struct_array = Arc::new(StructArray::new( + Fields::from(vec![Field::new( + "lists", + list_array.data_type().clone(), + true, + )]), + vec![list_array], + Some(struct_validity), + )); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_indices(vec![1]) + .with_structural_encodings(); + check_round_trip_encoding_of_data( + vec![struct_array.clone()], + &test_cases, + field_metadata.clone(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![struct_array], &test_cases, field_metadata.clone()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_empty_list_list() { + let items_builder = Int32Builder::new(); + let list_builder = ListBuilder::new(items_builder); + let mut outer_list_builder = ListBuilder::new(list_builder); + outer_list_builder.append_null(); + outer_list_builder.append_null(); + outer_list_builder.append_null(); + let list_array = Arc::new(outer_list_builder.finish()); + + let test_cases = TestCases::default().with_structural_encodings(); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + #[ignore] // This test is quite slow in debug mode + async fn test_jumbo_list() { + // This is an overflow test. We have a list of lists where each list + // has 1Mi items. We encode 5000 of these lists and so we have over 4Gi in the + // offsets range + let items = BooleanArray::new_null(1024 * 1024); + let offsets = OffsetBuffer::new(ScalarBuffer::from(vec![0, 1024 * 1024])); + let list_arr = Arc::new(ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + offsets, + Arc::new(items), + None, + )) as ArrayRef; + let arrs = vec![list_arr; 5000]; + + // We can't validate because our validation relies on concatenating all input arrays + let test_cases = TestCases::default().without_validation(); + check_round_trip_encoding_of_data(arrs, &test_cases, HashMap::new()).await; + } + + // Regression test for issue with ListArray encoding when crossing 1024 value boundary + // This test reproduces the bug where rows_avail assertion fails in schedule_instructions + // when encoding a ListArray with specific size patterns that cross the 1024 value boundary + #[tokio::test] + async fn test_fuzz_issue_4466() { + // This specific pattern of list sizes triggers the bug when total values cross 1024 + // 94 lists total 1009 values (passes), 95 lists total 1025 values (fails) + let list_sizes = vec![ + 13, 18, 12, 7, 14, 12, 6, 13, 18, 8, // 0-9: 119 values + 6, 11, 17, 12, 8, 19, 5, 6, 10, 13, // 10-19: 107 values + 8, 6, 10, 4, 8, 16, 14, 12, 18, 9, // 20-29: 105 values + 17, 8, 14, 18, 15, 3, 2, 4, 5, 1, // 30-39: 82 values + 3, 13, 1, 2, 10, 4, 10, 18, 7, 14, // 40-49: 75 values + 18, 13, 9, 17, 3, 13, 10, 14, 8, 19, // 50-59: 125 values + 17, 10, 5, 11, 6, 15, 10, 18, 18, 20, // 60-69: 130 values + 16, 11, 12, 15, 7, 9, 3, 10, 20, 5, // 70-79: 102 values + 2, 3, 17, 4, 8, 12, 15, 6, 3, 20, // 80-89: 90 values + 15, 20, 1, 19, 16, // 90-94: 71 values + ]; + + // Build the ListArray + let mut list_builder = ListBuilder::new(Int32Builder::new()); + let mut total_values = 0; + + for size in &list_sizes { + for i in 0..*size { + list_builder.values().append_value(i); + } + list_builder.append(true); + total_values += size; + } + + let list_array = Arc::new(list_builder.finish()); + + // Verify we have the expected number of values + assert_eq!(list_array.len(), 95); + assert_eq!(total_values, 1025); + + // This should trigger the assertion failure at primitive.rs:1362 + // debug_assert!(rows_avail > 0) + let test_cases = TestCases::default().with_structural_encodings(); + + // The bug manifests when encoding this specific pattern + // Expected: successful round-trip encoding + // Actual: panic at primitive.rs:1362 - assertion failed: rows_avail > 0 + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_sparse_large_string_list( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + // 2.5 million rows, mostly empty lists. ~100 lists have 10 short strings each. + let num_rows = 2_500_000u32; + let num_non_empty = 100u32; + let strings_per_list = 10; + + let items_builder = StringBuilder::new(); + let mut list_builder = ListBuilder::new(items_builder); + + // Spread non-empty lists evenly across the range + let step = num_rows / num_non_empty; + let mut next_non_empty = step / 2; + + for i in 0..num_rows { + if i == next_non_empty { + let vals: Vec> = (0..strings_per_list) + .map(|j| match j % 4 { + 0 => Some("a"), + 1 => Some("bb"), + 2 => Some("ccc"), + _ => Some("d"), + }) + .collect(); + list_builder.append_value(vals); + next_non_empty = next_non_empty.saturating_add(step); + } else { + list_builder.append_value([] as [Option<&str>; 0]); + } + } + let list_array = list_builder.finish(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..1000) + .with_range(0..num_rows as u64) + .with_indices(vec![0, (step / 2) as u64, num_rows as u64 - 1]) + .with_dense_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, field_metadata) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_sparse_boolean_list_uses_miniblock() { + // Redacted reproduction from a production schema shape containing ARRAY(BOOLEAN). + // The field names are not relevant; the failure requires sparse list structure + // with a 1-bit Boolean leaf value. + let num_rows = 200_000usize; + let num_non_empty = 10usize; + let booleans_per_list = 8usize; + let step = num_rows / num_non_empty; + + let mut offsets = Vec::with_capacity(num_rows + 1); + let mut values = Vec::with_capacity(num_non_empty * booleans_per_list); + offsets.push(0i32); + + let mut next_non_empty = step / 2; + for row in 0..num_rows { + if row == next_non_empty { + values.extend((0..booleans_per_list).map(|idx| idx % 2 == 0)); + next_non_empty += step; + } + offsets.push(values.len() as i32); + } + + let items = BooleanArray::from(values); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(items), + None, + ); + + let test_cases = TestCases::default() + .with_range(0..1000) + .with_range(0..num_rows as u64) + .with_indices(vec![0, (step / 2) as u64, num_rows as u64 - 1]) + .with_dense_encodings(); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = encode_v22_pages(list_array.clone()).await; + assert_split_miniblock_layout(&pages, false); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_sparse_boolean_list_with_long_empty_prefix() { + let empty_prefix_rows = 70_000usize; + let trailing_empty_rows = 9usize; + let booleans_per_list = 8usize; + let num_rows = empty_prefix_rows + 1 + trailing_empty_rows; + + let mut offsets = Vec::with_capacity(num_rows + 1); + offsets.extend(std::iter::repeat_n(0i32, empty_prefix_rows + 1)); + let values = (0..booleans_per_list) + .map(|idx| idx % 2 == 0) + .collect::>(); + offsets.push(values.len() as i32); + offsets.extend(std::iter::repeat_n( + values.len() as i32, + trailing_empty_rows, + )); + + let items = BooleanArray::from(values); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(items), + None, + ); + + let test_cases = TestCases::default() + .with_range(0..num_rows as u64) + .with_indices(vec![0, empty_prefix_rows as u64, num_rows as u64 - 1]) + .with_dense_encodings(); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = encode_v22_pages(list_array.clone()).await; + assert_split_miniblock_layout(&pages, true); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_sparse_boolean_list_with_long_null_prefix() { + let null_prefix_rows = 70_000usize; + let trailing_empty_rows = 9usize; + let booleans_per_list = 8usize; + let num_rows = null_prefix_rows + 1 + trailing_empty_rows; + + let mut offsets = Vec::with_capacity(num_rows + 1); + offsets.extend(std::iter::repeat_n(0i32, null_prefix_rows + 1)); + let values = (0..booleans_per_list) + .map(|idx| idx % 2 == 0) + .collect::>(); + offsets.push(values.len() as i32); + offsets.extend(std::iter::repeat_n( + values.len() as i32, + trailing_empty_rows, + )); + let validity = BooleanBuffer::from_iter((0..num_rows).map(|row| row >= null_prefix_rows)); + + let items = BooleanArray::from(values); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(items), + Some(NullBuffer::new(validity)), + ); + + let test_cases = TestCases::default() + .with_range(0..num_rows as u64) + .with_indices(vec![0, null_prefix_rows as u64, num_rows as u64 - 1]) + .with_dense_encodings(); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = encode_v22_pages(list_array.clone()).await; + assert_split_miniblock_layout(&pages, true); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_sparse_boolean_list_with_amortized_long_empty_prefix() { + let empty_prefix_rows = 62_000usize; + let booleans_per_list = 8_192usize; + let num_rows = empty_prefix_rows + 1; + + let mut offsets = Vec::with_capacity(num_rows + 1); + offsets.extend(std::iter::repeat_n(0i32, empty_prefix_rows + 1)); + let values = (0..booleans_per_list) + .map(|idx| idx % 2 == 0) + .collect::>(); + offsets.push(values.len() as i32); + + let items = BooleanArray::from(values); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + Arc::new(items), + None, + ); + + let test_cases = TestCases::default() + .with_range(0..num_rows as u64) + .with_indices(vec![0, empty_prefix_rows as u64]) + .with_dense_encodings(); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = encode_v22_pages(list_array.clone()).await; + assert_split_miniblock_layout(&pages, true); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_nested_sparse_boolean_list_fails_without_panic() { + let empty_inner_lists = 70_000usize; + let booleans_per_list = 8usize; + + let mut inner_offsets = vec![0i32; empty_inner_lists + 1]; + let values = (0..booleans_per_list) + .map(|idx| idx % 2 == 0) + .collect::>(); + inner_offsets.push(values.len() as i32); + + let inner_items = BooleanArray::from(values); + let inner_list = ListArray::new( + Arc::new(Field::new("item", DataType::Boolean, true)), + OffsetBuffer::new(ScalarBuffer::from(inner_offsets)), + Arc::new(inner_items), + None, + ); + let outer_list = ListArray::new( + Arc::new(Field::new("item", inner_list.data_type().clone(), true)), + OffsetBuffer::new(ScalarBuffer::from(vec![0i32, empty_inner_lists as i32 + 1])), + Arc::new(inner_list), + None, + ); + + let err = try_encode_v22_pages(Arc::new(outer_list)) + .await + .unwrap_err(); + assert!( + err.to_string().contains("Mini-block cannot encode"), + "unexpected error: {err}" + ); + } + + #[test_log::test(tokio::test)] + async fn test_nested_sparse_string_single_row_falls_back_to_fullzip() { + let empty_inner_lists = 70_000usize; + + let mut inner_offsets = vec![0i32; empty_inner_lists + 1]; + inner_offsets.push(1); + inner_offsets.push(2); + + let mut strings = StringBuilder::new(); + strings.append_value("value"); + strings.append_value("other"); + let inner_items = strings.finish(); + let inner_list = ListArray::new( + Arc::new(Field::new("item", DataType::Utf8, true)), + OffsetBuffer::new(ScalarBuffer::from(inner_offsets)), + Arc::new(inner_items), + None, + ); + let outer_list = ListArray::new( + Arc::new(Field::new("item", inner_list.data_type().clone(), true)), + OffsetBuffer::new(ScalarBuffer::from(vec![0i32, empty_inner_lists as i32 + 2])), + Arc::new(inner_list), + None, + ); + + let outer_list = Arc::new(outer_list) as ArrayRef; + let pages = encode_v22_pages(outer_list.clone()).await; + assert_has_fullzip_layout(&pages); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_indices(vec![0]) + .with_dense_encodings(); + check_round_trip_encoding_of_data(vec![outer_list], &test_cases, HashMap::new()).await; + } + + /// Builds the HNSW-flush repro shape: a dense prefix where every row has + /// `NEIGHBORS_PER_ROW` distinct values, followed by a long tail of empty + /// lists. Mirrors `HNSW::schema()` `__neighbors` / `__dists` columns: + /// dense level-0 lists, then ~6x as many mostly-empty higher-level rows. + fn make_hnsw_shaped_list_u32() -> ListArray { + const DENSE_ROWS: u32 = 40_000; + const NEIGHBORS_PER_ROW: u32 = 32; + const EMPTY_TAIL_ROWS: u32 = 240_000; + + let mut list_builder = ListBuilder::new(UInt32Builder::new()); + let mut next_val: u32 = 0; + for _ in 0..DENSE_ROWS { + for _ in 0..NEIGHBORS_PER_ROW { + list_builder.values().append_value(next_val); + next_val = next_val.wrapping_add(1); + } + list_builder.append(true); + } + for _ in 0..EMPTY_TAIL_ROWS { + list_builder.append(true); + } + list_builder.finish() + } + + /// Reproduces the HNSW-flush shape at v2.2 on the auto path (no + /// `STRUCTURAL_ENCODING` metadata): a dense level-0 prefix followed by a + /// long tail of empty lists. The global levels/values ratio looks dense, + /// so this used to encode as a single mini-block page whose final chunk + /// absorbed every trailing empty list and overflowed the per-chunk `u16` + /// `num_levels`, corrupting the read. The structural page planner now + /// splits on top-level row boundaries: the dense prefix stays on + /// mini-block pages and the empty tail becomes structural-only pages, so + /// the round-trip is lossless without falling back to full-zip. + #[test_log::test(tokio::test)] + async fn test_list_hnsw_shape_splits_to_miniblock_v2_2() { + let list_array = make_hnsw_shaped_list_u32(); + let dense_rows: u64 = 40_000; + let total_rows = list_array.len() as u64; + + let test_cases = TestCases::default() + .with_range(0..1000) + .with_range(dense_rows.saturating_sub(8)..(dense_rows + 8)) + .with_range(0..total_rows) + .with_indices(vec![0, dense_rows - 1, dense_rows, total_rows - 1]) + .with_encoding(TestEncoding::StructuralU32); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = encode_v22_pages(list_array.clone()).await; + assert_split_miniblock_layout(&pages, true); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + /// Companion to the auto-path test: even when the user explicitly requests + /// `STRUCTURAL_ENCODING_MINIBLOCK`, the structural page planner splits the + /// HNSW shape so every emitted page fits the mini-block per-chunk budget. + /// The request is honored (the dense prefix stays on mini-block pages + /// rather than being forced to full-zip) and the round-trip is lossless. + #[test_log::test(tokio::test)] + async fn test_forced_miniblock_hnsw_shape_splits_to_miniblock_v2_2() { + let list_array = make_hnsw_shaped_list_u32(); + let total_rows = list_array.len() as u64; + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_MINIBLOCK.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..total_rows) + .with_encoding(TestEncoding::StructuralU32); + let list_array = Arc::new(list_array) as ArrayRef; + let pages = try_encode_v22_pages_with_metadata(list_array.clone(), field_metadata.clone()) + .await + .unwrap(); + assert_split_miniblock_layout(&pages, true); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, field_metadata).await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/map.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/map.rs new file mode 100644 index 000000000..d07c267bd --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/map.rs @@ -0,0 +1,798 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ops::Range, sync::Arc}; + +use arrow_array::{Array, ArrayRef, ListArray, MapArray}; +use arrow_schema::DataType; +use futures::future::BoxFuture; +use lance_arrow::deepcopy::deep_copy_nulls; +use lance_arrow::list::ListArrayExt; +use lance_core::{Error, Result}; + +use crate::{ + decoder::{ + DecodedArray, FilterExpression, ScheduledScanLine, SchedulerContext, + StructuralDecodeArrayTask, StructuralFieldDecoder, StructuralFieldScheduler, + StructuralSchedulingJob, + }, + encoder::{EncodeTask, FieldEncoder, OutOfLineBuffers}, + repdef::RepDefBuilder, +}; + +/// A structural encoder for map fields +/// +/// Map in Arrow is represented as List> +/// The map's offsets are added to the rep/def builder +/// and the map's entries (struct array) are passed to the child encoder +pub struct MapStructuralEncoder { + keep_original_array: bool, + child: Box, +} + +impl MapStructuralEncoder { + pub fn new(keep_original_array: bool, child: Box) -> Self { + Self { + keep_original_array, + child, + } + } +} + +impl FieldEncoder for MapStructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let map_array = array + .as_any() + .downcast_ref::() + .expect("MapEncoder used for non-map data"); + + // Add offsets to RepDefBuilder to handle nullability and list structure + let has_garbage_values = if self.keep_original_array { + repdef.add_offsets(map_array.offsets().clone(), array.nulls().cloned()) + } else { + repdef.add_offsets(map_array.offsets().clone(), deep_copy_nulls(array.nulls())) + }; + + // MapArray is physically a ListArray, so convert and use ListArrayExt + let list_array: ListArray = map_array.clone().into(); + let entries = if has_garbage_values { + list_array.filter_garbage_nulls().trimmed_values() + } else { + list_array.trimmed_values() + }; + + self.child + .maybe_encode(entries, external_buffers, repdef, row_number, num_rows) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.child.flush(external_buffers) + } + + fn num_columns(&self) -> u32 { + self.child.num_columns() + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + self.child.finish(external_buffers) + } +} + +#[derive(Debug)] +pub struct StructuralMapScheduler { + child: Box, +} + +impl StructuralMapScheduler { + pub fn new(child: Box) -> Self { + Self { child } + } +} + +impl StructuralFieldScheduler for StructuralMapScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + let child = self.child.schedule_ranges(ranges, filter)?; + + Ok(Box::new(StructuralMapSchedulingJob::new(child))) + } + + fn initialize<'a>( + &'a mut self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + self.child.initialize(filter, context) + } +} + +/// Scheduling job for map data +/// +/// Scheduling is handled by the child encoder (struct) and nothing special +/// happens here, similar to list. +#[derive(Debug)] +struct StructuralMapSchedulingJob<'a> { + child: Box, +} + +impl<'a> StructuralMapSchedulingJob<'a> { + fn new(child: Box) -> Self { + Self { child } + } +} + +impl StructuralSchedulingJob for StructuralMapSchedulingJob<'_> { + fn schedule_next(&mut self, context: &mut SchedulerContext) -> Result> { + self.child.schedule_next(context) + } +} + +#[derive(Debug)] +pub struct StructuralMapDecoder { + child: Box, + data_type: DataType, +} + +impl StructuralMapDecoder { + pub fn new(child: Box, data_type: DataType) -> Self { + Self { child, data_type } + } +} + +impl StructuralFieldDecoder for StructuralMapDecoder { + fn accept_page(&mut self, child: crate::decoder::LoadedPageShard) -> Result<()> { + self.child.accept_page(child) + } + + fn drain(&mut self, num_rows: u64) -> Result> { + let child_task = self.child.drain(num_rows)?; + Ok(Box::new(StructuralMapDecodeTask::new( + child_task, + self.data_type.clone(), + ))) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +#[derive(Debug)] +struct StructuralMapDecodeTask { + child_task: Box, + data_type: DataType, +} + +impl StructuralMapDecodeTask { + fn new(child_task: Box, data_type: DataType) -> Self { + Self { + child_task, + data_type, + } + } +} + +impl StructuralDecodeArrayTask for StructuralMapDecodeTask { + fn decode(self: Box) -> Result { + let DecodedArray { + array, + mut repdef, + data_size, + } = self.child_task.decode()?; + + // Decode the offsets from RepDef + let (offsets, validity) = repdef.unravel_offsets::()?; + + // Extract the entries field and keys_sorted from the map data type + let (entries_field, keys_sorted) = match &self.data_type { + DataType::Map(field, keys_sorted) => { + if *keys_sorted { + return Err(Error::not_supported_source( + "Map type decoder does not support keys_sorted=true now" + .to_string() + .into(), + )); + } + (field.clone(), *keys_sorted) + } + _ => { + return Err(Error::schema( + "Map decoder did not have a map field".to_string(), + )); + } + }; + + // Convert the decoded array to StructArray + let entries = array + .as_any() + .downcast_ref::() + .ok_or_else(|| Error::schema("Map entries should be a StructArray".to_string()))? + .clone(); + + // Build the MapArray from offsets, entries, validity, and keys_sorted + let map_array = MapArray::try_new(entries_field, offsets, entries, validity, keys_sorted) + .map_err(|error| Error::invalid_input_source(error.to_string().into()))?; + + Ok(DecodedArray { + array: Arc::new(map_array), + repdef, + data_size, + }) + } +} + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{ + Array, Int32Array, MapArray, StringArray, StructArray, + builder::{Int32Builder, MapBuilder, StringBuilder}, + }; + use arrow_buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field, Fields}; + + use crate::decoder::{DecodedArray, StructuralDecodeArrayTask}; + use crate::encoder::{ColumnIndexSequence, EncodingOptions}; + use crate::encodings::logical::primitive::sparse::{ + SparseCountSet, SparsePositionSet, SparseStructuralLayerPlan, SparseStructuralPlan, + SparseValidityMeaning, SparseValiditySet, + }; + use crate::repdef::{CompositeRepDefUnraveler, RepDefUnraveler}; + use crate::testing::{ + TestCases, TestEncoding, check_round_trip_encoding_of_data, test_encoding_strategy, + }; + use arrow_schema::Field as ArrowField; + use lance_core::datatypes::Field as LanceField; + + use super::StructuralMapDecodeTask; + + fn make_map_type(key_type: DataType, value_type: DataType) -> DataType { + // Note: Arrow MapBuilder uses "keys" and "values" as field names (plural) + let entries = Field::new( + "entries", + DataType::Struct(Fields::from(vec![ + Field::new("keys", key_type, false), + Field::new("values", value_type, true), + ])), + false, + ); + DataType::Map(Arc::new(entries), false) + } + + #[derive(Debug)] + struct StaticMapEntriesTask { + entries: StructArray, + repdef: CompositeRepDefUnraveler, + } + + impl StructuralDecodeArrayTask for StaticMapEntriesTask { + fn decode(self: Box) -> lance_core::Result { + let Self { entries, repdef } = *self; + Ok(DecodedArray { + array: Arc::new(entries), + repdef, + data_size: 0, + }) + } + } + + #[test] + fn malformed_sparse_map_entries_return_invalid_input() { + let entry_fields = Fields::from(vec![ + Field::new("keys", DataType::Int32, false), + Field::new("values", DataType::Int32, true), + ]); + let entries = StructArray::try_new( + entry_fields.clone(), + vec![ + Arc::new(Int32Array::from(vec![1])), + Arc::new(Int32Array::from(vec![2])), + ], + Some(NullBuffer::from(vec![false])), + ) + .unwrap(); + let validity = SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions: SparsePositionSet::Empty, + }; + let plan = SparseStructuralPlan { + layers: vec![SparseStructuralLayerPlan::List { + num_slots: 1, + num_child_slots: 1, + non_empty_positions: SparsePositionSet::All { len: 1 }, + counts: SparseCountSet::Constant { value: 1, len: 1 }, + validity, + }], + num_items: 1, + num_visible_items: 1, + }; + let child_task = StaticMapEntriesTask { + entries, + repdef: CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new_sparse(plan)]), + }; + let map_type = DataType::Map( + Arc::new(Field::new("entries", DataType::Struct(entry_fields), false)), + false, + ); + + let Err(err) = + Box::new(StructuralMapDecodeTask::new(Box::new(child_task), map_type)).decode() + else { + panic!("expected malformed map entries to be rejected"); + }; + assert!(matches!(err, lance_core::Error::InvalidInput { .. })); + } + + #[test_log::test(tokio::test)] + async fn test_simple_map() { + // Create a simple Map + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // Map 1: {"key1": 10, "key2": 20} + map_builder.keys().append_value("key1"); + map_builder.values().append_value(10); + map_builder.keys().append_value("key2"); + map_builder.values().append_value(20); + map_builder.append(true).unwrap(); + + // Map 2: {"key3": 30} + map_builder.keys().append_value("key3"); + map_builder.values().append_value(30); + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_empty_maps() { + // Test maps with empty entries + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // Map 1: {"a": 1} + map_builder.keys().append_value("a"); + map_builder.values().append_value(1); + map_builder.append(true).unwrap(); + + // Map 2: {} (empty) + map_builder.append(true).unwrap(); + + // Map 3: null + map_builder.append(false).unwrap(); + + // Map 4: {} (empty) + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..4) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_with_null_values() { + // Test Map with null values + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // Map 1: {"key1": 10, "key2": null} + map_builder.keys().append_value("key1"); + map_builder.values().append_value(10); + map_builder.keys().append_value("key2"); + map_builder.values().append_null(); + map_builder.append(true).unwrap(); + + // Map 2: {"key3": null} + map_builder.keys().append_value("key3"); + map_builder.values().append_null(); + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_indices(vec![0]) + .with_indices(vec![1]) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_in_struct() { + // Test Struct containing Map + // Struct> + + let string_key_builder = StringBuilder::new(); + let string_val_builder = StringBuilder::new(); + let mut map_builder = MapBuilder::new(None, string_key_builder, string_val_builder); + + // First struct: id=1, properties={"name": "Alice", "city": "NYC"} + map_builder.keys().append_value("name"); + map_builder.values().append_value("Alice"); + map_builder.keys().append_value("city"); + map_builder.values().append_value("NYC"); + map_builder.append(true).unwrap(); + + // Second struct: id=2, properties={"name": "Bob"} + map_builder.keys().append_value("name"); + map_builder.values().append_value("Bob"); + map_builder.append(true).unwrap(); + + // Third struct: id=3, properties=null + map_builder.append(false).unwrap(); + + let map_array = Arc::new(map_builder.finish()); + let id_array = Arc::new(Int32Array::from(vec![1, 2, 3])); + + let struct_array = StructArray::new( + Fields::from(vec![ + Field::new("id", DataType::Int32, false), + Field::new( + "properties", + make_map_type(DataType::Utf8, DataType::Utf8), + true, + ), + ]), + vec![id_array, map_array], + None, + ); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_indices(vec![0, 2]) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data( + vec![Arc::new(struct_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_in_nullable_struct() { + // Test Struct where null struct rows have garbage map entries. + // The encoder must filter these garbage entries before encoding. + let entries_fields = Fields::from(vec![ + Field::new("keys", DataType::Utf8, false), + Field::new("values", DataType::Int32, true), + ]); + let entries_field = Arc::new(Field::new( + "entries", + DataType::Struct(entries_fields.clone()), + false, + )); + let map_entries = StructArray::new( + entries_fields, + vec![ + Arc::new(StringArray::from(vec!["a", "garbage", "b"])), + Arc::new(Int32Array::from(vec![1, 999, 2])), + ], + None, + ); + // map0: {"a": 1}, map1 (garbage): {"garbage": 999}, map2: {"b": 2} + let map_array: Arc = Arc::new(MapArray::new( + entries_field, + OffsetBuffer::new(ScalarBuffer::from(vec![0, 1, 2, 3])), + map_entries, + None, // No nulls at map level - nulls come from struct + false, + )); + + let struct_array = StructArray::new( + Fields::from(vec![ + Field::new("id", DataType::Int32, true), + Field::new("props", map_array.data_type().clone(), true), + ]), + vec![ + Arc::new(Int32Array::from(vec![Some(1), Some(2), Some(3)])), + map_array, + ], + Some(NullBuffer::from(vec![true, false, true])), // Middle row is null + ); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data( + vec![Arc::new(struct_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_list_of_maps() { + // Test List> + use arrow_array::builder::ListBuilder; + + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let map_builder = MapBuilder::new(None, string_builder, int_builder); + let mut list_builder = ListBuilder::new(map_builder); + + // List 1: [{"a": 1}, {"b": 2}] + list_builder.values().keys().append_value("a"); + list_builder.values().values().append_value(1); + list_builder.values().append(true).unwrap(); + + list_builder.values().keys().append_value("b"); + list_builder.values().values().append_value(2); + list_builder.values().append(true).unwrap(); + + list_builder.append(true); + + // List 2: [{"c": 3}] + list_builder.values().keys().append_value("c"); + list_builder.values().values().append_value(3); + list_builder.values().append(true).unwrap(); + + list_builder.append(true); + + // List 3: [] (empty list) + list_builder.append(true); + + let list_array = list_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_indices(vec![0, 2]) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_nested_map() { + // Test Map> + // This is more complex as we need to build nested maps manually + + // Build inner maps first + let inner_string_builder = StringBuilder::new(); + let inner_int_builder = Int32Builder::new(); + let mut inner_map_builder1 = MapBuilder::new(None, inner_string_builder, inner_int_builder); + + // Inner map 1: {"x": 10} + inner_map_builder1.keys().append_value("x"); + inner_map_builder1.values().append_value(10); + inner_map_builder1.append(true).unwrap(); + + // Inner map 2: {"y": 20, "z": 30} + inner_map_builder1.keys().append_value("y"); + inner_map_builder1.values().append_value(20); + inner_map_builder1.keys().append_value("z"); + inner_map_builder1.values().append_value(30); + inner_map_builder1.append(true).unwrap(); + + let inner_maps = Arc::new(inner_map_builder1.finish()); + + // Build outer map keys + let outer_keys = Arc::new(StringArray::from(vec!["key1", "key2"])); + + // Build outer map structure + let entries_struct = StructArray::new( + Fields::from(vec![ + Field::new("key", DataType::Utf8, false), + Field::new( + "value", + make_map_type(DataType::Utf8, DataType::Int32), + true, + ), + ]), + vec![outer_keys, inner_maps], + None, + ); + + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 2])); + let entries_field = Field::new("entries", entries_struct.data_type().clone(), false); + + let outer_map = MapArray::new( + Arc::new(entries_field), + offsets, + entries_struct, + None, + false, + ); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(outer_map)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_different_key_types() { + // Test Map (integer keys) + let int_builder = Int32Builder::new(); + let string_builder = StringBuilder::new(); + let mut map_builder = MapBuilder::new(None, int_builder, string_builder); + + // Map 1: {1: "one", 2: "two"} + map_builder.keys().append_value(1); + map_builder.values().append_value("one"); + map_builder.keys().append_value(2); + map_builder.values().append_value("two"); + map_builder.append(true).unwrap(); + + // Map 2: {3: "three"} + map_builder.keys().append_value(3); + map_builder.values().append_value("three"); + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_indices(vec![0, 1]) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_with_extreme_sizes() { + // Test maps with large number of entries + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // Create a map with many entries + for i in 0..100 { + map_builder.keys().append_value(format!("key{}", i)); + map_builder.values().append_value(i); + } + map_builder.append(true).unwrap(); + + // Create a second map with no entries + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_all_null() { + // Test map where all entries are null + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // All null maps + map_builder.append(false).unwrap(); // null + map_builder.append(false).unwrap(); // null + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_u32_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_map_encoder_keep_original_array_scenarios() { + // Test scenarios that highlight the difference between keep_original_array=true/false + // This test focuses on round-trip behavior which should be equivalent in both cases + let string_builder = StringBuilder::new(); + let int_builder = Int32Builder::new(); + let mut map_builder = MapBuilder::new(None, string_builder, int_builder); + + // Create a map with mixed null and non-null values to test both scenarios + // Map 1: {"key1": 10, "key2": null} + map_builder.keys().append_value("key1"); + map_builder.values().append_value(10); + map_builder.keys().append_value("key2"); + map_builder.values().append_null(); + map_builder.append(true).unwrap(); + + // Map 2: null + map_builder.append(false).unwrap(); + + // Map 3: {"key3": 30} + map_builder.keys().append_value("key3"); + map_builder.values().append_value(30); + map_builder.append(true).unwrap(); + + let map_array = map_builder.finish(); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_indices(vec![0, 1, 2]) + .with_u32_structural_encodings(); + + // This test ensures that regardless of the internal keep_original_array setting, + // the end-to-end behavior produces equivalent results + check_round_trip_encoding_of_data(vec![Arc::new(map_array)], &test_cases, HashMap::new()) + .await; + } + + #[test] + fn test_map_not_supported_write_in_v2_1() { + // Create a map field using Arrow Field first, then convert to Lance Field + let map_arrow_field = ArrowField::new( + "map_field", + make_map_type(DataType::Utf8, DataType::Int32), + true, + ); + let map_field = LanceField::try_from(&map_arrow_field).unwrap(); + + // Test encoder: Try to create encoder with V2_1 version - should fail + let encoder_strategy = test_encoding_strategy(TestEncoding::StructuralU16); + let mut column_index = ColumnIndexSequence::default(); + let options = EncodingOptions::default(); + + let encoder_result = crate::testing::create_test_field_encoder( + encoder_strategy.as_ref(), + &map_field, + &mut column_index, + &options, + ); + + assert!( + encoder_result.is_err(), + "Map type should not be supported in V2_1 for encoder" + ); + let Err(encoder_err) = encoder_result else { + panic!("Expected error but got Ok") + }; + + let encoder_err_msg = format!("{}", encoder_err); + assert!( + encoder_err_msg.contains("not enabled by the selected file format"), + "unexpected encoder error: {encoder_err_msg}" + ); + assert!( + encoder_err_msg.contains("Map data type"), + "Encoder error message should mention Map data type, got: {}", + encoder_err_msg + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive.rs new file mode 100644 index 000000000..15b39d62b --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive.rs @@ -0,0 +1,10307 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + any::Any, + collections::{HashMap, VecDeque}, + env, + fmt::Debug, + iter, + ops::Range, + sync::Arc, + vec, +}; + +use crate::{ + constants::{ + STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_META_KEY, STRUCTURAL_ENCODING_MINIBLOCK, + STRUCTURAL_ENCODING_SPARSE, + }, + data::DictionaryDataBlock, + encodings::logical::primitive::blob::{BlobDescriptionPageScheduler, BlobPageScheduler}, + format::{ + ProtobufUtils21, + pb21::{self, CompressiveEncoding, PageLayout, compressive_encoding::Compression}, + }, +}; +use arrow_array::{Array, ArrayRef, PrimitiveArray, cast::AsArray, make_array, types::UInt64Type}; +use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder, NullBuffer, ScalarBuffer}; +use arrow_schema::{DataType, Field as ArrowField}; +use bytes::Bytes; +use futures::{FutureExt, TryStreamExt, future::BoxFuture, stream::FuturesOrdered}; +use itertools::Itertools; +use lance_arrow::DataTypeExt; +use lance_arrow::deepcopy::deep_copy_nulls; +use lance_core::{ + cache::{CacheKey, CacheKeySchema, Context, DeepSizeOf, KeyBuilder}, + error::{Error, LanceOptionExt}, + utils::bit::pad_bytes, +}; +use log::{debug, trace}; + +use crate::encodings::logical::primitive::miniblock::MiniBlockChunk; +use crate::encodings::physical::rle::{RleDecompressor, RleRuns}; +use crate::utils::bytepack::ByteUnpacker; +use crate::{ + compression::{ + BlockDecompressor, CompressionStrategy, DecompressionStrategy, MiniBlockDecompressor, + create_rle_decompressor, + }, + data::{AllNullDataBlock, DataBlock, VariableWidthBlock}, + utils::bytepack::BytepackedIntegerEncoder, +}; +use crate::{ + compression::{FixedPerValueDecompressor, VariablePerValueDecompressor}, + encodings::logical::primitive::fullzip::PerValueDataBlock, +}; +use crate::{ + encodings::logical::primitive::miniblock::{MiniBlockCompressed, MiniBlockCompressionContext}, + statistics::{ComputeStat, GetStat, Stat}, +}; +use crate::{ + repdef::{ + CompositeRepDefUnraveler, ControlWordIterator, ControlWordParser, DefinitionInterpretation, + MiniBlockRepDefBudget, NormalizedStructuralPlan, RepDefSlicer, SerializedRepDefs, + build_control_word_iterator, + }, + utils::accumulation::AccumulationQueue, +}; +use lance_core::{Result, datatypes::Field, utils::tokio::spawn_cpu}; + +use crate::constants::{ + COMPRESSION_LEVEL_META_KEY, COMPRESSION_META_KEY, DICT_DIVISOR_META_KEY, + DICT_SIZE_RATIO_META_KEY, DICT_VALUES_COMPRESSION_ENV_VAR, + DICT_VALUES_COMPRESSION_LEVEL_ENV_VAR, DICT_VALUES_COMPRESSION_LEVEL_META_KEY, + DICT_VALUES_COMPRESSION_META_KEY, +}; +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{BlockInfo, DataBlockBuilder, FixedWidthDataBlock}, + decoder::{ + ColumnInfo, DecodePageTask, DecodedArray, DecodedPage, FilterExpression, LoadedPageShard, + MessageType, PageEncoding, PageInfo, ScheduledScanLine, SchedulerContext, + StructuralDecodeArrayTask, StructuralFieldDecoder, StructuralFieldScheduler, + StructuralPageDecoder, StructuralSchedulingJob, UnloadedPageShard, + }, + encoder::{ + EncodeTask, EncodedColumn, EncodedPage, EncodingOptions, FieldEncoder, OutOfLineBuffers, + }, + repdef::{LevelBuffer, RepDefBuilder, RepDefUnraveler}, +}; + +pub mod blob; +mod chunk_index; +pub mod constant; +pub mod dict; +pub mod fullzip; +mod layout; +pub mod miniblock; +pub(crate) mod sparse; + +use chunk_index::{ItemCounts, MiniBlockChunkIndex, PrefixSums, RowMapping, parse_nested_rep}; + +const FILL_BYTE: u8 = 0xFE; +const DEFAULT_DICT_DIVISOR: u64 = 2; +const DEFAULT_DICT_MAX_CARDINALITY: u64 = 100_000; +const DEFAULT_DICT_SIZE_RATIO: f64 = 0.8; +const DEFAULT_DICT_VALUES_COMPRESSION: &str = "lz4"; + +struct PageLoadTask { + decoder_fut: BoxFuture<'static, Result>>, + num_rows: u64, +} + +/// A trait for figuring out how to schedule the data within +/// a single page. +trait StructuralPageScheduler: std::fmt::Debug + Send { + /// Fetches any metadata required for the page + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>>; + /// Loads metadata from a previous initialize call + fn load(&mut self, data: &Arc); + /// Schedules the read of the given ranges in the page + /// + /// The read may be split into multiple "shards" if the page is extremely large. + /// Each shard maps to one or more rows and can be decoded independently. + /// + /// Note: this sharding is for splitting up very large pages into smaller reads to + /// avoid buffering too much data in memory. It is not related to the batch size or + /// compute units in any way. + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result>; +} + +/// Metadata describing the decoded size of a mini-block +#[derive(Debug)] +struct ChunkMeta { + num_values: u64, + chunk_size_bytes: u64, + offset_bytes: u64, +} + +/// A mini-block chunk that has been decoded and decompressed +#[derive(Debug, Clone)] +struct DecodedMiniBlockChunk { + rep: Option>, + def: Option>, + values: DataBlock, +} + +/// A task to decode a one or more mini-blocks of data into an output batch +/// +/// Note: Two batches might share the same mini-block of data. When this happens +/// then each batch gets a copy of the block and each batch decodes the block independently. +/// +/// This means we have duplicated work but it is necessary to avoid having to synchronize +/// the decoding of the block. (TODO: test this theory) +#[derive(Debug)] +struct DecodeMiniBlockTask { + rep_decompressor: Option>, + def_decompressor: Option>, + value_decompressor: Arc, + dictionary_data: Option>, + def_meaning: Arc<[DefinitionInterpretation]>, + num_buffers: u64, + max_visible_level: u16, + instructions: Vec<(ChunkDrainInstructions, LoadedChunk)>, + has_large_chunk: bool, +} + +impl DecodeMiniBlockTask { + fn decoded_size_bytes(&self) -> Option { + if self.rep_decompressor.is_some() || self.def_decompressor.is_some() { + return None; + } + let num_values = self + .instructions + .iter() + .try_fold(0_u64, |total, (instruction, _)| { + total.checked_add(instruction.rows_to_take) + })?; + self.value_decompressor.decoded_size_bytes(num_values) + } + + fn decode_levels( + rep_decompressor: &dyn BlockDecompressor, + levels: LanceBuffer, + num_levels: u16, + ) -> Result> { + let rep = rep_decompressor.decompress(levels, num_levels as u64)?; + let rep = rep.as_fixed_width().unwrap(); + debug_assert_eq!(rep.num_values, num_levels as u64); + debug_assert_eq!(rep.bits_per_value, 16); + Ok(rep.data.borrow_to_typed_slice::()) + } + + // We are building a LevelBuffer (levels) and want to copy into it `total_len` + // values from `level_buf` starting at `offset`. + // + // We need to handle both the case where `levels` is None (no nulls encountered + // yet) and the case where `level_buf` is None (the input we are copying from has + // no nulls) + fn extend_levels( + range: Range, + levels: &mut Option, + level_buf: &Option>, + dest_offset: usize, + ) { + if let Some(level_buf) = level_buf { + if levels.is_none() { + // This is the first non-empty def buf we've hit, fill in the past + // with 0 (valid) + let mut new_levels_vec = + LevelBuffer::with_capacity(dest_offset + (range.end - range.start) as usize); + new_levels_vec.extend(iter::repeat_n(0, dest_offset)); + *levels = Some(new_levels_vec); + } + levels.as_mut().unwrap().extend( + level_buf.as_ref()[range.start as usize..range.end as usize] + .iter() + .copied(), + ); + } else if let Some(levels) = levels { + let num_values = (range.end - range.start) as usize; + // This is an all-valid level_buf but we had nulls earlier and so we + // need to materialize it + levels.extend(iter::repeat_n(0, num_values)); + } + } + + /// Maps a range of rows to a range of items and a range of levels + /// + /// If there is no repetition information this just returns the range as-is. + /// + /// If there is repetition information then we need to do some work to figure out what + /// range of items corresponds to the requested range of rows. + /// + /// For example, if the data is [[1, 2, 3], [4, 5], [6, 7]] and the range is 1..2 (i.e. just row + /// 1) then the user actually wants items 3..5. In the above case the rep levels would be: + /// + /// Idx: 0 1 2 3 4 5 6 + /// Rep: 1 0 0 1 0 1 0 + /// + /// So the start (1) maps to the second 1 (idx=3) and the end (2) maps to the third 1 (idx=5) + /// + /// If there are invisible items then we don't count them when calculating the range of items we + /// are interested in but we do count them when calculating the range of levels we are interested + /// in. As a result we have to return both the item range (first return value) and the level range + /// (second return value). + /// + /// For example, if the data is [[1, 2, 3], [4, 5], NULL, [6, 7, 8]] and the range is 2..4 then the + /// user wants items 5..8 but they want levels 5..9. In the above case the rep/def levels would be: + /// + /// Idx: 0 1 2 3 4 5 6 7 8 + /// Rep: 1 0 0 1 0 1 1 0 0 + /// Def: 0 0 0 0 0 1 0 0 0 + /// Itm: 1 2 3 4 5 6 7 8 + /// + /// Finally, we have to contend with the fact that chunks may or may not start with a "preamble" of + /// trailing values that finish up a list from the previous chunk. In this case the first item does + /// not start at max_rep because it is a continuation of the previous chunk. For our purposes we do + /// not consider this a "row" and so the range 0..1 will refer to the first row AFTER the preamble. + /// + /// We have a separate parameter (`preamble_action`) to control whether we want the preamble or not. + /// + /// Note that the "trailer" is considered a "row" and if we want it we should include it in the range. + fn map_range( + range: Range, + rep: Option<&impl AsRef<[u16]>>, + def: Option<&impl AsRef<[u16]>>, + max_rep: u16, + max_visible_def: u16, + // The total number of items (not rows) in the chunk. This is not quite the same as + // rep.len() / def.len() because it doesn't count invisible items + total_items: u64, + preamble_action: PreambleAction, + ) -> (Range, Range) { + if let Some(rep) = rep { + let mut rep = rep.as_ref(); + // If there is a preamble and we need to skip it then do that first. The work is the same + // whether there is def information or not + let mut items_in_preamble = 0_u64; + let first_row_start = match preamble_action { + PreambleAction::Skip | PreambleAction::Take => { + let first_row_start = if let Some(def) = def.as_ref() { + let mut first_row_start = None; + for (idx, (rep, def)) in rep.iter().zip(def.as_ref()).enumerate() { + if *rep == max_rep { + first_row_start = Some(idx as u64); + break; + } + if *def <= max_visible_def { + items_in_preamble += 1; + } + } + first_row_start + } else { + let first_row_start = + rep.iter().position(|&r| r == max_rep).map(|r| r as u64); + items_in_preamble = first_row_start.unwrap_or(rep.len() as u64); + first_row_start + }; + // It is possible for a chunk to be entirely partial values but if it is then it + // should never show up as a preamble to skip + if first_row_start.is_none() { + assert!(preamble_action == PreambleAction::Take); + return (0..total_items, 0..rep.len() as u64); + } + let first_row_start = first_row_start.unwrap(); + rep = &rep[first_row_start as usize..]; + first_row_start + } + PreambleAction::Absent => { + debug_assert!(rep[0] == max_rep); + 0 + } + }; + + // We hit this case when all we needed was the preamble + if range.start == range.end { + debug_assert!(preamble_action == PreambleAction::Take); + debug_assert!(items_in_preamble <= total_items); + return (0..items_in_preamble, 0..first_row_start); + } + assert!(range.start < range.end); + + let mut rows_seen = 0; + let mut new_start = 0; + let mut new_levels_start = 0; + + if let Some(def) = def { + let def = &def.as_ref()[first_row_start as usize..]; + + // range.start == 0 always maps to 0 (even with invis items), otherwise we need to walk + let mut lead_invis_seen = 0; + + if range.start > 0 { + if def[0] > max_visible_def { + lead_invis_seen += 1; + } + for (idx, (rep, def)) in rep.iter().zip(def).skip(1).enumerate() { + if *rep == max_rep { + rows_seen += 1; + if rows_seen == range.start { + new_start = idx as u64 + 1 - lead_invis_seen; + new_levels_start = idx as u64 + 1; + break; + } + } + if *def > max_visible_def { + lead_invis_seen += 1; + } + } + } + + rows_seen += 1; + + let mut new_end = u64::MAX; + let mut new_levels_end = rep.len() as u64; + let new_start_is_visible = def[new_levels_start as usize] <= max_visible_def; + let mut tail_invis_seen = if new_start_is_visible { 0 } else { 1 }; + for (idx, (rep, def)) in rep[(new_levels_start + 1) as usize..] + .iter() + .zip(&def[(new_levels_start + 1) as usize..]) + .enumerate() + { + if *rep == max_rep { + rows_seen += 1; + if rows_seen == range.end + 1 { + new_end = idx as u64 + new_start + 1 - tail_invis_seen; + new_levels_end = idx as u64 + new_levels_start + 1; + break; + } + } + if *def > max_visible_def { + tail_invis_seen += 1; + } + } + + if new_end == u64::MAX { + new_levels_end = rep.len() as u64; + let total_invis_seen = lead_invis_seen + tail_invis_seen; + new_end = rep.len() as u64 - total_invis_seen; + } + + assert_ne!(new_end, u64::MAX); + + // Adjust for any skipped preamble + if preamble_action == PreambleAction::Skip { + new_start += items_in_preamble; + new_end += items_in_preamble; + new_levels_start += first_row_start; + new_levels_end += first_row_start; + } else if preamble_action == PreambleAction::Take { + debug_assert_eq!(new_start, 0); + debug_assert_eq!(new_levels_start, 0); + new_end += items_in_preamble; + new_levels_end += first_row_start; + } + + debug_assert!(new_end <= total_items); + (new_start..new_end, new_levels_start..new_levels_end) + } else { + // Easy case, there are no invisible items, so we don't need to check for them + // The items range and levels range will be the same. We do still need to walk + // the rep levels to find the row boundaries + + // range.start == 0 always maps to 0, otherwise we need to walk + if range.start > 0 { + for (idx, rep) in rep.iter().skip(1).enumerate() { + if *rep == max_rep { + rows_seen += 1; + if rows_seen == range.start { + new_start = idx as u64 + 1; + break; + } + } + } + } + let mut new_end = rep.len() as u64; + // range.end == max_items always maps to rep.len(), otherwise we need to walk + if range.end < total_items { + for (idx, rep) in rep[(new_start + 1) as usize..].iter().enumerate() { + if *rep == max_rep { + rows_seen += 1; + if rows_seen == range.end { + new_end = idx as u64 + new_start + 1; + break; + } + } + } + } + + // Adjust for any skipped preamble + if preamble_action == PreambleAction::Skip { + new_start += first_row_start; + new_end += first_row_start; + } else if preamble_action == PreambleAction::Take { + debug_assert_eq!(new_start, 0); + new_end += first_row_start; + } + + debug_assert!(new_end <= total_items); + (new_start..new_end, new_start..new_end) + } + } else { + // No repetition info, easy case, just use the range as-is and the item + // and level ranges are the same + (range.clone(), range) + } + } + + // read `num_buffers` buffer sizes from `buf` starting at `offset` + fn read_buffer_sizes( + buf: &[u8], + offset: &mut usize, + num_buffers: u64, + ) -> Vec { + let read_size = if LARGE { 4 } else { 2 }; + (0..num_buffers) + .map(|_| { + let bytes = &buf[*offset..*offset + read_size]; + let size = if LARGE { + u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) + } else { + // the buffer size is read from u16 but is stored as u32 after decoding for consistency + u16::from_le_bytes([bytes[0], bytes[1]]) as u32 + }; + *offset += read_size; + size + }) + .collect() + } + + // Unserialize a miniblock into a collection of vectors + fn decode_miniblock_chunk( + &self, + buf: &LanceBuffer, + items_in_chunk: u64, + ) -> Result { + let mut offset = 0; + let num_levels = u16::from_le_bytes([buf[offset], buf[offset + 1]]); + offset += 2; + + let rep_size = if self.rep_decompressor.is_some() { + let rep_size = u16::from_le_bytes([buf[offset], buf[offset + 1]]); + offset += 2; + Some(rep_size) + } else { + None + }; + let def_size = if self.def_decompressor.is_some() { + let def_size = u16::from_le_bytes([buf[offset], buf[offset + 1]]); + offset += 2; + Some(def_size) + } else { + None + }; + + let buffer_sizes = if self.has_large_chunk { + Self::read_buffer_sizes::(buf, &mut offset, self.num_buffers) + } else { + Self::read_buffer_sizes::(buf, &mut offset, self.num_buffers) + }; + + offset += pad_bytes::(offset); + + let rep = rep_size.map(|rep_size| { + let rep = buf.slice_with_length(offset, rep_size as usize); + offset += rep_size as usize; + offset += pad_bytes::(offset); + rep + }); + + let def = def_size.map(|def_size| { + let def = buf.slice_with_length(offset, def_size as usize); + offset += def_size as usize; + offset += pad_bytes::(offset); + def + }); + + let buffers = buffer_sizes + .into_iter() + .map(|buf_size| { + let buf = buf.slice_with_length(offset, buf_size as usize); + offset += buf_size as usize; + offset += pad_bytes::(offset); + buf + }) + .collect::>(); + + let values = self + .value_decompressor + .decompress(buffers, items_in_chunk)?; + + let rep = rep + .map(|rep| { + Self::decode_levels( + self.rep_decompressor.as_ref().unwrap().as_ref(), + rep, + num_levels, + ) + }) + .transpose()?; + let def = def + .map(|def| { + Self::decode_levels( + self.def_decompressor.as_ref().unwrap().as_ref(), + def, + num_levels, + ) + }) + .transpose()?; + + Ok(DecodedMiniBlockChunk { rep, def, values }) + } +} + +impl DecodePageTask for DecodeMiniBlockTask { + fn decode(self: Box) -> Result { + // First, we create output buffers for the rep and def and data + let mut repbuf: Option = None; + let mut defbuf: Option = None; + + let max_rep = self.def_meaning.iter().filter(|l| l.is_list()).count() as u16; + + let estimated_size_bytes = self.decoded_size_bytes().unwrap_or_else(|| { + // Variable-width and rep/def encoded output sizes are not known before decoding. + self.instructions + .iter() + .map(|(_, chunk)| chunk.data.len() as u64) + .sum::() + * 2 + }); + let mut data_builder = DataBlockBuilder::with_capacity_estimate(estimated_size_bytes); + + // We need to keep track of the offset into repbuf/defbuf that we are building up + let mut level_offset = 0; + + // Pre-compute caching needs for each chunk by checking if the next chunk is the same + let needs_caching: Vec = self + .instructions + .windows(2) + .map(|w| w[0].1.chunk_idx == w[1].1.chunk_idx) + .chain(std::iter::once(false)) // the last one never needs caching + .collect(); + + // Cache for storing decoded chunks when beneficial + let mut chunk_cache: Option<(usize, DecodedMiniBlockChunk)> = None; + + // Now we iterate through each instruction and process it + for (idx, (instructions, chunk)) in self.instructions.iter().enumerate() { + let should_cache_this_chunk = needs_caching[idx]; + + let decoded_chunk = match &chunk_cache { + Some((cached_chunk_idx, cached_chunk)) if *cached_chunk_idx == chunk.chunk_idx => { + // Clone only when we have a cache hit (much cheaper than decoding) + cached_chunk.clone() + } + _ => { + // Cache miss, need to decode + let decoded = self.decode_miniblock_chunk(&chunk.data, chunk.items_in_chunk)?; + + // Only update cache if this chunk will benefit the next access + if should_cache_this_chunk { + chunk_cache = Some((chunk.chunk_idx, decoded.clone())); + } + decoded + } + }; + + let DecodedMiniBlockChunk { rep, def, values } = decoded_chunk; + + // Our instructions tell us which rows we want to take from this chunk + let row_range_start = + instructions.rows_to_skip + instructions.chunk_instructions.rows_to_skip; + let row_range_end = row_range_start + instructions.rows_to_take; + + // We use the rep info to map the row range to an item range / levels range + let (item_range, level_range) = Self::map_range( + row_range_start..row_range_end, + rep.as_ref(), + def.as_ref(), + max_rep, + self.max_visible_level, + chunk.items_in_chunk, + instructions.preamble_action, + ); + if item_range.end - item_range.start > chunk.items_in_chunk { + return Err(lance_core::Error::internal(format!( + "Item range {:?} is greater than chunk items in chunk {:?}", + item_range, chunk.items_in_chunk + ))); + } + + // Now we append the data to the output buffers + Self::extend_levels(level_range.clone(), &mut repbuf, &rep, level_offset); + Self::extend_levels(level_range.clone(), &mut defbuf, &def, level_offset); + level_offset += (level_range.end - level_range.start) as usize; + data_builder.append(&values, item_range)?; + } + + let mut data = data_builder.finish(); + + let unraveler = + RepDefUnraveler::new(repbuf, defbuf, self.def_meaning.clone(), data.num_values()); + + if let Some(dictionary) = &self.dictionary_data { + // Don't decode here, that happens later (if needed) + let DataBlock::FixedWidth(indices) = data else { + return Err(lance_core::Error::internal(format!( + "Expected FixedWidth DataBlock for dictionary indices, got {:?}", + data + ))); + }; + data = DataBlock::Dictionary(DictionaryDataBlock::from_parts( + indices, + dictionary.as_ref().clone(), + )); + } + + Ok(DecodedPage { + data, + repdef: unraveler, + }) + } +} + +/// A chunk that has been loaded by the miniblock scheduler (but not +/// yet decoded) +#[derive(Debug)] +struct LoadedChunk { + data: LanceBuffer, + items_in_chunk: u64, + byte_range: Range, + chunk_idx: usize, +} + +impl Clone for LoadedChunk { + fn clone(&self) -> Self { + Self { + // Safe as we always create borrowed buffers here + data: self.data.clone(), + items_in_chunk: self.items_in_chunk, + byte_range: self.byte_range.clone(), + chunk_idx: self.chunk_idx, + } + } +} + +/// Decodes mini-block formatted data. See [`PrimitiveStructuralEncoder`] for more +/// details on the different layouts. +#[derive(Debug)] +struct MiniBlockDecoder { + rep_decompressor: Option>, + def_decompressor: Option>, + value_decompressor: Arc, + def_meaning: Arc<[DefinitionInterpretation]>, + loaded_chunks: VecDeque, + instructions: VecDeque, + offset_in_current_chunk: u64, + num_rows: u64, + num_buffers: u64, + dictionary: Option>, + has_large_chunk: bool, +} + +/// See [`MiniBlockScheduler`] for more details on the scheduling and decoding +/// process for miniblock encoded data. +impl StructuralPageDecoder for MiniBlockDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let mut items_desired = num_rows; + let mut need_preamble = false; + let mut skip_in_chunk = self.offset_in_current_chunk; + let mut drain_instructions = Vec::new(); + while items_desired > 0 || need_preamble { + let (instructions, consumed) = self + .instructions + .front() + .unwrap() + .drain_from_instruction(&mut items_desired, &mut need_preamble, &mut skip_in_chunk); + + while self.loaded_chunks.front().unwrap().chunk_idx + != instructions.chunk_instructions.chunk_idx + { + self.loaded_chunks.pop_front(); + } + drain_instructions.push((instructions, self.loaded_chunks.front().unwrap().clone())); + if consumed { + self.instructions.pop_front(); + } + } + // We can throw away need_preamble here because it must be false. If it were true it would mean + // we were still in the middle of loading rows. We do need to latch skip_in_chunk though. + self.offset_in_current_chunk = skip_in_chunk; + + let max_visible_level = self + .def_meaning + .iter() + .take_while(|l| !l.is_list()) + .map(|l| l.num_def_levels()) + .sum::(); + + Ok(Box::new(DecodeMiniBlockTask { + instructions: drain_instructions, + def_decompressor: self.def_decompressor.clone(), + rep_decompressor: self.rep_decompressor.clone(), + value_decompressor: self.value_decompressor.clone(), + dictionary_data: self.dictionary.clone(), + def_meaning: self.def_meaning.clone(), + num_buffers: self.num_buffers, + max_visible_level, + has_large_chunk: self.has_large_chunk, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// How a complex-all-null page's rep/def level buffer is compressed on disk. +/// Captured at scheduler construction so `initialize` can keep RLE levels in run +/// form instead of expanding them. +#[derive(Debug, Clone)] +pub(crate) enum LevelCodec { + /// Raw little-endian u16 levels (no block compression). + Uncompressed, + /// RLE-compressed levels; the validated physical runs select their cached representation. + Rle(Arc), + /// Any other block compression; decoded eagerly into [`LazyLevels::Dense`] + /// (these encodings don't expand, so laziness buys nothing). + Block(Arc), +} + +impl LevelCodec { + fn try_new( + encoding: Option<&CompressiveEncoding>, + decompression_strategy: &dyn DecompressionStrategy, + ) -> Result { + match encoding { + None => Ok(Self::Uncompressed), + Some(encoding) => match encoding.compression.as_ref() { + Some(Compression::Rle(rle)) => Ok(Self::Rle(Arc::new(create_rle_decompressor( + rle, + decompression_strategy, + )?))), + _ => Ok(Self::Block(Arc::from( + decompression_strategy.create_block_decompressor(encoding)?, + ))), + }, + } + } +} + +#[derive(Debug)] +enum RunEnds { + U16(Box<[u16]>), + U32(Box<[u32]>), + U64(Box<[u64]>), +} + +impl RunEnds { + fn width_for(num_values: usize) -> usize { + if u16::try_from(num_values).is_ok() { + std::mem::size_of::() + } else if u32::try_from(num_values).is_ok() { + std::mem::size_of::() + } else { + std::mem::size_of::() + } + } + + fn len(&self) -> usize { + match self { + Self::U16(ends) => ends.len(), + Self::U32(ends) => ends.len(), + Self::U64(ends) => ends.len(), + } + } + + fn get(&self, run: usize) -> usize { + match self { + Self::U16(ends) => ends[run] as usize, + Self::U32(ends) => ends[run] as usize, + Self::U64(ends) => ends[run] as usize, + } + } + + fn partition_point(&self, logical_index: usize) -> usize { + match self { + Self::U16(ends) => ends.partition_point(|&end| end as usize <= logical_index), + Self::U32(ends) => ends.partition_point(|&end| end as usize <= logical_index), + Self::U64(ends) => ends.partition_point(|&end| end as usize <= logical_index), + } + } + + fn deep_size(&self) -> usize { + match self { + Self::U16(ends) => std::mem::size_of_val(ends.as_ref()), + Self::U32(ends) => std::mem::size_of_val(ends.as_ref()), + Self::U64(ends) => std::mem::size_of_val(ends.as_ref()), + } + } +} + +enum RunEndsBuilder { + U16(Vec), + U32(Vec), + U64(Vec), +} + +impl RunEndsBuilder { + fn with_capacity(num_values: usize, capacity: usize) -> Self { + if u16::try_from(num_values).is_ok() { + Self::U16(Vec::with_capacity(capacity)) + } else if u32::try_from(num_values).is_ok() { + Self::U32(Vec::with_capacity(capacity)) + } else { + Self::U64(Vec::with_capacity(capacity)) + } + } + + fn push(&mut self, end: usize) -> Result<()> { + match self { + Self::U16(ends) => ends.push( + u16::try_from(end) + .map_err(|_| Error::internal(format!("Run end {end} does not fit in u16")))?, + ), + Self::U32(ends) => ends.push( + u32::try_from(end) + .map_err(|_| Error::internal(format!("Run end {end} does not fit in u32")))?, + ), + Self::U64(ends) => ends.push(end as u64), + } + Ok(()) + } + + fn set_last(&mut self, end: usize) -> Result<()> { + match self { + Self::U16(ends) => { + let last = ends.last_mut().ok_or_else(|| { + Error::internal("Cannot extend an empty coalesced run buffer") + })?; + *last = u16::try_from(end) + .map_err(|_| Error::internal(format!("Run end {end} does not fit in u16")))?; + } + Self::U32(ends) => { + let last = ends.last_mut().ok_or_else(|| { + Error::internal("Cannot extend an empty coalesced run buffer") + })?; + *last = u32::try_from(end) + .map_err(|_| Error::internal(format!("Run end {end} does not fit in u32")))?; + } + Self::U64(ends) => { + let last = ends.last_mut().ok_or_else(|| { + Error::internal("Cannot extend an empty coalesced run buffer") + })?; + *last = end as u64; + } + } + Ok(()) + } + + fn finish(self) -> RunEnds { + match self { + Self::U16(ends) => RunEnds::U16(ends.into_boxed_slice()), + Self::U32(ends) => RunEnds::U32(ends.into_boxed_slice()), + Self::U64(ends) => RunEnds::U64(ends.into_boxed_slice()), + } + } +} + +#[derive(Debug)] +enum RunStorage { + Physical(RleRuns), + Coalesced { values: Box<[u16]>, ends: RunEnds }, +} + +impl RunStorage { + fn len(&self) -> usize { + match self { + Self::Physical(runs) => runs.num_values(), + Self::Coalesced { ends, .. } => ends.get(ends.len() - 1), + } + } + + fn num_runs(&self) -> usize { + match self { + Self::Physical(runs) => runs.num_runs(), + Self::Coalesced { values, .. } => values.len(), + } + } + + fn value(&self, run: usize) -> u16 { + match self { + Self::Physical(runs) => runs.value(run), + Self::Coalesced { values, .. } => values[run], + } + } + + fn first_value_above(&self, max: u16) -> Option<(usize, u16)> { + (0..self.num_runs()).find_map(|run| { + let value = self.value(run); + (value > max).then_some((run, value)) + }) + } + + fn seek(&self, position: &mut RunPosition, logical_index: usize) { + if logical_index >= self.len() { + *position = RunPosition { + run: self.num_runs(), + start: self.len(), + end: self.len(), + }; + return; + } + + match self { + Self::Physical(runs) => { + if position.run >= runs.num_runs() + || position.end == 0 + || logical_index < position.start + { + *position = RunPosition { + run: 0, + start: 0, + end: runs.length(0), + }; + } + while position.end <= logical_index { + self.advance(position); + } + } + Self::Coalesced { ends, .. } => { + if logical_index < position.start || logical_index >= position.end { + let run = ends.partition_point(logical_index); + *position = RunPosition { + run, + start: if run == 0 { 0 } else { ends.get(run - 1) }, + end: ends.get(run), + }; + } + } + } + } + + fn advance(&self, position: &mut RunPosition) { + let next_run = position.run + 1; + if next_run >= self.num_runs() { + *position = RunPosition { + run: self.num_runs(), + start: self.len(), + end: self.len(), + }; + return; + } + + let start = position.end; + position.run = next_run; + position.start = start; + position.end = match self { + Self::Physical(runs) => start + runs.length(next_run), + Self::Coalesced { ends, .. } => ends.get(next_run), + }; + } + + fn deep_size(&self) -> usize { + match self { + Self::Physical(runs) => runs.deep_size(), + Self::Coalesced { values, ends } => { + std::mem::size_of_val(values.as_ref()) + ends.deep_size() + } + } + } +} + +/// Rep/def levels for a complex-all-null page. +/// +/// RLE pages retain the smallest of their validated physical runs, coalesced +/// runs, and dense values. The decoder materializes only the per-drain slices +/// it touches. +#[derive(Debug, Clone)] +enum LazyLevels { + Dense(ScalarBuffer), + Runs(Arc), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum LevelPlan { + Physical, + Coalesced, + Dense, +} + +#[derive(Debug, Default, Clone, Copy)] +struct RunPosition { + run: usize, + start: usize, + end: usize, +} + +/// Monotonic forward cursor into a [`LazyLevels`] sequence. +/// +/// Drains seek to strictly increasing rows, so each [`LazyLevels::seek_row_start`] +/// resumes from the last position instead of rescanning — every run is visited at +/// most once per page while locating and counting level ranges. +#[derive(Debug, Default, Clone, Copy)] +struct LevelCursor { + /// Logical level index where the current row begins. + level: usize, + /// Row index at `level` (the number of `max_rep` occurrences before it). + row: u64, + /// Run containing `level`. Unused for [`LazyLevels::Dense`]. + run: RunPosition, +} + +impl LazyLevels { + fn from_rle_runs(runs: RleRuns) -> Result { + let plan = Self::select_plan(&runs); + match plan { + LevelPlan::Physical => Ok(Self::Runs(Arc::new(RunStorage::Physical( + runs.into_owned(), + )))), + LevelPlan::Coalesced => Self::build_coalesced(runs), + LevelPlan::Dense => Self::build_dense(runs), + } + } + + /// Minimize retained payload bytes first, then expected traversal work. + /// If both are equal, keep the physical runs and avoid another allocation. + fn select_plan(runs: &RleRuns) -> LevelPlan { + if runs.num_values() == 0 { + return LevelPlan::Dense; + } + + let run_storage_size = std::mem::size_of::() as u128; + let physical_size = run_storage_size + runs.owned_size() as u128; + let coalesced_size = run_storage_size + + (runs.coalesced_runs() as u128) + * (std::mem::size_of::() + RunEnds::width_for(runs.num_values())) as u128; + let dense_size = (runs.num_values() as u128) * std::mem::size_of::() as u128; + [ + (physical_size, runs.num_runs(), 0usize, LevelPlan::Physical), + ( + coalesced_size, + runs.coalesced_runs(), + 1usize, + LevelPlan::Coalesced, + ), + (dense_size, runs.num_values(), 2usize, LevelPlan::Dense), + ] + .into_iter() + .min_by_key(|(size, traversal, priority, _)| (*size, *traversal, *priority)) + .map(|(_, _, _, plan)| plan) + .unwrap_or(LevelPlan::Dense) + } + + fn build_coalesced(runs: RleRuns) -> Result { + let mut values = Vec::with_capacity(runs.coalesced_runs()); + let mut ends = RunEndsBuilder::with_capacity(runs.num_values(), runs.coalesced_runs()); + let mut logical_end = 0usize; + for (value, length) in runs.iter() { + logical_end = logical_end + .checked_add(length) + .ok_or_else(|| Error::internal("Validated RLE run length sum overflowed usize"))?; + if values.last().copied() == Some(value) { + ends.set_last(logical_end)?; + } else { + values.push(value); + ends.push(logical_end)?; + } + } + Ok(Self::Runs(Arc::new(RunStorage::Coalesced { + values: values.into_boxed_slice(), + ends: ends.finish(), + }))) + } + + fn build_dense(runs: RleRuns) -> Result { + let mut values = Vec::new(); + values.try_reserve_exact(runs.num_values()).map_err(|_| { + Error::internal(format!( + "Cannot allocate {} dense repetition/definition levels", + runs.num_values() + )) + })?; + for (value, length) in runs.iter() { + values.resize(values.len() + length, value); + } + Ok(Self::Dense(ScalarBuffer::from(values))) + } + + fn len(&self) -> usize { + match self { + Self::Dense(buf) => buf.len(), + Self::Runs(runs) => runs.len(), + } + } + + fn validate_max(&self, level_type: &str, max: u16) -> Result<()> { + let invalid = match self { + Self::Dense(levels) => levels + .iter() + .enumerate() + .find_map(|(index, &value)| (value > max).then_some(("index", index, value))), + Self::Runs(runs) => runs + .first_value_above(max) + .map(|(run, value)| ("run", run, value)), + }; + if let Some((position_type, position, value)) = invalid { + return Err(Error::invalid_input_source( + format!( + "Invalid {level_type} level {value} at {position_type} {position}: maximum is {max}" + ) + .into(), + )); + } + Ok(()) + } + + /// Advance `cursor` to the start of row `target_row`, returning that row's + /// starting level index. + /// + /// Rows begin at `max_rep` positions, so this finds the `target_row`-th one. + /// `target_row` must be `>= cursor.row`: the cursor only moves forward, which + /// is what keeps a full page decode O(runs) rather than O(rows). + fn seek_row_start( + &self, + cursor: &mut LevelCursor, + target_row: u64, + max_rep: u16, + ) -> Result { + let mut need = target_row.checked_sub(cursor.row).ok_or_else(|| { + Error::internal(format!( + "Complex all-null row ranges are not sorted: target row {target_row} follows {}", + cursor.row + )) + })?; + if need == 0 { + return Ok(cursor.level); + } + match self { + Self::Dense(buf) => { + let mut level = cursor.level; + while need > 0 { + if level >= buf.len() { + return Err(Error::internal( + "Invalid complex all-null layout: repetition buffer too short", + )); + } + if buf[level] != max_rep { + return Err(Error::internal( + "Invalid complex all-null layout: row did not start at max repetition level", + )); + } + level += 1; + while level < buf.len() && buf[level] != max_rep { + level += 1; + } + need -= 1; + } + cursor.level = level; + cursor.row = target_row; + Ok(level) + } + Self::Runs(runs) => { + let mut level = cursor.level; + let mut run = cursor.run; + runs.seek(&mut run, level); + while need > 0 { + if run.run >= runs.num_runs() { + return Err(Error::internal( + "Invalid complex all-null layout: repetition buffer too short", + )); + } + if runs.value(run.run) != max_rep { + return Err(Error::internal( + "Invalid complex all-null layout: row did not start at max repetition level", + )); + } + let avail = (run.end - level) as u64; + if need < avail { + // Target lands inside this max-rep run. + level += need as usize; + need = 0; + } else { + // Consume every row start in this run, then skip the + // trailing non-max-rep runs to reach the next row start. + need -= avail; + runs.advance(&mut run); + while run.run < runs.num_runs() && runs.value(run.run) != max_rep { + runs.advance(&mut run); + } + level = if run.run < runs.num_runs() { + run.start + } else { + self.len() + }; + } + } + cursor.level = level; + cursor.row = target_row; + cursor.run = run; + Ok(level) + } + } + } + + /// Count of levels in `range` that are `<= max`, resuming from `*run_cursor` + /// and leaving it on the last run that overlaps `range`. + /// + /// Successive calls must pass ascending, non-overlapping ranges (`range.start + /// >=` the previous `range.end`) so runs are swept at most once per page. + fn count_le_cursor( + &self, + run_cursor: &mut RunPosition, + range: Range, + max: u16, + ) -> (u64, RunPosition) { + if range.is_empty() { + return (0, *run_cursor); + } + match self { + Self::Dense(buf) => ( + buf[range].iter().filter(|&&d| d <= max).count() as u64, + RunPosition::default(), + ), + Self::Runs(runs) => { + // Advance to the first run overlapping the range. + runs.seek(run_cursor, range.start); + let start = *run_cursor; + let mut count = 0u64; + let mut current = *run_cursor; + while current.run < runs.num_runs() && current.start < range.end { + if runs.value(current.run) <= max { + let lo = current.start.max(range.start); + let hi = current.end.min(range.end); + count += (hi - lo) as u64; + } + if current.end >= range.end { + break; + } + runs.advance(&mut current); + } + // Resume the next (ascending) range from the last overlapping run; + // `current` remains valid because `range` is non-empty. + *run_cursor = current; + (count, start) + } + } + } + + fn extend_into(&self, range: Range, run: RunPosition, out: &mut Vec) { + if range.is_empty() { + return; + } + match self { + Self::Dense(buf) => out.extend_from_slice(&buf[range]), + Self::Runs(runs) => { + let mut current = run; + runs.seek(&mut current, range.start); + while current.run < runs.num_runs() && current.start < range.end { + let lo = current.start.max(range.start); + let hi = current.end.min(range.end); + if hi > lo { + out.resize(out.len() + (hi - lo), runs.value(current.run)); + } + runs.advance(&mut current); + } + } + } + } + + #[cfg(test)] + fn deep_size(&self) -> usize { + self.deep_size_of_children(&mut Context::new()) + } +} + +impl DeepSizeOf for LazyLevels { + fn deep_size_of_children(&self, ctx: &mut Context) -> usize { + match self { + Self::Dense(buf) => buf.deep_size_of_children(ctx), + Self::Runs(runs) => { + let pointer = Arc::as_ptr(runs) as *const () as usize; + if ctx.mark_seen(pointer) { + std::mem::size_of_val(runs.as_ref()) + runs.deep_size() + } else { + 0 + } + } + } + } +} + +fn validate_complex_all_null_levels( + rep: &Option, + def: &Option, + max_rep: u16, + max_def: u16, +) -> Result<()> { + if let Some(rep) = rep { + rep.validate_max("repetition", max_rep)?; + } + if let Some(def) = def { + def.validate_max("definition", max_def)?; + } + if let (Some(rep), Some(def)) = (rep, def) + && rep.len() != def.len() + { + return Err(Error::invalid_input_source( + format!( + "Mismatched complex all-null level counts: repetition has {}, definition has {}", + rep.len(), + def.len() + ) + .into(), + )); + } + Ok(()) +} + +fn expected_level_bytes(num_values: u64, level_type: &str) -> Result { + usize::try_from(num_values) + .ok() + .and_then(|num_values| num_values.checked_mul(std::mem::size_of::())) + .ok_or_else(|| { + Error::invalid_input_source( + format!("{level_type} level count {num_values} does not fit in memory").into(), + ) + }) +} + +fn dense_levels_from_block( + decompressed: DataBlock, + num_values: u64, + level_type: &str, +) -> Result { + let DataBlock::FixedWidth(block) = decompressed else { + return Err(Error::invalid_input_source( + format!("Expected fixed-width data block for {level_type} levels").into(), + )); + }; + if block.num_values != num_values { + return Err(Error::invalid_input_source( + format!( + "Unexpected {level_type} level count after decompression: expected {num_values}, got {}", + block.num_values + ) + .into(), + )); + } + if block.bits_per_value != 16 { + return Err(Error::invalid_input_source( + format!( + "Unexpected {level_type} level bit width after decompression: expected 16, got {}", + block.bits_per_value + ) + .into(), + )); + } + let expected_bytes = expected_level_bytes(num_values, level_type)?; + if block.data.len() != expected_bytes { + return Err(Error::invalid_input_source( + format!( + "Unexpected decompressed {level_type} level size: expected {expected_bytes} bytes for {num_values} values, got {}", + block.data.len() + ) + .into(), + )); + } + Ok(LazyLevels::Dense(block.data.borrow_to_typed_slice::())) +} + +#[derive(Debug)] +struct CachedComplexAllNullState { + rep: Option, + def: Option, +} + +impl DeepSizeOf for CachedComplexAllNullState { + fn deep_size_of_children(&self, ctx: &mut Context) -> usize { + self.rep.deep_size_of_children(ctx) + self.def.deep_size_of_children(ctx) + } +} + +impl CachedPageData for CachedComplexAllNullState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +/// A scheduler for all-null data that has repetition and definition levels +/// +/// We still need to do some I/O in this case because we need to figure out what kind of null we +/// are dealing with (null list, null struct, what level null struct, etc.) +/// +/// TODO: Right now we just load the entire rep/def at initialization time and cache it. This is a touch +/// RAM aggressive and maybe we want something more lazy in the future. On the other hand, it's simple +/// and fast so...maybe not :) +#[derive(Debug)] +pub struct ComplexAllNullScheduler { + // Set from protobuf + buffer_offsets_and_sizes: Arc<[(u64, u64)]>, + def_meaning: Arc<[DefinitionInterpretation]>, + repdef: Option>, + max_rep: u16, + max_def: u16, + max_visible_level: u16, + rep_codec: LevelCodec, + def_codec: LevelCodec, + num_rep_values: u64, + num_def_values: u64, +} + +impl ComplexAllNullScheduler { + pub(crate) fn new( + buffer_offsets_and_sizes: Arc<[(u64, u64)]>, + def_meaning: Arc<[DefinitionInterpretation]>, + rep_codec: LevelCodec, + def_codec: LevelCodec, + num_rep_values: u64, + num_def_values: u64, + ) -> Self { + let max_rep = def_meaning.iter().filter(|l| l.is_list()).count() as u16; + let max_def = def_meaning + .iter() + .map(|meaning| meaning.num_def_levels()) + .sum::(); + let max_visible_level = def_meaning + .iter() + .take_while(|l| !l.is_list()) + .map(|l| l.num_def_levels()) + .sum::(); + Self { + buffer_offsets_and_sizes, + def_meaning, + repdef: None, + max_rep, + max_def, + max_visible_level, + rep_codec, + def_codec, + num_rep_values, + num_def_values, + } + } +} + +impl StructuralPageScheduler for ComplexAllNullScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + // Fully load the rep & def buffers, as needed + let (rep_pos, rep_size) = self.buffer_offsets_and_sizes[0]; + let (def_pos, def_size) = self.buffer_offsets_and_sizes[1]; + let has_rep = rep_size > 0; + let has_def = def_size > 0; + + let mut reads = Vec::with_capacity(2); + if has_rep { + reads.push(rep_pos..rep_pos + rep_size); + } + if has_def { + reads.push(def_pos..def_pos + def_size); + } + + let data = io.submit_request(reads, 0); + let rep_codec = self.rep_codec.clone(); + let def_codec = self.def_codec.clone(); + let num_rep_values = self.num_rep_values; + let num_def_values = self.num_def_values; + let max_rep = self.max_rep; + let max_def = self.max_def; + + async move { + let data = data.await?; + let mut data_iter = data.into_iter(); + + // RLE levels select the smallest validated cache representation; + // everything else expands eagerly to `LazyLevels::Dense`. + let build_levels = |compressed_bytes: Bytes, + codec: &LevelCodec, + num_values: u64, + level_type: &str| + -> Result { + match codec { + LevelCodec::Uncompressed => { + if num_values == 0 { + if !compressed_bytes + .len() + .is_multiple_of(std::mem::size_of::()) + { + return Err(Error::invalid_input_source( + format!( + "Unexpected uncompressed {level_type} level size: {} bytes is not divisible by {}", + compressed_bytes.len(), + std::mem::size_of::() + ) + .into(), + )); + } + } else { + let expected_bytes = expected_level_bytes(num_values, level_type)?; + if compressed_bytes.len() != expected_bytes { + return Err(Error::invalid_input_source( + format!( + "Unexpected uncompressed {level_type} level size: expected {expected_bytes} bytes for {num_values} values, got {}", + compressed_bytes.len() + ) + .into(), + )); + } + } + let buffer = LanceBuffer::from_bytes(compressed_bytes, 2); + Ok(LazyLevels::Dense(buffer.borrow_to_typed_slice::())) + } + LevelCodec::Rle(decompressor) => { + let frame = LanceBuffer::from_bytes(compressed_bytes, 1); + let runs = decompressor.decode_u16_runs(frame, num_values)?; + LazyLevels::from_rle_runs(runs) + } + LevelCodec::Block(decompressor) => { + let frame = LanceBuffer::from_bytes(compressed_bytes, 1); + let decompressed = decompressor.decompress(frame, num_values)?; + dense_levels_from_block(decompressed, num_values, level_type) + } + } + }; + + let rep = if has_rep { + let rep = data_iter.next().unwrap(); + Some(build_levels(rep, &rep_codec, num_rep_values, "repetition")?) + } else { + None + }; + + let def = if has_def { + let def = data_iter.next().unwrap(); + Some(build_levels(def, &def_codec, num_def_values, "definition")?) + } else { + None + }; + + validate_complex_all_null_levels(&rep, &def, max_rep, max_def)?; + let repdef = Arc::new(CachedComplexAllNullState { rep, def }); + + self.repdef = Some(repdef.clone()); + + Ok(repdef as Arc) + } + .boxed() + } + + fn load(&mut self, data: &Arc) { + self.repdef = Some( + data.clone() + .as_arc_any() + .downcast::() + .unwrap(), + ); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + _io: &Arc, + ) -> Result> { + let ranges = VecDeque::from_iter(ranges.iter().cloned()); + let num_rows = ranges.iter().map(|r| r.end - r.start).sum::(); + let decoder = Box::new(ComplexAllNullPageDecoder { + ranges, + rep: self.repdef.as_ref().unwrap().rep.clone(), + def: self.repdef.as_ref().unwrap().def.clone(), + num_rows, + def_meaning: self.def_meaning.clone(), + max_rep: self.max_rep, + max_visible_level: self.max_visible_level, + rep_cursor: LevelCursor::default(), + def_run_cursor: RunPosition::default(), + }) as Box; + let page_load_task = PageLoadTask { + decoder_fut: std::future::ready(Ok(decoder)).boxed(), + num_rows, + }; + Ok(vec![page_load_task]) + } +} + +#[derive(Debug)] +pub struct ComplexAllNullPageDecoder { + ranges: VecDeque>, + rep: Option, + def: Option, + num_rows: u64, + def_meaning: Arc<[DefinitionInterpretation]>, + max_rep: u16, + max_visible_level: u16, + /// Monotonic cursor into `rep` tracking the current row's level start. + rep_cursor: LevelCursor, + /// Monotonic run cursor into `def` for `count_le_cursor`. + def_run_cursor: RunPosition, +} + +impl ComplexAllNullPageDecoder { + fn drain_ranges(&mut self, num_rows: u64) -> Vec> { + let mut rows_desired = num_rows; + let mut ranges = Vec::with_capacity(self.ranges.len()); + while rows_desired > 0 { + let front = self.ranges.front_mut().unwrap(); + let avail = front.end - front.start; + if avail > rows_desired { + ranges.push(front.start..front.start + rows_desired); + front.start += rows_desired; + rows_desired = 0; + } else { + ranges.push(self.ranges.pop_front().unwrap()); + rows_desired -= avail; + } + } + ranges + } + + /// Level index at which row `target_row` starts, advancing the monotonic + /// repetition cursor. Callers must request non-decreasing `target_row`. + fn seek_row_start(&mut self, target_row: u64) -> Result { + match &self.rep { + Some(rep) => rep.seek_row_start(&mut self.rep_cursor, target_row, self.max_rep), + None => { + // Without repetition every level is its own row. + self.rep_cursor.row = target_row; + self.rep_cursor.level = target_row as usize; + Ok(target_row as usize) + } + } + } + + /// Number of visible items in the level range `levels` (definition levels + /// `<= max_visible_level`), advancing the monotonic definition cursor. + fn count_visible(&mut self, levels: Range) -> Result<(u64, RunPosition)> { + match &self.def { + Some(def) => { + if levels.end > def.len() { + return Err(Error::internal( + "Invalid complex all-null layout: definition buffer too short", + )); + } + Ok(def.count_le_cursor(&mut self.def_run_cursor, levels, self.max_visible_level)) + } + None => Ok(((levels.end - levels.start) as u64, RunPosition::default())), + } + } +} + +impl StructuralPageDecoder for ComplexAllNullPageDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let drained_ranges = self.drain_ranges(num_rows); + let mut level_slices: Vec = Vec::with_capacity(drained_ranges.len()); + let mut visible_items_total = 0; + + // Each row range is one contiguous level slice `[start_row_level, + // end_row_level)`, so we seek both boundaries and count its visibility at + // once rather than per row. The cursors only move forward, so locating and + // counting all requested ranges visits each intervening run at most once. + for range in drained_ranges { + let level_start = self.seek_row_start(range.start)?; + let rep_run = self.rep_cursor.run; + let level_end = self.seek_row_start(range.end)?; + let (visible_items, def_run) = self.count_visible(level_start..level_end)?; + visible_items_total += visible_items; + if let Some(last) = level_slices.last_mut() + && last.range.end == level_start + { + last.range.end = level_end; + } else { + level_slices.push(LevelSlice { + range: level_start..level_end, + rep_run, + def_run, + }); + } + } + + Ok(Box::new(DecodeComplexAllNullTask { + level_slices, + visible_items_total, + rep: self.rep.clone(), + def: self.def.clone(), + def_meaning: self.def_meaning.clone(), + max_visible_level: self.max_visible_level, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// We use `level_slices` to slice into `rep` and `def` and create rep/def buffers +/// for the null data. +#[derive(Debug, Clone)] +struct LevelSlice { + range: Range, + rep_run: RunPosition, + def_run: RunPosition, +} + +#[derive(Clone, Copy)] +enum LevelKind { + Repetition, + Definition, +} + +impl LevelSlice { + fn run(&self, kind: LevelKind) -> RunPosition { + match kind { + LevelKind::Repetition => self.rep_run, + LevelKind::Definition => self.def_run, + } + } +} + +#[derive(Debug)] +pub struct DecodeComplexAllNullTask { + level_slices: Vec, + visible_items_total: u64, + rep: Option, + def: Option, + def_meaning: Arc<[DefinitionInterpretation]>, + max_visible_level: u16, +} + +impl DecodeComplexAllNullTask { + fn decode_level(&self, levels: &Option, kind: LevelKind) -> Option> { + levels.as_ref().map(|levels| { + let num_levels = self + .level_slices + .iter() + .map(|slice| slice.range.end - slice.range.start) + .sum(); + let mut referenced_levels = Vec::with_capacity(num_levels); + for slice in &self.level_slices { + levels.extend_into(slice.range.clone(), slice.run(kind), &mut referenced_levels); + } + referenced_levels + }) + } +} + +impl DecodePageTask for DecodeComplexAllNullTask { + fn decode(self: Box) -> Result { + let rep = self.decode_level(&self.rep, LevelKind::Repetition); + let def = self.decode_level(&self.def, LevelKind::Definition); + + // If there are definition levels there may be empty / null lists which are not visible + // in the items array. We need to account for that here to figure out how many values + // should be in the items array. + let num_values = if let Some(def) = &def { + def.iter().filter(|&d| *d <= self.max_visible_level).count() as u64 + } else { + self.visible_items_total + }; + + let data = DataBlock::AllNull(AllNullDataBlock { num_values }); + let unraveler = RepDefUnraveler::new(rep, def, self.def_meaning, num_values); + Ok(DecodedPage { + data, + repdef: unraveler, + }) + } +} + +/// A scheduler for simple all-null data +/// +/// "simple" all-null data is data that is all null and only has a single level of definition and +/// no repetition. We don't need to read any data at all in this case. +#[derive(Debug, Default)] +pub struct SimpleAllNullScheduler {} + +impl StructuralPageScheduler for SimpleAllNullScheduler { + fn initialize<'a>( + &'a mut self, + _io: &Arc, + ) -> BoxFuture<'a, Result>> { + std::future::ready(Ok(Arc::new(NoCachedPageData) as Arc)).boxed() + } + + fn load(&mut self, _cache: &Arc) {} + + fn schedule_ranges( + &self, + ranges: &[Range], + _io: &Arc, + ) -> Result> { + let num_rows = ranges.iter().map(|r| r.end - r.start).sum::(); + let decoder = + Box::new(SimpleAllNullPageDecoder { num_rows }) as Box; + let page_load_task = PageLoadTask { + decoder_fut: std::future::ready(Ok(decoder)).boxed(), + num_rows, + }; + Ok(vec![page_load_task]) + } +} + +/// A page decode task for all-null data without any +/// repetition and only a single level of definition +#[derive(Debug)] +struct SimpleAllNullDecodePageTask { + num_values: u64, +} +impl DecodePageTask for SimpleAllNullDecodePageTask { + fn decode(self: Box) -> Result { + let unraveler = RepDefUnraveler::new( + None, + Some(vec![1; self.num_values as usize]), + Arc::new([DefinitionInterpretation::NullableItem]), + self.num_values, + ); + Ok(DecodedPage { + data: DataBlock::AllNull(AllNullDataBlock { + num_values: self.num_values, + }), + repdef: unraveler, + }) + } +} + +#[derive(Debug)] +pub struct SimpleAllNullPageDecoder { + num_rows: u64, +} + +impl StructuralPageDecoder for SimpleAllNullPageDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + Ok(Box::new(SimpleAllNullDecodePageTask { + num_values: num_rows, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +#[derive(Debug, Clone)] +struct MiniBlockSchedulerDictionary { + // These come from the protobuf + dictionary_decompressor: Arc, + dictionary_buf_position_and_size: (u64, u64), + dictionary_data_alignment: u64, + num_dictionary_items: u64, +} + +/// State that is loaded once and cached for future lookups +#[derive(Debug)] +struct MiniBlockCacheableState { + /// Compact per-chunk index (byte ranges + row/item mapping) for the page + chunk_index: MiniBlockChunkIndex, + /// The dictionary for the page, if any + dictionary: Option>, +} + +impl DeepSizeOf for MiniBlockCacheableState { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.chunk_index.deep_size_of_children(context) + + self + .dictionary + .as_ref() + .map(|dict| dict.data_size() as usize) + .unwrap_or(0) + } +} + +impl CachedPageData for MiniBlockCacheableState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +/// A scheduler for a page that has been encoded with the mini-block layout +/// +/// Scheduling mini-block encoded data is simple in concept and somewhat complex +/// in practice. +/// +/// First, during initialization, we load the chunk metadata, the repetition index, +/// and the dictionary (these last two may not be present) +/// +/// Then, during scheduling, we use the user's requested row ranges and the repetition +/// index to determine which chunks we need and which rows we need from those chunks. +/// +/// For example, if the repetition index is: [50, 3], [50, 0], [10, 0] and the range +/// from the user is 40..60 then we need to: +/// +/// - Read the first chunk and skip the first 40 rows, then read 10 full rows, and +/// then read 3 items for the 11th row of our range. +/// - Read the second chunk and read the remaining items in our 11th row and then read +/// the remaining 9 full rows. +/// +/// Then, if we are going to decode that in batches of 5, we need to make decode tasks. +/// The first two decode tasks will just need the first chunk. The third decode task will +/// need the first chunk (for the trailer which has the 11th row in our range) and the second +/// chunk. The final decode task will just need the second chunk. +/// +/// The above prose descriptions are what are represented by `ChunkInstructions` and +/// `ChunkDrainInstructions`. +#[derive(Debug)] +pub struct MiniBlockScheduler { + // These come from the protobuf + buffer_offsets_and_sizes: Vec<(u64, u64)>, + priority: u64, + items_in_page: u64, + repetition_index_depth: u16, + num_buffers: u64, + rep_decompressor: Option>, + def_decompressor: Option>, + value_decompressor: Arc, + def_meaning: Arc<[DefinitionInterpretation]>, + dictionary: Option, + // This is set after initialization + page_meta: Option>, + has_large_chunk: bool, +} + +impl MiniBlockScheduler { + fn try_new( + buffer_offsets_and_sizes: &[(u64, u64)], + priority: u64, + items_in_page: u64, + layout: &pb21::MiniBlockLayout, + decompressors: &dyn DecompressionStrategy, + ) -> Result { + let rep_decompressor = layout + .rep_compression + .as_ref() + .map(|rep_compression| { + decompressors + .create_block_decompressor(rep_compression) + .map(Arc::from) + }) + .transpose()?; + let def_decompressor = layout + .def_compression + .as_ref() + .map(|def_compression| { + decompressors + .create_block_decompressor(def_compression) + .map(Arc::from) + }) + .transpose()?; + let def_meaning = layout + .layers + .iter() + .map(|l| ProtobufUtils21::repdef_layer_to_def_interp(*l)) + .collect::>(); + let value_decompressor = decompressors.create_miniblock_decompressor( + layout.value_compression.as_ref().unwrap(), + decompressors, + )?; + + let dictionary = if let Some(dictionary_encoding) = layout.dictionary.as_ref() { + let num_dictionary_items = layout.num_dictionary_items; + let dictionary_decompressor = decompressors + .create_block_decompressor(dictionary_encoding)? + .into(); + let dictionary_data_alignment = match dictionary_encoding.compression.as_ref().unwrap() + { + Compression::Variable(_) => 4, + Compression::Flat(_) => 16, + Compression::General(_) => 1, + Compression::InlineBitpacking(_) | Compression::OutOfLineBitpacking(_) => { + crate::encoder::MIN_PAGE_BUFFER_ALIGNMENT + } + _ => { + return Err(Error::invalid_input_source( + format!( + "Unsupported mini-block dictionary encoding: {:?}", + dictionary_encoding.compression.as_ref().unwrap() + ) + .into(), + )); + } + }; + Some(MiniBlockSchedulerDictionary { + dictionary_decompressor, + dictionary_buf_position_and_size: buffer_offsets_and_sizes[2], + dictionary_data_alignment, + num_dictionary_items, + }) + } else { + None + }; + + Ok(Self { + buffer_offsets_and_sizes: buffer_offsets_and_sizes.to_vec(), + rep_decompressor, + def_decompressor, + value_decompressor: value_decompressor.into(), + repetition_index_depth: layout.repetition_index_depth as u16, + num_buffers: layout.num_buffers, + priority, + items_in_page, + dictionary, + def_meaning: def_meaning.into(), + page_meta: None, + has_large_chunk: layout.has_large_chunk, + }) + } + + fn lookup_chunks(&self, chunk_indices: &[usize]) -> Vec { + let chunk_index = &self.page_meta.as_ref().unwrap().chunk_index; + chunk_indices + .iter() + .map(|&chunk_idx| LoadedChunk { + byte_range: chunk_index.byte_range(chunk_idx), + items_in_chunk: chunk_index.items_in_chunk(chunk_idx), + chunk_idx, + data: LanceBuffer::empty(), + }) + .collect() + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum PreambleAction { + Take, + Skip, + Absent, +} + +// When we schedule a chunk we use the repetition index (or, if none exists, just the # of items +// in each chunk) to map a user requested range into a set of ChunkInstruction objects which tell +// us how exactly to read from the chunk. +// +// Examples: +// +// | Chunk 0 | Chunk 1 | Chunk 2 | Chunk 3 | +// | xxxxyyyyzzz | zzzzzzzzz | zzzzzzzzz | aaabbcc | +// +// Full read (0..6) +// +// Chunk 0: (several rows, ends with trailer) +// preamble: absent +// rows_to_skip: 0 +// rows_to_take: 3 (x, y, z) +// take_trailer: true +// +// Chunk 1: (all preamble, ends with trailer) +// preamble: take +// rows_to_skip: 0 +// rows_to_take: 0 +// take_trailer: true +// +// Chunk 2: (all preamble, no trailer) +// preamble: take +// rows_to_skip: 0 +// rows_to_take: 0 +// take_trailer: false +// +// Chunk 3: (several rows, no trailer or preamble) +// preamble: absent +// rows_to_skip: 0 +// rows_to_take: 3 (a, b, c) +// take_trailer: false +#[derive(Clone, Debug, PartialEq, Eq)] +struct ChunkInstructions { + // The index of the chunk to read + chunk_idx: usize, + // A "preamble" is when a chunk begins with a continuation of the previous chunk's list. If there + // is no repetition index there is never a preamble. + // + // It's possible for a chunk to be entirely premable. For example, if there is a really large list + // that spans several chunks. + preamble: PreambleAction, + // How many complete rows (not including the preamble or trailer) to skip + // + // If this is non-zero then premable must not be Take + rows_to_skip: u64, + // How many rows to take. If a row splits across chunks then we will count the row in the first + // chunk that contains the row. + rows_to_take: u64, + // A "trailer" is when a chunk ends with a partial list. If there is no repetition index there is + // never a trailer. + // + // A chunk that is all preamble may or may not have a trailer. + // + // If this is true then we want to include the trailer + take_trailer: bool, +} + +// First, we schedule a bunch of [`ChunkInstructions`] based on the users ranges. Then we +// start decoding them, based on a batch size, which might not align with what we scheduled. +// +// This results in `ChunkDrainInstructions` which targets a contiguous slice of a `ChunkInstructions` +// +// So if `ChunkInstructions` is "skip preamble, skip 10, take 50, take trailer" and we are decoding in +// batches of size 10 we might have a `ChunkDrainInstructions` that targets that chunk and has its own +// skip of 17 and take of 10. This would mean we decode the chunk, skip the preamble and 27 rows, and +// then take 10 rows. +// +// One very confusing bit is that `rows_to_take` includes the trailer. So if we have two chunks: +// -no preamble, skip 5, take 10, take trailer +// -take preamble, skip 0, take 50, no trailer +// +// and we are draining 20 rows then the drain instructions for the first batch will be: +// - no preamble, skip 0 (from chunk 0), take 11 (from chunk 0) +// - take preamble (from chunk 1), skip 0 (from chunk 1), take 9 (from chunk 1) +#[derive(Debug, PartialEq, Eq)] +struct ChunkDrainInstructions { + chunk_instructions: ChunkInstructions, + rows_to_skip: u64, + rows_to_take: u64, + preamble_action: PreambleAction, +} + +impl ChunkInstructions { + // Given a repetition index and a set of user ranges we need to figure out how to read from the chunks + // + // We assume that `user_ranges` are in sorted order and non-overlapping + // + // The output will be a set of `ChunkInstructions` which tell us how to read from the chunks + fn schedule_instructions( + chunk_index: &MiniBlockChunkIndex, + user_ranges: &[Range], + ) -> Vec { + // Bind the per-page chunk count once; re-deriving it each iteration + // costs a width match plus a length read. + let num_chunks = chunk_index.num_chunks(); + // This is an in-exact capacity guess but pretty good. The actual capacity can be + // smaller if instructions are merged. It can be larger if there are multiple instructions + // per row which can happen with lists. + let mut chunk_instructions = Vec::with_capacity(user_ranges.len()); + + for user_range in user_ranges { + let mut rows_needed = user_range.end - user_range.start; + let mut need_preamble = false; + + // Need to find the first chunk with a first row >= user_range.start. If there are + // multiple chunks with the same first row we need to take the first one. + let mut block_index = chunk_index.find_chunk(user_range.start); + + let mut to_skip = user_range.start - chunk_index.first_row(block_index); + + while rows_needed > 0 || need_preamble { + // Check if we've gone past the last block (should not happen) + if block_index >= num_chunks { + log::warn!( + "schedule_instructions inconsistency: block_index >= num_chunks, exiting early" + ); + break; + } + + let starts_including_trailer = chunk_index.rows_in_chunk(block_index); + let has_preamble = chunk_index.has_preamble(block_index); + let has_trailer = chunk_index.has_trailer(block_index); + let rows_avail = starts_including_trailer.saturating_sub(to_skip); + + // Handle blocks that are entirely preamble (rows_avail = 0) + // These blocks have no rows to take but may have a preamble we need + // We only look for preamble if to_skip == 0 (we're not skipping rows) + if rows_avail == 0 && to_skip == 0 { + // Only process if this chunk has a preamble we need + if has_preamble && need_preamble { + chunk_instructions.push(Self { + chunk_idx: block_index, + preamble: PreambleAction::Take, + rows_to_skip: 0, + rows_to_take: 0, + // We still need to look at has_trailer to distinguish between "all preamble + // and row ends at end of chunk" and "all preamble and row bleeds into next + // chunk". Both cases will have 0 rows available. + take_trailer: has_trailer, + }); + // Only set need_preamble = false if the chunk has at least one row, + // Or we are reaching the last block, + // Otherwise, the chunk is entirely preamble and we need the next chunk's preamble too + if starts_including_trailer > 0 || block_index == num_chunks - 1 { + need_preamble = false; + } + } + // Move to next block + block_index += 1; + continue; + } + + // Edge case: if rows_avail == 0 but to_skip > 0 + // This theoretically shouldn't happen (binary search should avoid it) + // but handle it for safety + if rows_avail == 0 && to_skip > 0 { + // This block doesn't have enough rows to skip, move to next block + // Adjust to_skip by the number of rows in this block + to_skip -= starts_including_trailer; + block_index += 1; + continue; + } + + let rows_to_take = rows_avail.min(rows_needed); + rows_needed -= rows_to_take; + + let mut take_trailer = false; + let preamble = if has_preamble { + if need_preamble { + PreambleAction::Take + } else { + PreambleAction::Skip + } + } else { + PreambleAction::Absent + }; + + // Are we taking the trailer? If so, make sure we mark that we need the preamble + if rows_to_take == rows_avail && has_trailer { + take_trailer = true; + need_preamble = true; + } else { + need_preamble = false; + }; + + chunk_instructions.push(Self { + preamble, + chunk_idx: block_index, + rows_to_skip: to_skip, + rows_to_take, + take_trailer, + }); + + to_skip = 0; + block_index += 1; + } + } + + // If there were multiple ranges we may have multiple instructions for a single chunk. Merge them now if they + // are _adjacent_ (i.e. don't merge "take first row of chunk 0" and "take third row of chunk 0" into "take 2 + // rows of chunk 0 starting at 0") + if user_ranges.len() > 1 { + // Merge adjacent instructions in place. `write` indexes the last + // retained instruction; each following instruction is either folded + // into it (contiguous within the same chunk) or compacted forward. + let mut write = 0; + for read in 1..chunk_instructions.len() { + let merges = { + let last = &chunk_instructions[write]; + let candidate = &chunk_instructions[read]; + last.chunk_idx == candidate.chunk_idx + && last.rows_to_take + last.rows_to_skip == candidate.rows_to_skip + }; + if merges { + let rows_to_take = chunk_instructions[read].rows_to_take; + let take_trailer = chunk_instructions[read].take_trailer; + let last = &mut chunk_instructions[write]; + last.rows_to_take += rows_to_take; + last.take_trailer |= take_trailer; + } else { + write += 1; + if write != read { + chunk_instructions.swap(write, read); + } + } + } + chunk_instructions.truncate(write + 1); + } + chunk_instructions + } + + fn drain_from_instruction( + &self, + rows_desired: &mut u64, + need_preamble: &mut bool, + skip_in_chunk: &mut u64, + ) -> (ChunkDrainInstructions, bool) { + // If we need the premable then we shouldn't be skipping anything + debug_assert!(!*need_preamble || *skip_in_chunk == 0); + let rows_avail = self.rows_to_take - *skip_in_chunk; + let has_preamble = self.preamble != PreambleAction::Absent; + let preamble_action = match (*need_preamble, has_preamble) { + (true, true) => PreambleAction::Take, + (true, false) => panic!("Need preamble but there isn't one"), + (false, true) => PreambleAction::Skip, + (false, false) => PreambleAction::Absent, + }; + + // How many rows are we actually taking in this take step (including the preamble + // and trailer both as individual rows) + let rows_taking = if *rows_desired >= rows_avail { + // We want all the rows. If there is a trailer we are grabbing it and will need + // the preamble of the next chunk + // If there is a trailer and we are taking all the rows then we need the preamble + // of the next chunk. + // + // Also, if this chunk is entirely preamble (rows_avail == 0 && !take_trailer) then we + // need the preamble of the next chunk. + *need_preamble = self.take_trailer; + rows_avail + } else { + // We aren't taking all the rows. Even if there is a trailer we aren't taking + // it so we will not need the preamble + *need_preamble = false; + *rows_desired + }; + let rows_skipped = *skip_in_chunk; + + // Update the state for the next iteration + let consumed_chunk = if *rows_desired >= rows_avail { + *rows_desired -= rows_avail; + *skip_in_chunk = 0; + true + } else { + *skip_in_chunk += *rows_desired; + *rows_desired = 0; + false + }; + + ( + ChunkDrainInstructions { + chunk_instructions: self.clone(), + rows_to_skip: rows_skipped, + rows_to_take: rows_taking, + preamble_action, + }, + consumed_chunk, + ) + } +} + +enum Words { + U16(ScalarBuffer), + U32(ScalarBuffer), +} + +struct WordsIter<'a> { + iter: Box + 'a>, +} + +impl Words { + pub fn len(&self) -> usize { + match self { + Self::U16(b) => b.len(), + Self::U32(b) => b.len(), + } + } + + pub fn iter(&self) -> WordsIter<'_> { + match self { + Self::U16(buf) => WordsIter { + iter: Box::new(buf.iter().map(|&x| x as u32)), + }, + Self::U32(buf) => WordsIter { + iter: Box::new(buf.iter().copied()), + }, + } + } + + pub fn from_bytes(bytes: Bytes, has_large_chunk: bool) -> Result { + let bytes_per_value = if has_large_chunk { 4 } else { 2 }; + assert_eq!(bytes.len() % bytes_per_value, 0); + let buffer = LanceBuffer::from_bytes(bytes, bytes_per_value as u64); + if has_large_chunk { + Ok(Self::U32(buffer.borrow_to_typed_slice::())) + } else { + Ok(Self::U16(buffer.borrow_to_typed_slice::())) + } + } +} + +impl<'a> Iterator for WordsIter<'a> { + type Item = u32; + + fn next(&mut self) -> Option { + self.iter.next() + } +} + +/// Per-chunk leaf value-count analysis derived from the metadata words. +/// +/// `values_per_chunk` is the count shared by every non-last chunk (meaningful +/// when `uniform`), and `last_chunk_values` is the final chunk's count. +struct FlatValueCounts { + logs: Vec, + uniform: bool, + values_per_chunk: u64, + last_chunk_values: u64, +} + +fn analyze_value_counts(words: &Words, items_in_page: u64) -> Result { + let num_chunks = words.len(); + let logs = words.iter().map(|w| (w & 0x0F) as u8).collect::>(); + let mut counted = 0u64; + for (chunk_index, &log) in logs.iter().take(num_chunks.saturating_sub(1)).enumerate() { + if log == 0 { + return Err(Error::corrupt_file_named( + "miniblock_metadata", + format!( + "non-final chunk {chunk_index} of {num_chunks} has invalid log_num_values=0" + ), + )); + } + counted = counted.checked_add(1u64 << log).ok_or_else(|| { + Error::corrupt_file_named( + "miniblock_metadata", + format!( + "value count overflow at chunk {chunk_index}: counted_values={counted}, \ + log_num_values={log}, items_in_page={items_in_page}" + ), + ) + })?; + } + let last_chunk_values = items_in_page.checked_sub(counted).ok_or_else(|| { + Error::corrupt_file_named( + "miniblock_metadata", + format!( + "non-final chunks account for counted_values={counted}, exceeding \ + items_in_page={items_in_page}" + ), + ) + })?; + if let Some(&last_log) = logs.last() + && last_log != 0 + && (1u64 << last_log) != last_chunk_values + { + return Err(Error::corrupt_file_named( + "miniblock_metadata", + format!( + "final chunk log_num_values={last_log} does not match \ + last_chunk_values={last_chunk_values}: counted_values={counted}, \ + items_in_page={items_in_page}" + ), + )); + } + let uniform = num_chunks <= 1 || logs[..num_chunks - 1].iter().all(|&log| log == logs[0]); + // A single-chunk page has no "non-last" chunk to derive a stride from; use the + // page item count (min 1 so it stays a valid divisor in `find_chunk`). + let values_per_chunk = if num_chunks <= 1 { + items_in_page.max(1) + } else { + 1u64 << logs[0] + }; + Ok(FlatValueCounts { + logs, + uniform, + values_per_chunk, + last_chunk_values, + }) +} + +/// Iterator over per-chunk value counts for a non-uniform flat page. Non-last +/// chunks yield `1 << log`; the last yields the validated remaining item count. +fn flat_value_counts_iter(logs: &[u8], last_chunk_values: u64) -> impl Iterator + '_ { + let num_chunks = logs.len(); + (0..num_chunks).map(move |i| { + if i + 1 < num_chunks { + 1u64 << logs[i] + } else { + last_chunk_values + } + }) +} + +/// Builds the compact per-chunk index from the metadata words and, for nested +/// pages, the raw repetition-index bytes. The row axis is picked by page shape: +/// `UniformFlat` when all non-last chunks share a value count (fixed-width / +/// bitpacking), `Flat` for non-uniform flat pages (RLE / FSST), else `Nested`. +fn build_chunk_index( + words: &Words, + items_in_page: u64, + base: u64, + data_buf_size: u64, + rep_index_bytes: Option<&[u8]>, + repetition_index_depth: u16, +) -> Result { + let num_chunks = words.len(); + // Validate item counts before byte sizes because both share a metadata word, + // and an invalid count must not reach the final-chunk subtraction. + let value_counts = analyze_value_counts(words, items_in_page)?; + + // Each chunk stores `(divided_bytes + 1) * MINIBLOCK_ALIGNMENT` bytes, so the + // deltas are the chunk sizes and their grand total is the data buffer size. + let byte_starts = PrefixSums::from_deltas( + words + .iter() + .map(|word| ((word >> 4) as u64 + 1) * MINIBLOCK_ALIGNMENT as u64), + num_chunks, + data_buf_size, + ); + + // Nested pages track rows via the repetition index and keep leaf item counts + // separately; flat pages have row == value index, so value counts are rows. + let rows = if let Some(rep_index_data) = rep_index_bytes { + assert!(rep_index_data.len() % 8 == 0); + let stride = repetition_index_depth as usize + 1; + let (row_starts, has_trailer) = parse_nested_rep(rep_index_data, stride); + let item_counts = if value_counts.uniform { + ItemCounts::Uniform { + values_per_chunk: value_counts.values_per_chunk, + last_chunk_values: value_counts.last_chunk_values, + } + } else { + ItemCounts::PerChunkLog { + logs: value_counts.logs, + last_chunk_values: value_counts.last_chunk_values, + } + }; + RowMapping::Nested { + row_starts, + has_trailer, + item_counts, + } + } else { + if value_counts.uniform { + RowMapping::UniformFlat { + values_per_chunk: value_counts.values_per_chunk, + last_chunk_values: value_counts.last_chunk_values, + num_chunks, + } + } else { + let value_starts = PrefixSums::from_deltas( + flat_value_counts_iter(&value_counts.logs, value_counts.last_chunk_values), + num_chunks, + items_in_page, + ); + RowMapping::Flat { value_starts } + } + }; + + Ok(MiniBlockChunkIndex::new(base, byte_starts, rows)) +} + +impl StructuralPageScheduler for MiniBlockScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + // We always need to fetch chunk metadata. We may also need to fetch a dictionary and + // we may also need to fetch the repetition index. Here, we gather what buffers we + // need. + let (meta_buf_position, meta_buf_size) = self.buffer_offsets_and_sizes[0]; + let base = self.buffer_offsets_and_sizes[1].0; + let data_buf_size = self.buffer_offsets_and_sizes[1].1; + let mut bufs_needed = 1; + if self.dictionary.is_some() { + bufs_needed += 1; + } + if self.repetition_index_depth > 0 { + bufs_needed += 1; + } + let mut required_ranges = Vec::with_capacity(bufs_needed); + required_ranges.push(meta_buf_position..meta_buf_position + meta_buf_size); + if let Some(ref dictionary) = self.dictionary { + required_ranges.push( + dictionary.dictionary_buf_position_and_size.0 + ..dictionary.dictionary_buf_position_and_size.0 + + dictionary.dictionary_buf_position_and_size.1, + ); + } + if self.repetition_index_depth > 0 { + let (rep_index_pos, rep_index_size) = self.buffer_offsets_and_sizes.last().unwrap(); + required_ranges.push(*rep_index_pos..*rep_index_pos + *rep_index_size); + } + let io_req = io.submit_request(required_ranges, 0); + + async move { + let mut buffers = io_req.await?.into_iter().fuse(); + let meta_bytes = buffers.next().unwrap(); + let dictionary_bytes = self.dictionary.as_ref().and_then(|_| buffers.next()); + let rep_index_bytes = buffers.next(); + + let words = Words::from_bytes(meta_bytes, self.has_large_chunk)?; + let chunk_index = build_chunk_index( + &words, + self.items_in_page, + base, + data_buf_size, + rep_index_bytes.as_deref(), + self.repetition_index_depth, + )?; + + // decode dictionary + let dictionary = if let Some(ref mut dictionary) = self.dictionary { + let dictionary_data = dictionary_bytes.unwrap(); + Some(Arc::new(dictionary.dictionary_decompressor.decompress( + LanceBuffer::from_bytes(dictionary_data, dictionary.dictionary_data_alignment), + dictionary.num_dictionary_items, + )?)) + } else { + None + }; + + let page_meta = Arc::new(MiniBlockCacheableState { + chunk_index, + dictionary, + }); + self.page_meta = Some(page_meta.clone()); + Ok(page_meta as Arc) + } + .boxed() + } + + fn load(&mut self, data: &Arc) { + self.page_meta = Some( + data.clone() + .as_arc_any() + .downcast::() + .unwrap(), + ); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + let num_rows = ranges.iter().map(|r| r.end - r.start).sum(); + + let page_meta = self.page_meta.as_ref().unwrap(); + + let chunk_instructions = + ChunkInstructions::schedule_instructions(&page_meta.chunk_index, ranges); + + debug_assert_eq!( + num_rows, + chunk_instructions + .iter() + .map(|ci| ci.rows_to_take) + .sum::() + ); + + let chunks_needed = chunk_instructions + .iter() + .map(|ci| ci.chunk_idx) + .unique() + .collect::>(); + + let mut loaded_chunks = self.lookup_chunks(&chunks_needed); + let chunk_ranges = loaded_chunks + .iter() + .map(|c| c.byte_range.clone()) + .collect::>(); + let loaded_chunk_data = io.submit_request(chunk_ranges, self.priority); + + let rep_decompressor = self.rep_decompressor.clone(); + let def_decompressor = self.def_decompressor.clone(); + let value_decompressor = self.value_decompressor.clone(); + let num_buffers = self.num_buffers; + let has_large_chunk = self.has_large_chunk; + let dictionary = page_meta + .dictionary + .as_ref() + .map(|dictionary| dictionary.clone()); + let def_meaning = self.def_meaning.clone(); + + let res = async move { + let loaded_chunk_data = loaded_chunk_data.await?; + for (loaded_chunk, chunk_data) in loaded_chunks.iter_mut().zip(loaded_chunk_data) { + loaded_chunk.data = LanceBuffer::from_bytes(chunk_data, 1); + } + + Ok(Box::new(MiniBlockDecoder { + rep_decompressor, + def_decompressor, + value_decompressor, + def_meaning, + loaded_chunks: VecDeque::from_iter(loaded_chunks), + instructions: VecDeque::from(chunk_instructions), + offset_in_current_chunk: 0, + dictionary, + num_rows, + num_buffers, + has_large_chunk, + }) as Box) + } + .boxed(); + let page_load_task = PageLoadTask { + decoder_fut: res, + num_rows, + }; + Ok(vec![page_load_task]) + } +} + +#[derive(Debug, Clone, Copy)] +struct FullZipRepIndexDetails { + buf_position: u64, + bytes_per_value: u64, // Will be 1, 2, 4, or 8 +} + +#[derive(Debug)] +enum PerValueDecompressor { + Fixed(Arc), + Variable(Arc), +} + +#[derive(Debug)] +struct FullZipDecodeDetails { + value_decompressor: PerValueDecompressor, + def_meaning: Arc<[DefinitionInterpretation]>, + ctrl_word_parser: ControlWordParser, + max_rep: u16, + max_visible_def: u16, +} + +/// Describes where FullZip byte ranges should be read from. +/// +/// FullZip decoding always needs a list of byte ranges, but those bytes can come +/// from two different places: +/// - Remote I/O (normal path): ranges are fetched from the underlying `EncodingsIo`. +/// - A prefetched full page (full scan fast path): the entire page has already been +/// loaded once and ranges should be sliced from memory. +/// +/// This abstraction keeps scheduling code focused on "which ranges are needed" +/// instead of "how bytes are fetched", and it lets full-page scans avoid the +/// two-stage rep-index -> data I/O pipeline. +#[derive(Debug, Clone)] +enum FullZipReadSource { + /// Fetch ranges from the storage backend through the encoding I/O interface. + Remote(Arc), + /// Slice ranges from an already-loaded FullZip page buffer. + PrefetchedPage { base_offset: u64, data: LanceBuffer }, +} + +impl FullZipReadSource { + /// Materialize the requested ranges as decode-ready `LanceBuffer`s. + /// + /// The returned buffers preserve the input range order. + fn fetch( + &self, + ranges: &[Range], + priority: u64, + ) -> BoxFuture<'static, Result>> { + match self { + Self::Remote(io) => { + let io = io.clone(); + let ranges = ranges.to_vec(); + async move { + let data = io.submit_request(ranges, priority).await?; + Ok(data + .into_iter() + .map(|bytes| LanceBuffer::from_bytes(bytes, 1)) + .collect::>()) + } + .boxed() + } + Self::PrefetchedPage { base_offset, data } => { + let base_offset = *base_offset; + let data = data.clone(); + let page_end = base_offset + data.len() as u64; + std::future::ready( + ranges + .iter() + .map(|range| { + if range.start > range.end + || range.start < base_offset + || range.end > page_end + { + return Err(Error::internal(format!( + "Requested range {:?} is outside page range {}..{}", + range, base_offset, page_end + ))); + } + let start = (range.start - base_offset) as usize; + let len = (range.end - range.start) as usize; + Ok(data.slice_with_length(start, len)) + }) + .collect::>>(), + ) + .boxed() + } + } + } +} + +/// A scheduler for full-zip encoded data +/// +/// When the data type has a fixed-width then we simply need to map from +/// row ranges to byte ranges using the fixed-width of the data type. +/// +/// When the data type is variable-width or has any repetition then a +/// repetition index is required. +#[derive(Debug)] +pub struct FullZipScheduler { + data_buf_position: u64, + data_buf_size: u64, + rep_index: Option, + priority: u64, + rows_in_page: u64, + bits_per_offset: u8, + details: Arc, + /// Cached state containing the decoded repetition index + cached_state: Option>, + /// Whether repetition index metadata should be cached during initialize. + enable_cache: bool, +} + +impl FullZipScheduler { + fn try_new( + buffer_offsets_and_sizes: &[(u64, u64)], + priority: u64, + rows_in_page: u64, + layout: &pb21::FullZipLayout, + decompressors: &dyn DecompressionStrategy, + ) -> Result { + let (data_buf_position, data_buf_size) = buffer_offsets_and_sizes[0]; + let rep_index = buffer_offsets_and_sizes.get(1).map(|(pos, len)| { + let num_reps = rows_in_page + 1; + let bytes_per_rep = len / num_reps; + debug_assert_eq!(len % num_reps, 0); + debug_assert!( + bytes_per_rep == 1 + || bytes_per_rep == 2 + || bytes_per_rep == 4 + || bytes_per_rep == 8 + ); + FullZipRepIndexDetails { + buf_position: *pos, + bytes_per_value: bytes_per_rep, + } + }); + + let value_decompressor = match layout.details { + Some(pb21::full_zip_layout::Details::BitsPerValue(_)) => { + let decompressor = decompressors.create_fixed_per_value_decompressor( + layout.value_compression.as_ref().unwrap(), + )?; + PerValueDecompressor::Fixed(decompressor.into()) + } + Some(pb21::full_zip_layout::Details::BitsPerOffset(_)) => { + let decompressor = decompressors.create_variable_per_value_decompressor( + layout.value_compression.as_ref().unwrap(), + )?; + PerValueDecompressor::Variable(decompressor.into()) + } + None => { + panic!("Full-zip layout must have a `details` field"); + } + }; + let ctrl_word_parser = ControlWordParser::new( + layout.bits_rep.try_into().unwrap(), + layout.bits_def.try_into().unwrap(), + ); + let def_meaning = layout + .layers + .iter() + .map(|l| ProtobufUtils21::repdef_layer_to_def_interp(*l)) + .collect::>(); + + let max_rep = def_meaning.iter().filter(|d| d.is_list()).count() as u16; + let max_visible_def = def_meaning + .iter() + .filter(|d| !d.is_list()) + .map(|d| d.num_def_levels()) + .sum(); + + let bits_per_offset = match layout.details { + Some(pb21::full_zip_layout::Details::BitsPerValue(_)) => 32, + Some(pb21::full_zip_layout::Details::BitsPerOffset(bits_per_offset)) => { + bits_per_offset as u8 + } + None => panic!("Full-zip layout must have a `details` field"), + }; + + let details = Arc::new(FullZipDecodeDetails { + value_decompressor, + def_meaning: def_meaning.into(), + ctrl_word_parser, + max_rep, + max_visible_def, + }); + Ok(Self { + data_buf_position, + data_buf_size, + rep_index, + details, + priority, + rows_in_page, + bits_per_offset, + cached_state: None, + enable_cache: false, + }) + } + + fn covers_entire_page(ranges: &[Range], rows_in_page: u64) -> bool { + if ranges.is_empty() { + return false; + } + let mut expected_start = 0; + for range in ranges { + if range.start != expected_start || range.end > rows_in_page || range.end < range.start + { + return false; + } + expected_start = range.end; + } + expected_start == rows_in_page + } + + fn create_page_load_task( + io_future: BoxFuture<'static, Result>>, + num_rows: u64, + details: Arc, + bits_per_offset: u8, + ) -> PageLoadTask { + let load_task = async move { + let buffers = io_future.await?; + let data = buffers + .into_iter() + .map(|bytes| LanceBuffer::from_bytes(bytes, 1)) + .collect::>(); + Self::create_decoder(details, data, num_rows, bits_per_offset) + } + .boxed(); + PageLoadTask { + decoder_fut: load_task, + num_rows, + } + } + + /// Creates a decoder from the loaded data + fn create_decoder( + details: Arc, + data: VecDeque, + num_rows: u64, + bits_per_offset: u8, + ) -> Result> { + match &details.value_decompressor { + PerValueDecompressor::Fixed(decompressor) => { + let bits_per_value = decompressor.bits_per_value(); + if bits_per_value % 8 != 0 { + return Err(lance_core::Error::not_supported_source("Bit-packed full-zip encoding (non-byte-aligned values) is not yet implemented".into())); + } + let bytes_per_value = bits_per_value / 8; + let total_bytes_per_value = + bytes_per_value as usize + details.ctrl_word_parser.bytes_per_word(); + if total_bytes_per_value == 0 { + return Err(lance_core::Error::internal( + "Invalid encoding: per-row byte width must be greater than 0", + )); + } + Ok(Box::new(FixedFullZipDecoder { + details, + data, + num_rows, + offset_in_current: 0, + bytes_per_value: bytes_per_value as usize, + total_bytes_per_value, + }) as Box) + } + PerValueDecompressor::Variable(_decompressor) => { + Ok(Box::new(VariableFullZipDecoder::new( + details, + data, + num_rows, + bits_per_offset, + bits_per_offset, + )?)) + } + } + } + + /// Extracts byte ranges from a repetition index buffer + /// The buffer contains pairs of (start, end) values for each range + fn extract_byte_ranges_from_pairs( + buffer: LanceBuffer, + bytes_per_value: u64, + data_buf_position: u64, + ) -> Vec> { + ByteUnpacker::new(buffer, bytes_per_value as usize) + .chunks(2) + .into_iter() + .map(|mut c| { + let start = c.next().unwrap() + data_buf_position; + let end = c.next().unwrap() + data_buf_position; + start..end + }) + .collect::>() + } + + /// Extracts byte ranges from a cached repetition index buffer + /// The buffer contains all values and we need to extract specific ranges + fn extract_byte_ranges_from_cached( + buffer: &LanceBuffer, + ranges: &[Range], + bytes_per_value: u64, + data_buf_position: u64, + ) -> Vec> { + ranges + .iter() + .map(|r| { + let start_offset = (r.start * bytes_per_value) as usize; + let end_offset = (r.end * bytes_per_value) as usize; + + let start_slice = &buffer[start_offset..start_offset + bytes_per_value as usize]; + let start_val = + ByteUnpacker::new(start_slice.iter().copied(), bytes_per_value as usize) + .next() + .unwrap(); + + let end_slice = &buffer[end_offset..end_offset + bytes_per_value as usize]; + let end_val = + ByteUnpacker::new(end_slice.iter().copied(), bytes_per_value as usize) + .next() + .unwrap(); + + (data_buf_position + start_val)..(data_buf_position + end_val) + }) + .collect() + } + + /// Computes the ranges in the repetition index that need to be loaded + fn compute_rep_index_ranges( + ranges: &[Range], + rep_index: &FullZipRepIndexDetails, + ) -> Vec> { + ranges + .iter() + .flat_map(|r| { + let first_val_start = + rep_index.buf_position + (r.start * rep_index.bytes_per_value); + let first_val_end = first_val_start + rep_index.bytes_per_value; + let last_val_start = rep_index.buf_position + (r.end * rep_index.bytes_per_value); + let last_val_end = last_val_start + rep_index.bytes_per_value; + [first_val_start..first_val_end, last_val_start..last_val_end] + }) + .collect() + } + + /// Schedules ranges in the presence of a repetition index + fn schedule_ranges_rep( + &self, + ranges: &[Range], + io: &Arc, + rep_index: FullZipRepIndexDetails, + ) -> Result> { + let num_rows = ranges.iter().map(|r| r.end - r.start).sum(); + let data_buf_position = self.data_buf_position; + let priority = self.priority; + let details = self.details.clone(); + let bits_per_offset = self.bits_per_offset; + + if Self::covers_entire_page(ranges, self.rows_in_page) { + let full_range = self.data_buf_position..(self.data_buf_position + self.data_buf_size); + let page_data = io.submit_single(full_range.clone(), priority); + let load_task = async move { + let page_data = page_data.await?; + let source = FullZipReadSource::PrefetchedPage { + base_offset: full_range.start, + data: LanceBuffer::from_bytes(page_data, 1), + }; + let read_ranges = vec![full_range]; + let data = source.fetch(&read_ranges, priority).await?; + Self::create_decoder(details, data, num_rows, bits_per_offset) + } + .boxed(); + let page_load_task = PageLoadTask { + decoder_fut: load_task, + num_rows, + }; + return Ok(vec![page_load_task]); + } + + if let Some(cached_state) = &self.cached_state { + let byte_ranges = Self::extract_byte_ranges_from_cached( + &cached_state.rep_index_buffer, + ranges, + rep_index.bytes_per_value, + data_buf_position, + ); + let io_future = io.submit_request(byte_ranges, priority); + let page_load_task = + Self::create_page_load_task(io_future, num_rows, details, bits_per_offset); + return Ok(vec![page_load_task]); + } + + let rep_ranges = Self::compute_rep_index_ranges(ranges, &rep_index); + let rep_data = io.submit_request(rep_ranges, priority); + let io_clone = io.clone(); + let load_task = async move { + let rep_data = rep_data.await?; + let rep_buffer = LanceBuffer::concat( + &rep_data + .into_iter() + .map(|d| LanceBuffer::from_bytes(d, 1)) + .collect::>(), + ); + let byte_ranges = Self::extract_byte_ranges_from_pairs( + rep_buffer, + rep_index.bytes_per_value, + data_buf_position, + ); + let source = FullZipReadSource::Remote(io_clone); + let data = source.fetch(&byte_ranges, priority).await?; + Self::create_decoder(details, data, num_rows, bits_per_offset) + } + .boxed(); + let page_load_task = PageLoadTask { + decoder_fut: load_task, + num_rows, + }; + Ok(vec![page_load_task]) + } + + // In the simple case there is no repetition and we just have large fixed-width + // rows of data. We can just map row ranges to byte ranges directly using the + // fixed-width of the data type. + fn schedule_ranges_simple( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + // Convert row ranges to item ranges (i.e. multiply by items per row) + let num_rows = ranges.iter().map(|r| r.end - r.start).sum(); + + let PerValueDecompressor::Fixed(decompressor) = &self.details.value_decompressor else { + unreachable!() + }; + + // Convert item ranges to byte ranges (i.e. multiply by bytes per item) + let bits_per_value = decompressor.bits_per_value(); + if !bits_per_value.is_multiple_of(8) { + return Err(Error::invalid_input_source( + format!( + "Full-zip fixed-width values must be byte aligned, got {} bits per value", + bits_per_value + ) + .into(), + )); + } + let bytes_per_value = bits_per_value / 8; + let bytes_per_cw = self.details.ctrl_word_parser.bytes_per_word(); + let total_bytes_per_value = bytes_per_value + bytes_per_cw as u64; + let byte_ranges = ranges + .iter() + .map(|r| { + debug_assert!(r.end <= self.rows_in_page); + let start = self.data_buf_position + r.start * total_bytes_per_value; + let end = self.data_buf_position + r.end * total_bytes_per_value; + start..end + }) + .collect::>(); + + let io_future = io.submit_request(byte_ranges, self.priority); + let page_load_task = Self::create_page_load_task( + io_future, + num_rows, + self.details.clone(), + self.bits_per_offset, + ); + Ok(vec![page_load_task]) + } +} + +/// Cacheable state for FullZip encoding, storing the decoded repetition index +#[derive(Debug)] +struct FullZipCacheableState { + /// The raw repetition index buffer for future decoding + rep_index_buffer: LanceBuffer, +} + +impl DeepSizeOf for FullZipCacheableState { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + self.rep_index_buffer.len() + } +} + +impl CachedPageData for FullZipCacheableState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +impl StructuralPageScheduler for FullZipScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + if self.enable_cache + && let Some(rep_index) = self.rep_index + { + let total_size = (self.rows_in_page + 1) * rep_index.bytes_per_value; + let rep_index_range = rep_index.buf_position..(rep_index.buf_position + total_size); + let io_clone = io.clone(); + return async move { + let rep_index_data = io_clone.submit_request(vec![rep_index_range], 0).await?; + let state = Arc::new(FullZipCacheableState { + rep_index_buffer: LanceBuffer::from_bytes(rep_index_data[0].clone(), 1), + }); + self.cached_state = Some(state.clone()); + Ok(state as Arc) + } + .boxed(); + } + std::future::ready(Ok(Arc::new(NoCachedPageData) as Arc)).boxed() + } + + /// Loads previously cached repetition index data from the cache system. + /// This method is called when a scheduler instance needs to use cached data + /// that was initialized by another instance or in a previous operation. + fn load(&mut self, cache: &Arc) { + // Try to downcast to our specific cache type + if let Ok(cached_state) = cache + .clone() + .as_arc_any() + .downcast::() + { + // Store the cached state for use in schedule_ranges + self.cached_state = Some(cached_state); + } + } + + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + if let Some(rep_index) = self.rep_index { + self.schedule_ranges_rep(ranges, io, rep_index) + } else { + self.schedule_ranges_simple(ranges, io) + } + } +} + +/// A decoder for full-zip encoded data when the data has a fixed-width +/// +/// Here we need to unzip the control words from the values themselves and +/// then decompress the requested values. +/// +/// We use a PerValueDecompressor because we will only be decompressing the +/// requested data. This decoder / scheduler does not do any read amplification. +#[derive(Debug)] +struct FixedFullZipDecoder { + details: Arc, + data: VecDeque, + offset_in_current: usize, + bytes_per_value: usize, + total_bytes_per_value: usize, + num_rows: u64, +} + +impl FixedFullZipDecoder { + fn slice_next_task(&mut self, num_rows: u64) -> FullZipDecodeTaskItem { + debug_assert!(num_rows > 0); + let cur_buf = self.data.front_mut().unwrap(); + let start = self.offset_in_current; + if self.details.ctrl_word_parser.has_rep() { + // This is a slightly slower path. In order to figure out where to split we need to + // examine the rep index so we can convert num_lists to num_rows + let mut rows_started = 0; + // We always need at least one value. Now loop through until we have passed num_rows + // values + let mut num_items = 0; + while self.offset_in_current < cur_buf.len() { + let control = self.details.ctrl_word_parser.parse_desc( + &cur_buf[self.offset_in_current..], + self.details.max_rep, + self.details.max_visible_def, + ); + if control.is_new_row { + if rows_started == num_rows { + break; + } + rows_started += 1; + } + num_items += 1; + if control.is_visible { + self.offset_in_current += self.total_bytes_per_value; + } else { + self.offset_in_current += self.details.ctrl_word_parser.bytes_per_word(); + } + } + + let task_slice = cur_buf.slice_with_length(start, self.offset_in_current - start); + if self.offset_in_current == cur_buf.len() { + self.data.pop_front(); + self.offset_in_current = 0; + } + + FullZipDecodeTaskItem { + data: PerValueDataBlock::Fixed(FixedWidthDataBlock { + data: task_slice, + bits_per_value: self.bytes_per_value as u64 * 8, + num_values: num_items, + block_info: BlockInfo::new(), + }), + rows_in_buf: rows_started, + } + } else { + // If there's no repetition we can calculate the slicing point by just multiplying + // the number of rows by the total bytes per value + let cur_buf = self.data.front_mut().unwrap(); + let bytes_avail = cur_buf.len() - self.offset_in_current; + let offset_in_cur = self.offset_in_current; + + let bytes_needed = num_rows as usize * self.total_bytes_per_value; + let mut rows_taken = num_rows; + let task_slice = if bytes_needed >= bytes_avail { + self.offset_in_current = 0; + rows_taken = bytes_avail as u64 / self.total_bytes_per_value as u64; + self.data + .pop_front() + .unwrap() + .slice_with_length(offset_in_cur, bytes_avail) + } else { + self.offset_in_current += bytes_needed; + cur_buf.slice_with_length(offset_in_cur, bytes_needed) + }; + FullZipDecodeTaskItem { + data: PerValueDataBlock::Fixed(FixedWidthDataBlock { + data: task_slice, + bits_per_value: self.bytes_per_value as u64 * 8, + num_values: rows_taken, + block_info: BlockInfo::new(), + }), + rows_in_buf: rows_taken, + } + } + } +} + +impl StructuralPageDecoder for FixedFullZipDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let mut task_data = Vec::with_capacity(self.data.len()); + let mut remaining = num_rows; + while remaining > 0 { + let task_item = self.slice_next_task(remaining); + remaining -= task_item.rows_in_buf; + task_data.push(task_item); + } + Ok(Box::new(FixedFullZipDecodeTask { + details: self.details.clone(), + data: task_data, + bytes_per_value: self.bytes_per_value, + num_rows: num_rows as usize, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +/// A decoder for full-zip encoded data when the data has a variable-width +/// +/// Here we need to unzip the control words AND lengths from the values and +/// then decompress the requested values. +#[derive(Debug)] +struct VariableFullZipDecoder { + details: Arc, + decompressor: Arc, + data: LanceBuffer, + offsets: LanceBuffer, + rep: ScalarBuffer, + def: ScalarBuffer, + repdef_starts: Vec, + data_starts: Vec, + offset_starts: Vec, + visible_item_counts: Vec, + bits_per_offset: u8, + current_idx: usize, + num_rows: u64, +} + +impl VariableFullZipDecoder { + fn new( + details: Arc, + data: VecDeque, + num_rows: u64, + in_bits_per_length: u8, + out_bits_per_offset: u8, + ) -> Result { + let decompressor = match details.value_decompressor { + PerValueDecompressor::Variable(ref d) => d.clone(), + _ => unreachable!(), + }; + + assert_eq!(in_bits_per_length % 8, 0); + assert!(out_bits_per_offset == 32 || out_bits_per_offset == 64); + + let mut decoder = Self { + details, + decompressor, + data: LanceBuffer::empty(), + offsets: LanceBuffer::empty(), + rep: LanceBuffer::empty().borrow_to_typed_slice(), + def: LanceBuffer::empty().borrow_to_typed_slice(), + bits_per_offset: out_bits_per_offset, + repdef_starts: Vec::with_capacity(num_rows as usize + 1), + data_starts: Vec::with_capacity(num_rows as usize + 1), + offset_starts: Vec::with_capacity(num_rows as usize + 1), + visible_item_counts: Vec::with_capacity(num_rows as usize + 1), + current_idx: 0, + num_rows, + }; + + // There's no great time to do this and this is the least worst time. If we don't unzip then + // we can't slice the data during the decode phase. This is because we need the offsets to be + // unpacked to know where the values start and end. + // + // We don't want to unzip on the decode thread because that is a single-threaded path + // We don't want to unzip on the scheduling thread because that is a single-threaded path + // + // Fortunately, we know variable length data will always be read indirectly and so we can do it + // here, which should be on the indirect thread. The primary disadvantage to doing it here is that + // we load all the data into memory and then throw it away only to load it all into memory again during + // the decode. + // + // There are some alternatives to investigate: + // - Instead of just reading the beginning and end of the rep index we could read the entire + // range in between. This will give us the break points that we need for slicing and won't increase + // the number of IOPs but it will mean we are doing more total I/O and we need to load the rep index + // even when doing a full scan. + // - We could force each decode task to do a full unzip of all the data. Each decode task now + // has to do more work but the work is all fused. + // - We could just try doing this work on the decode thread and see if it is a problem. + decoder.unzip(data, in_bits_per_length, out_bits_per_offset, num_rows)?; + + Ok(decoder) + } + + fn slice_batch_data_and_rebase_offsets_typed( + data: &LanceBuffer, + offsets: &LanceBuffer, + ) -> Result<(LanceBuffer, LanceBuffer)> + where + T: arrow_buffer::ArrowNativeType + + Copy + + PartialOrd + + std::ops::Sub + + std::fmt::Display + + TryInto, + { + let offsets_slice = offsets.borrow_to_typed_slice::(); + let offsets_slice = offsets_slice.as_ref(); + if offsets_slice.is_empty() { + return Err(Error::internal( + "Variable offsets cannot be empty".to_string(), + )); + } + + let base = offsets_slice[0]; + let end = *offsets_slice.last().unwrap(); + if end < base { + return Err(Error::internal(format!( + "Invalid variable offsets: end ({end}) is less than base ({base})" + ))); + } + + let data_start = base.try_into().map_err(|_| { + Error::internal(format!("Variable offset ({base}) does not fit into usize")) + })?; + let data_end = end.try_into().map_err(|_| { + Error::internal(format!("Variable offset ({end}) does not fit into usize")) + })?; + if data_end > data.len() { + return Err(Error::internal(format!( + "Invalid variable offsets: end ({data_end}) exceeds data len ({})", + data.len() + ))); + } + + let mut rebased_offsets = Vec::with_capacity(offsets_slice.len()); + for &offset in offsets_slice { + if offset < base { + return Err(Error::internal(format!( + "Invalid variable offsets: offset ({offset}) is less than base ({base})" + ))); + } + rebased_offsets.push(offset - base); + } + + let sliced_data = data.slice_with_length(data_start, data_end - data_start); + // Copy into a compact buffer so each output batch owns only what it references. + let sliced_data = LanceBuffer::copy_slice(&sliced_data); + let rebased_offsets = LanceBuffer::reinterpret_vec(rebased_offsets); + Ok((sliced_data, rebased_offsets)) + } + + fn slice_batch_data_and_rebase_offsets( + data: &LanceBuffer, + offsets: &LanceBuffer, + bits_per_offset: u8, + ) -> Result<(LanceBuffer, LanceBuffer)> { + match bits_per_offset { + 32 => Self::slice_batch_data_and_rebase_offsets_typed::(data, offsets), + 64 => Self::slice_batch_data_and_rebase_offsets_typed::(data, offsets), + _ => Err(Error::internal(format!( + "Unsupported bits_per_offset={bits_per_offset}" + ))), + } + } + + /// Reads a single length prefix from the front of `data`. + /// + /// The bytes come from the file. A page whose item walk ends with a partial + /// trailing item leaves fewer than `bits_per_offset / 8` bytes here, so this + /// is bounds checked and reports a corrupt file rather than reading past the + /// end of the buffer. + fn parse_length(data: &[u8], bits_per_offset: u8) -> Result { + let width = bits_per_offset as usize / 8; + if data.len() < width { + return Err(Error::corrupt_file_named( + "variable_full_zip", + format!( + "truncated length prefix: {} byte(s) remain in the page buffer but a \ + {}-bit length prefix requires {}", + data.len(), + bits_per_offset, + width + ), + )); + } + Ok(match bits_per_offset { + 8 => data[0] as u64, + 16 => u16::from_le_bytes(data[..2].try_into().unwrap()) as u64, + 32 => u32::from_le_bytes(data[..4].try_into().unwrap()) as u64, + 64 => u64::from_le_bytes(data[..8].try_into().unwrap()), + _ => unreachable!(), + }) + } + + fn unzip( + &mut self, + data: VecDeque, + in_bits_per_length: u8, + out_bits_per_offset: u8, + num_rows: u64, + ) -> Result<()> { + // This undercounts if there are lists but, at this point, we don't really know how many items we have + let mut rep = Vec::with_capacity(num_rows as usize); + let mut def = Vec::with_capacity(num_rows as usize); + let bytes_cw = self.details.ctrl_word_parser.bytes_per_word() * num_rows as usize; + + // This undercounts if there are lists + // It can also overcount if there are invisible items + let bytes_per_offset = out_bits_per_offset as usize / 8; + let bytes_offsets = bytes_per_offset * (num_rows as usize + 1); + let mut offsets_data = Vec::with_capacity(bytes_offsets); + + let bytes_per_length = in_bits_per_length as usize / 8; + let bytes_lengths = bytes_per_length * num_rows as usize; + + let bytes_data = data.iter().map(|d| d.len()).sum::(); + // This overcounts since bytes_lengths and bytes_cw are undercounts + // It can also undercount if there are invisible items (hence the saturating_sub) + let mut unzipped_data = + Vec::with_capacity((bytes_data - bytes_cw).saturating_sub(bytes_lengths)); + + let mut current_offset = 0_u64; + let mut visible_item_count = 0_u64; + for databuf in data.into_iter() { + let mut databuf = databuf.as_ref(); + while !databuf.is_empty() { + let data_start = unzipped_data.len(); + let offset_start = offsets_data.len(); + // We might have only-rep or only-def, neither, or both. They move at the same + // speed though so we only need one index into it + let repdef_start = rep.len().max(def.len()); + // TODO: Kind of inefficient we parse the control word twice here + let ctrl_desc = self.details.ctrl_word_parser.parse_desc( + databuf, + self.details.max_rep, + self.details.max_visible_def, + ); + self.details + .ctrl_word_parser + .parse(databuf, &mut rep, &mut def); + databuf = &databuf[self.details.ctrl_word_parser.bytes_per_word()..]; + + if ctrl_desc.is_new_row { + self.repdef_starts.push(repdef_start); + self.data_starts.push(data_start); + self.offset_starts.push(offset_start); + self.visible_item_counts.push(visible_item_count); + } + if ctrl_desc.is_visible { + visible_item_count += 1; + if ctrl_desc.is_valid_item { + let length = Self::parse_length(databuf, in_bits_per_length)?; + match out_bits_per_offset { + 32 => offsets_data + .extend_from_slice(&(current_offset as u32).to_le_bytes()), + 64 => offsets_data.extend_from_slice(¤t_offset.to_le_bytes()), + _ => unreachable!(), + }; + databuf = &databuf[bytes_per_offset..]; + unzipped_data.extend_from_slice(&databuf[..length as usize]); + databuf = &databuf[length as usize..]; + current_offset += length; + } else { + // Null items still get an offset + match out_bits_per_offset { + 32 => offsets_data + .extend_from_slice(&(current_offset as u32).to_le_bytes()), + 64 => offsets_data.extend_from_slice(¤t_offset.to_le_bytes()), + _ => unreachable!(), + } + } + } + } + } + self.repdef_starts.push(rep.len().max(def.len())); + self.data_starts.push(unzipped_data.len()); + self.offset_starts.push(offsets_data.len()); + self.visible_item_counts.push(visible_item_count); + match out_bits_per_offset { + 32 => offsets_data.extend_from_slice(&(current_offset as u32).to_le_bytes()), + 64 => offsets_data.extend_from_slice(¤t_offset.to_le_bytes()), + _ => unreachable!(), + }; + self.rep = ScalarBuffer::from(rep); + self.def = ScalarBuffer::from(def); + self.data = LanceBuffer::from(unzipped_data); + self.offsets = LanceBuffer::from(offsets_data); + Ok(()) + } +} + +impl StructuralPageDecoder for VariableFullZipDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let start = self.current_idx; + let end = start + num_rows as usize; + + let offset_start = self.offset_starts[start]; + let offset_end = self.offset_starts[end] + (self.bits_per_offset as usize / 8); + let offsets = self + .offsets + .slice_with_length(offset_start, offset_end - offset_start); + // Keep each batch's variable data buffer bounded to the selected rows. + let (data, offsets) = + Self::slice_batch_data_and_rebase_offsets(&self.data, &offsets, self.bits_per_offset)?; + + let repdef_start = self.repdef_starts[start]; + let repdef_end = self.repdef_starts[end]; + let rep = if self.rep.is_empty() { + self.rep.clone() + } else { + self.rep.slice(repdef_start, repdef_end - repdef_start) + }; + let def = if self.def.is_empty() { + self.def.clone() + } else { + self.def.slice(repdef_start, repdef_end - repdef_start) + }; + + let visible_item_counts_start = self.visible_item_counts[start]; + let visible_item_counts_end = self.visible_item_counts[end]; + let num_visible_items = visible_item_counts_end - visible_item_counts_start; + + self.current_idx += num_rows as usize; + + Ok(Box::new(VariableFullZipDecodeTask { + details: self.details.clone(), + decompressor: self.decompressor.clone(), + data, + offsets, + bits_per_offset: self.bits_per_offset, + num_visible_items, + rep, + def, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +#[derive(Debug)] +struct VariableFullZipDecodeTask { + details: Arc, + decompressor: Arc, + data: LanceBuffer, + offsets: LanceBuffer, + bits_per_offset: u8, + num_visible_items: u64, + rep: ScalarBuffer, + def: ScalarBuffer, +} + +impl DecodePageTask for VariableFullZipDecodeTask { + fn decode(self: Box) -> Result { + let block = VariableWidthBlock { + data: self.data, + offsets: self.offsets, + bits_per_offset: self.bits_per_offset, + num_values: self.num_visible_items, + block_info: BlockInfo::new(), + }; + let decomopressed = self.decompressor.decompress(block)?; + let rep = if self.rep.is_empty() { + None + } else { + Some(self.rep.to_vec()) + }; + let def = if self.def.is_empty() { + None + } else { + Some(self.def.to_vec()) + }; + let unraveler = RepDefUnraveler::new( + rep, + def, + self.details.def_meaning.clone(), + self.num_visible_items, + ); + Ok(DecodedPage { + data: decomopressed, + repdef: unraveler, + }) + } +} + +#[derive(Debug)] +struct FullZipDecodeTaskItem { + data: PerValueDataBlock, + rows_in_buf: u64, +} + +/// A task to unzip and decompress full-zip encoded data when that data +/// has a fixed-width. +#[derive(Debug)] +struct FixedFullZipDecodeTask { + details: Arc, + data: Vec, + num_rows: usize, + bytes_per_value: usize, +} + +impl DecodePageTask for FixedFullZipDecodeTask { + fn decode(self: Box) -> Result { + let estimated_size_bytes = if self.details.ctrl_word_parser.bytes_per_word() == 0 { + let PerValueDecompressor::Fixed(decompressor) = &self.details.value_decompressor else { + return Err(Error::internal( + "FixedFullZipDecodeTask requires a fixed-width decompressor", + )); + }; + decompressor + .decoded_size_bytes(self.num_rows as u64) + .unwrap_or_else(|| { + self.data + .iter() + .map(|task_item| task_item.data.data_size()) + .sum::() + * 2 + }) + } else { + // Rep/def levels can suppress values, so the exact output size is not known + // until they are decoded. Keep the existing conservative estimate. + self.data + .iter() + .map(|task_item| task_item.data.data_size()) + .sum::() + * 2 + }; + let mut data_builder = DataBlockBuilder::with_capacity_estimate(estimated_size_bytes); + + if self.details.ctrl_word_parser.bytes_per_word() == 0 { + // Fast path, no need to unzip because there is no rep/def + // + // We decompress each buffer and add it to our output buffer + for task_item in self.data.into_iter() { + let PerValueDataBlock::Fixed(fixed_data) = task_item.data else { + unreachable!() + }; + let PerValueDecompressor::Fixed(decompressor) = &self.details.value_decompressor + else { + unreachable!() + }; + debug_assert_eq!(fixed_data.num_values, task_item.rows_in_buf); + let decompressed = decompressor.decompress(fixed_data, task_item.rows_in_buf)?; + data_builder.append(&decompressed, 0..task_item.rows_in_buf)?; + } + + let unraveler = RepDefUnraveler::new( + None, + None, + self.details.def_meaning.clone(), + self.num_rows as u64, + ); + + Ok(DecodedPage { + data: data_builder.finish(), + repdef: unraveler, + }) + } else { + // Slow path, unzipping needed + let mut rep = Vec::with_capacity(self.num_rows); + let mut def = Vec::with_capacity(self.num_rows); + + for task_item in self.data.into_iter() { + let PerValueDataBlock::Fixed(fixed_data) = task_item.data else { + unreachable!() + }; + let mut buf_slice = fixed_data.data.as_ref(); + let num_values = fixed_data.num_values as usize; + // We will be unzipping repdef in to `rep` and `def` and the + // values into `values` (which contains the compressed values) + let mut values = Vec::with_capacity( + fixed_data.data.len() + - (self.details.ctrl_word_parser.bytes_per_word() * num_values), + ); + let mut visible_items = 0; + for _ in 0..num_values { + // Extract rep/def + self.details + .ctrl_word_parser + .parse(buf_slice, &mut rep, &mut def); + buf_slice = &buf_slice[self.details.ctrl_word_parser.bytes_per_word()..]; + + let is_visible = def + .last() + .map(|d| *d <= self.details.max_visible_def) + .unwrap_or(true); + if is_visible { + // Extract value + values.extend_from_slice(buf_slice[..self.bytes_per_value].as_ref()); + buf_slice = &buf_slice[self.bytes_per_value..]; + visible_items += 1; + } + } + + // Finally, we decompress the values and add them to our output buffer + let values_buf = LanceBuffer::from(values); + let fixed_data = FixedWidthDataBlock { + bits_per_value: self.bytes_per_value as u64 * 8, + block_info: BlockInfo::new(), + data: values_buf, + num_values: visible_items, + }; + let PerValueDecompressor::Fixed(decompressor) = &self.details.value_decompressor + else { + unreachable!() + }; + let decompressed = decompressor.decompress(fixed_data, visible_items)?; + data_builder.append(&decompressed, 0..visible_items)?; + } + + let repetition = if rep.is_empty() { None } else { Some(rep) }; + let definition = if def.is_empty() { None } else { Some(def) }; + + let unraveler = RepDefUnraveler::new( + repetition, + definition, + self.details.def_meaning.clone(), + self.num_rows as u64, + ); + let data = data_builder.finish(); + + Ok(DecodedPage { + data, + repdef: unraveler, + }) + } + } +} + +#[derive(Debug)] +struct StructuralPrimitiveFieldSchedulingJob<'a> { + scheduler: &'a StructuralPrimitiveFieldScheduler, + ranges: Vec>, + page_idx: usize, + range_idx: usize, + global_row_offset: u64, +} + +impl<'a> StructuralPrimitiveFieldSchedulingJob<'a> { + pub fn new(scheduler: &'a StructuralPrimitiveFieldScheduler, ranges: Vec>) -> Self { + Self { + scheduler, + ranges, + page_idx: 0, + range_idx: 0, + global_row_offset: 0, + } + } +} + +impl StructuralSchedulingJob for StructuralPrimitiveFieldSchedulingJob<'_> { + fn schedule_next(&mut self, context: &mut SchedulerContext) -> Result> { + if self.range_idx >= self.ranges.len() { + return Ok(Vec::new()); + } + // Get our current range + let mut range = self.ranges[self.range_idx].clone(); + let priority = range.start; + + let mut cur_page = &self.scheduler.page_schedulers[self.page_idx]; + trace!( + "Current range is {:?} and current page has {} rows", + range, cur_page.num_rows + ); + // Skip entire pages until we have some overlap with our next range + while cur_page.num_rows + self.global_row_offset <= range.start { + self.global_row_offset += cur_page.num_rows; + self.page_idx += 1; + trace!("Skipping entire page of {} rows", cur_page.num_rows); + cur_page = &self.scheduler.page_schedulers[self.page_idx]; + } + + // Now the cur_page has overlap with range. Continue looping through ranges + // until we find a range that exceeds the current page + + let mut ranges_in_page = Vec::new(); + while cur_page.num_rows + self.global_row_offset > range.start { + range.start = range.start.max(self.global_row_offset); + let start_in_page = range.start - self.global_row_offset; + let end_in_page = start_in_page + (range.end - range.start); + let end_in_page = end_in_page.min(cur_page.num_rows); + let last_in_range = (end_in_page + self.global_row_offset) >= range.end; + + ranges_in_page.push(start_in_page..end_in_page); + if last_in_range { + self.range_idx += 1; + if self.range_idx == self.ranges.len() { + break; + } + range = self.ranges[self.range_idx].clone(); + } else { + break; + } + } + + trace!( + "Scheduling {} rows across {} ranges from page with {} rows (priority={}, column_index={}, page_index={})", + ranges_in_page.iter().map(|r| r.end - r.start).sum::(), + ranges_in_page.len(), + cur_page.num_rows, + priority, + self.scheduler.column_index, + cur_page.page_index, + ); + + self.global_row_offset += cur_page.num_rows; + self.page_idx += 1; + + let page_decoders = cur_page + .scheduler + .schedule_ranges(&ranges_in_page, context.io())?; + + let cur_path = context.current_path(); + page_decoders + .into_iter() + .map(|page_load_task| { + let cur_path = cur_path.clone(); + let page_decoder = page_load_task.decoder_fut; + let unloaded_page = async move { + let page_decoder = page_decoder.await?; + Ok(LoadedPageShard { + decoder: page_decoder, + path: cur_path, + }) + } + .boxed(); + Ok(ScheduledScanLine { + decoders: vec![MessageType::UnloadedPage(UnloadedPageShard(unloaded_page))], + rows_scheduled: page_load_task.num_rows, + }) + }) + .collect::>>() + } +} + +#[derive(Debug)] +struct PageInfoAndScheduler { + page_index: usize, + num_rows: u64, + scheduler: Box, +} + +/// A scheduler for a leaf node +/// +/// Here we look at the layout of the various pages and delegate scheduling to a scheduler +/// appropriate for the layout of the page. +#[derive(Debug)] +pub struct StructuralPrimitiveFieldScheduler { + page_schedulers: Vec, + column_index: u32, + // Identifies the requested decode shape (e.g. blob descriptor struct vs + // raw bytes). Blob columns can produce multiple page scheduler variants + // for the same physical column depending on the target field's data type, + // and the cached page state types differ per variant. The view tag is + // mixed into the cache key so different variants do not collide. + view_tag: String, +} + +impl StructuralPrimitiveFieldScheduler { + pub fn try_new( + column_info: &ColumnInfo, + decompressors: &dyn DecompressionStrategy, + cache_repetition_index: bool, + target_field: &Field, + ) -> Result { + let page_schedulers = column_info + .page_infos + .iter() + .enumerate() + .map(|(page_index, page_info)| { + Self::page_info_to_scheduler( + page_info, + page_index, + decompressors, + cache_repetition_index, + target_field, + ) + }) + .collect::>>()?; + Ok(Self { + page_schedulers, + column_index: column_info.index, + view_tag: format!("{:?}", target_field.data_type()), + }) + } + + fn page_layout_to_scheduler( + page_info: &PageInfo, + page_layout: &PageLayout, + decompressors: &dyn DecompressionStrategy, + cache_repetition_index: bool, + target_field: &Field, + ) -> Result> { + use pb21::page_layout::Layout; + Ok(match page_layout.layout.as_ref().expect_ok()? { + Layout::MiniBlockLayout(mini_block) => Box::new(MiniBlockScheduler::try_new( + &page_info.buffer_offsets_and_sizes, + page_info.priority, + mini_block.num_items, + mini_block, + decompressors, + )?), + Layout::SparseLayout(sparse_layout) => { + Box::new(sparse::SparseStructuralScheduler::try_new( + &page_info.buffer_offsets_and_sizes, + page_info.priority, + page_info.num_rows, + target_field.data_type(), + sparse_layout, + decompressors, + )?) + } + Layout::FullZipLayout(full_zip) => { + let mut scheduler = FullZipScheduler::try_new( + &page_info.buffer_offsets_and_sizes, + page_info.priority, + page_info.num_rows, + full_zip, + decompressors, + )?; + scheduler.enable_cache = cache_repetition_index; + Box::new(scheduler) + } + Layout::ConstantLayout(constant_layout) => { + let def_meaning = constant_layout + .layers + .iter() + .map(|l| ProtobufUtils21::repdef_layer_to_def_interp(*l)) + .collect::>(); + let has_scalar_value = constant_layout.inline_value.is_some() + || page_info.buffer_offsets_and_sizes.len() == 1 + || page_info.buffer_offsets_and_sizes.len() == 3; + if has_scalar_value { + Box::new(constant::ConstantPageScheduler::try_new( + page_info.buffer_offsets_and_sizes.clone(), + constant_layout.inline_value.clone(), + target_field.data_type(), + def_meaning.into(), + )?) as Box + } else if def_meaning.len() == 1 + && def_meaning[0] == DefinitionInterpretation::NullableItem + { + Box::new(SimpleAllNullScheduler::default()) as Box + } else { + // RLE levels select a validated cache representation; other + // block compressions keep flowing through the eager decompressor. + let rep_codec = LevelCodec::try_new( + constant_layout.rep_compression.as_ref(), + decompressors, + )?; + let def_codec = LevelCodec::try_new( + constant_layout.def_compression.as_ref(), + decompressors, + )?; + + Box::new(ComplexAllNullScheduler::new( + page_info.buffer_offsets_and_sizes.clone(), + def_meaning.into(), + rep_codec, + def_codec, + constant_layout.num_rep_values, + constant_layout.num_def_values, + )) as Box + } + } + Layout::BlobLayout(blob) => { + let inner_scheduler = Self::page_layout_to_scheduler( + page_info, + blob.inner_layout.as_ref().expect_ok()?.as_ref(), + decompressors, + cache_repetition_index, + target_field, + )?; + let def_meaning = blob + .layers + .iter() + .map(|l| ProtobufUtils21::repdef_layer_to_def_interp(*l)) + .collect::>(); + if matches!(target_field.data_type(), DataType::Struct(_)) { + // User wants to decode blob into struct + Box::new(BlobDescriptionPageScheduler::new( + inner_scheduler, + def_meaning.into(), + )) + } else { + // User wants to decode blob into binary data + Box::new(BlobPageScheduler::new( + inner_scheduler, + page_info.priority, + page_info.num_rows, + def_meaning.into(), + )) + } + } + }) + } + + fn page_info_to_scheduler( + page_info: &PageInfo, + page_index: usize, + decompressors: &dyn DecompressionStrategy, + cache_repetition_index: bool, + target_field: &Field, + ) -> Result { + let page_layout = page_info.encoding.as_structural(); + let scheduler = Self::page_layout_to_scheduler( + page_info, + page_layout, + decompressors, + cache_repetition_index, + target_field, + )?; + Ok(PageInfoAndScheduler { + page_index, + num_rows: page_info.num_rows, + scheduler, + }) + } +} + +pub trait CachedPageData: Any + Send + Sync + DeepSizeOf + 'static { + fn as_arc_any(self: Arc) -> Arc; +} + +pub struct NoCachedPageData; + +impl DeepSizeOf for NoCachedPageData { + fn deep_size_of_children(&self, _ctx: &mut Context) -> usize { + 0 + } +} +impl CachedPageData for NoCachedPageData { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +pub struct CachedFieldData { + pages: Vec>, +} + +impl DeepSizeOf for CachedFieldData { + fn deep_size_of_children(&self, ctx: &mut Context) -> usize { + self.pages.deep_size_of_children(ctx) + } +} + +// Cache key for field data +// +// Both `column_index` and `view_tag` are part of the key because a single +// physical column can be decoded under more than one shape — a blob column, +// for instance, materializes as a `Struct` descriptor in one +// scheduler variant and as the raw `LargeBinary` bytes in another. Each +// variant builds different `CachedPageData` types per page, so two readers +// that hit the same `column_index` with different shapes used to collide and +// crash with a downcast failure when loading cached state. +#[derive(Debug, Clone)] +pub struct FieldDataCacheKey { + pub column_index: u32, + pub view_tag: String, +} + +impl CacheKey for FieldDataCacheKey { + type ValueType = CachedFieldData; + + fn key(&self) -> std::borrow::Cow<'_, str> { + format!("{}:{}", self.column_index, self.view_tag).into() + } + + fn type_name() -> &'static str { + "FieldData" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("lance.encoding.logical.primitive.field-data-key", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u32(self.column_index); + builder.write_str(&self.view_tag); + } +} + +impl StructuralFieldScheduler for StructuralPrimitiveFieldScheduler { + fn initialize<'a>( + &'a mut self, + _filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + let cache_key = FieldDataCacheKey { + column_index: self.column_index, + view_tag: self.view_tag.clone(), + }; + let cache = context.cache().clone(); + + async move { + if let Some(cached_data) = cache.get_with_key(&cache_key).await { + self.page_schedulers + .iter_mut() + .zip(cached_data.pages.iter()) + .for_each(|(page_scheduler, cached_data)| { + page_scheduler.scheduler.load(cached_data); + }); + return Ok(()); + } + + let page_data = self + .page_schedulers + .iter_mut() + .map(|s| s.scheduler.initialize(context.io())) + .collect::>(); + + let page_data = page_data.try_collect::>().await?; + let cached_data = Arc::new(CachedFieldData { pages: page_data }); + cache.insert_with_key(&cache_key, cached_data).await; + Ok(()) + } + .boxed() + } + + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + _filter: &FilterExpression, + ) -> Result> { + let ranges = ranges.to_vec(); + Ok(Box::new(StructuralPrimitiveFieldSchedulingJob::new( + self, ranges, + ))) + } +} + +/// Takes the output from several pages decoders and +/// concatenates them. +#[derive(Debug)] +pub struct StructuralCompositeDecodeArrayTask { + tasks: Vec>, + should_validate: bool, + data_type: DataType, +} + +impl StructuralCompositeDecodeArrayTask { + fn restore_validity( + array: Arc, + unraveler: &mut CompositeRepDefUnraveler, + ) -> Result> { + let validity = unraveler.unravel_validity(array.len())?; + let Some(validity) = validity else { + return Ok(array); + }; + if array.data_type() == &DataType::Null { + // We unravel from a null array but we don't add the null buffer because arrow-rs doesn't like it + return Ok(array); + } + if validity.len() != array.len() { + return Err(Error::invalid_input_source( + format!( + "Structural validity has {} entries for an array with {} values", + validity.len(), + array.len() + ) + .into(), + )); + } + // SAFETY: The array buffers have already been validated and the null buffer length + // matches the array. We are only attaching the null buffer here. + Ok(make_array(unsafe { + array + .to_data() + .into_builder() + .nulls(Some(validity)) + .build_unchecked() + })) + } +} + +impl StructuralDecodeArrayTask for StructuralCompositeDecodeArrayTask { + fn decode(self: Box) -> Result { + let mut arrays = Vec::with_capacity(self.tasks.len()); + let mut unravelers = Vec::with_capacity(self.tasks.len()); + let mut data_size = 0u64; + for task in self.tasks { + let decoded = task.decode()?; + data_size += decoded.data.data_size(); + unravelers.push(decoded.repdef); + + let array = make_array( + decoded + .data + .into_arrow(self.data_type.clone(), self.should_validate)?, + ); + + arrays.push(array); + } + let array_refs = arrays.iter().map(|arr| arr.as_ref()).collect::>(); + let array = arrow_select::concat::concat(&array_refs)?; + let mut repdef = CompositeRepDefUnraveler::new(unravelers); + + let array = Self::restore_validity(array, &mut repdef)?; + + Ok(DecodedArray { + array, + repdef, + data_size, + }) + } +} + +#[derive(Debug)] +pub struct StructuralPrimitiveFieldDecoder { + field: Arc, + page_decoders: VecDeque>, + should_validate: bool, + rows_drained_in_current: u64, +} + +impl StructuralPrimitiveFieldDecoder { + pub fn new(field: &Arc, should_validate: bool) -> Self { + Self { + field: field.clone(), + page_decoders: VecDeque::new(), + should_validate, + rows_drained_in_current: 0, + } + } +} + +impl StructuralFieldDecoder for StructuralPrimitiveFieldDecoder { + fn accept_page(&mut self, child: LoadedPageShard) -> Result<()> { + assert!(child.path.is_empty()); + self.page_decoders.push_back(child.decoder); + Ok(()) + } + + fn drain(&mut self, num_rows: u64) -> Result> { + let mut remaining = num_rows; + let mut tasks = Vec::new(); + while remaining > 0 { + let queued_pages = self.page_decoders.len(); + let Some(cur_page) = self.page_decoders.front_mut() else { + return Err(Error::internal(format!( + "Primitive decoder missing page decoder while draining field '{}' (data_type={:?}, requested_rows={}, remaining_rows={}, rows_drained_in_current={}, queued_pages={})", + self.field.name(), + self.field.data_type(), + num_rows, + remaining, + self.rows_drained_in_current, + queued_pages + ))); + }; + let num_in_page = cur_page.num_rows() - self.rows_drained_in_current; + let to_take = num_in_page.min(remaining); + + let task = cur_page.drain(to_take)?; + tasks.push(task); + + if to_take == num_in_page { + self.page_decoders.pop_front(); + self.rows_drained_in_current = 0; + } else { + self.rows_drained_in_current += to_take; + } + + remaining -= to_take; + } + Ok(Box::new(StructuralCompositeDecodeArrayTask { + tasks, + should_validate: self.should_validate, + data_type: self.field.data_type().clone(), + })) + } + + fn data_type(&self) -> &DataType { + self.field.data_type() + } +} + +/// The serialized representation of full-zip data +struct SerializedFullZip { + /// The zipped values buffer + values: LanceBuffer, + /// The repetition index (only present if there is repetition) + repetition_index: Option, +} + +// We align and pad mini-blocks to 8 byte boundaries for two reasons. First, +// to allow us to store a chunk size in 12 bits. +// +// If we directly record the size in bytes with 12 bits we would be limited to +// 4KiB which is too small. Since we know each mini-block consists of 8 byte +// words we can store the # of words instead which gives us 32KiB. +// +// Second, each chunk in a mini-block is aligned to 8 bytes. This allows multi-byte +// values like offsets to be stored in a mini-block and safely read back out. It also +// helps ensure zero-copy reads in cases where zero-copy is possible (e.g. no decoding +// needed). +// +// Note: by "aligned to 8 bytes" we mean BOTH "aligned to 8 bytes from the start of +// the page" and "aligned to 8 bytes from the start of the file." +const MINIBLOCK_ALIGNMENT: usize = 8; + +/// An encoder for primitive (leaf) arrays +/// +/// This encoder is fairly complicated and follows a number of paths depending +/// on the data. +/// +/// First, we convert the validity & offsets information into repetition and +/// definition levels. Then we compress the data itself into a single buffer. +/// +/// If the data is narrow then we encode the data in small chunks (each chunk +/// should be a few disk sectors and contains a buffer of repetition, a buffer +/// of definition, and a buffer of value data). This approach is called +/// "mini-block". These mini-blocks are stored into a single data buffer. +/// +/// If the data is wide then we zip together the repetition and definition value +/// with the value data into a single buffer. This approach is called "zipped". +/// +/// If there is any repetition information then we create a repetition index +/// +/// In addition, the compression process may create zero or more metadata buffers. +/// For example, a dictionary compression will create dictionary metadata. Any +/// mini-block approach has a metadata buffer of block sizes. This metadata is +/// stored in a separate buffer on disk and read at initialization time. +/// +/// TODO: We should concatenate metadata buffers from all pages into a single buffer +/// at (roughly) the end of the file so there is, at most, one read per column of +/// metadata per file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum MiniblockChunkSize { + U16, + U32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ComplexNullEncoding { + RawLevels, + CompressedLevels, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum FixedWidthDictionaryEncoding { + Exclude64Bit, + Include64Bit, +} + +trait PrimitivePageEncodingBehavior: Send + Sync + Debug { + fn validate_field(&self, _field: &Field, _metadata: &HashMap) -> Result<()> { + Ok(()) + } + + fn try_plan_pages( + &self, + _ctx: &PrimitivePlanContext<'_>, + _arrays: &[ArrayRef], + _normalized: &NormalizedStructuralPlan, + _row_number: u64, + _num_rows: u64, + _num_values: u64, + ) -> Result>> { + Ok(None) + } + + fn try_encode_page( + &self, + _ctx: &PrimitiveEncodeContext, + page: PrimitivePageData, + ) -> Result { + Ok(PrimitiveEncodeAttempt::Unhandled(page)) + } +} + +/// One executable primitive-page behavior selected by an exact file +/// composition. +#[derive(Debug, Clone)] +pub struct PrimitivePageEncoding { + behavior: Arc, +} + +impl PrimitivePageEncoding { + /// Reject an explicit request for sparse structural encoding. + pub fn reject_sparse() -> Self { + Self { + behavior: Arc::new(RejectSparsePrimitiveEncoding), + } + } + + /// Encode constant non-null values as a constant page when applicable. + pub fn constant() -> Self { + Self { + behavior: Arc::new(ConstantPrimitiveEncoding), + } + } + + /// Plan and encode sparse structural pages when applicable. + pub fn sparse(compression: Arc) -> Self { + Self { + behavior: Arc::new(SparsePrimitiveEncoding { compression }), + } + } + + /// Encode dense pages with the original u16 miniblock grammar. + pub fn dense_u16(compression: Arc) -> Self { + Self { + behavior: Arc::new(DenseU16PrimitiveEncoding { compression }), + } + } + + /// Encode dense pages with the u32 miniblock grammar. + pub fn dense_u32(compression: Arc) -> Self { + Self { + behavior: Arc::new(DenseU32PrimitiveEncoding { compression }), + } + } +} + +#[derive(Debug)] +struct RejectSparsePrimitiveEncoding; + +#[derive(Debug)] +struct ConstantPrimitiveEncoding; + +#[derive(Debug)] +struct SparsePrimitiveEncoding { + compression: Arc, +} + +#[derive(Debug)] +struct DenseU16PrimitiveEncoding { + compression: Arc, +} + +#[derive(Debug)] +struct DenseU32PrimitiveEncoding { + compression: Arc, +} + +pub struct PrimitiveStructuralEncoder { + // Accumulates arrays until we have enough data to justify a disk page + accumulation_queue: AccumulationQueue, + + keep_original_array: bool, + accumulated_repdefs: Vec, + page_encodings: Arc<[PrimitivePageEncoding]>, + column_index: u32, + field: Field, + encoding_metadata: Arc>, +} + +struct CompressedLevelsChunk { + data: LanceBuffer, + num_levels: u16, +} + +struct CompressedLevels { + data: Vec, + compression: CompressiveEncoding, + rep_index: Option, +} + +struct SerializedMiniBlockPage { + num_buffers: u64, + data: LanceBuffer, + metadata: LanceBuffer, +} + +#[derive(Debug, Clone, Copy)] +struct DictEncodingBudget { + max_dict_entries: u32, + max_encoded_size: usize, +} + +enum PrimitivePageStructure { + Dense { + repdef: SerializedRepDefs, + single_row_miniblock_repdef_levels: Option, + }, + Sparse { + plan: sparse::SparseStructuralPlan, + prepared_values: Option, + }, +} + +// A primitive page after structural encoding selection and optional dense splitting. +struct PrimitivePageData { + // Arrow leaf arrays that contain this page's visible values. + arrays: Vec, + // Structural representation aligned to this page. + structure: PrimitivePageStructure, + // Top-level row number of the first row in this page. + row_number: u64, + // Number of top-level rows in this page. + num_rows: u64, +} + +struct PrimitivePlanContext<'a> { + column_idx: u32, + field: &'a Field, + encoding_metadata: &'a HashMap, +} + +enum PrimitiveEncodeAttempt { + Encoded(EncodedPage), + Unhandled(PrimitivePageData), +} + +// Immutable encoder state shared by per-page encode tasks. +// +// Cloning this only clones Arc-backed configuration and field metadata. Page data +// stays in PrimitivePageData and is moved into exactly one task. +#[derive(Clone)] +struct PrimitiveEncodeContext { + // Column being encoded. + column_idx: u32, + field: Field, + encoding_metadata: Arc>, + is_simple_validity: bool, + has_repdef_info: bool, +} + +impl PrimitiveStructuralEncoder { + pub fn try_new( + options: &EncodingOptions, + page_encodings: Arc<[PrimitivePageEncoding]>, + column_index: u32, + field: Field, + encoding_metadata: Arc>, + ) -> Result { + for page_encoding in page_encodings.iter() { + page_encoding + .behavior + .validate_field(&field, &encoding_metadata)?; + } + Ok(Self { + accumulation_queue: AccumulationQueue::new( + options.cache_bytes_per_column, + column_index, + options.keep_original_array, + ), + keep_original_array: options.keep_original_array, + accumulated_repdefs: Vec::new(), + column_index, + page_encodings, + field, + encoding_metadata, + }) + } + + fn encode_page( + page_encodings: &[PrimitivePageEncoding], + ctx: &PrimitiveEncodeContext, + mut page: PrimitivePageData, + ) -> Result { + for page_encoding in page_encodings { + match page_encoding.behavior.try_encode_page(ctx, page)? { + PrimitiveEncodeAttempt::Encoded(page) => return Ok(page), + PrimitiveEncodeAttempt::Unhandled(unhandled) => page = unhandled, + } + } + Err(Error::invalid_input_source( + format!( + "No primitive page encoding atom supports field '{}'", + ctx.field.name + ) + .into(), + )) + } + + // TODO: This is a heuristic we may need to tune at some point + // + // As data gets narrow then the "zipping" process gets too expensive + // and we prefer mini-block + // As data gets wide then the # of values per block shrinks (very wide) + // data doesn't even fit in a mini-block and the block overhead gets + // too large and we prefer zipped. + fn is_narrow(data_block: &DataBlock) -> bool { + const MINIBLOCK_MAX_BYTE_LENGTH_PER_VALUE: u64 = 256; + + if let Some(max_len_array) = data_block.get_stat(Stat::MaxLength) { + let max_len_array = max_len_array + .as_any() + .downcast_ref::>() + .unwrap(); + if max_len_array.value(0) < MINIBLOCK_MAX_BYTE_LENGTH_PER_VALUE { + return true; + } + } + false + } + + fn prefers_miniblock( + data_block: &DataBlock, + encoding_metadata: &HashMap, + ) -> bool { + // If the user specifically requested miniblock then use it + if let Some(user_requested) = encoding_metadata.get(STRUCTURAL_ENCODING_META_KEY) { + return user_requested.to_lowercase() == STRUCTURAL_ENCODING_MINIBLOCK; + } + // Otherwise only use miniblock if it is narrow + Self::is_narrow(data_block) + } + + fn prefers_fullzip(encoding_metadata: &HashMap) -> bool { + // Fullzip is the backup option so the only reason we wouldn't use it is if the + // user specifically requested not to use it (in which case we're probably going + // to emit an error) + if let Some(user_requested) = encoding_metadata.get(STRUCTURAL_ENCODING_META_KEY) { + return user_requested.to_lowercase() == STRUCTURAL_ENCODING_FULLZIP; + } + true + } + + // Converts value data, repetition levels, and definition levels into a single + // buffer of mini-blocks. In addition, creates a buffer of mini-block metadata + // which tells us the size of each block. Finally, if repetition is present then + // we also create a buffer for the repetition index. + // + // Each chunk is serialized as: + // | num_bufs (1 byte) | buf_lens (2 bytes per buffer) | P | buf0 | P | buf1 | ... | bufN | P | + // + // P - Padding inserted to ensure each buffer is 8-byte aligned and the buffer size is a multiple + // of 8 bytes (so that the next chunk is 8-byte aligned). + // + // Each block has a u16 word of metadata. The upper 12 bits contain the + // # of 8-byte words in the block (if the block does not fill the final word + // then up to 7 bytes of padding are added). The lower 4 bits describe the log_2 + // number of values (e.g. if there are 1024 then the lower 4 bits will be + // 0xA) All blocks except the last must have power-of-two number of values. + // This not only makes metadata smaller but it makes decoding easier since + // batch sizes are typically a power of 2. 4 bits would allow us to express + // up to 32Ki values. + // + // This means blocks can have 1 to 32Ki values and 8 - 32Ki bytes. + // + // All metadata words are serialized (as little endian) into a single buffer + // of metadata values. + // + // If there is repetition then we also create a repetition index. This is a + // single buffer of integer vectors (stored in row major order). There is one + // entry for each chunk. The size of the vector is based on the depth of random + // access we want to support. + // + // A vector of size 2 is the minimum and will support row-based random access (e.g. + // "take the 57th row"). A vector of size 3 will support 1 level of nested access + // (e.g. "take the 3rd item in the 57th row"). A vector of size 4 will support 2 + // levels of nested access and so on. + // + // The first number in the vector is the number of top-level rows that complete in + // the chunk. The second number is the number of second-level rows that complete + // after the final top-level row completed (or beginning of the chunk if no top-level + // row completes in the chunk). And so on. The final number in the vector is always + // the number of leftover items not covered by earlier entries in the vector. + // + // Currently we are limited to 0 levels of nested access but that will change in the + // future. + // + // The repetition index and the chunk metadata are read at initialization time and + // cached in memory. + fn serialize_miniblocks( + miniblocks: MiniBlockCompressed, + rep: Option>, + def: Option>, + miniblock_chunk_size: MiniblockChunkSize, + ) -> Result { + let bytes_rep = rep + .as_ref() + .map(|rep| rep.iter().map(|r| r.data.len()).sum::()) + .unwrap_or(0); + let bytes_def = def + .as_ref() + .map(|def| def.iter().map(|d| d.data.len()).sum::()) + .unwrap_or(0); + let bytes_data = miniblocks.data.iter().map(|d| d.len()).sum::(); + let mut num_buffers = miniblocks.data.len(); + if rep.is_some() { + num_buffers += 1; + } + if def.is_some() { + num_buffers += 1; + } + // 2 bytes for the length of each buffer and up to 7 bytes of padding per buffer + let max_extra = 9 * num_buffers; + let mut data_buffer = Vec::with_capacity(bytes_rep + bytes_def + bytes_data + max_extra); + let chunk_size_bytes = match miniblock_chunk_size { + MiniblockChunkSize::U16 => 2, + MiniblockChunkSize::U32 => 4, + }; + let mut meta_buffer = Vec::with_capacity(miniblocks.chunks.len() * chunk_size_bytes); + + let mut rep_iter = rep.map(|r| r.into_iter()); + let mut def_iter = def.map(|d| d.into_iter()); + + let mut buffer_offsets = vec![0; miniblocks.data.len()]; + for chunk in miniblocks.chunks { + let start_pos = data_buffer.len(); + // Start of chunk should be aligned + debug_assert_eq!(start_pos % MINIBLOCK_ALIGNMENT, 0); + + let rep = rep_iter.as_mut().map(|r| r.next().unwrap()); + let def = def_iter.as_mut().map(|d| d.next().unwrap()); + + // Write the number of levels, or 0 if there is no rep/def + let num_levels = rep + .as_ref() + .map(|r| r.num_levels) + .unwrap_or(def.as_ref().map(|d| d.num_levels).unwrap_or(0)); + data_buffer.extend_from_slice(&num_levels.to_le_bytes()); + + // Write the buffer lengths + if let Some(rep) = rep.as_ref() { + let bytes_rep = u16::try_from(rep.data.len()).map_err(|_| { + Error::internal(format!( + "Repetition buffer size ({} bytes) too large", + rep.data.len() + )) + })?; + data_buffer.extend_from_slice(&bytes_rep.to_le_bytes()); + } + if let Some(def) = def.as_ref() { + let bytes_def = u16::try_from(def.data.len()).map_err(|_| { + Error::internal(format!( + "Definition buffer size ({} bytes) too large", + def.data.len() + )) + })?; + data_buffer.extend_from_slice(&bytes_def.to_le_bytes()); + } + + if miniblock_chunk_size == MiniblockChunkSize::U32 { + for &buffer_size in &chunk.buffer_sizes { + data_buffer.extend_from_slice(&buffer_size.to_le_bytes()); + } + } else { + for &buffer_size in &chunk.buffer_sizes { + let buffer_size = u16::try_from(buffer_size).map_err(|_| { + Error::internal(format!( + "Mini-block buffer size ({} bytes) too large for 16-bit metadata", + buffer_size + )) + })?; + data_buffer.extend_from_slice(&buffer_size.to_le_bytes()); + } + } + + // Pad + let add_padding = |data_buffer: &mut Vec| { + let pad = pad_bytes::(data_buffer.len()); + data_buffer.extend(iter::repeat_n(FILL_BYTE, pad)); + }; + add_padding(&mut data_buffer); + + // Write the buffers themselves + if let Some(rep) = rep.as_ref() { + data_buffer.extend_from_slice(&rep.data); + add_padding(&mut data_buffer); + } + if let Some(def) = def.as_ref() { + data_buffer.extend_from_slice(&def.data); + add_padding(&mut data_buffer); + } + for (buffer_size, (buffer, buffer_offset)) in chunk + .buffer_sizes + .iter() + .zip(miniblocks.data.iter().zip(buffer_offsets.iter_mut())) + { + let start = *buffer_offset; + let end = start + *buffer_size as usize; + *buffer_offset += *buffer_size as usize; + data_buffer.extend_from_slice(&buffer[start..end]); + add_padding(&mut data_buffer); + } + + let chunk_bytes = data_buffer.len() - start_pos; + let max_chunk_size = match miniblock_chunk_size { + MiniblockChunkSize::U16 => 32 * 1024, + MiniblockChunkSize::U32 => 1_u64 << 31, + }; + if chunk_bytes == 0 || chunk_bytes as u64 > max_chunk_size { + return Err(Error::internal(format!( + "Mini-block chunk size {} bytes exceeds the {} byte metadata limit", + chunk_bytes, max_chunk_size + ))); + } + if chunk_bytes % MINIBLOCK_ALIGNMENT != 0 { + return Err(Error::internal(format!( + "Mini-block chunk size {} bytes is not aligned to {} bytes", + chunk_bytes, MINIBLOCK_ALIGNMENT + ))); + } + if chunk.log_num_values > 15 { + return Err(Error::internal(format!( + "Mini-block log_num_values {} exceeds the 4-bit metadata limit", + chunk.log_num_values + ))); + } + // We subtract 1 here from chunk_bytes because we want to be able to express + // a size of 32KiB and not (32Ki - 8)B which is what we'd get otherwise with + // 0xFFF + let divided_bytes = chunk_bytes / MINIBLOCK_ALIGNMENT; + let divided_bytes_minus_one = (divided_bytes - 1) as u64; + + let metadata = (divided_bytes_minus_one << 4) | chunk.log_num_values as u64; + if miniblock_chunk_size == MiniblockChunkSize::U32 { + meta_buffer.extend_from_slice(&(metadata as u32).to_le_bytes()); + } else { + meta_buffer.extend_from_slice(&(metadata as u16).to_le_bytes()); + } + } + + let data_buffer = LanceBuffer::from(data_buffer); + let metadata_buffer = LanceBuffer::from(meta_buffer); + + Ok(SerializedMiniBlockPage { + num_buffers: miniblocks.data.len() as u64, + data: data_buffer, + metadata: metadata_buffer, + }) + } + + /// Compresses a buffer of levels into chunks + /// + /// If these are repetition levels then we also calculate the repetition index here (that + /// is the third return value) + fn compress_levels( + mut levels: RepDefSlicer<'_>, + num_elements: u64, + compression_strategy: &dyn CompressionStrategy, + chunks: &[MiniBlockChunk], + // This will be 0 if we are compressing def levels + max_rep: u16, + ) -> Result { + let mut rep_index = if max_rep > 0 { + Vec::with_capacity(chunks.len()) + } else { + vec![] + }; + // Make the levels into a FixedWidth data block + let num_levels = levels.num_levels() as u64; + let levels_buf = levels.all_levels().clone(); + + let mut fixed_width_block = FixedWidthDataBlock { + data: levels_buf, + bits_per_value: 16, + num_values: num_levels, + block_info: BlockInfo::new(), + }; + // Compute statistics to enable optimal compression for rep/def levels + fixed_width_block.compute_stat(); + + let levels_block = DataBlock::FixedWidth(fixed_width_block); + let levels_field = Field::new_arrow("", DataType::UInt16, false)?; + // Pick a block compressor + let (compressor, compressor_desc) = + compression_strategy.create_block_compressor(&levels_field, &levels_block)?; + // Compress blocks of levels (sized according to the chunks) + let mut level_chunks = Vec::with_capacity(chunks.len()); + let mut values_counter = 0; + for (chunk_idx, chunk) in chunks.iter().enumerate() { + let chunk_num_values = chunk.num_values(values_counter, num_elements); + debug_assert!(chunk_num_values > 0); + values_counter += chunk_num_values; + let chunk_levels = if chunk_idx < chunks.len() - 1 { + levels.slice_next(chunk_num_values as usize) + } else { + levels.slice_rest() + }; + let num_chunk_levels = (chunk_levels.len() / 2) as u64; + if max_rep > 0 { + // If max_rep > 0 then we are working with rep levels and we need + // to calculate the repetition index. The repetition index for a + // chunk is currently 2 values (in the future it may be more). + // + // The first value is the number of rows that _finish_ in the + // chunk. + // + // The second value is the number of "leftovers" after the last + // finished row in the chunk. + let rep_values = chunk_levels.borrow_to_typed_slice::(); + let rep_values = rep_values.as_ref(); + + // We skip 1 here because a max_rep at spot 0 doesn't count as a finished list (we + // will count it in the previous chunk) + let mut num_rows = rep_values.iter().skip(1).filter(|v| **v == max_rep).count(); + let num_leftovers = if chunk_idx < chunks.len() - 1 { + rep_values + .iter() + .rev() + .position(|v| *v == max_rep) + // # of leftovers includes the max_rep spot + .map(|pos| pos + 1) + .unwrap_or(rep_values.len()) + } else { + // Last chunk can't have leftovers + 0 + }; + + if chunk_idx != 0 && rep_values.first() == Some(&max_rep) { + // This chunk starts with a new row and so, if we thought we had leftovers + // in the previous chunk, we were mistaken + // TODO: Can use unchecked here + let rep_len = rep_index.len(); + if rep_index[rep_len - 1] != 0 { + // We thought we had leftovers but that was actually a full row + rep_index[rep_len - 2] += 1; + rep_index[rep_len - 1] = 0; + } + } + + if chunk_idx == chunks.len() - 1 { + // The final list + num_rows += 1; + } + rep_index.push(num_rows as u64); + rep_index.push(num_leftovers as u64); + } + let mut chunk_fixed_width = FixedWidthDataBlock { + data: chunk_levels, + bits_per_value: 16, + num_values: num_chunk_levels, + block_info: BlockInfo::new(), + }; + chunk_fixed_width.compute_stat(); + let chunk_levels_block = DataBlock::FixedWidth(chunk_fixed_width); + let compressed_levels = compressor.compress(chunk_levels_block)?; + let num_levels = u16::try_from(num_chunk_levels).map_err(|_| { + Error::invalid_input_source( + format!( + "Mini-block cannot encode {} rep/def levels in one chunk. \ + This usually means a top-level row contains too much nested structure \ + for the current layout.", + num_chunk_levels + ) + .into(), + ) + })?; + level_chunks.push(CompressedLevelsChunk { + data: compressed_levels, + num_levels, + }); + } + debug_assert_eq!(levels.num_levels_remaining(), 0); + let rep_index = if rep_index.is_empty() { + None + } else { + Some(LanceBuffer::reinterpret_vec(rep_index)) + }; + Ok(CompressedLevels { + data: level_chunks, + compression: compressor_desc, + rep_index, + }) + } + + fn encode_simple_all_null( + column_idx: u32, + num_rows: u64, + row_number: u64, + ) -> Result { + let description = + ProtobufUtils21::constant_layout(&[DefinitionInterpretation::NullableItem], None); + Ok(EncodedPage { + column_idx, + data: vec![], + description: PageEncoding::Structural(description), + num_rows, + row_number, + }) + } + + fn encode_complex_all_null_vals( + data: &Arc<[u16]>, + compression_strategy: &dyn CompressionStrategy, + ) -> Result<(LanceBuffer, pb21::CompressiveEncoding)> { + let buffer = LanceBuffer::reinterpret_slice(data.clone()); + let mut fixed_width_block = FixedWidthDataBlock { + data: buffer, + bits_per_value: 16, + num_values: data.len() as u64, + block_info: BlockInfo::new(), + }; + fixed_width_block.compute_stat(); + + let levels_block = DataBlock::FixedWidth(fixed_width_block); + let levels_field = Field::new_arrow("", DataType::UInt16, false)?; + let (compressor, encoding) = + compression_strategy.create_block_compressor(&levels_field, &levels_block)?; + let compressed_buffer = compressor.compress(levels_block)?; + Ok((compressed_buffer, encoding)) + } + + // Encodes a page where all values are null but we have rep/def + // information that we need to store (e.g. to distinguish between + // different kinds of null) + fn encode_complex_all_null( + column_idx: u32, + repdef: crate::repdef::SerializedRepDefs, + row_number: u64, + num_rows: u64, + complex_null_encoding: ComplexNullEncoding, + compression_strategy: &dyn CompressionStrategy, + ) -> Result { + if complex_null_encoding == ComplexNullEncoding::RawLevels { + let rep_bytes = if let Some(rep) = repdef.repetition_levels.as_ref() { + LanceBuffer::reinterpret_slice(rep.clone()) + } else { + LanceBuffer::empty() + }; + + let def_bytes = if let Some(def) = repdef.definition_levels.as_ref() { + LanceBuffer::reinterpret_slice(def.clone()) + } else { + LanceBuffer::empty() + }; + + let description = ProtobufUtils21::constant_layout(&repdef.def_meaning, None); + return Ok(EncodedPage { + column_idx, + data: vec![rep_bytes, def_bytes], + description: PageEncoding::Structural(description), + num_rows, + row_number, + }); + } + + let (rep_bytes, rep_encoding, num_rep_values) = if let Some(rep) = + repdef.repetition_levels.as_ref() + { + let num_values = rep.len() as u64; + let (buffer, encoding) = Self::encode_complex_all_null_vals(rep, compression_strategy)?; + (buffer, Some(encoding), num_values) + } else { + (LanceBuffer::empty(), None, 0) + }; + + let (def_bytes, def_encoding, num_def_values) = if let Some(def) = + repdef.definition_levels.as_ref() + { + let num_values = def.len() as u64; + let (buffer, encoding) = Self::encode_complex_all_null_vals(def, compression_strategy)?; + (buffer, Some(encoding), num_values) + } else { + (LanceBuffer::empty(), None, 0) + }; + + let description = ProtobufUtils21::compressed_all_null_constant_layout( + &repdef.def_meaning, + rep_encoding, + def_encoding, + num_rep_values, + num_def_values, + ); + Ok(EncodedPage { + column_idx, + data: vec![rep_bytes, def_bytes], + description: PageEncoding::Structural(description), + num_rows, + row_number, + }) + } + + fn leaf_validity( + repdef: &crate::repdef::SerializedRepDefs, + num_values: usize, + ) -> Result> { + let rep = repdef + .repetition_levels + .as_ref() + .map(|rep| rep.as_ref().to_vec()); + let def = repdef + .definition_levels + .as_ref() + .map(|def| def.as_ref().to_vec()); + let mut unraveler = RepDefUnraveler::new( + rep, + def, + repdef.def_meaning.clone().into(), + num_values as u64, + ); + if unraveler.is_all_valid() { + return Ok(None); + } + let mut validity = BooleanBufferBuilder::new(num_values); + unraveler.unravel_validity(&mut validity)?; + Ok(Some(validity.finish())) + } + + fn is_constant_values( + arrays: &[ArrayRef], + scalar: &ArrayRef, + validity: Option<&BooleanBuffer>, + ) -> Result { + debug_assert_eq!(scalar.len(), 1); + debug_assert_eq!(scalar.null_count(), 0); + + match scalar.data_type() { + DataType::Boolean => { + let mut global_idx = 0usize; + let scalar_val = scalar.as_boolean().value(0); + for arr in arrays { + let bool_arr = arr.as_boolean(); + for i in 0..arr.len() { + let is_valid = validity.map(|v| v.value(global_idx)).unwrap_or(true); + global_idx += 1; + if !is_valid { + continue; + } + if bool_arr.value(i) != scalar_val { + return Ok(false); + } + } + } + Ok(true) + } + DataType::Utf8 => Self::is_constant_utf8::(arrays, scalar, validity), + DataType::LargeUtf8 => Self::is_constant_utf8::(arrays, scalar, validity), + DataType::Binary => Self::is_constant_binary::(arrays, scalar, validity), + DataType::LargeBinary => Self::is_constant_binary::(arrays, scalar, validity), + data_type => { + let mut global_idx = 0usize; + let Some(byte_width) = data_type.byte_width_opt() else { + return Ok(false); + }; + let scalar_data = scalar.to_data(); + if scalar_data.buffers().len() != 1 || !scalar_data.child_data().is_empty() { + return Ok(false); + } + let scalar_bytes = scalar_data.buffers()[0].as_slice(); + if scalar_bytes.len() != byte_width { + return Ok(false); + } + + for arr in arrays { + let data = arr.to_data(); + if data.buffers().is_empty() { + return Ok(false); + } + let buf = data.buffers()[0].as_slice(); + let base = data.offset(); + for i in 0..arr.len() { + let is_valid = validity.map(|v| v.value(global_idx)).unwrap_or(true); + global_idx += 1; + if !is_valid { + continue; + } + let start = (base + i) * byte_width; + if buf[start..start + byte_width] != scalar_bytes[..] { + return Ok(false); + } + } + } + Ok(true) + } + } + } + + fn is_constant_utf8( + arrays: &[ArrayRef], + scalar: &ArrayRef, + validity: Option<&BooleanBuffer>, + ) -> Result { + debug_assert_eq!(scalar.len(), 1); + let scalar_val = scalar.as_string::().value(0).as_bytes(); + let mut global_idx = 0usize; + for arr in arrays { + let str_arr = arr.as_string::(); + for i in 0..arr.len() { + let is_valid = validity.map(|v| v.value(global_idx)).unwrap_or(true); + global_idx += 1; + if !is_valid { + continue; + } + if str_arr.value(i).as_bytes() != scalar_val { + return Ok(false); + } + } + } + Ok(true) + } + + fn is_constant_binary( + arrays: &[ArrayRef], + scalar: &ArrayRef, + validity: Option<&BooleanBuffer>, + ) -> Result { + debug_assert_eq!(scalar.len(), 1); + let scalar_val = scalar.as_binary::().value(0); + let mut global_idx = 0usize; + for arr in arrays { + let bin_arr = arr.as_binary::(); + for i in 0..arr.len() { + let is_valid = validity.map(|v| v.value(global_idx)).unwrap_or(true); + global_idx += 1; + if !is_valid { + continue; + } + if bin_arr.value(i) != scalar_val { + return Ok(false); + } + } + } + Ok(true) + } + + fn find_constant_scalar( + arrays: &[ArrayRef], + validity: Option<&BooleanBuffer>, + ) -> Result> { + if arrays.is_empty() { + return Ok(None); + } + + let global_scalar_idx = if let Some(validity) = validity { + let Some(idx) = (0..validity.len()).find(|&i| validity.value(i)) else { + return Ok(None); + }; + idx + } else { + 0 + }; + + let mut idx_remaining = global_scalar_idx; + let mut scalar_arr_idx = 0usize; + while scalar_arr_idx < arrays.len() { + let len = arrays[scalar_arr_idx].len(); + if idx_remaining < len { + break; + } + idx_remaining -= len; + scalar_arr_idx += 1; + } + + if scalar_arr_idx >= arrays.len() { + return Ok(None); + } + + let scalar = + lance_arrow::scalar::extract_scalar_value(&arrays[scalar_arr_idx], idx_remaining)?; + if scalar.null_count() != 0 { + return Ok(None); + } + if !Self::is_constant_values(arrays, &scalar, validity)? { + return Ok(None); + } + Ok(Some(scalar)) + } + + fn resolve_dict_values_compression_metadata( + field_metadata: &HashMap, + env_compression: Option, + env_compression_level: Option, + ) -> HashMap { + let mut metadata = HashMap::new(); + + let compression = field_metadata + .get(DICT_VALUES_COMPRESSION_META_KEY) + .cloned() + .or(env_compression) + .unwrap_or_else(|| DEFAULT_DICT_VALUES_COMPRESSION.to_string()); + metadata.insert(COMPRESSION_META_KEY.to_string(), compression); + + if let Some(compression_level) = field_metadata + .get(DICT_VALUES_COMPRESSION_LEVEL_META_KEY) + .cloned() + .or(env_compression_level) + { + metadata.insert(COMPRESSION_LEVEL_META_KEY.to_string(), compression_level); + } + + metadata + } + + fn build_dict_values_compressor_field(field: &Field) -> Result { + // This is an internal synthetic field used only to feed metadata into + // `create_block_compressor` for dictionary values. The concrete type/name here + // are not semantically meaningful; we rely on explicit metadata below to control + // general compression selection for dictionary values. + let mut dict_values_field = Field::new_arrow("", DataType::UInt16, false)?; + dict_values_field.metadata = Self::resolve_dict_values_compression_metadata( + &field.metadata, + env::var(DICT_VALUES_COMPRESSION_ENV_VAR).ok(), + env::var(DICT_VALUES_COMPRESSION_LEVEL_ENV_VAR).ok(), + ); + Ok(dict_values_field) + } + + #[allow(clippy::too_many_arguments)] + fn encode_miniblock( + column_idx: u32, + field: &Field, + compression_strategy: &dyn CompressionStrategy, + data: DataBlock, + repdef: crate::repdef::SerializedRepDefs, + row_number: u64, + dictionary_data: Option, + num_rows: u64, + miniblock_chunk_size: MiniblockChunkSize, + ) -> Result { + if let DataBlock::AllNull(_null_block) = data { + // We should not be using mini-block for all-null. There are other structural + // encodings for that. + unreachable!() + } + + let num_items = data.num_values(); + + let compressor = compression_strategy.create_miniblock_compressor(field, &data)?; + let common_chunk_buffers = + u64::from(repdef.rep_slicer().is_some()) + u64::from(repdef.def_slicer().is_some()); + let support_large_chunk = miniblock_chunk_size == MiniblockChunkSize::U32; + let compression_context = + MiniBlockCompressionContext::new(common_chunk_buffers, support_large_chunk, true); + let (compressed_data, value_encoding) = compressor.compress(compression_context, data)?; + + let max_rep = repdef.def_meaning.iter().filter(|l| l.is_list()).count() as u16; + + let mut compressed_rep = repdef + .rep_slicer() + .map(|rep_slicer| { + Self::compress_levels( + rep_slicer, + num_items, + compression_strategy, + &compressed_data.chunks, + max_rep, + ) + }) + .transpose()?; + + let (rep_index, rep_index_depth) = + match compressed_rep.as_mut().and_then(|cr| cr.rep_index.as_mut()) { + Some(rep_index) => (Some(rep_index.clone()), 1), + None => (None, 0), + }; + + let mut compressed_def = repdef + .def_slicer() + .map(|def_slicer| { + Self::compress_levels( + def_slicer, + num_items, + compression_strategy, + &compressed_data.chunks, + /*max_rep=*/ 0, + ) + }) + .transpose()?; + + // TODO: Parquet sparsely encodes values here. We could do the same but + // then we won't have log2 values per chunk. This means more metadata + // and potentially more decoder asymmetry. However, it may be worth + // investigating at some point + + let rep_data = compressed_rep + .as_mut() + .map(|cr| std::mem::take(&mut cr.data)); + let def_data = compressed_def + .as_mut() + .map(|cd| std::mem::take(&mut cd.data)); + + let serialized = + Self::serialize_miniblocks(compressed_data, rep_data, def_data, miniblock_chunk_size)?; + let has_large_chunk = miniblock_chunk_size == MiniblockChunkSize::U32; + + // Metadata, Data, Dictionary, (maybe) Repetition Index + let mut data = Vec::with_capacity(4); + data.push(serialized.metadata); + data.push(serialized.data); + + if let Some(dictionary_data) = dictionary_data { + let num_dictionary_items = dictionary_data.num_values(); + let dict_values_field = Self::build_dict_values_compressor_field(field)?; + + let (compressor, dictionary_encoding) = compression_strategy + .create_block_compressor(&dict_values_field, &dictionary_data)?; + let dictionary_buffer = compressor.compress(dictionary_data)?; + + data.push(dictionary_buffer); + if let Some(rep_index) = rep_index { + data.push(rep_index); + } + + let description = ProtobufUtils21::miniblock_layout( + compressed_rep.map(|cr| cr.compression), + compressed_def.map(|cd| cd.compression), + value_encoding, + rep_index_depth, + serialized.num_buffers, + Some((dictionary_encoding, num_dictionary_items)), + &repdef.def_meaning, + num_items, + has_large_chunk, + ); + Ok(EncodedPage { + num_rows, + column_idx, + data, + description: PageEncoding::Structural(description), + row_number, + }) + } else { + let description = ProtobufUtils21::miniblock_layout( + compressed_rep.map(|cr| cr.compression), + compressed_def.map(|cd| cd.compression), + value_encoding, + rep_index_depth, + serialized.num_buffers, + None, + &repdef.def_meaning, + num_items, + has_large_chunk, + ); + + if let Some(rep_index) = rep_index { + let view = rep_index.borrow_to_typed_slice::(); + let total = view.chunks_exact(2).map(|c| c[0]).sum::(); + debug_assert_eq!(total, num_rows); + + data.push(rep_index); + } + + Ok(EncodedPage { + num_rows, + column_idx, + data, + description: PageEncoding::Structural(description), + row_number, + }) + } + } + + // For fixed-size data we encode < control word | data > for each value + fn serialize_full_zip_fixed( + fixed: FixedWidthDataBlock, + mut repdef: ControlWordIterator, + num_values: u64, + ) -> Result { + if !fixed.bits_per_value.is_multiple_of(8) { + return Err(Error::invalid_input_source( + format!( + "Full-zip fixed-width values must be byte aligned, got {} bits per value", + fixed.bits_per_value + ) + .into(), + )); + } + + let len = fixed.data.len() + repdef.bytes_per_word() * num_values as usize; + let mut zipped_data = Vec::with_capacity(len); + + let max_rep_index_val = if repdef.has_repetition() { + len as u64 + } else { + // Setting this to 0 means we won't write a repetition index + 0 + }; + let mut rep_index_builder = + BytepackedIntegerEncoder::with_capacity(num_values as usize + 1, max_rep_index_val); + + let bytes_per_value = fixed.bits_per_value as usize / 8; + let mut offset = 0; + + if bytes_per_value == 0 { + // No data, just dump the repdef into the buffer + while let Some(control) = repdef.append_next(&mut zipped_data) { + if control.is_new_row { + // We have finished a row + debug_assert!(offset <= len); + // SAFETY: We know that `start <= len` + unsafe { rep_index_builder.append(offset as u64) }; + } + offset = zipped_data.len(); + } + } else { + // We have data, zip it with the repdef + let mut data_iter = fixed.data.chunks_exact(bytes_per_value); + while let Some(control) = repdef.append_next(&mut zipped_data) { + if control.is_new_row { + // We have finished a row + debug_assert!(offset <= len); + // SAFETY: We know that `start <= len` + unsafe { rep_index_builder.append(offset as u64) }; + } + if control.is_visible { + let value = data_iter.next().unwrap(); + zipped_data.extend_from_slice(value); + } + offset = zipped_data.len(); + } + } + + debug_assert_eq!(zipped_data.len(), len); + // Put the final value in the rep index + // SAFETY: `zipped_data.len() == len` + unsafe { + rep_index_builder.append(zipped_data.len() as u64); + } + + let zipped_data = LanceBuffer::from(zipped_data); + let rep_index = rep_index_builder.into_data(); + let rep_index = if rep_index.is_empty() { + None + } else { + Some(LanceBuffer::from(rep_index)) + }; + Ok(SerializedFullZip { + values: zipped_data, + repetition_index: rep_index, + }) + } + + // For variable-size data we encode < control word | length | data > for each value + // + // In addition, we create a second buffer, the repetition index + fn serialize_full_zip_variable( + variable: VariableWidthBlock, + mut repdef: ControlWordIterator, + num_items: u64, + ) -> Result { + let bytes_per_offset = variable.bits_per_offset as usize / 8; + if !variable.bits_per_offset.is_multiple_of(8) { + return Err(Error::invalid_input_source( + format!( + "Full-zip variable-width offsets must be byte aligned, got {} bits per offset", + variable.bits_per_offset + ) + .into(), + )); + } + let len = variable.data.len() + + repdef.bytes_per_word() * num_items as usize + + bytes_per_offset * variable.num_values as usize; + let mut buf = Vec::with_capacity(len); + + let max_rep_index_val = len as u64; + let mut rep_index_builder = + BytepackedIntegerEncoder::with_capacity(num_items as usize + 1, max_rep_index_val); + + // TODO: byte pack the item lengths with varint encoding + match bytes_per_offset { + 4 => { + let offs = variable.offsets.borrow_to_typed_slice::(); + let mut rep_offset = 0; + let mut windows_iter = offs.as_ref().windows(2); + while let Some(control) = repdef.append_next(&mut buf) { + if control.is_new_row { + // We have finished a row + debug_assert!(rep_offset <= len); + // SAFETY: We know that `buf.len() <= len` + unsafe { rep_index_builder.append(rep_offset as u64) }; + } + if control.is_visible { + let window = windows_iter.next().unwrap(); + if control.is_valid_item { + buf.extend_from_slice(&(window[1] - window[0]).to_le_bytes()); + buf.extend_from_slice( + &variable.data[window[0] as usize..window[1] as usize], + ); + } + } + rep_offset = buf.len(); + } + } + 8 => { + let offs = variable.offsets.borrow_to_typed_slice::(); + let mut rep_offset = 0; + let mut windows_iter = offs.as_ref().windows(2); + while let Some(control) = repdef.append_next(&mut buf) { + if control.is_new_row { + // We have finished a row + debug_assert!(rep_offset <= len); + // SAFETY: We know that `buf.len() <= len` + unsafe { rep_index_builder.append(rep_offset as u64) }; + } + if control.is_visible { + let window = windows_iter.next().unwrap(); + if control.is_valid_item { + buf.extend_from_slice(&(window[1] - window[0]).to_le_bytes()); + buf.extend_from_slice( + &variable.data[window[0] as usize..window[1] as usize], + ); + } + } + rep_offset = buf.len(); + } + } + _ => { + return Err(Error::invalid_input_source( + format!( + "Full-zip variable-width offsets must be 32 or 64 bits, got {} bits", + variable.bits_per_offset + ) + .into(), + )); + } + } + + // We might have saved a few bytes by not copying lengths when the length was zero. However, + // if we are over `len` then we have a bug. + debug_assert!(buf.len() <= len); + // Put the final value in the rep index + // SAFETY: `zipped_data.len() == len` + unsafe { + rep_index_builder.append(buf.len() as u64); + } + + let zipped_data = LanceBuffer::from(buf); + let rep_index = rep_index_builder.into_data(); + debug_assert!(!rep_index.is_empty()); + let rep_index = Some(LanceBuffer::from(rep_index)); + Ok(SerializedFullZip { + values: zipped_data, + repetition_index: rep_index, + }) + } + + /// Serializes data into a single buffer according to the full-zip format which zips + /// together the repetition, definition, and value data into a single buffer. + fn serialize_full_zip( + compressed_data: PerValueDataBlock, + repdef: ControlWordIterator, + num_items: u64, + ) -> Result { + match compressed_data { + PerValueDataBlock::Fixed(fixed) => { + Self::serialize_full_zip_fixed(fixed, repdef, num_items) + } + PerValueDataBlock::Variable(var) => { + Self::serialize_full_zip_variable(var, repdef, num_items) + } + } + } + + fn expand_boolean_to_bytes(fixed: FixedWidthDataBlock) -> FixedWidthDataBlock { + debug_assert_eq!(fixed.bits_per_value, 1); + let num_values = fixed.num_values as usize; + let bool_buf = BooleanBuffer::new(fixed.data.into_buffer(), 0, num_values); + let expanded: Vec = (0..num_values).map(|i| bool_buf.value(i) as u8).collect(); + FixedWidthDataBlock { + data: LanceBuffer::from(expanded), + bits_per_value: 8, + num_values: fixed.num_values, + block_info: BlockInfo::new(), + } + } + + fn encode_full_zip( + column_idx: u32, + field: &Field, + compression_strategy: &dyn CompressionStrategy, + data: DataBlock, + repdef: crate::repdef::SerializedRepDefs, + row_number: u64, + num_lists: u64, + ) -> Result { + let max_rep = repdef + .repetition_levels + .as_ref() + .map_or(0, |r| r.iter().max().copied().unwrap_or(0)); + let max_def = repdef + .definition_levels + .as_ref() + .map_or(0, |d| d.iter().max().copied().unwrap_or(0)); + + // To handle FSL we just flatten + // let data = data.flatten(); + + let (num_items, num_visible_items) = + if let Some(rep_levels) = repdef.repetition_levels.as_ref() { + // If there are rep levels there may be "invisible" items and we need to encode + // rep_levels.len() things which might be larger than data.num_values() + (rep_levels.len() as u64, data.num_values()) + } else { + // If there are no rep levels then we encode data.num_values() things + (data.num_values(), data.num_values()) + }; + + let max_visible_def = repdef.max_visible_level.unwrap_or(u16::MAX); + + let repdef_iter = build_control_word_iterator( + repdef.repetition_levels.as_deref(), + max_rep, + repdef.definition_levels.as_deref(), + max_def, + max_visible_def, + num_items as usize, + ); + let bits_rep = repdef_iter.bits_rep(); + let bits_def = repdef_iter.bits_def(); + + // Full-zip requires byte-aligned values; expand 1-bit booleans to 1 byte each. + let data = match data { + DataBlock::FixedWidth(fixed) if fixed.bits_per_value == 1 => { + DataBlock::FixedWidth(Self::expand_boolean_to_bytes(fixed)) + } + other => other, + }; + + let compressor = compression_strategy.create_per_value(field, &data)?; + let (compressed_data, value_encoding) = compressor.compress(data)?; + + let description = match &compressed_data { + PerValueDataBlock::Fixed(fixed) => ProtobufUtils21::fixed_full_zip_layout( + bits_rep, + bits_def, + fixed.bits_per_value as u32, + value_encoding, + &repdef.def_meaning, + num_items as u32, + num_visible_items as u32, + ), + PerValueDataBlock::Variable(variable) => ProtobufUtils21::variable_full_zip_layout( + bits_rep, + bits_def, + variable.bits_per_offset as u32, + value_encoding, + &repdef.def_meaning, + num_items as u32, + num_visible_items as u32, + ), + }; + + let zipped = Self::serialize_full_zip(compressed_data, repdef_iter, num_items)?; + + let data = if let Some(repindex) = zipped.repetition_index { + vec![zipped.values, repindex] + } else { + vec![zipped.values] + }; + + Ok(EncodedPage { + num_rows: num_lists, + column_idx, + data, + description: PageEncoding::Structural(description), + row_number, + }) + } + + fn should_dictionary_encode( + data_block: &DataBlock, + field: &Field, + fixed_width_dictionary_encoding: FixedWidthDictionaryEncoding, + ) -> Option { + const DEFAULT_SAMPLE_SIZE: usize = 4096; + const DEFAULT_SAMPLE_UNIQUE_RATIO: f64 = 0.98; + + // Since we only dictionary encode FixedWidth and VariableWidth blocks for now, we skip + // estimating the size for other types. + match data_block { + DataBlock::FixedWidth(fixed) => { + if fixed.bits_per_value == 64 + && fixed_width_dictionary_encoding == FixedWidthDictionaryEncoding::Exclude64Bit + { + return None; + } + if fixed.bits_per_value != 64 && fixed.bits_per_value != 128 { + return None; + } + if fixed.bits_per_value % 8 != 0 { + return None; + } + } + DataBlock::VariableWidth(var) => { + if var.bits_per_offset != 32 && var.bits_per_offset != 64 { + return None; + } + } + _ => return None, + } + + // Don't dictionary encode tiny arrays. + let too_small = env::var("LANCE_ENCODING_DICT_TOO_SMALL") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(100); + if data_block.num_values() < too_small { + return None; + } + + let num_values = data_block.num_values(); + + // Apply divisor threshold and cap. This is intentionally conservative: the goal is to + // avoid spending too much CPU trying to estimate very high cardinalities. + let divisor: u64 = field + .metadata + .get(DICT_DIVISOR_META_KEY) + .and_then(|val| val.parse().ok()) + .or_else(|| { + env::var("LANCE_ENCODING_DICT_DIVISOR") + .ok() + .and_then(|val| val.parse().ok()) + }) + .unwrap_or(DEFAULT_DICT_DIVISOR); + + let max_cardinality: u64 = env::var("LANCE_ENCODING_DICT_MAX_CARDINALITY") + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(DEFAULT_DICT_MAX_CARDINALITY); + + let threshold_cardinality = num_values + .checked_div(divisor.max(1)) + .unwrap_or(0) + .min(max_cardinality); + if threshold_cardinality == 0 { + return None; + } + + // Get size ratio from metadata or env var. + let threshold_ratio = field + .metadata + .get(DICT_SIZE_RATIO_META_KEY) + .and_then(|val| val.parse::().ok()) + .or_else(|| { + env::var("LANCE_ENCODING_DICT_SIZE_RATIO") + .ok() + .and_then(|val| val.parse().ok()) + }) + .unwrap_or(DEFAULT_DICT_SIZE_RATIO); + + if threshold_ratio <= 0.0 || threshold_ratio > 1.0 { + panic!( + "Invalid parameter: dict-size-ratio is {} which is not in the range (0, 1].", + threshold_ratio + ); + } + + let data_size = data_block.data_size(); + if data_size == 0 { + return None; + } + + let max_encoded_size = (data_size as f64 * threshold_ratio) as u64; + let max_encoded_size = usize::try_from(max_encoded_size).ok()?; + + // Avoid probing dictionary encoding on data that appears to be near-unique + // or likely to exceed the dictionary budget. + if let Some(sample_unique_ratio) = + Self::sample_unique_ratio(data_block, DEFAULT_SAMPLE_SIZE)? + { + if sample_unique_ratio >= DEFAULT_SAMPLE_UNIQUE_RATIO { + return None; + } + + let projected_cardinality = (sample_unique_ratio * num_values as f64).ceil() as u64; + if projected_cardinality > threshold_cardinality { + return None; + } + } + + let max_dict_entries = u32::try_from(threshold_cardinality.min(i32::MAX as u64)).ok()?; + Some(DictEncodingBudget { + max_dict_entries, + max_encoded_size, + }) + } + + /// Samples whether a page looks near-unique before attempting dictionary encoding. + /// + /// The probe uses deterministic block sampling (not RNG sampling), which keeps + /// the check cheap and reproducible across runs. The result is only a gate for + /// whether we try dictionary encoding, not a cardinality statistic. + /// Returns `Some(None)` when there are too few reliable samples or the block type does not + /// support dictionary encoding. Returns `None` for malformed data. + fn sample_unique_ratio(data_block: &DataBlock, max_samples: usize) -> Option> { + use std::collections::HashSet; + + const NUM_SAMPLE_BLOCKS: usize = 32; + const MIN_RELIABLE_SAMPLES: usize = 1024; + + let num_values = usize::try_from(data_block.num_values()).ok()?; + if num_values == 0 { + return Some(None); + } + + let sample_count = num_values.min(max_samples).max(1); + if sample_count < MIN_RELIABLE_SAMPLES { + return Some(None); + } + + let block_count = NUM_SAMPLE_BLOCKS.min(sample_count).min(num_values).max(1); + let samples_per_block = (sample_count / block_count).max(1); + let mut indices = Vec::with_capacity(sample_count); + for block_idx in 0..block_count { + let block_start = block_idx * num_values / block_count; + let next_block_start = ((block_idx + 1) * num_values / block_count).min(num_values); + let block_len = next_block_start.saturating_sub(block_start); + let samples_in_block = samples_per_block.min(block_len); + indices.extend((0..samples_in_block).map(|offset| block_start + offset)); + } + + if indices.len() < MIN_RELIABLE_SAMPLES { + return Some(None); + } + + let ratio = match data_block { + DataBlock::FixedWidth(fixed) => match fixed.bits_per_value { + 64 => { + let values = fixed.data.borrow_to_typed_slice::(); + let values = values.as_ref(); + let mut unique: HashSet = + HashSet::with_capacity(indices.len().min(MIN_RELIABLE_SAMPLES)); + for idx in indices.iter().copied() { + unique.insert(values.get(idx).copied()?); + } + unique.len() as f64 / indices.len() as f64 + } + 128 => { + let values = fixed.data.borrow_to_typed_slice::(); + let values = values.as_ref(); + let mut unique: HashSet = + HashSet::with_capacity(indices.len().min(MIN_RELIABLE_SAMPLES)); + for idx in indices.iter().copied() { + unique.insert(values.get(idx).copied()?); + } + unique.len() as f64 / indices.len() as f64 + } + _ => return Some(None), + }, + DataBlock::VariableWidth(var) => { + use xxhash_rust::xxh3::xxh3_64; + + // Hash variable-width slices instead of storing borrowed slice keys. + let mut unique: HashSet = + HashSet::with_capacity(indices.len().min(MIN_RELIABLE_SAMPLES)); + match var.bits_per_offset { + 32 => { + let offsets_ref = var.offsets.borrow_to_typed_slice::(); + let offsets: &[u32] = offsets_ref.as_ref(); + for i in indices.iter().copied() { + let start = usize::try_from(*offsets.get(i)?).ok()?; + let end = usize::try_from(*offsets.get(i + 1)?).ok()?; + if start > end || end > var.data.len() { + return None; + } + unique.insert(xxh3_64(&var.data[start..end])); + } + } + 64 => { + let offsets_ref = var.offsets.borrow_to_typed_slice::(); + let offsets: &[u64] = offsets_ref.as_ref(); + for i in indices.iter().copied() { + let start = usize::try_from(*offsets.get(i)?).ok()?; + let end = usize::try_from(*offsets.get(i + 1)?).ok()?; + if start > end || end > var.data.len() { + return None; + } + unique.insert(xxh3_64(&var.data[start..end])); + } + } + _ => return Some(None), + } + unique.len() as f64 / indices.len() as f64 + } + _ => return Some(None), + }; + + Some(Some(ratio)) + } + + fn slice_repdef(repdef: &SerializedRepDefs, range: Range) -> SerializedRepDefs { + let repetition_levels = repdef + .repetition_levels + .as_ref() + .map(|levels| levels[range.clone()].to_vec()); + let definition_levels = repdef + .definition_levels + .as_ref() + .map(|levels| levels[range].to_vec()); + SerializedRepDefs::new_with_fixed_size_list_levels( + repetition_levels, + definition_levels, + repdef.def_meaning.clone(), + repdef.has_fixed_size_list_levels(), + ) + } + + fn slice_arrays( + arrays: &[ArrayRef], + value_start: u64, + num_values: u64, + ) -> Result> { + if num_values == 0 { + return Ok(Vec::new()); + } + + let mut values_to_skip = usize::try_from(value_start).map_err(|_| { + Error::invalid_input(format!("Value start {} is too large", value_start)) + })?; + let mut values_remaining = usize::try_from(num_values).map_err(|_| { + Error::invalid_input(format!("Value count {} is too large", num_values)) + })?; + let mut sliced = Vec::new(); + + for array in arrays { + if values_to_skip >= array.len() { + values_to_skip -= array.len(); + continue; + } + + let offset = values_to_skip; + let len = (array.len() - offset).min(values_remaining); + sliced.push(array.slice(offset, len)); + values_remaining -= len; + values_to_skip = 0; + + if values_remaining == 0 { + break; + } + } + + if values_remaining != 0 { + return Err(Error::internal(format!( + "Page split requested {} values starting at {}, but the page did not contain enough values", + num_values, value_start + ))); + } + + Ok(sliced) + } + + fn split_pages_for_miniblock_repdef_budget( + arrays: Vec, + repdef: SerializedRepDefs, + budget: MiniBlockRepDefBudget, + row_number: u64, + num_rows: u64, + ) -> Result> { + if budget == MiniBlockRepDefBudget::WithinBudget { + return Ok(vec![PrimitivePageData { + arrays, + structure: PrimitivePageStructure::Dense { + repdef, + single_row_miniblock_repdef_levels: None, + }, + row_number, + num_rows, + }]); + } + if let MiniBlockRepDefBudget::SingleRowOverBudget(num_levels) = budget { + return Ok(vec![PrimitivePageData { + arrays, + structure: PrimitivePageStructure::Dense { + repdef, + single_row_miniblock_repdef_levels: Some(num_levels), + }, + row_number, + num_rows, + }]); + } + + let MiniBlockRepDefBudget::RequiresPageSplit(splits) = budget else { + unreachable!(); + }; + + let mut pages = Vec::with_capacity(splits.len()); + for split in splits { + let arrays = Self::slice_arrays(&arrays, split.value_start, split.num_values)?; + let repdef = Self::slice_repdef(&repdef, split.level_range); + pages.push(PrimitivePageData { + arrays, + structure: PrimitivePageStructure::Dense { + repdef, + single_row_miniblock_repdef_levels: None, + }, + row_number: row_number + split.row_start, + num_rows: split.num_rows, + }); + } + Ok(pages) + } + + fn encode_dense_page( + ctx: PrimitiveEncodeContext, + page: PrimitivePageData, + compression_strategy: Arc, + miniblock_chunk_size: MiniblockChunkSize, + complex_null_encoding: ComplexNullEncoding, + fixed_width_dictionary_encoding: FixedWidthDictionaryEncoding, + ) -> Result { + let PrimitiveEncodeContext { + column_idx, + field, + encoding_metadata, + is_simple_validity, + has_repdef_info, + } = ctx; + let PrimitivePageData { + arrays, + structure, + row_number, + num_rows, + } = page; + let num_values = arrays.iter().map(|arr| arr.len() as u64).sum(); + + let (repdef, single_row_miniblock_repdef_levels) = match structure { + PrimitivePageStructure::Dense { + repdef, + single_row_miniblock_repdef_levels, + } => (repdef, single_row_miniblock_repdef_levels), + PrimitivePageStructure::Sparse { .. } => { + unreachable!("dense atom received sparse page") + } + }; + + if num_values == 0 { + // This page contains only structural events, such as empty/null list rows. + // The existing complex-null layout stores the rep/def stream without value buffers. + log::debug!( + "Encoding column {} with {} items ({} rows) using complex-null layout", + column_idx, + num_values, + num_rows + ); + return Self::encode_complex_all_null( + column_idx, + repdef, + row_number, + num_rows, + complex_null_encoding, + compression_strategy.as_ref(), + ); + } + + let leaf_validity = Self::leaf_validity(&repdef, num_values as usize)?; + let all_null = leaf_validity + .as_ref() + .map(|validity| validity.count_set_bits() == 0) + .unwrap_or(false); + + if all_null { + return if is_simple_validity { + log::debug!( + "Encoding column {} with {} items ({} rows) using simple-null layout", + column_idx, + num_values, + num_rows + ); + Self::encode_simple_all_null(column_idx, num_values, row_number) + } else { + log::debug!( + "Encoding column {} with {} items ({} rows) using complex-null layout", + column_idx, + num_values, + num_rows + ); + Self::encode_complex_all_null( + column_idx, + repdef, + row_number, + num_rows, + complex_null_encoding, + compression_strategy.as_ref(), + ) + }; + } + + if let DataType::Struct(fields) = &field.data_type() + && fields.is_empty() + { + if has_repdef_info { + return Err(Error::invalid_input_source(format!("Empty structs with rep/def information are not yet supported. The field {} is an empty struct that either has nulls or is in a list.", field.name).into())); + } + // This is maybe a little confusing but the reader should never look at this anyways and it + // seems like overkill to invent a new layout just for "empty structs". + return Self::encode_simple_all_null(column_idx, num_values, row_number); + } + + let data_block = DataBlock::from_arrays(&arrays, num_values); + + if let Some(num_levels) = single_row_miniblock_repdef_levels { + let requested_encoding = encoding_metadata + .get(STRUCTURAL_ENCODING_META_KEY) + .map(|requested| requested.to_lowercase()); + let fullzip_error = match &data_block { + DataBlock::FixedWidth(fixed) if !fixed.bits_per_value.is_multiple_of(8) => { + Some(format!( + "Full-zip fixed-width values must be byte aligned, got {} bits per value", + fixed.bits_per_value + )) + } + DataBlock::VariableWidth(variable) + if !variable.bits_per_offset.is_multiple_of(8) => + { + Some(format!( + "Full-zip variable-width offsets must be byte aligned, got {} bits per offset", + variable.bits_per_offset + )) + } + DataBlock::VariableWidth(variable) + if variable.bits_per_offset != 32 && variable.bits_per_offset != 64 => + { + Some(format!( + "Full-zip variable-width offsets must be 32 or 64 bits, got {} bits", + variable.bits_per_offset + )) + } + DataBlock::Struct(struct_data_block) + if !struct_data_block.has_variable_width_child() => + { + Some( + "Full-zip packed struct requires at least one variable-width child" + .to_string(), + ) + } + DataBlock::Dictionary(_) => { + Some("Full-zip does not encode dictionary data blocks directly".to_string()) + } + DataBlock::FixedSizeList(fsl) => match fsl.clone().try_into_flat() { + Some(flat) if flat.bits_per_value.is_multiple_of(8) => None, + Some(flat) => Some(format!( + "Full-zip fixed-size-list values must be byte aligned after flattening, got {} bits per value", + flat.bits_per_value + )), + None => Some( + "Full-zip fixed-size-list capability requires a flat fixed-width child" + .to_string(), + ), + }, + DataBlock::FixedWidth(_) | DataBlock::VariableWidth(_) | DataBlock::Struct(_) => { + None + } + other => Some(format!( + "Full-zip does not support value block type {}", + other.name() + )), + }; + match requested_encoding.as_deref() { + Some(STRUCTURAL_ENCODING_FULLZIP) => { + if let Some(reason) = fullzip_error { + return Err(Error::invalid_input_source(reason.into())); + } + return Self::encode_full_zip( + column_idx, + &field, + compression_strategy.as_ref(), + data_block, + repdef, + row_number, + num_rows, + ); + } + Some(STRUCTURAL_ENCODING_MINIBLOCK) | None => { + if requested_encoding.is_none() && fullzip_error.is_none() { + log::debug!( + "Encoding column {} with {} items using full-zip layout because mini-block cannot split the structural page", + column_idx, + num_values + ); + return Self::encode_full_zip( + column_idx, + &field, + compression_strategy.as_ref(), + data_block, + repdef, + row_number, + num_rows, + ); + } + return Err(Error::invalid_input_source( + format!( + "Mini-block cannot encode {} rep/def levels in one top-level row. \ + This usually means the row contains too much nested structure \ + for the current layout.", + num_levels + ) + .into(), + )); + } + _ => {} + } + } + + let requires_full_zip_packed_struct = + if let DataBlock::Struct(ref struct_data_block) = data_block { + struct_data_block.has_variable_width_child() + } else { + false + }; + + if requires_full_zip_packed_struct { + log::debug!( + "Encoding column {} with {} items using full-zip packed struct layout", + column_idx, + num_values + ); + return Self::encode_full_zip( + column_idx, + &field, + compression_strategy.as_ref(), + data_block, + repdef, + row_number, + num_rows, + ); + } + + if let DataBlock::Dictionary(dict) = data_block { + log::debug!( + "Encoding column {} with {} items using dictionary encoding (already dictionary encoded)", + column_idx, + num_values + ); + let (mut indices_data_block, dictionary_data_block) = dict.into_parts(); + // TODO: https://github.com/lancedb/lance/issues/4809 + // If we compute stats on dictionary_data_block => panic. + // If we don't compute stats on indices_data_block => panic. + // This is messy. Don't make me call compute_stat ever. + indices_data_block.compute_stat(); + return Self::encode_miniblock( + column_idx, + &field, + compression_strategy.as_ref(), + indices_data_block, + repdef, + row_number, + Some(dictionary_data_block), + num_rows, + miniblock_chunk_size, + ); + } + + // Try dictionary encoding first if applicable. If encoding aborts, fall back to the + // preferred structural encoding. + let dict_result = Self::should_dictionary_encode( + &data_block, + &field, + fixed_width_dictionary_encoding, + ) + .and_then(|budget| { + log::debug!( + "Encoding column {} with {} items using dictionary encoding (mini-block layout)", + column_idx, + num_values + ); + dict::dictionary_encode( + &data_block, + budget.max_dict_entries, + budget.max_encoded_size, + ) + }); + + if let Some((indices_data_block, dictionary_data_block)) = dict_result { + Self::encode_miniblock( + column_idx, + &field, + compression_strategy.as_ref(), + indices_data_block, + repdef, + row_number, + Some(dictionary_data_block), + num_rows, + miniblock_chunk_size, + ) + } else if Self::prefers_miniblock(&data_block, encoding_metadata.as_ref()) { + log::debug!( + "Encoding column {} with {} items using mini-block layout", + column_idx, + num_values + ); + Self::encode_miniblock( + column_idx, + &field, + compression_strategy.as_ref(), + data_block, + repdef, + row_number, + None, + num_rows, + miniblock_chunk_size, + ) + } else if Self::prefers_fullzip(encoding_metadata.as_ref()) { + log::debug!( + "Encoding column {} with {} items using full-zip layout", + column_idx, + num_values + ); + Self::encode_full_zip( + column_idx, + &field, + compression_strategy.as_ref(), + data_block, + repdef, + row_number, + num_rows, + ) + } else { + Err(Error::invalid_input_source(format!("Cannot determine structural encoding for field {}. This typically indicates an invalid value of the field metadata key {}", field.name, STRUCTURAL_ENCODING_META_KEY).into())) + } + } + + // Creates encode tasks, consuming all buffered data + fn do_flush( + &mut self, + arrays: Vec, + repdefs: Vec, + row_number: u64, + num_rows: u64, + ) -> Result> { + let num_values = arrays.iter().map(|arr| arr.len() as u64).sum(); + let is_simple_validity = repdefs.iter().all(|rd| rd.is_simple_validity()); + let has_repdef_info = repdefs.iter().any(|rd| !rd.is_empty()); + let normalized = RepDefBuilder::normalize(repdefs); + let plan_ctx = PrimitivePlanContext { + column_idx: self.column_index, + field: &self.field, + encoding_metadata: &self.encoding_metadata, + }; + let mut pages = None; + for page_encoding in self.page_encodings.iter() { + if let Some(planned) = page_encoding.behavior.try_plan_pages( + &plan_ctx, + &arrays, + &normalized, + row_number, + num_rows, + num_values, + )? { + pages = Some(planned); + break; + } + } + let pages = pages.ok_or_else(|| { + Error::invalid_input_source( + format!( + "No primitive page planner supports field '{}'", + self.field.name + ) + .into(), + ) + })?; + + let mut tasks = Vec::with_capacity(pages.len()); + let ctx = PrimitiveEncodeContext { + column_idx: self.column_index, + field: self.field.clone(), + encoding_metadata: self.encoding_metadata.clone(), + is_simple_validity, + has_repdef_info, + }; + for page in pages { + let ctx = ctx.clone(); + let page_encodings = self.page_encodings.clone(); + let task = + spawn_cpu(move || Self::encode_page(page_encodings.as_ref(), &ctx, page)).boxed(); + tasks.push(task); + } + Ok(tasks) + } + + fn extract_validity_buf( + array: Arc, + repdef: &mut RepDefBuilder, + keep_original_array: bool, + ) -> Result> { + if let Some(validity) = array.nulls() { + if keep_original_array { + repdef.add_validity_bitmap(validity.clone()); + } else { + repdef.add_validity_bitmap(deep_copy_nulls(Some(validity)).unwrap()); + } + let data_no_nulls = array.to_data().into_builder().nulls(None).build()?; + Ok(make_array(data_no_nulls)) + } else { + repdef.add_no_null(array.len()); + Ok(array) + } + } + + fn extract_validity( + mut array: Arc, + repdef: &mut RepDefBuilder, + keep_original_array: bool, + ) -> Result> { + match array.data_type() { + DataType::Null => { + repdef.add_validity_bitmap(NullBuffer::new(BooleanBuffer::new_unset(array.len()))); + Ok(array) + } + DataType::Dictionary(_, _) => { + array = dict::normalize_dict_nulls(array)?; + Self::extract_validity_buf(array, repdef, keep_original_array) + } + // Extract our validity buf but NOT any child validity bufs. (they will be encoded in + // as part of the values). Note: for FSL we do not use repdef.add_fsl because we do + // NOT want to increase the repdef depth. + // + // This would be quite catasrophic for something like vector embeddings. Imagine we + // had thousands of vectors and some were null but no vector contained null items. If + // we treated the vectors (primitive FSL) like we treat structural FSL we would end up + // with a rep/def value for every single item in the vector. + _ => Self::extract_validity_buf(array, repdef, keep_original_array), + } + } +} + +impl PrimitivePageEncodingBehavior for RejectSparsePrimitiveEncoding { + fn validate_field(&self, field: &Field, metadata: &HashMap) -> Result<()> { + if metadata + .get(STRUCTURAL_ENCODING_META_KEY) + .is_some_and(|requested| requested.eq_ignore_ascii_case(STRUCTURAL_ENCODING_SPARSE)) + { + return Err(Error::invalid_input_source( + format!( + "Field '{}' requests sparse structural encoding, which is not enabled by the selected file format", + field.name + ) + .into(), + )); + } + Ok(()) + } +} + +fn plan_dense_primitive_pages( + arrays: &[ArrayRef], + normalized: &NormalizedStructuralPlan, + row_number: u64, + num_rows: u64, + num_values: u64, +) -> Result> { + let (repdef, miniblock_repdef_budget) = normalized.serialize_with_miniblock_repdef_budget( + miniblock::max_repdef_levels_per_chunk, + num_rows, + num_values, + )?; + PrimitiveStructuralEncoder::split_pages_for_miniblock_repdef_budget( + arrays.to_vec(), + repdef, + miniblock_repdef_budget, + row_number, + num_rows, + ) +} + +impl PrimitivePageEncodingBehavior for DenseU16PrimitiveEncoding { + fn try_plan_pages( + &self, + _ctx: &PrimitivePlanContext<'_>, + arrays: &[ArrayRef], + normalized: &NormalizedStructuralPlan, + row_number: u64, + num_rows: u64, + num_values: u64, + ) -> Result>> { + Ok(Some(plan_dense_primitive_pages( + arrays, normalized, row_number, num_rows, num_values, + )?)) + } + + fn try_encode_page( + &self, + ctx: &PrimitiveEncodeContext, + page: PrimitivePageData, + ) -> Result { + if !matches!(&page.structure, PrimitivePageStructure::Dense { .. }) { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + } + Ok(PrimitiveEncodeAttempt::Encoded( + PrimitiveStructuralEncoder::encode_dense_page( + ctx.clone(), + page, + self.compression.clone(), + MiniblockChunkSize::U16, + ComplexNullEncoding::RawLevels, + FixedWidthDictionaryEncoding::Exclude64Bit, + )?, + )) + } +} + +impl PrimitivePageEncodingBehavior for DenseU32PrimitiveEncoding { + fn try_plan_pages( + &self, + _ctx: &PrimitivePlanContext<'_>, + arrays: &[ArrayRef], + normalized: &NormalizedStructuralPlan, + row_number: u64, + num_rows: u64, + num_values: u64, + ) -> Result>> { + Ok(Some(plan_dense_primitive_pages( + arrays, normalized, row_number, num_rows, num_values, + )?)) + } + + fn try_encode_page( + &self, + ctx: &PrimitiveEncodeContext, + page: PrimitivePageData, + ) -> Result { + if !matches!(&page.structure, PrimitivePageStructure::Dense { .. }) { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + } + Ok(PrimitiveEncodeAttempt::Encoded( + PrimitiveStructuralEncoder::encode_dense_page( + ctx.clone(), + page, + self.compression.clone(), + MiniblockChunkSize::U32, + ComplexNullEncoding::CompressedLevels, + FixedWidthDictionaryEncoding::Include64Bit, + )?, + )) + } +} + +impl PrimitivePageEncodingBehavior for SparsePrimitiveEncoding { + fn try_plan_pages( + &self, + ctx: &PrimitivePlanContext<'_>, + arrays: &[ArrayRef], + normalized: &NormalizedStructuralPlan, + row_number: u64, + num_rows: u64, + num_values: u64, + ) -> Result>> { + let requested_encoding = ctx.encoding_metadata.get(STRUCTURAL_ENCODING_META_KEY); + let requests_sparse = requested_encoding + .is_some_and(|requested| requested.eq_ignore_ascii_case(STRUCTURAL_ENCODING_SPARSE)); + if requests_sparse { + let plan = sparse::writer::plan(normalized, num_values)?; + if sparse::writer::uses_constant_layout(&plan, ctx.field) { + return Ok(None); + } + return Ok(Some(vec![PrimitivePageData { + arrays: arrays.to_vec(), + structure: PrimitivePageStructure::Sparse { + plan, + prepared_values: None, + }, + row_number, + num_rows, + }])); + } + + let (_, miniblock_repdef_budget) = normalized.serialize_with_miniblock_repdef_budget( + miniblock::max_repdef_levels_per_chunk, + num_rows, + num_values, + )?; + let automatic_sparse = layout::select_automatic_sparse( + requested_encoding.map(String::as_str), + &miniblock_repdef_budget, + || { + let data = DataBlock::from_arrays(arrays, num_values); + if !sparse::writer::supports_value_block(&data) { + return Ok(None); + } + let prepared_values = match sparse::writer::prepare_values( + ctx.field, + self.compression.as_ref(), + data, + MiniblockChunkSize::U32, + ) { + Ok(prepared_values) => prepared_values, + Err(error) => { + debug!( + "Keeping column {} on its dense structural path because sparse value preparation is unavailable: {}", + ctx.column_idx, error + ); + return Ok(None); + } + }; + let plan = sparse::writer::plan(normalized, num_values)?; + if sparse::writer::uses_constant_layout(&plan, ctx.field) { + return Ok(None); + } + Ok(Some((plan, prepared_values))) + }, + )?; + Ok(automatic_sparse.map(|(plan, prepared_values)| { + vec![PrimitivePageData { + arrays: arrays.to_vec(), + structure: PrimitivePageStructure::Sparse { + plan, + prepared_values: Some(prepared_values), + }, + row_number, + num_rows, + }] + })) + } + + fn try_encode_page( + &self, + ctx: &PrimitiveEncodeContext, + page: PrimitivePageData, + ) -> Result { + if !matches!(&page.structure, PrimitivePageStructure::Sparse { .. }) { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + } + let PrimitivePageData { + arrays, + structure: + PrimitivePageStructure::Sparse { + plan, + prepared_values, + }, + row_number, + num_rows, + } = page + else { + unreachable!() + }; + let num_values = arrays.iter().map(|array| array.len() as u64).sum(); + log::debug!( + "Encoding column {} with {} visible items ({} rows) using sparse layout", + ctx.column_idx, + num_values, + num_rows + ); + Ok(PrimitiveEncodeAttempt::Encoded( + sparse::writer::encode_page( + ctx.column_idx, + &ctx.field, + self.compression.as_ref(), + prepared_values.map_or_else( + || { + sparse::writer::SparseValueInput::Unprepared(DataBlock::from_arrays( + &arrays, num_values, + )) + }, + sparse::writer::SparseValueInput::Prepared, + ), + plan, + row_number, + num_rows, + MiniblockChunkSize::U32, + )?, + )) + } +} + +impl PrimitivePageEncodingBehavior for ConstantPrimitiveEncoding { + fn try_encode_page( + &self, + ctx: &PrimitiveEncodeContext, + page: PrimitivePageData, + ) -> Result { + let PrimitivePageStructure::Dense { repdef, .. } = &page.structure else { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + }; + let num_values: u64 = page.arrays.iter().map(|array| array.len() as u64).sum(); + if num_values == 0 { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + } + let leaf_validity = PrimitiveStructuralEncoder::leaf_validity(repdef, num_values as usize)?; + if leaf_validity + .as_ref() + .is_some_and(|validity| validity.count_set_bits() == 0) + || matches!(ctx.field.data_type(), DataType::Struct(fields) if fields.is_empty()) + { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + } + let Some(scalar) = + PrimitiveStructuralEncoder::find_constant_scalar(&page.arrays, leaf_validity.as_ref())? + else { + return Ok(PrimitiveEncodeAttempt::Unhandled(page)); + }; + let PrimitivePageData { + structure: PrimitivePageStructure::Dense { repdef, .. }, + row_number, + num_rows, + .. + } = page + else { + unreachable!() + }; + log::debug!( + "Encoding column {} with {} items ({} rows) using constant layout", + ctx.column_idx, + num_values, + num_rows + ); + Ok(PrimitiveEncodeAttempt::Encoded( + constant::encode_constant_page(ctx.column_idx, scalar, repdef, row_number, num_rows)?, + )) + } +} + +impl FieldEncoder for PrimitiveStructuralEncoder { + // Buffers data, if there is enough to write a page then we create an encode task + fn maybe_encode( + &mut self, + array: ArrayRef, + _external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let array = Self::extract_validity(array, &mut repdef, self.keep_original_array)?; + self.accumulated_repdefs.push(repdef); + + if let Some((arrays, row_number, num_rows)) = + self.accumulation_queue.insert(array, row_number, num_rows) + { + let accumulated_repdefs = std::mem::take(&mut self.accumulated_repdefs); + Ok(self.do_flush(arrays, accumulated_repdefs, row_number, num_rows)?) + } else { + Ok(vec![]) + } + } + + // If there is any data left in the buffer then create an encode task from it + fn flush(&mut self, _external_buffers: &mut OutOfLineBuffers) -> Result> { + if let Some((arrays, row_number, num_rows)) = self.accumulation_queue.flush() { + let accumulated_repdefs = std::mem::take(&mut self.accumulated_repdefs); + Ok(self.do_flush(arrays, accumulated_repdefs, row_number, num_rows)?) + } else { + Ok(vec![]) + } + } + + fn num_columns(&self) -> u32 { + 1 + } + + fn finish( + &mut self, + _external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + std::future::ready(Ok(vec![EncodedColumn::default()])).boxed() + } +} + +#[cfg(test)] +#[allow(clippy::single_range_in_vec_init)] +mod tests { + use super::{ + ChunkInstructions, DataBlock, DecodeMiniBlockTask, DecodePageTask, FixedFullZipDecodeTask, + FixedPerValueDecompressor, FixedWidthDataBlock, FixedWidthDictionaryEncoding, + FullZipCacheableState, FullZipDecodeDetails, FullZipDecodeTaskItem, FullZipReadSource, + FullZipRepIndexDetails, FullZipScheduler, LazyLevels, LevelCodec, LevelCursor, LevelPlan, + MiniBlockChunk, MiniBlockChunkIndex, MiniBlockCompressed, MiniblockChunkSize, + PerValueDataBlock, PerValueDecompressor, PreambleAction, RunEndsBuilder, RunPosition, + RunStorage, StructuralPageScheduler, VariableFullZipDecoder, dense_levels_from_block, + validate_complex_all_null_levels, + }; + use crate::buffer::LanceBuffer; + use crate::compression::{ + BlockCompressor, DefaultDecompressionStrategy, MiniBlockDecompressor, + }; + use crate::constants::{ + COMPRESSION_LEVEL_META_KEY, COMPRESSION_META_KEY, DICT_VALUES_COMPRESSION_LEVEL_META_KEY, + DICT_VALUES_COMPRESSION_META_KEY, STRUCTURAL_ENCODING_META_KEY, + STRUCTURAL_ENCODING_MINIBLOCK, + }; + use crate::data::BlockInfo; + use crate::decoder::{PageEncoding, StructuralFieldDecoder}; + use crate::encodings::logical::primitive::fullzip::PerValueCompressor; + use crate::encodings::logical::primitive::{ + ChunkDrainInstructions, LoadedChunk, PrimitiveStructuralEncoder, + StructuralPrimitiveFieldDecoder, + }; + use crate::encodings::physical::rle::{RleDecompressor, RleEncoder, RleRuns, RunLengthWidth}; + use crate::encodings::physical::value::{ValueDecompressor, ValueEncoder}; + use crate::format::ProtobufUtils21; + use crate::format::pb21; + use crate::format::pb21::compressive_encoding::Compression; + use crate::repdef::build_control_word_iterator; + use crate::testing::TestEncoding; + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + use arrow_array::{ + Array, ArrayRef, FixedSizeListArray, Float32Array, Int8Array, StringArray, UInt8Array, + make_array, + }; + use arrow_buffer::ScalarBuffer; + use arrow_schema::{DataType, Field as ArrowField}; + use std::collections::HashMap; + use std::{collections::VecDeque, sync::Arc}; + + #[test] + fn test_is_narrow() { + let int8_array = Int8Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int8_array); + let block = DataBlock::from_array(array_ref); + + assert!(PrimitiveStructuralEncoder::is_narrow(&block)); + + let string_array = StringArray::from(vec![Some("hello"), Some("world")]); + let block = DataBlock::from_array(string_array); + assert!(PrimitiveStructuralEncoder::is_narrow(&block)); + + let string_array = StringArray::from(vec![ + Some("hello world".repeat(100)), + Some("world".to_string()), + ]); + let block = DataBlock::from_array(string_array); + assert!((!PrimitiveStructuralEncoder::is_narrow(&block))); + } + + #[test] + fn test_primitive_decoder_empty_page_queue_returns_error() { + let field = Arc::new(ArrowField::new("vector", DataType::Float32, true)); + let mut decoder = StructuralPrimitiveFieldDecoder::new(&field, false); + + let err = decoder.drain(1).unwrap_err(); + assert!( + matches!(&err, lance_core::Error::Internal { .. }), + "expected internal error, got: {err:?}" + ); + let message = err.to_string(); + for expected in [ + "Primitive decoder missing page decoder", + "field 'vector'", + "data_type=Float32", + "requested_rows=1", + "remaining_rows=1", + "rows_drained_in_current=0", + "queued_pages=0", + ] { + assert!( + message.contains(expected), + "expected error to contain {expected:?}, got: {message}" + ); + } + } + + #[test] + fn test_fullzip_fixed_rejects_non_byte_aligned_values() { + let fixed = FixedWidthDataBlock { + data: LanceBuffer::from(vec![0_u8]), + bits_per_value: 1, + num_values: 8, + block_info: BlockInfo::new(), + }; + let repdef = build_control_word_iterator(None, 0, None, 0, u16::MAX, 8); + + let Err(err) = PrimitiveStructuralEncoder::serialize_full_zip_fixed(fixed, repdef, 8) + else { + panic!("expected full-zip to reject 1-bit fixed-width values"); + }; + assert!( + err.to_string().contains("byte aligned"), + "unexpected error: {err}" + ); + } + + fn decode_fixed_fullzip_no_levels( + decompressor: Arc, + data: Vec, + num_rows: usize, + bytes_per_value: usize, + ) -> DataBlock { + Box::new(FixedFullZipDecodeTask { + details: Arc::new(FullZipDecodeDetails { + value_decompressor: PerValueDecompressor::Fixed(decompressor), + def_meaning: Arc::from([]), + ctrl_word_parser: crate::repdef::ControlWordParser::new(0, 0), + max_rep: 0, + max_visible_def: u16::MAX, + }), + data, + num_rows, + bytes_per_value, + }) + .decode() + .unwrap() + .data + } + + #[test] + fn test_fixed_fullzip_decode_preallocates_exact_output_size() { + #[derive(Debug)] + struct IdentityFixedDecompressor; + + impl FixedPerValueDecompressor for IdentityFixedDecompressor { + fn decompress( + &self, + data: FixedWidthDataBlock, + num_rows: u64, + ) -> crate::Result { + assert_eq!(data.num_values, num_rows); + Ok(DataBlock::FixedWidth(data)) + } + + fn bits_per_value(&self) -> u64 { + 32 + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + num_values.checked_mul(4) + } + } + + let make_item = |num_rows: u64| FullZipDecodeTaskItem { + data: PerValueDataBlock::Fixed(FixedWidthDataBlock { + data: LanceBuffer::from(vec![7_u8; num_rows as usize * 4]), + bits_per_value: 32, + num_values: num_rows, + block_info: BlockInfo::new(), + }), + rows_in_buf: num_rows, + }; + + let num_rows = 512; + let decoded = decode_fixed_fullzip_no_levels( + Arc::new(IdentityFixedDecompressor), + vec![make_item(128), make_item(384)], + num_rows, + 4, + ); + let values = decoded.as_fixed_width_ref().unwrap(); + let expected_size = num_rows * 4; + assert_eq!(values.data.len(), expected_size); + assert_eq!(values.data.clone().into_buffer().capacity(), expected_size); + } + + #[test] + fn test_fixed_fullzip_decode_falls_back_when_output_size_is_not_exact() { + #[derive(Debug)] + struct FallbackFixedDecompressor; + + impl FixedPerValueDecompressor for FallbackFixedDecompressor { + fn decompress( + &self, + data: FixedWidthDataBlock, + num_rows: u64, + ) -> crate::Result { + assert_eq!(data.num_values, num_rows); + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(vec![7_u8; num_rows as usize * 4]), + bits_per_value: 32, + num_values: num_rows, + block_info: BlockInfo::new(), + })) + } + + fn bits_per_value(&self) -> u64 { + // This deliberately cannot be multiplied by num_rows. If FullZip treats + // bits_per_value as an exact decoded-size estimate, decoding will fail. + u64::MAX - 7 + } + } + + let num_rows = 2; + let decoded = decode_fixed_fullzip_no_levels( + Arc::new(FallbackFixedDecompressor), + vec![FullZipDecodeTaskItem { + data: PerValueDataBlock::Fixed(FixedWidthDataBlock { + data: LanceBuffer::from(vec![0_u8; num_rows * 4]), + bits_per_value: 32, + num_values: num_rows as u64, + block_info: BlockInfo::new(), + }), + rows_in_buf: num_rows as u64, + }], + num_rows, + 4, + ); + let values = decoded.as_fixed_width_ref().unwrap(); + assert_eq!(values.num_values, num_rows as u64); + assert_eq!(values.data.len(), num_rows * 4); + } + + #[test] + fn test_fixed_fullzip_real_fsl_preallocates_exact_output_size() { + let num_rows = 64; + let dimension = 32; + let items = Arc::new(Float32Array::from_iter_values( + (0..num_rows * dimension).map(|value| value as f32), + )); + let item_field = Arc::new(ArrowField::new("item", DataType::Float32, false)); + let sample = FixedSizeListArray::new(item_field, dimension as i32, items, None); + + let (data, compression) = PerValueCompressor::compress( + &ValueEncoder::default(), + DataBlock::from_array(sample.clone()), + ) + .unwrap(); + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!("expected fixed-size-list compression"); + }; + let decompressor = ValueDecompressor::from_fsl(fsl.as_ref()); + let expected_size = num_rows * dimension * size_of::(); + assert_eq!( + FixedPerValueDecompressor::decoded_size_bytes(&decompressor, num_rows as u64), + Some(expected_size as u64) + ); + + let decoded = decode_fixed_fullzip_no_levels( + Arc::new(decompressor), + vec![FullZipDecodeTaskItem { + data, + rows_in_buf: num_rows as u64, + }], + num_rows, + dimension * size_of::(), + ); + let fsl = decoded.as_fixed_size_list_ref().unwrap(); + let values = fsl.child.as_fixed_width_ref().unwrap(); + assert_eq!(values.data.len(), expected_size); + assert_eq!(values.data.clone().into_buffer().capacity(), expected_size); + + let decoded_array = make_array( + decoded + .into_arrow(sample.data_type().clone(), true) + .unwrap(), + ); + assert_eq!(decoded_array.as_ref(), &sample); + } + + #[test] + fn test_fixed_fullzip_nullable_fsl_uses_fallback_end_to_end() { + #[derive(Debug)] + struct NullableFslDecompressor { + inner: ValueDecompressor, + } + + impl FixedPerValueDecompressor for NullableFslDecompressor { + fn decompress( + &self, + data: FixedWidthDataBlock, + num_rows: u64, + ) -> crate::Result { + FixedPerValueDecompressor::decompress(&self.inner, data, num_rows) + } + + fn bits_per_value(&self) -> u64 { + // FullZip must not use this physical row width as an exact decoded-size + // estimate for the nullable, multi-buffer Arrow output. + u64::MAX - 7 + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + FixedPerValueDecompressor::decoded_size_bytes(&self.inner, num_values) + } + } + + let num_rows = 64; + let items = Arc::new(UInt8Array::from_iter( + (0..num_rows).map(|value| (value % 3 != 0).then_some(value as u8)), + )); + let item_field = Arc::new(ArrowField::new("item", DataType::UInt8, true)); + let sample = FixedSizeListArray::new(item_field, 1, items, None); + + let (data, compression) = PerValueCompressor::compress( + &ValueEncoder::default(), + DataBlock::from_array(sample.clone()), + ) + .unwrap(); + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!("expected fixed-size-list compression"); + }; + let decompressor = NullableFslDecompressor { + inner: ValueDecompressor::from_fsl(fsl.as_ref()), + }; + assert_eq!( + FixedPerValueDecompressor::decoded_size_bytes(&decompressor, num_rows as u64), + None + ); + + let decoded = decode_fixed_fullzip_no_levels( + Arc::new(decompressor), + vec![FullZipDecodeTaskItem { + data, + rows_in_buf: num_rows as u64, + }], + num_rows, + 2, + ); + let decoded_array = make_array( + decoded + .into_arrow(sample.data_type().clone(), true) + .unwrap(), + ); + assert_eq!(decoded_array.as_ref(), &sample); + } + + #[test] + fn test_miniblock_decode_uses_exact_fixed_width_output_size() { + #[derive(Debug)] + struct FixedWidthMiniBlockDecompressor; + + impl MiniBlockDecompressor for FixedWidthMiniBlockDecompressor { + fn decompress( + &self, + data: Vec, + num_values: u64, + ) -> crate::Result { + assert_eq!(data.len(), 1); + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: data.into_iter().next().unwrap(), + bits_per_value: 32, + num_values, + block_info: BlockInfo::new(), + })) + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + num_values.checked_mul(4) + } + } + + let num_rows = 512; + let expected_size = num_rows * 4; + let mut chunk_data = Vec::new(); + chunk_data.extend_from_slice(&0_u16.to_le_bytes()); + chunk_data.extend_from_slice(&(expected_size as u16).to_le_bytes()); + let header_padding = + lance_core::utils::bit::pad_bytes::<{ super::MINIBLOCK_ALIGNMENT }>(chunk_data.len()); + chunk_data.resize(chunk_data.len() + header_padding, 0); + chunk_data.resize(chunk_data.len() + expected_size as usize, 7); + + let task = DecodeMiniBlockTask { + rep_decompressor: None, + def_decompressor: None, + value_decompressor: Arc::new(FixedWidthMiniBlockDecompressor), + dictionary_data: None, + def_meaning: Arc::from([]), + num_buffers: 1, + max_visible_level: 0, + instructions: vec![( + ChunkDrainInstructions { + chunk_instructions: ChunkInstructions { + chunk_idx: 0, + preamble: PreambleAction::Absent, + rows_to_skip: 0, + rows_to_take: num_rows, + take_trailer: false, + }, + rows_to_skip: 0, + rows_to_take: num_rows, + preamble_action: PreambleAction::Absent, + }, + LoadedChunk { + byte_range: 0..chunk_data.len() as u64, + data: LanceBuffer::from(chunk_data), + items_in_chunk: num_rows, + chunk_idx: 0, + }, + )], + has_large_chunk: false, + }; + + let decoded = Box::new(task).decode().unwrap(); + let values = decoded.data.as_fixed_width_ref().unwrap(); + assert_eq!(values.data.len(), expected_size as usize); + assert_eq!( + values.data.clone().into_buffer().capacity(), + expected_size as usize + ); + } + + #[test] + fn test_map_range() { + // Null in the middle + // [[A, B, C], [D, E], NULL, [F, G, H]] + let rep = Some(vec![1, 0, 0, 1, 0, 1, 1, 0, 0]); + let def = Some(vec![0, 0, 0, 0, 0, 1, 0, 0, 0]); + let max_visible_def = 0; + let total_items = 8; + let max_rep = 1; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 0..3, 0..3); + check(1..2, 3..5, 3..5); + check(2..3, 5..5, 5..6); + check(3..4, 5..8, 6..9); + check(0..2, 0..5, 0..5); + check(1..3, 3..5, 3..6); + check(2..4, 5..8, 5..9); + check(0..3, 0..5, 0..6); + check(1..4, 3..8, 3..9); + check(0..4, 0..8, 0..9); + + // Null at start + // [NULL, [A, B], [C]] + let rep = Some(vec![1, 1, 0, 1]); + let def = Some(vec![1, 0, 0, 0]); + let max_visible_def = 0; + let total_items = 3; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 0..0, 0..1); + check(1..2, 0..2, 1..3); + check(2..3, 2..3, 3..4); + check(0..2, 0..2, 0..3); + check(1..3, 0..3, 1..4); + check(0..3, 0..3, 0..4); + + // Null at end + // [[A], [B, C], NULL] + let rep = Some(vec![1, 1, 0, 1]); + let def = Some(vec![0, 0, 0, 1]); + let max_visible_def = 0; + let total_items = 3; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 0..1, 0..1); + check(1..2, 1..3, 1..3); + check(2..3, 3..3, 3..4); + check(0..2, 0..3, 0..3); + check(1..3, 1..3, 1..4); + check(0..3, 0..3, 0..4); + + // No nulls, with repetition + // [[A, B], [C, D], [E, F]] + let rep = Some(vec![1, 0, 1, 0, 1, 0]); + let def: Option<&[u16]> = None; + let max_visible_def = 0; + let total_items = 6; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 0..2, 0..2); + check(1..2, 2..4, 2..4); + check(2..3, 4..6, 4..6); + check(0..2, 0..4, 0..4); + check(1..3, 2..6, 2..6); + check(0..3, 0..6, 0..6); + + // No repetition, with nulls (this case is trivial) + // [A, B, NULL, C] + let rep: Option<&[u16]> = None; + let def = Some(vec![0, 0, 1, 0]); + let max_visible_def = 1; + let total_items = 4; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 0..1, 0..1); + check(1..2, 1..2, 1..2); + check(2..3, 2..3, 2..3); + check(0..2, 0..2, 0..2); + check(1..3, 1..3, 1..3); + check(0..3, 0..3, 0..3); + + // Tricky case, this chunk is a continuation and starts with a rep-index = 0 + // [[..., A] [B, C], NULL] + // + // What we do will depend on the preamble action + let rep = Some(vec![0, 1, 0, 1]); + let def = Some(vec![0, 0, 0, 1]); + let max_visible_def = 0; + let total_items = 3; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Take, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + // If we are taking the preamble then the range must start at 0 + check(0..1, 0..3, 0..3); + check(0..2, 0..3, 0..4); + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Skip, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 1..3, 1..3); + check(1..2, 3..3, 3..4); + check(0..2, 1..3, 1..4); + + // Another preamble case but now it doesn't end with a new list + // [[..., A], NULL, [D, E]] + // + // What we do will depend on the preamble action + let rep = Some(vec![0, 1, 1, 0]); + let def = Some(vec![0, 1, 0, 0]); + let max_visible_def = 0; + let total_items = 4; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Take, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + // If we are taking the preamble then the range must start at 0 + check(0..1, 0..1, 0..2); + check(0..2, 0..3, 0..4); + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Skip, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + // If we are taking the preamble then the range must start at 0 + check(0..1, 1..1, 1..2); + check(1..2, 1..3, 2..4); + check(0..2, 1..3, 1..4); + + // Now a preamble case without any definition levels + // [[..., A] [B, C], [D]] + let rep = Some(vec![0, 1, 0, 1]); + let def: Option> = None; + let max_visible_def = 0; + let total_items = 4; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Take, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + // If we are taking the preamble then the range must start at 0 + check(0..1, 0..3, 0..3); + check(0..2, 0..4, 0..4); + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Skip, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..1, 1..3, 1..3); + check(1..2, 3..4, 3..4); + check(0..2, 1..4, 1..4); + + // If we have nested lists then non-top level lists may be empty/null + // and we need to make sure we still handle them as invisible items (we + // failed to do this previously) + let rep = Some(vec![2, 1, 2, 0, 1, 2]); + let def = Some(vec![0, 1, 2, 0, 0, 0]); + let max_rep = 2; + let max_visible_def = 0; + let total_items = 4; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Absent, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..3, 0..4, 0..6); + check(0..1, 0..1, 0..2); + check(1..2, 1..3, 2..5); + check(2..3, 3..4, 5..6); + + // Invisible items in a preamble that we are taking (regressing a previous failure) + let rep = Some(vec![0, 0, 1, 0, 1, 1]); + let def = Some(vec![0, 1, 0, 0, 0, 0]); + let max_rep = 1; + let max_visible_def = 0; + let total_items = 5; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Take, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(0..0, 0..1, 0..2); + check(0..1, 0..3, 0..4); + check(0..2, 0..4, 0..5); + + // Skip preamble (with invis items) and skip a few rows (with invis items) + // and then take a few rows but not all the rows + let rep = Some(vec![0, 1, 0, 1, 0, 1, 0, 1]); + let def = Some(vec![1, 0, 1, 1, 0, 0, 0, 0]); + let max_rep = 1; + let max_visible_def = 0; + let total_items = 5; + + let check = |range, expected_item_range, expected_level_range| { + let (item_range, level_range) = DecodeMiniBlockTask::map_range( + range, + rep.as_ref(), + def.as_ref(), + max_rep, + max_visible_def, + total_items, + PreambleAction::Skip, + ); + assert_eq!(item_range, expected_item_range); + assert_eq!(level_range, expected_level_range); + }; + + check(2..3, 2..4, 5..7); + } + + #[test] + fn test_slice_batch_data_and_rebase_offsets_u32() { + let data = LanceBuffer::copy_slice(b"0123456789abcdefghij"); + let offsets = LanceBuffer::reinterpret_vec(vec![6_u32, 8_u32, 8_u32, 12_u32]); + + let (sliced_data, normalized_offsets) = + VariableFullZipDecoder::slice_batch_data_and_rebase_offsets(&data, &offsets, 32) + .unwrap(); + + assert_eq!(sliced_data.as_ref(), b"6789ab"); + let normalized = normalized_offsets.borrow_to_typed_slice::(); + assert_eq!(normalized.as_ref(), &[0, 2, 2, 6]); + } + + #[test] + fn test_slice_batch_data_and_rebase_offsets_u64() { + let data = LanceBuffer::copy_slice(b"abcdefghijklmnopqrstuvwxyz"); + let offsets = LanceBuffer::reinterpret_vec(vec![10_u64, 12_u64, 16_u64, 20_u64]); + + let (sliced_data, normalized_offsets) = + VariableFullZipDecoder::slice_batch_data_and_rebase_offsets(&data, &offsets, 64) + .unwrap(); + + assert_eq!(sliced_data.as_ref(), b"klmnopqrst"); + let normalized = normalized_offsets.borrow_to_typed_slice::(); + assert_eq!(normalized.as_ref(), &[0, 2, 6, 10]); + } + + #[test] + fn test_slice_batch_data_and_rebase_offsets_rejects_invalid_offsets() { + let data = LanceBuffer::copy_slice(b"abcd"); + let offsets = LanceBuffer::reinterpret_vec(vec![3_u32, 2_u32]); + + let err = VariableFullZipDecoder::slice_batch_data_and_rebase_offsets(&data, &offsets, 32) + .expect_err("offset end before start should error"); + assert!(err.to_string().contains("less than base")); + } + + #[test] + fn test_schedule_instructions() { + // Convert repetition index to bytes for testing + let rep_data: Vec = vec![5, 2, 3, 0, 4, 7, 2, 0]; + let rep_bytes: Vec = rep_data.iter().flat_map(|v| v.to_le_bytes()).collect(); + let chunk_index = MiniBlockChunkIndex::new_nested_for_test(&rep_bytes, 2); + + let check = |user_ranges, expected_instructions| { + let instructions = ChunkInstructions::schedule_instructions(&chunk_index, user_ranges); + assert_eq!(instructions, expected_instructions); + }; + + // The instructions we expect if we're grabbing the whole range + let expected_take_all = vec![ + ChunkInstructions { + chunk_idx: 0, + preamble: PreambleAction::Absent, + rows_to_skip: 0, + rows_to_take: 6, + take_trailer: true, + }, + ChunkInstructions { + chunk_idx: 1, + preamble: PreambleAction::Take, + rows_to_skip: 0, + rows_to_take: 2, + take_trailer: false, + }, + ChunkInstructions { + chunk_idx: 2, + preamble: PreambleAction::Absent, + rows_to_skip: 0, + rows_to_take: 5, + take_trailer: true, + }, + ChunkInstructions { + chunk_idx: 3, + preamble: PreambleAction::Take, + rows_to_skip: 0, + rows_to_take: 1, + take_trailer: false, + }, + ]; + + // Take all as 1 range + check(&[0..14], expected_take_all.clone()); + + // Take all a individual rows + check( + &[ + 0..1, + 1..2, + 2..3, + 3..4, + 4..5, + 5..6, + 6..7, + 7..8, + 8..9, + 9..10, + 10..11, + 11..12, + 12..13, + 13..14, + ], + expected_take_all, + ); + + // Test some partial takes + + // 2 rows in the same chunk but not contiguous + check( + &[0..1, 3..4], + vec![ + ChunkInstructions { + chunk_idx: 0, + preamble: PreambleAction::Absent, + rows_to_skip: 0, + rows_to_take: 1, + take_trailer: false, + }, + ChunkInstructions { + chunk_idx: 0, + preamble: PreambleAction::Absent, + rows_to_skip: 3, + rows_to_take: 1, + take_trailer: false, + }, + ], + ); + + // Taking just a trailer/preamble + check( + &[5..6], + vec![ + ChunkInstructions { + chunk_idx: 0, + preamble: PreambleAction::Absent, + rows_to_skip: 5, + rows_to_take: 1, + take_trailer: true, + }, + ChunkInstructions { + chunk_idx: 1, + preamble: PreambleAction::Take, + rows_to_skip: 0, + rows_to_take: 0, + take_trailer: false, + }, + ], + ); + + // Skipping an entire chunk + check( + &[7..10], + vec![ + ChunkInstructions { + chunk_idx: 1, + preamble: PreambleAction::Skip, + rows_to_skip: 1, + rows_to_take: 1, + take_trailer: false, + }, + ChunkInstructions { + chunk_idx: 2, + preamble: PreambleAction::Absent, + rows_to_skip: 0, + rows_to_take: 2, + take_trailer: false, + }, + ], + ); + } + + #[test] + fn test_drain_instructions() { + fn drain_from_instructions( + instructions: &mut VecDeque, + mut rows_desired: u64, + need_preamble: &mut bool, + skip_in_chunk: &mut u64, + ) -> Vec { + // Note: instructions.len() is an upper bound, we typically take much fewer + let mut drain_instructions = Vec::with_capacity(instructions.len()); + while rows_desired > 0 || *need_preamble { + let (next_instructions, consumed_chunk) = instructions + .front() + .unwrap() + .drain_from_instruction(&mut rows_desired, need_preamble, skip_in_chunk); + if consumed_chunk { + instructions.pop_front(); + } + drain_instructions.push(next_instructions); + } + drain_instructions + } + + // Convert repetition index to bytes for testing + let rep_data: Vec = vec![5, 2, 3, 0, 4, 7, 2, 0]; + let rep_bytes: Vec = rep_data.iter().flat_map(|v| v.to_le_bytes()).collect(); + let chunk_index = MiniBlockChunkIndex::new_nested_for_test(&rep_bytes, 2); + let user_ranges = vec![1..7, 10..14]; + + // First, schedule the ranges + let scheduled = ChunkInstructions::schedule_instructions(&chunk_index, &user_ranges); + + let mut to_drain = VecDeque::from(scheduled.clone()); + + // Now we drain in batches of 4 + + let mut need_preamble = false; + let mut skip_in_chunk = 0; + + let next_batch = + drain_from_instructions(&mut to_drain, 4, &mut need_preamble, &mut skip_in_chunk); + + assert!(!need_preamble); + assert_eq!(skip_in_chunk, 4); + assert_eq!( + next_batch, + vec![ChunkDrainInstructions { + chunk_instructions: scheduled[0].clone(), + rows_to_take: 4, + rows_to_skip: 0, + preamble_action: PreambleAction::Absent, + }] + ); + + let next_batch = + drain_from_instructions(&mut to_drain, 4, &mut need_preamble, &mut skip_in_chunk); + + assert!(!need_preamble); + assert_eq!(skip_in_chunk, 2); + + assert_eq!( + next_batch, + vec![ + ChunkDrainInstructions { + chunk_instructions: scheduled[0].clone(), + rows_to_take: 1, + rows_to_skip: 4, + preamble_action: PreambleAction::Absent, + }, + ChunkDrainInstructions { + chunk_instructions: scheduled[1].clone(), + rows_to_take: 1, + rows_to_skip: 0, + preamble_action: PreambleAction::Take, + }, + ChunkDrainInstructions { + chunk_instructions: scheduled[2].clone(), + rows_to_take: 2, + rows_to_skip: 0, + preamble_action: PreambleAction::Absent, + } + ] + ); + + let next_batch = + drain_from_instructions(&mut to_drain, 2, &mut need_preamble, &mut skip_in_chunk); + + assert!(!need_preamble); + assert_eq!(skip_in_chunk, 0); + + assert_eq!( + next_batch, + vec![ + ChunkDrainInstructions { + chunk_instructions: scheduled[2].clone(), + rows_to_take: 1, + rows_to_skip: 2, + preamble_action: PreambleAction::Absent, + }, + ChunkDrainInstructions { + chunk_instructions: scheduled[3].clone(), + rows_to_take: 1, + rows_to_skip: 0, + preamble_action: PreambleAction::Take, + }, + ] + ); + + // Regression case. Need a chunk with preamble, rows, and trailer (the middle chunk here) + let rep_data: Vec = vec![5, 2, 3, 3, 20, 0]; + let rep_bytes: Vec = rep_data.iter().flat_map(|v| v.to_le_bytes()).collect(); + let chunk_index = MiniBlockChunkIndex::new_nested_for_test(&rep_bytes, 2); + let user_ranges = vec![0..28]; + + // First, schedule the ranges + let scheduled = ChunkInstructions::schedule_instructions(&chunk_index, &user_ranges); + + let mut to_drain = VecDeque::from(scheduled.clone()); + + // Drain first chunk and some of second chunk + + let mut need_preamble = false; + let mut skip_in_chunk = 0; + + let next_batch = + drain_from_instructions(&mut to_drain, 7, &mut need_preamble, &mut skip_in_chunk); + + assert_eq!( + next_batch, + vec![ + ChunkDrainInstructions { + chunk_instructions: scheduled[0].clone(), + rows_to_take: 6, + rows_to_skip: 0, + preamble_action: PreambleAction::Absent, + }, + ChunkDrainInstructions { + chunk_instructions: scheduled[1].clone(), + rows_to_take: 1, + rows_to_skip: 0, + preamble_action: PreambleAction::Take, + }, + ] + ); + + assert!(!need_preamble); + assert_eq!(skip_in_chunk, 1); + + // Now, the tricky part. We drain the second chunk, including the trailer, and need to make sure + // we get a drain task to take the preamble of the third chunk (and nothing else) + let next_batch = + drain_from_instructions(&mut to_drain, 2, &mut need_preamble, &mut skip_in_chunk); + + assert_eq!( + next_batch, + vec![ + ChunkDrainInstructions { + chunk_instructions: scheduled[1].clone(), + rows_to_take: 2, + rows_to_skip: 1, + preamble_action: PreambleAction::Skip, + }, + ChunkDrainInstructions { + chunk_instructions: scheduled[2].clone(), + rows_to_take: 0, + rows_to_skip: 0, + preamble_action: PreambleAction::Take, + }, + ] + ); + + assert!(!need_preamble); + assert_eq!(skip_in_chunk, 0); + } + + use super::chunk_index::{PrefixSums, RowMapping}; + use super::{MINIBLOCK_ALIGNMENT, Words, build_chunk_index}; + use bytes::Bytes; + use lance_core::cache::{Context, DeepSizeOf}; + use rstest::rstest; + + /// Builds a `Words` metadata buffer (u16 words) from `(log_num_values, num_bytes)` + /// pairs, returning the words and the total data-buffer size. + fn words_from(entries: &[(u32, u32)]) -> (Words, u64) { + let mut raw = Vec::with_capacity(entries.len() * 2); + let mut total = 0u64; + for &(log, num_bytes) in entries { + assert!(num_bytes > 0 && num_bytes % MINIBLOCK_ALIGNMENT as u32 == 0); + let divided = num_bytes / MINIBLOCK_ALIGNMENT as u32 - 1; + let word = (divided << 4) | log; + assert!(word <= u16::MAX as u32, "test word {word} exceeds u16"); + raw.extend_from_slice(&(word as u16).to_le_bytes()); + total += num_bytes as u64; + } + (Words::from_bytes(Bytes::from(raw), false).unwrap(), total) + } + + fn rep_bytes_from(values: &[u64]) -> Vec { + values.iter().flat_map(|v| v.to_le_bytes()).collect() + } + + #[rstest] + // Two full chunks of 8 values (log 3) plus a partial last chunk; byte sizes vary + // independently of value counts. + #[case::uniform_partial_last(&[(3, 16), (3, 24), (0, 8)], 19, "uniform_flat", 8, 3)] + // Single chunk covers the whole page. + #[case::single_chunk(&[(0, 24)], 5, "uniform_flat", 5, 5)] + // Last chunk is also full (exact multiple). + #[case::exact_multiple(&[(3, 16), (3, 16)], 16, "uniform_flat", 8, 8)] + // Non-last chunks differ in size, so this is a non-uniform flat page. + #[case::non_uniform(&[(4, 16), (2, 16), (0, 8)], 21, "flat", 16, 1)] + fn test_flat_detection( + #[case] entries: &[(u32, u32)], + #[case] items_in_page: u64, + #[case] expected_kind: &str, + #[case] expected_first_items: u64, + #[case] expected_last_items: u64, + ) { + let base = 100u64; + let (words, data_buf_size) = words_from(entries); + let index = build_chunk_index(&words, items_in_page, base, data_buf_size, None, 0).unwrap(); + + assert_eq!(index.row_mapping_debug(), expected_kind); + assert_eq!(index.num_chunks(), entries.len()); + assert_eq!(index.items_in_chunk(0), expected_first_items); + assert_eq!(index.items_in_chunk(entries.len() - 1), expected_last_items); + + // Byte ranges are absolute, contiguous, and exactly cover the data buffer. + let mut expected_start = base; + for (i, &(_, num_bytes)) in entries.iter().enumerate() { + let range = index.byte_range(i); + assert_eq!(range.start, expected_start); + assert_eq!(range.end - range.start, num_bytes as u64); + expected_start = range.end; + } + assert_eq!(expected_start, base + data_buf_size); + + // For flat pages rows == items, so the per-chunk items sum to the page total. + let total_items: u64 = (0..index.num_chunks()) + .map(|i| index.items_in_chunk(i)) + .sum(); + assert_eq!(total_items, items_in_page); + } + + #[test] + fn test_nested_detection_and_axes() { + // Repetition index (stride 2): three chunks holding 5, 4, 3 rows, no trailers. + let rep = rep_bytes_from(&[5, 0, 4, 0, 3, 0]); + + // Uniform leaf chunking: value counts 4, 4, 2. + let (words, data_buf_size) = words_from(&[(2, 8), (2, 8), (0, 8)]); + let index = build_chunk_index(&words, 10, 0, data_buf_size, Some(&rep), 1).unwrap(); + assert_eq!(index.row_mapping_debug(), "nested"); + assert_eq!(index.num_chunks(), 3); + // Rows come from the repetition index, not the value counts. + assert_eq!(index.first_row(0), 0); + assert_eq!(index.rows_in_chunk(0), 5); + assert_eq!(index.first_row(1), 5); + assert_eq!(index.rows_in_chunk(1), 4); + assert_eq!(index.first_row(2), 9); + assert_eq!(index.rows_in_chunk(2), 3); + // Items come from the value words. + assert_eq!(index.items_in_chunk(0), 4); + assert_eq!(index.items_in_chunk(1), 4); + assert_eq!(index.items_in_chunk(2), 2); + + // Non-uniform leaf chunking: value counts 8, 2, 5. + let (words_nu, dbs_nu) = words_from(&[(3, 8), (1, 8), (0, 8)]); + let index_nu = build_chunk_index(&words_nu, 15, 0, dbs_nu, Some(&rep), 1).unwrap(); + assert_eq!(index_nu.row_mapping_debug(), "nested"); + assert_eq!(index_nu.items_in_chunk(0), 8); + assert_eq!(index_nu.items_in_chunk(1), 2); + assert_eq!(index_nu.items_in_chunk(2), 5); + // The row axis is unchanged by the leaf chunking. + assert_eq!(index_nu.rows_in_chunk(0), 5); + } + + #[test] + fn test_uniform_flat_matches_prefix_sum_flat() { + // Distribution: 4 chunks of 4 values, last chunk 3 (15 items total). + let (words, data_buf_size) = words_from(&[(2, 8), (2, 8), (2, 8), (0, 8)]); + let uniform = build_chunk_index(&words, 15, 0, data_buf_size, None, 0).unwrap(); + assert_eq!(uniform.row_mapping_debug(), "uniform_flat"); + + // The same distribution expressed as a non-uniform Flat prefix-sum index. + let byte_starts = PrefixSums::from_deltas([8u64, 8, 8, 8].into_iter(), 4, 32); + let value_starts = PrefixSums::from_deltas([4u64, 4, 4, 3].into_iter(), 4, 15); + let flat = MiniBlockChunkIndex::new(0, byte_starts, RowMapping::Flat { value_starts }); + assert_eq!(flat.row_mapping_debug(), "flat"); + + // Lookup parity: identical byte ranges and item counts. + for i in 0..4 { + assert_eq!(uniform.byte_range(i), flat.byte_range(i)); + assert_eq!(uniform.items_in_chunk(i), flat.items_in_chunk(i)); + } + + // Scheduler parity across scan / single-row / partial / scattered multi-range. + let range_sets: Vec>> = vec![ + vec![0..15], + vec![0..1], + vec![7..8], + vec![14..15], + vec![3..10], + vec![0..2, 5..6, 12..15], + ]; + for ranges in &range_sets { + let from_uniform = ChunkInstructions::schedule_instructions(&uniform, ranges); + let from_flat = ChunkInstructions::schedule_instructions(&flat, ranges); + assert_eq!(from_uniform, from_flat, "mismatch for ranges {ranges:?}"); + } + + // A full scan yields one Absent, no-trailer instruction per chunk. + let full = ChunkInstructions::schedule_instructions(&uniform, &[0..15]); + assert_eq!(full.len(), 4); + for (i, inst) in full.iter().enumerate() { + assert_eq!(inst.chunk_idx, i); + assert_eq!(inst.preamble, PreambleAction::Absent); + assert_eq!(inst.rows_to_skip, 0); + assert!(!inst.take_trailer); + } + assert_eq!(full.iter().map(|i| i.rows_to_take).sum::(), 15); + } + + #[test] + fn test_deep_size_per_variant_below_legacy() { + // The previous representation cached 48 bytes per chunk (24 for ChunkMeta plus + // 24 for a rep-index block); every variant's heap must be well below that. + const LEGACY_PER_CHUNK: usize = 48; + let num_chunks = 3; + let heap = |index: &MiniBlockChunkIndex| index.deep_size_of_children(&mut Context::new()); + + let (uniform_words, uniform_dbs) = words_from(&[(2, 8), (2, 8), (0, 8)]); + let uniform = build_chunk_index(&uniform_words, 10, 0, uniform_dbs, None, 0).unwrap(); + assert_eq!(uniform.row_mapping_debug(), "uniform_flat"); + assert!(heap(&uniform) < LEGACY_PER_CHUNK * num_chunks); + + let (flat_words, flat_dbs) = words_from(&[(3, 8), (1, 8), (0, 8)]); + let flat = build_chunk_index(&flat_words, 11, 0, flat_dbs, None, 0).unwrap(); + assert_eq!(flat.row_mapping_debug(), "flat"); + assert!(heap(&flat) < LEGACY_PER_CHUNK * num_chunks); + // Flat carries a value-starts array that UniformFlat derives arithmetically. + assert!(heap(&flat) > heap(&uniform)); + + let rep = rep_bytes_from(&[4, 0, 3, 0, 3, 0]); + let (nested_words, nested_dbs) = words_from(&[(2, 8), (2, 8), (0, 8)]); + let nested = build_chunk_index(&nested_words, 10, 0, nested_dbs, Some(&rep), 1).unwrap(); + assert_eq!(nested.row_mapping_debug(), "nested"); + assert!(heap(&nested) < LEGACY_PER_CHUNK * num_chunks); + } + + #[tokio::test] + async fn test_fullzip_initialize_is_lazy() { + use futures::{FutureExt, future::BoxFuture}; + use std::ops::Range; + use std::sync::Mutex; + + #[derive(Debug, Clone)] + struct RecordingScheduler { + data: bytes::Bytes, + requests: Arc>>>>, + } + + impl RecordingScheduler { + fn new(data: bytes::Bytes) -> Self { + Self { + data, + requests: Arc::new(Mutex::new(Vec::new())), + } + } + + fn requests(&self) -> Vec>> { + self.requests.lock().unwrap().clone() + } + } + + impl crate::EncodingsIo for RecordingScheduler { + fn submit_request( + &self, + ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, crate::Result>> { + self.requests.lock().unwrap().push(ranges.clone()); + let data = ranges + .into_iter() + .map(|range| self.data.slice(range.start as usize..range.end as usize)) + .collect::>(); + std::future::ready(Ok(data)).boxed() + } + } + + #[derive(Debug)] + struct TestFixedDecompressor; + + impl FixedPerValueDecompressor for TestFixedDecompressor { + fn decompress( + &self, + _data: FixedWidthDataBlock, + _num_rows: u64, + ) -> crate::Result { + unimplemented!("Test decompressor") + } + + fn bits_per_value(&self) -> u64 { + 32 + } + } + + let io = Arc::new(RecordingScheduler::new(bytes::Bytes::from(vec![ + 0; + 16 * 1024 + ]))); + let mut scheduler = FullZipScheduler { + data_buf_position: 0, + data_buf_size: 4096, + rep_index: Some(FullZipRepIndexDetails { + buf_position: 1000, + bytes_per_value: 4, + }), + priority: 0, + rows_in_page: 100, + bits_per_offset: 32, + details: Arc::new(FullZipDecodeDetails { + value_decompressor: PerValueDecompressor::Fixed(Arc::new(TestFixedDecompressor)), + def_meaning: Arc::new([crate::repdef::DefinitionInterpretation::NullableItem]), + ctrl_word_parser: crate::repdef::ControlWordParser::new(0, 1), + max_rep: 0, + max_visible_def: 0, + }), + cached_state: None, + enable_cache: false, + }; + + let io_dyn: Arc = io.clone(); + let cached_data = scheduler.initialize(&io_dyn).await.unwrap(); + + assert!( + cached_data + .as_arc_any() + .downcast_ref::() + .is_some(), + "FullZip initialize should not eagerly load repetition index data" + ); + assert!(scheduler.cached_state.is_none()); + assert!( + io.requests().is_empty(), + "FullZip initialize should not issue any I/O" + ); + } + + #[tokio::test] + async fn test_fullzip_read_source_slices_prefetched_page() { + let page_start = 200_u64; + let page_data = LanceBuffer::copy_slice(&[0, 1, 2, 3, 4, 5, 6, 7]); + let source = FullZipReadSource::PrefetchedPage { + base_offset: page_start, + data: page_data, + }; + let ranges = vec![ + page_start..(page_start + 3), + (page_start + 4)..(page_start + 8), + ]; + let mut data = source.fetch(&ranges, 0).await.unwrap(); + assert_eq!(data.pop_front().unwrap().as_ref(), &[0, 1, 2]); + assert_eq!(data.pop_front().unwrap().as_ref(), &[4, 5, 6, 7]); + } + + #[tokio::test] + async fn test_fullzip_initialize_caches_rep_index_when_enabled() { + use futures::{FutureExt, future::BoxFuture}; + use std::ops::Range; + use std::sync::Mutex; + + #[derive(Debug, Clone)] + struct RecordingScheduler { + data: bytes::Bytes, + requests: Arc>>>>, + } + + impl RecordingScheduler { + fn new(data: bytes::Bytes) -> Self { + Self { + data, + requests: Arc::new(Mutex::new(Vec::new())), + } + } + + fn requests(&self) -> Vec>> { + self.requests.lock().unwrap().clone() + } + } + + impl crate::EncodingsIo for RecordingScheduler { + fn submit_request( + &self, + ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, crate::Result>> { + self.requests.lock().unwrap().push(ranges.clone()); + let data = ranges + .into_iter() + .map(|range| self.data.slice(range.start as usize..range.end as usize)) + .collect::>(); + std::future::ready(Ok(data)).boxed() + } + } + + #[derive(Debug)] + struct TestFixedDecompressor; + + impl FixedPerValueDecompressor for TestFixedDecompressor { + fn decompress( + &self, + _data: FixedWidthDataBlock, + _num_rows: u64, + ) -> crate::Result { + unimplemented!("Test decompressor") + } + + fn bits_per_value(&self) -> u64 { + 32 + } + } + + let rows_in_page = 100_u64; + let bytes_per_value = 4_u64; + let rep_start = 1000_u64; + let rep_size = ((rows_in_page + 1) * bytes_per_value) as usize; + let mut data = vec![0_u8; 16 * 1024]; + data[rep_start as usize..rep_start as usize + rep_size].fill(7); + let io = Arc::new(RecordingScheduler::new(bytes::Bytes::from(data))); + + let mut scheduler = FullZipScheduler { + data_buf_position: 0, + data_buf_size: 4096, + rep_index: Some(FullZipRepIndexDetails { + buf_position: rep_start, + bytes_per_value, + }), + priority: 0, + rows_in_page, + bits_per_offset: 32, + details: Arc::new(FullZipDecodeDetails { + value_decompressor: PerValueDecompressor::Fixed(Arc::new(TestFixedDecompressor)), + def_meaning: Arc::new([crate::repdef::DefinitionInterpretation::NullableItem]), + ctrl_word_parser: crate::repdef::ControlWordParser::new(0, 1), + max_rep: 0, + max_visible_def: 0, + }), + cached_state: None, + enable_cache: true, + }; + + let io_dyn: Arc = io.clone(); + let cached_data = scheduler.initialize(&io_dyn).await.unwrap(); + assert!( + cached_data + .as_arc_any() + .downcast_ref::() + .is_some() + ); + assert!(scheduler.cached_state.is_some()); + assert_eq!( + io.requests(), + vec![vec![ + rep_start..(rep_start + (rows_in_page + 1) * bytes_per_value) + ]] + ); + } + + #[tokio::test] + async fn test_fullzip_full_page_bypasses_rep_index_io() { + use futures::{FutureExt, future::BoxFuture}; + use std::ops::Range; + use std::sync::Mutex; + + #[derive(Debug, Clone)] + struct RecordingScheduler { + data: bytes::Bytes, + requests: Arc>>>>, + } + + impl RecordingScheduler { + fn new(data: bytes::Bytes) -> Self { + Self { + data, + requests: Arc::new(Mutex::new(Vec::new())), + } + } + + fn requests(&self) -> Vec>> { + self.requests.lock().unwrap().clone() + } + } + + impl crate::EncodingsIo for RecordingScheduler { + fn submit_request( + &self, + ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, crate::Result>> { + self.requests.lock().unwrap().push(ranges.clone()); + let data = ranges + .into_iter() + .map(|range| self.data.slice(range.start as usize..range.end as usize)) + .collect::>(); + std::future::ready(Ok(data)).boxed() + } + } + + #[derive(Debug)] + struct TestFixedDecompressor; + + impl FixedPerValueDecompressor for TestFixedDecompressor { + fn decompress( + &self, + _data: FixedWidthDataBlock, + _num_rows: u64, + ) -> crate::Result { + unimplemented!("Test decompressor") + } + + fn bits_per_value(&self) -> u64 { + 32 + } + } + + let rows_in_page = 100_u64; + let data_start = 256_u64; + let data_size = 500_u64; + let rep_start = 4096_u64; + let bytes_per_value = 4_u64; + + let mut bytes = vec![0_u8; 16 * 1024]; + for i in 0..=rows_in_page { + let offset = (i * 5) as u32; + let pos = rep_start as usize + (i * bytes_per_value) as usize; + bytes[pos..pos + 4].copy_from_slice(&offset.to_le_bytes()); + } + let io = Arc::new(RecordingScheduler::new(bytes::Bytes::from(bytes))); + + let scheduler = FullZipScheduler { + data_buf_position: data_start, + data_buf_size: data_size, + rep_index: Some(FullZipRepIndexDetails { + buf_position: rep_start, + bytes_per_value, + }), + priority: 0, + rows_in_page, + bits_per_offset: 32, + details: Arc::new(FullZipDecodeDetails { + value_decompressor: PerValueDecompressor::Fixed(Arc::new(TestFixedDecompressor)), + def_meaning: Arc::new([crate::repdef::DefinitionInterpretation::NullableItem]), + ctrl_word_parser: crate::repdef::ControlWordParser::new(0, 1), + max_rep: 0, + max_visible_def: 0, + }), + cached_state: None, + enable_cache: false, + }; + + let io_dyn: Arc = io.clone(); + let tasks = scheduler + .schedule_ranges_rep( + &[0..rows_in_page], + &io_dyn, + FullZipRepIndexDetails { + buf_position: rep_start, + bytes_per_value, + }, + ) + .unwrap(); + + let requests = io.requests(); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0], vec![data_start..(data_start + data_size)]); + + let _ = tasks.into_iter().next().unwrap().decoder_fut.await.unwrap(); + let requests_after_await = io.requests(); + assert_eq!( + requests_after_await.len(), + 1, + "full page path should not issue rep-index I/O" + ); + } + + /// This test is used to reproduce fuzz test https://github.com/lancedb/lance/issues/4492 + #[tokio::test] + async fn test_fuzz_issue_4492_empty_rep_values() { + use lance_datagen::{RowCount, Seed, array, gen_batch}; + + let seed = 1823859942947654717u64; + let num_rows = 2741usize; + + // Generate the exact same data that caused the failure + let batch_gen = gen_batch().with_seed(Seed::from(seed)); + let base_generator = array::rand_type(&DataType::FixedSizeBinary(32)); + let list_generator = array::rand_list_any(base_generator, false); + + let batch = batch_gen + .anon_col(list_generator) + .into_batch_rows(RowCount::from(num_rows as u64)) + .unwrap(); + + let list_array = batch.column(0).clone(); + + // Force miniblock encoding + let mut metadata = HashMap::new(); + metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_MINIBLOCK.to_string(), + ); + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_batch_size(100) + .with_range(0..num_rows.min(500) as u64) + .with_indices(vec![0, num_rows as u64 / 2, (num_rows - 1) as u64]); + + check_round_trip_encoding_of_data(vec![list_array], &test_cases, metadata).await + } + + async fn test_minichunk_size_helper( + string_data: Vec>, + minichunk_size: u64, + encodings: &[TestEncoding], + ) { + use crate::constants::MINICHUNK_SIZE_META_KEY; + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + use arrow_array::{ArrayRef, StringArray}; + use std::sync::Arc; + + let string_array: ArrayRef = Arc::new(StringArray::from(string_data)); + + let mut metadata = HashMap::new(); + metadata.insert( + MINICHUNK_SIZE_META_KEY.to_string(), + minichunk_size.to_string(), + ); + metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_MINIBLOCK.to_string(), + ); + + let test_cases = TestCases::default() + .with_encodings(encodings.iter().copied()) + .with_batch_size(1000); + + check_round_trip_encoding_of_data(vec![string_array], &test_cases, metadata).await; + } + + #[tokio::test] + async fn test_minichunk_size_roundtrip() { + // Test that minichunk size can be configured and works correctly in round-trip encoding + let mut string_data = Vec::new(); + for i in 0..100 { + string_data.push(Some(format!("test_string_{}", i).repeat(50))); + } + // configure minichunk size to 64 bytes (smaller than the default 4kb) for Lance 2.1 + test_minichunk_size_helper( + string_data, + 64, + &[ + TestEncoding::StructuralU16, + TestEncoding::StructuralU32, + TestEncoding::StructuralSparse, + ], + ) + .await; + } + + #[tokio::test] + async fn test_minichunk_size_128kb_v2_2() { + // Test that minichunk size can be configured to 128KB and works correctly with Lance 2.2 + let mut string_data = Vec::new(); + // create a 500kb string array + for i in 0..10000 { + string_data.push(Some(format!("test_string_{}", i).repeat(50))); + } + test_minichunk_size_helper( + string_data, + 128 * 1024, + &[TestEncoding::StructuralU32, TestEncoding::StructuralSparse], + ) + .await; + } + + #[tokio::test] + async fn test_binary_large_minichunk_size_over_max_miniblock_values() { + let mut string_data = Vec::new(); + // 128kb/chunk / 6 bytes (t_9999) = 21845 items per chunk + for i in 0..10000 { + string_data.push(Some(format!("t_{}", i))); + } + test_minichunk_size_helper( + string_data, + 128 * 1024, + &[TestEncoding::StructuralU32, TestEncoding::StructuralSparse], + ) + .await; + } + + #[tokio::test] + async fn test_large_dictionary_general_compression() { + use arrow_array::{ArrayRef, StringArray}; + use std::collections::HashMap; + use std::sync::Arc; + + // Create large string dictionary data (>32KiB) with low cardinality + // Use 100 unique strings, each 500 bytes long = 50KB dictionary + let unique_values: Vec = (0..100) + .map(|i| format!("value_{:04}_{}", i, "x".repeat(500))) + .collect(); + + // Repeat these strings many times to create a large array + let repeated_strings: Vec<_> = unique_values + .iter() + .cycle() + .take(100_000) + .map(|s| Some(s.as_str())) + .collect(); + + let string_array = Arc::new(StringArray::from(repeated_strings)) as ArrayRef; + + // Configure test to use V2_2 and verify encoding + let test_cases = TestCases::default() + .with_u32_structural_encodings() + .with_verify_encoding(Arc::new(|cols: &[crate::encoder::EncodedColumn], _| { + assert_eq!(cols.len(), 1); + let col = &cols[0]; + + // Navigate to the dictionary encoding in the page layout + if let Some(PageEncoding::Structural(page_layout)) = + &col.final_pages.first().map(|p| &p.description) + && let Some(pb21::page_layout::Layout::MiniBlockLayout(mini_block)) = + &page_layout.layout + && let Some(dictionary_encoding) = &mini_block.dictionary + { + match dictionary_encoding.compression.as_ref() { + Some(Compression::General(general)) => { + // Verify it's using LZ4 or Zstd + let compression = general.compression.as_ref().unwrap(); + assert!( + compression.scheme() + == pb21::CompressionScheme::CompressionAlgorithmLz4 + || compression.scheme() + == pb21::CompressionScheme::CompressionAlgorithmZstd, + "Expected LZ4 or Zstd compression for large dictionary" + ); + } + _ => panic!("Expected General compression for large dictionary"), + } + } + })); + + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()).await; + } + + fn dictionary_encoding_from_page( + page: &crate::encoder::EncodedPage, + ) -> &crate::format::pb21::CompressiveEncoding { + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural page encoding"); + }; + let pb21::page_layout::Layout::MiniBlockLayout(layout) = layout.layout.as_ref().unwrap() + else { + panic!("Expected mini-block layout"); + }; + layout + .dictionary + .as_ref() + .unwrap_or_else(|| panic!("Expected dictionary encoding")) + } + + async fn encode_variable_dict_page( + metadata: HashMap, + ) -> crate::encoder::EncodedPage { + use arrow_array::types::Int32Type; + use arrow_array::{ArrayRef, DictionaryArray, Int32Array, StringArray}; + + let values = Arc::new(StringArray::from( + (0..128) + .map(|i| format!("value_{i:04}_{}", "x".repeat(256))) + .collect::>(), + )) as ArrayRef; + let keys = Int32Array::from_iter_values((0..20_000).map(|i| i % 128)); + let dict_array = + Arc::new(DictionaryArray::::try_new(keys, values).unwrap()) as ArrayRef; + + let field = arrow_schema::Field::new( + "dict_col", + DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)), + false, + ) + .with_metadata(metadata); + + encode_first_page(field, dict_array, TestEncoding::StructuralU32).await + } + + async fn encode_auto_fixed_dict_page( + metadata: HashMap, + ) -> crate::encoder::EncodedPage { + use arrow_array::{ArrayRef, Decimal128Array}; + + // 128-bit fixed-width values with low cardinality to trigger dictionary encoding. + let values = (0..20_000) + .map(|i| match i % 3 { + 0 => 10_i128, + 1 => 20_i128, + _ => 30_i128, + }) + .collect::>(); + let decimal = Decimal128Array::from_iter_values(values) + .with_precision_and_scale(38, 0) + .unwrap(); + let decimal = Arc::new(decimal) as ArrayRef; + + let mut field_metadata = metadata; + // Strongly encourage dictionary encoding for this synthetic test data. + field_metadata.insert( + "lance-encoding:dict-size-ratio".to_string(), + "0.99".to_string(), + ); + let field = arrow_schema::Field::new("fixed_col", DataType::Decimal128(38, 0), false) + .with_metadata(field_metadata); + + encode_first_page(field, decimal, TestEncoding::StructuralU32).await + } + + #[tokio::test] + async fn test_dict_values_general_compression_default_lz4_for_variable_dict_values() { + let page = encode_variable_dict_page(HashMap::new()).await; + let dictionary_encoding = dictionary_encoding_from_page(&page); + let Some(Compression::General(general)) = dictionary_encoding.compression.as_ref() else { + panic!("Expected General compression for dictionary values"); + }; + let compression = general.compression.as_ref().unwrap(); + assert_eq!( + compression.scheme(), + pb21::CompressionScheme::CompressionAlgorithmLz4 + ); + } + + #[tokio::test] + async fn test_dict_values_general_compression_default_lz4_for_fixed_dict_values() { + let page = encode_auto_fixed_dict_page(HashMap::new()).await; + let dictionary_encoding = dictionary_encoding_from_page(&page); + let Some(Compression::General(general)) = dictionary_encoding.compression.as_ref() else { + panic!("Expected General compression for dictionary values"); + }; + let compression = general.compression.as_ref().unwrap(); + assert_eq!( + compression.scheme(), + pb21::CompressionScheme::CompressionAlgorithmLz4 + ); + } + + #[tokio::test] + async fn test_dict_values_general_compression_zstd() { + let mut metadata = HashMap::new(); + metadata.insert( + DICT_VALUES_COMPRESSION_META_KEY.to_string(), + "zstd".to_string(), + ); + let page = encode_variable_dict_page(metadata).await; + let dictionary_encoding = dictionary_encoding_from_page(&page); + let Some(Compression::General(general)) = dictionary_encoding.compression.as_ref() else { + panic!("Expected General compression for dictionary values"); + }; + let compression = general.compression.as_ref().unwrap(); + assert_eq!( + compression.scheme(), + pb21::CompressionScheme::CompressionAlgorithmZstd + ); + } + + #[tokio::test] + async fn test_dict_values_general_compression_none() { + let mut metadata = HashMap::new(); + metadata.insert( + DICT_VALUES_COMPRESSION_META_KEY.to_string(), + "none".to_string(), + ); + let page = encode_variable_dict_page(metadata).await; + let dictionary_encoding = dictionary_encoding_from_page(&page); + assert!( + !matches!( + dictionary_encoding.compression.as_ref(), + Some(Compression::General(_)) + ), + "Expected dictionary values to avoid General compression" + ); + } + + #[test] + fn test_resolve_dict_values_compression_metadata_defaults_to_lz4() { + let metadata = PrimitiveStructuralEncoder::resolve_dict_values_compression_metadata( + &HashMap::new(), + None, + None, + ); + assert_eq!(metadata.get(COMPRESSION_META_KEY), Some(&"lz4".to_string()),); + assert!(!metadata.contains_key(COMPRESSION_LEVEL_META_KEY)); + } + + #[test] + fn test_resolve_dict_values_compression_metadata_metadata_overrides_env() { + let field_metadata = HashMap::from([ + ( + DICT_VALUES_COMPRESSION_META_KEY.to_string(), + "none".to_string(), + ), + ( + DICT_VALUES_COMPRESSION_LEVEL_META_KEY.to_string(), + "7".to_string(), + ), + ]); + let metadata = PrimitiveStructuralEncoder::resolve_dict_values_compression_metadata( + &field_metadata, + Some("zstd".to_string()), + Some("3".to_string()), + ); + assert_eq!( + metadata.get(COMPRESSION_META_KEY), + Some(&"none".to_string()), + ); + assert_eq!( + metadata.get(COMPRESSION_LEVEL_META_KEY), + Some(&"7".to_string()), + ); + } + + #[test] + fn test_resolve_dict_values_compression_metadata_env_fallback() { + let metadata = PrimitiveStructuralEncoder::resolve_dict_values_compression_metadata( + &HashMap::new(), + Some("zstd".to_string()), + Some("9".to_string()), + ); + assert_eq!( + metadata.get(COMPRESSION_META_KEY), + Some(&"zstd".to_string()), + ); + assert_eq!( + metadata.get(COMPRESSION_LEVEL_META_KEY), + Some(&"9".to_string()), + ); + } + + #[tokio::test] + async fn test_dictionary_encode_int64() { + use crate::constants::{DICT_SIZE_RATIO_META_KEY, STRUCTURAL_ENCODING_META_KEY}; + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + use arrow_array::{ArrayRef, Int64Array}; + use std::collections::HashMap; + use std::sync::Arc; + + // Low cardinality with poor RLE opportunity. + let values = (0..1000) + .map(|i| match i % 3 { + 0 => 10i64, + 1 => 20i64, + _ => 30i64, + }) + .collect::>(); + let array = Arc::new(Int64Array::from(values)) as ArrayRef; + + let mut metadata = HashMap::new(); + metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_MINIBLOCK.to_string(), + ); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.99".to_string()); + + let test_cases = TestCases::default() + .with_u32_structural_encodings() + .with_batch_size(1000) + .with_range(0..1000) + .with_indices(vec![0, 1, 10, 999]) + .with_expected_encoding("dictionary"); + + check_round_trip_encoding_of_data(vec![array], &test_cases, metadata).await; + } + + #[tokio::test] + async fn test_dictionary_encode_float64() { + use crate::constants::{DICT_SIZE_RATIO_META_KEY, STRUCTURAL_ENCODING_META_KEY}; + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + use arrow_array::{ArrayRef, Float64Array}; + use std::collections::HashMap; + use std::sync::Arc; + + // Low cardinality with poor RLE opportunity. + let values = (0..1000) + .map(|i| match i % 3 { + 0 => 0.1f64, + 1 => 0.2f64, + _ => 0.3f64, + }) + .collect::>(); + let array = Arc::new(Float64Array::from(values)) as ArrayRef; + + let mut metadata = HashMap::new(); + metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_MINIBLOCK.to_string(), + ); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.99".to_string()); + + let test_cases = TestCases::default() + .with_u32_structural_encodings() + .with_batch_size(1000) + .with_range(0..1000) + .with_indices(vec![0, 1, 10, 999]) + .with_expected_encoding("dictionary"); + + check_round_trip_encoding_of_data(vec![array], &test_cases, metadata).await; + } + + #[test] + fn test_miniblock_dictionary_out_of_line_bitpacking_decode() { + let rows = 10_000; + let unique_values = 2_000; + + let dictionary_encoding = + ProtobufUtils21::out_of_line_bitpacking(64, ProtobufUtils21::flat(11, None)); + let layout = pb21::MiniBlockLayout { + rep_compression: None, + def_compression: None, + value_compression: Some(ProtobufUtils21::flat(64, None)), + dictionary: Some(dictionary_encoding), + num_dictionary_items: unique_values, + layers: vec![pb21::RepDefLayer::RepdefAllValidItem as i32], + num_buffers: 1, + repetition_index_depth: 0, + num_items: rows, + has_large_chunk: false, + }; + + let buffer_offsets_and_sizes = vec![(0, 0), (0, 0), (0, 0)]; + let scheduler = super::MiniBlockScheduler::try_new( + &buffer_offsets_and_sizes, + /*priority=*/ 0, + /*items_in_page=*/ rows, + &layout, + &DefaultDecompressionStrategy::default(), + ) + .unwrap(); + + let dictionary = scheduler.dictionary.unwrap(); + assert_eq!(dictionary.num_dictionary_items, unique_values); + assert_eq!( + dictionary.dictionary_data_alignment, + crate::encoder::MIN_PAGE_BUFFER_ALIGNMENT + ); + } + + // Dictionary encoding decision tests + fn create_test_fixed_data_block( + num_values: u64, + cardinality: u64, + bits_per_value: u64, + ) -> DataBlock { + assert!(cardinality > 0); + assert!(cardinality <= num_values); + let block_info = BlockInfo::default(); + + assert_eq!(bits_per_value % 8, 0); + let data = match bits_per_value { + 32 => { + let values = (0..num_values) + .map(|i| (i % cardinality) as u32) + .collect::>(); + crate::buffer::LanceBuffer::reinterpret_vec(values) + } + 64 => { + let values = (0..num_values).map(|i| i % cardinality).collect::>(); + crate::buffer::LanceBuffer::reinterpret_vec(values) + } + 128 => { + let values = (0..num_values) + .map(|i| (i % cardinality) as u128) + .collect::>(); + crate::buffer::LanceBuffer::reinterpret_vec(values) + } + _ => unreachable!(), + }; + DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value, + data, + num_values, + block_info, + }) + } + + /// Helper to create VariableWidth (string) test data block with exact cardinality + fn create_test_variable_width_block(num_values: u64, cardinality: u64) -> DataBlock { + use arrow_array::StringArray; + + assert!(cardinality <= num_values && cardinality > 0); + + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + values.push(format!("value_{:016}", i % cardinality)); + } + + let array = StringArray::from(values); + DataBlock::from_array(Arc::new(array) as ArrayRef) + } + + fn create_sorted_string_array(num_values: u64, cardinality: u64) -> ArrayRef { + use arrow_array::StringArray; + + assert!(cardinality <= num_values && cardinality > 0); + + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + let value_idx = i * cardinality / num_values; + values.push(format!("value_{:016}", value_idx)); + } + + Arc::new(StringArray::from(values)) as ArrayRef + } + + fn create_sorted_variable_width_block(num_values: u64, cardinality: u64) -> DataBlock { + DataBlock::from_array(create_sorted_string_array(num_values, cardinality)) + } + + #[test] + fn test_should_dictionary_encode() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use lance_core::datatypes::Field as LanceField; + + // Create data where dict encoding saves space + let block = create_test_variable_width_block(1000, 10); + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.8".to_string()); + let arrow_field = + arrow_schema::Field::new("test", DataType::Utf8, false).with_metadata(metadata); + let field = LanceField::try_from(&arrow_field).unwrap(); + + let result = PrimitiveStructuralEncoder::should_dictionary_encode( + &block, + &field, + FixedWidthDictionaryEncoding::Exclude64Bit, + ); + + assert!( + result.is_some(), + "Should use dictionary encode based on size" + ); + } + + #[test] + fn test_block_sampling_detects_low_cardinality_in_short_sorted_runs() { + let sample_count: usize = 4096; + let num_values: u64 = 200_000; + let cardinality: u64 = 8_000; + let run_length = num_values / cardinality; + let stride = num_values as usize / sample_count; + assert!( + stride > run_length as usize, + "test must construct the stride > run_length case" + ); + + let block = create_sorted_variable_width_block(num_values, cardinality); + let sample_unique_ratio = + PrimitiveStructuralEncoder::sample_unique_ratio(&block, sample_count).unwrap(); + + assert!( + sample_unique_ratio.is_some_and(|ratio| ratio < 0.98), + "sorted low-cardinality data must not be classified as near-unique" + ); + } + + #[test] + fn test_should_dictionary_encode_sorted_low_cardinality() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use lance_core::datatypes::Field as LanceField; + + let block = create_sorted_variable_width_block(200_000, 8_000); + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.8".to_string()); + let arrow_field = + arrow_schema::Field::new("test", DataType::Utf8, false).with_metadata(metadata); + let field = LanceField::try_from(&arrow_field).unwrap(); + + let result = PrimitiveStructuralEncoder::should_dictionary_encode( + &block, + &field, + FixedWidthDictionaryEncoding::Include64Bit, + ); + + assert!( + result.is_some(), + "sorted low-cardinality data should reach dictionary encoding" + ); + } + + #[test] + fn test_should_not_dictionary_encode_sorted_high_cardinality_short_runs() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use lance_core::datatypes::Field as LanceField; + + let num_values = 200_002; + let cardinality = 100_001; + let block = create_sorted_variable_width_block(num_values, cardinality); + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.8".to_string()); + let arrow_field = + arrow_schema::Field::new("test", DataType::Utf8, false).with_metadata(metadata); + let field = LanceField::try_from(&arrow_field).unwrap(); + + let result = PrimitiveStructuralEncoder::should_dictionary_encode( + &block, + &field, + FixedWidthDictionaryEncoding::Include64Bit, + ); + + assert!( + result.is_none(), + "sorted high-cardinality short runs should not trigger a full dictionary probe" + ); + } + + #[tokio::test] + async fn test_encode_sorted_low_cardinality_uses_dictionary_layout() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.8".to_string()); + let field = arrow_schema::Field::new("test", DataType::Utf8, false).with_metadata(metadata); + let array = create_sorted_string_array(200_000, 8_000); + + let page = encode_first_page(field, array, TestEncoding::StructuralU32).await; + let _ = dictionary_encoding_from_page(&page); + } + + #[test] + fn test_should_not_dictionary_encode_unsupported_bits() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use lance_core::datatypes::Field as LanceField; + + let block = create_test_fixed_data_block(1000, 1000, 32); + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.8".to_string()); + let arrow_field = + arrow_schema::Field::new("test", DataType::Int32, false).with_metadata(metadata); + let field = LanceField::try_from(&arrow_field).unwrap(); + + let result = PrimitiveStructuralEncoder::should_dictionary_encode( + &block, + &field, + FixedWidthDictionaryEncoding::Exclude64Bit, + ); + + assert!( + result.is_none(), + "Should not use dictionary encode for unsupported bit width" + ); + } + + #[test] + fn test_should_not_dictionary_encode_near_unique_sample() { + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use lance_core::datatypes::Field as LanceField; + + let num_values = 5000; + let block = create_test_variable_width_block(num_values, num_values); + + let mut metadata = HashMap::new(); + metadata.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "1.0".to_string()); + let arrow_field = + arrow_schema::Field::new("test", DataType::Utf8, false).with_metadata(metadata); + let field = LanceField::try_from(&arrow_field).unwrap(); + + let result = PrimitiveStructuralEncoder::should_dictionary_encode( + &block, + &field, + FixedWidthDictionaryEncoding::Exclude64Bit, + ); + + assert!( + result.is_none(), + "Should not probe dictionary encoding for near-unique data" + ); + } + + #[test] + fn test_v2_1_miniblock_serializes_log_num_values_15() { + let miniblocks = MiniBlockCompressed { + data: vec![LanceBuffer::from(vec![1_u8; 16])], + chunks: vec![ + MiniBlockChunk { + buffer_sizes: vec![8], + log_num_values: 15, + }, + MiniBlockChunk { + buffer_sizes: vec![8], + log_num_values: 0, + }, + ], + num_values: 32_769, + }; + + let serialized = PrimitiveStructuralEncoder::serialize_miniblocks( + miniblocks, + None, + None, + MiniblockChunkSize::U16, + ) + .unwrap(); + + let chunk_metadata = serialized.metadata.borrow_to_typed_slice::(); + assert_eq!(chunk_metadata.len(), 2); + assert_eq!( + chunk_metadata[0] & 0x0F, + 15, + "V2.1 metadata should use all 4 bits for log_num_values" + ); + } + + async fn encode_first_page( + field: arrow_schema::Field, + array: ArrayRef, + version: TestEncoding, + ) -> crate::encoder::EncodedPage { + use crate::repdef::RepDefBuilder; + use crate::{ + encoder::{ + ColumnIndexSequence, EncodingOptions, MIN_PAGE_BUFFER_ALIGNMENT, OutOfLineBuffers, + }, + testing::{create_test_field_encoder, test_encoding_strategy}, + }; + + let lance_field = lance_core::datatypes::Field::try_from(&field).unwrap(); + let encoding_strategy = test_encoding_strategy(version); + let mut column_index_seq = ColumnIndexSequence::default(); + let encoding_options = EncodingOptions { + cache_bytes_per_column: 1, + max_page_bytes: 32 * 1024 * 1024, + keep_original_array: true, + buffer_alignment: MIN_PAGE_BUFFER_ALIGNMENT, + }; + + let mut encoder = create_test_field_encoder( + encoding_strategy.as_ref(), + &lance_field, + &mut column_index_seq, + &encoding_options, + ) + .unwrap(); + + let mut external_buffers = OutOfLineBuffers::new(0, MIN_PAGE_BUFFER_ALIGNMENT); + let repdef = RepDefBuilder::default(); + let num_rows = array.len() as u64; + let mut pages = Vec::new(); + for task in encoder + .maybe_encode(array, &mut external_buffers, repdef, 0, num_rows) + .unwrap() + { + pages.push(task.await.unwrap()); + } + for task in encoder.flush(&mut external_buffers).unwrap() { + pages.push(task.await.unwrap()); + } + pages.into_iter().next().unwrap() + } + + #[tokio::test] + async fn test_constant_layout_out_of_line_fixed_size_binary_v2_2() { + use crate::format::pb21::page_layout::Layout; + + let val = vec![0xABu8; 33]; + let arr: ArrayRef = Arc::new( + arrow_array::FixedSizeBinaryArray::try_from_sparse_iter_with_size( + std::iter::repeat_n(Some(val.as_slice()), 256), + 33, + ) + .unwrap(), + ); + let field = arrow_schema::Field::new("c", DataType::FixedSizeBinary(33), true); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout) = layout.layout.as_ref().unwrap() else { + panic!("Expected constant layout in slot 2"); + }; + assert!(layout.inline_value.is_none()); + assert_eq!(page.data.len(), 1); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_constant_layout_out_of_line_utf8_v2_2() { + use crate::format::pb21::page_layout::Layout; + + let arr: ArrayRef = Arc::new(arrow_array::StringArray::from_iter_values( + std::iter::repeat_n("hello", 512), + )); + let field = arrow_schema::Field::new("c", DataType::Utf8, true); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout) = layout.layout.as_ref().unwrap() else { + panic!("Expected constant layout in slot 2"); + }; + assert!(layout.inline_value.is_none()); + assert_eq!(page.data.len(), 1); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_constant_layout_nullable_item_v2_2() { + use crate::format::pb21::page_layout::Layout; + + let arr: ArrayRef = Arc::new(arrow_array::Int32Array::from(vec![ + Some(7), + None, + Some(7), + None, + Some(7), + ])); + let field = arrow_schema::Field::new("c", DataType::Int32, true); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout) = layout.layout.as_ref().unwrap() else { + panic!("Expected constant layout in slot 2"); + }; + assert!(layout.inline_value.is_some()); + assert_eq!(page.data.len(), 2); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_constant_layout_list_repdef_v2_2() { + use crate::format::pb21::page_layout::Layout; + use arrow_array::builder::{Int32Builder, ListBuilder}; + + let mut builder = ListBuilder::new(Int32Builder::new()); + builder.values().append_value(7); + builder.values().append_null(); + builder.values().append_value(7); + builder.append(true); + + builder.append(true); + + builder.values().append_value(7); + builder.append(true); + + builder.append_null(); + + let arr: ArrayRef = Arc::new(builder.finish()); + let field = arrow_schema::Field::new( + "c", + DataType::List(Arc::new(arrow_schema::Field::new( + "item", + DataType::Int32, + true, + ))), + true, + ); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout) = layout.layout.as_ref().unwrap() else { + panic!("Expected constant layout in slot 2"); + }; + assert!(layout.inline_value.is_some()); + assert_eq!(page.data.len(), 2); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_constant_layout_fixed_size_list_not_used_v2_2() { + use crate::format::pb21::page_layout::Layout; + use arrow_array::builder::{FixedSizeListBuilder, Int32Builder}; + + let mut builder = FixedSizeListBuilder::new(Int32Builder::new(), 3); + for _ in 0..64 { + builder.values().append_value(1); + builder.values().append_null(); + builder.values().append_value(3); + builder.append(true); + } + let arr: ArrayRef = Arc::new(builder.finish()); + let field = arrow_schema::Field::new( + "c", + DataType::FixedSizeList( + Arc::new(arrow_schema::Field::new("item", DataType::Int32, true)), + 3, + ), + true, + ); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + if let PageEncoding::Structural(layout) = &page.description { + assert!( + !matches!(layout.layout.as_ref().unwrap(), Layout::ConstantLayout(_)), + "FixedSizeList should not use constant layout yet" + ); + } + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_constant_layout_not_written_before_v2_2() { + use crate::format::pb21::page_layout::Layout; + + let arr: ArrayRef = Arc::new(arrow_array::Int32Array::from(vec![7; 1024])); + let field = arrow_schema::Field::new("c", DataType::Int32, true); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU16).await; + + let PageEncoding::Structural(layout) = &page.description else { + return; + }; + assert!( + !matches!(layout.layout.as_ref().unwrap(), Layout::ConstantLayout(_)), + "Should not emit constant layout before v2.2" + ); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU16) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_all_null_constant_layout_still_works_v2_2() { + use crate::format::pb21::page_layout::Layout; + + let arr: ArrayRef = Arc::new(arrow_array::Int32Array::from(vec![None, None, None])); + let field = arrow_schema::Field::new("c", DataType::Int32, true); + let page = encode_first_page(field, arr.clone(), TestEncoding::StructuralU32).await; + + let PageEncoding::Structural(layout) = &page.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout) = layout.layout.as_ref().unwrap() else { + panic!("Expected layout in slot 2"); + }; + assert!(layout.inline_value.is_none()); + assert_eq!(page.data.len(), 0); + + let test_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralU32) + .with_page_sizes(vec![4096]); + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } + + #[test] + fn test_encode_decode_complex_all_null_vals_roundtrip() { + use crate::compression::{DecompressionStrategy, DefaultDecompressionStrategy}; + + let values: Arc<[u16]> = Arc::from((0..2048).map(|i| (i % 5) as u16).collect::>()); + + let compression_strategy = crate::testing::test_compression_strategy( + TestEncoding::StructuralU16, + crate::compression_config::CompressionParams::default(), + ); + let decompression_strategy = DefaultDecompressionStrategy::default(); + + let (compressed_buf, encoding) = PrimitiveStructuralEncoder::encode_complex_all_null_vals( + &values, + compression_strategy.as_ref(), + ) + .unwrap(); + + let decompressor = decompression_strategy + .create_block_decompressor(&encoding) + .unwrap(); + let decompressed = decompressor + .decompress(compressed_buf, values.len() as u64) + .unwrap(); + let decompressed_fixed_width = decompressed.as_fixed_width().unwrap(); + assert_eq!(decompressed_fixed_width.num_values, values.len() as u64); + assert_eq!(decompressed_fixed_width.bits_per_value, 16); + let rep_result = decompressed_fixed_width.data.borrow_to_typed_slice::(); + assert_eq!(rep_result.as_ref(), values.as_ref()); + } + + #[tokio::test] + async fn test_complex_all_null_compression_gated_by_version() { + use crate::format::pb21::page_layout::Layout; + use arrow_array::ListArray; + + let list_array = ListArray::from_iter_primitive::( + (0..1000).map(|i| if i % 2 == 0 { None } else { Some(vec![]) }), + ); + let arr: ArrayRef = Arc::new(list_array); + let field = arrow_schema::Field::new( + "c", + DataType::List(Arc::new(arrow_schema::Field::new( + "item", + DataType::Int32, + true, + ))), + true, + ); + + let page_v21 = + encode_first_page(field.clone(), arr.clone(), TestEncoding::StructuralU16).await; + let PageEncoding::Structural(layout_v21) = &page_v21.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout_v21) = layout_v21.layout.as_ref().unwrap() else { + panic!("Expected constant layout"); + }; + assert!(layout_v21.rep_compression.is_none()); + assert!(layout_v21.def_compression.is_none()); + assert_eq!(layout_v21.num_rep_values, 0); + assert_eq!(layout_v21.num_def_values, 0); + + let page_v22 = encode_first_page(field, arr, TestEncoding::StructuralU32).await; + let PageEncoding::Structural(layout_v22) = &page_v22.description else { + panic!("Expected structural encoding"); + }; + let Layout::ConstantLayout(layout_v22) = layout_v22.layout.as_ref().unwrap() else { + panic!("Expected constant layout"); + }; + assert!(layout_v22.def_compression.is_some()); + assert!(layout_v22.num_def_values > 0); + } + + #[tokio::test] + async fn test_complex_all_null_round_trip() { + use arrow_array::ListArray; + + let list_array = ListArray::from_iter_primitive::( + (0..1000).map(|i| if i % 2 == 0 { None } else { Some(vec![]) }), + ); + + let test_cases = TestCases::default().with_u32_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, HashMap::new()) + .await; + } + + #[tokio::test] + async fn test_complex_all_null_constant_def_round_trip() { + use arrow_array::ListArray; + + // Every row is a null list => constant def levels => a single RLE run, + // exercising the lazy run-form decode end to end. + let list_array = ListArray::from_iter_primitive::( + (0..5000).map(|_| None::>>), + ); + + let test_cases = TestCases::default().with_u32_structural_encodings(); + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, HashMap::new()) + .await; + } + + fn encoded_u16_frame(levels: &[u16], run_length_width: RunLengthWidth) -> LanceBuffer { + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_slice(Arc::from(levels)), + bits_per_value: 16, + num_values: levels.len() as u64, + block_info: BlockInfo::new(), + }); + BlockCompressor::compress(&RleEncoder::with_run_length_width(run_length_width), block) + .unwrap() + } + + fn encoded_u16_runs(levels: &[u16], run_length_width: RunLengthWidth) -> RleRuns { + let frame = encoded_u16_frame(levels, run_length_width); + RleDecompressor::with_run_length_width(16, run_length_width) + .decode_u16_runs(frame, levels.len() as u64) + .unwrap() + } + + fn physical_levels(levels: &[u16]) -> LazyLevels { + LazyLevels::Runs(Arc::new(RunStorage::Physical( + encoded_u16_runs(levels, RunLengthWidth::U8).into_owned(), + ))) + } + + fn coalesced_levels(levels: &[u16]) -> LazyLevels { + let mut values = Vec::new(); + let mut ends = RunEndsBuilder::with_capacity(levels.len(), levels.len()); + for (index, &value) in levels.iter().enumerate() { + if values.last() == Some(&value) { + ends.set_last(index + 1).unwrap(); + } else { + values.push(value); + ends.push(index + 1).unwrap(); + } + } + LazyLevels::Runs(Arc::new(RunStorage::Coalesced { + values: values.into_boxed_slice(), + ends: ends.finish(), + })) + } + + #[test] + fn lazy_levels_runs_match_dense() { + // Runs: 3x2, 1x1, 3x3, 0x2 => [3,3,1,3,3,3,0,0] + let expanded: Vec = vec![3, 3, 1, 3, 3, 3, 0, 0]; + let coalesced = coalesced_levels(&expanded); + let physical = physical_levels(&expanded); + let dense = LazyLevels::Dense(ScalarBuffer::::from(expanded.clone())); + let n = expanded.len(); + + assert_eq!(coalesced.len(), n); + assert_eq!(physical.len(), n); + assert_eq!(dense.len(), n); + + // Rows begin at each `max_rep` (3) position; row `num_rows` maps to `len`. + let max_rep = 3u16; + let row_starts: Vec = (0..n).filter(|&i| expanded[i] == max_rep).collect(); + for target in 0..=row_starts.len() as u64 { + let want = row_starts.get(target as usize).copied().unwrap_or(n); + for runs in [&coalesced, &physical] { + let mut cursor = LevelCursor::default(); + assert_eq!( + runs.seek_row_start(&mut cursor, target, max_rep).unwrap(), + want, + "seek_row_start({target})" + ); + } + let mut c_dense = LevelCursor::default(); + assert_eq!( + dense.seek_row_start(&mut c_dense, target, max_rep).unwrap(), + want + ); + } + + // `count_le_cursor` (fresh cursor per range) and `extend_into` agree with + // the dense reference on every sub-range. + for start in 0..=n { + for end in start..=n { + for max in [0u16, 1, 2, 3] { + let want = expanded[start..end].iter().filter(|&&d| d <= max).count() as u64; + for runs in [&coalesced, &physical] { + let mut cursor = RunPosition::default(); + assert_eq!( + runs.count_le_cursor(&mut cursor, start..end, max).0, + want, + "count_le_cursor({start}..{end}, {max})" + ); + } + let mut d_cur = RunPosition::default(); + assert_eq!(dense.count_le_cursor(&mut d_cur, start..end, max).0, want); + } + for runs in [&coalesced, &physical] { + let mut got = Vec::new(); + runs.extend_into(start..end, RunPosition::default(), &mut got); + assert_eq!( + got, + expanded[start..end].to_vec(), + "extend_into({start}..{end})" + ); + } + let mut got_dense = Vec::new(); + dense.extend_into(start..end, RunPosition::default(), &mut got_dense); + assert_eq!(got_dense, expanded[start..end].to_vec()); + } + } + } + + #[test] + fn physical_run_hints_support_deferred_materialization() { + let expanded: Vec = vec![3, 3, 1, 1, 2, 2, 0, 0]; + let physical = physical_levels(&expanded); + let LazyLevels::Runs(runs) = &physical else { + panic!("expected physical runs"); + }; + let mut first_hint = RunPosition::default(); + runs.seek(&mut first_hint, 2); + let mut second_hint = RunPosition::default(); + runs.seek(&mut second_hint, 6); + + let mut second = Vec::new(); + physical.extend_into(6..8, second_hint, &mut second); + let mut first = Vec::new(); + physical.extend_into(2..4, first_hint, &mut first); + assert_eq!(second, expanded[6..8]); + assert_eq!(first, expanded[2..4]); + } + + /// Fuzz parity for the run-oriented complex-all-null drain: the cursor walk + /// over `LazyLevels` must yield the exact level slices and visible + /// count that a brute-force reference over the fully expanded levels does, for + /// dense, physical-run, and coalesced-run forms and arbitrarily shaped range requests. + mod complex_all_null_drain_parity { + use std::ops::Range; + + use arrow_buffer::ScalarBuffer; + use proptest::prelude::*; + + use super::super::{LazyLevels, LevelCursor, RunPosition}; + use super::{coalesced_levels, physical_levels}; + use crate::Result; + + #[derive(Debug, Clone)] + struct DrainInput { + max_rep: u16, + max_visible: u16, + rep: Option>, + def: Option>, + ranges: Vec>, + } + + fn dense_levels(levels: &[u16]) -> LazyLevels { + LazyLevels::Dense(ScalarBuffer::from(levels.to_vec())) + } + + fn rle_levels(levels: &[u16]) -> LazyLevels { + coalesced_levels(levels) + } + + fn seek( + rep: Option<&LazyLevels>, + cursor: &mut LevelCursor, + row: u64, + max_rep: u16, + ) -> Result { + match rep { + Some(rep) => rep.seek_row_start(cursor, row, max_rep), + None => { + cursor.row = row; + cursor.level = row as usize; + Ok(row as usize) + } + } + } + + /// Mirror of `ComplexAllNullPageDecoder::drain`, driving the real + /// `seek_row_start` / `count_le_cursor` with monotonic cursors. + fn simulate_drain( + rep: Option<&LazyLevels>, + def: Option<&LazyLevels>, + max_rep: u16, + max_visible: u16, + ranges: &[Range], + ) -> Result<(Vec>, u64)> { + let mut rep_cursor = LevelCursor::default(); + let mut def_run_cursor = RunPosition::default(); + let mut slices: Vec> = Vec::new(); + let mut visible = 0u64; + for range in ranges { + let level_start = seek(rep, &mut rep_cursor, range.start, max_rep)?; + let level_end = seek(rep, &mut rep_cursor, range.end, max_rep)?; + visible += match def { + Some(def) => { + def.count_le_cursor( + &mut def_run_cursor, + level_start..level_end, + max_visible, + ) + .0 + } + None => (level_end - level_start) as u64, + }; + match slices.last_mut() { + Some(last) if last.end == level_start => last.end = level_end, + _ => slices.push(level_start..level_end), + } + } + Ok((slices, visible)) + } + + /// Independent brute-force reference over fully expanded levels. + fn reference_drain( + rep: Option<&[u16]>, + def: Option<&[u16]>, + max_rep: u16, + max_visible: u16, + ranges: &[Range], + ) -> (Vec>, u64) { + let total_levels = rep + .map(|r| r.len()) + .or_else(|| def.map(|d| d.len())) + .unwrap_or(0); + // Level index where each row starts (or `total_levels` for the end row). + let row_starts: Vec = match rep { + Some(rep) => (0..rep.len()).filter(|&i| rep[i] == max_rep).collect(), + None => (0..total_levels).collect(), + }; + let level_of_row = |row: u64| { + row_starts + .get(row as usize) + .copied() + .unwrap_or(total_levels) + }; + + let mut slices: Vec> = Vec::new(); + let mut visible = 0u64; + for range in ranges { + let ls = level_of_row(range.start); + let le = level_of_row(range.end); + visible += match def { + Some(def) => def[ls..le].iter().filter(|&&d| d <= max_visible).count() as u64, + None => (le - ls) as u64, + }; + match slices.last_mut() { + Some(last) if last.end == ls => last.end = le, + _ => slices.push(ls..le), + } + } + (slices, visible) + } + + fn ranges_strategy(num_rows: u64) -> BoxedStrategy>> { + if num_rows == 0 { + return Just(Vec::new()).boxed(); + } + // (gap, len) pairs; a zero gap yields ranges adjacent in row space, + // which exercises the level-slice coalescing path. + proptest::collection::vec((0u64..=3, 1u64..=4), 0..=8) + .prop_map(move |pairs| { + let mut ranges = Vec::new(); + let mut pos = 0u64; + for (gap, len) in pairs { + pos = pos.saturating_add(gap); + if pos >= num_rows { + break; + } + let end = (pos + len).min(num_rows); + ranges.push(pos..end); + pos = end; + } + ranges + }) + .boxed() + } + + fn drain_input() -> impl Strategy { + ( + 1u16..=3, + 0u16..=3, + any::(), + any::(), + 1usize..=48, + ) + .prop_flat_map(|(max_rep, max_visible, has_rep, has_def, len)| { + // Complex-all-null always has definition levels when there is + // no repetition, so force `def` present in that case. + let has_def = has_def || !has_rep; + let rep = if has_rep { + proptest::collection::vec(0u16..=max_rep, len) + .prop_map(move |mut v| { + // Row 0 must start at a max-rep boundary. + v[0] = max_rep; + Some(v) + }) + .boxed() + } else { + Just(None).boxed() + }; + let def = if has_def { + proptest::collection::vec(0u16..=(max_visible + 2), len) + .prop_map(Some) + .boxed() + } else { + Just(None).boxed() + }; + (Just(max_rep), Just(max_visible), rep, def) + }) + .prop_flat_map(|(max_rep, max_visible, rep, def)| { + let num_rows = match &rep { + Some(rep) => rep.iter().filter(|&&v| v == max_rep).count() as u64, + None => def.as_ref().map(|d| d.len() as u64).unwrap_or(0), + }; + ranges_strategy(num_rows).prop_map(move |ranges| DrainInput { + max_rep, + max_visible, + rep: rep.clone(), + def: def.clone(), + ranges, + }) + }) + } + + proptest! { + #![proptest_config(ProptestConfig::with_cases(512))] + + #[test] + fn drain_matches_reference(input in drain_input()) { + let DrainInput { max_rep, max_visible, rep, def, ranges } = input; + + let reference = + reference_drain(rep.as_deref(), def.as_deref(), max_rep, max_visible, &ranges); + + let rep_dense = rep.as_deref().map(dense_levels); + let def_dense = def.as_deref().map(dense_levels); + let got_dense = + simulate_drain(rep_dense.as_ref(), def_dense.as_ref(), max_rep, max_visible, &ranges) + .unwrap(); + prop_assert_eq!(&got_dense, &reference, "dense form diverged from reference"); + + let rep_rle = rep.as_deref().map(rle_levels); + let def_rle = def.as_deref().map(rle_levels); + let got_rle = + simulate_drain(rep_rle.as_ref(), def_rle.as_ref(), max_rep, max_visible, &ranges) + .unwrap(); + prop_assert_eq!(&got_rle, &reference, "rle form diverged from reference"); + + let rep_physical = rep.as_deref().map(physical_levels); + let def_physical = def.as_deref().map(physical_levels); + let got_physical = + simulate_drain(rep_physical.as_ref(), def_physical.as_ref(), max_rep, max_visible, &ranges) + .unwrap(); + prop_assert_eq!(&got_physical, &reference, "physical form diverged from reference"); + } + } + } + + #[test] + fn lazy_levels_runs_are_compact() { + let single_run = |n: usize| { + let mut ends = RunEndsBuilder::with_capacity(n, 1); + ends.push(n).unwrap(); + LazyLevels::Runs(Arc::new(RunStorage::Coalesced { + values: vec![1u16].into_boxed_slice(), + ends: ends.finish(), + })) + }; + // Run-form footprint is independent of the logical length within an end width... + assert_eq!(single_run(100).deep_size(), single_run(10_000).deep_size()); + assert!(single_run(10_000_000).deep_size() < 100); + assert_eq!(single_run(10_000_000).len(), 10_000_000); + // ...while Dense pays 2 bytes per value. + assert_eq!( + LazyLevels::Dense(ScalarBuffer::::from(vec![1u16; 1000])).deep_size(), + 2000 + ); + } + + #[test] + fn lazy_levels_selects_smallest_representation() { + let runs = encoded_u16_runs(&[7u16; 10], RunLengthWidth::U8); + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Dense); + + let equal_size: Vec = std::iter::repeat_n(0, 256) + .chain(std::iter::repeat_n(1, 100)) + .chain(std::iter::repeat_n(2, 100)) + .collect(); + let runs = encoded_u16_runs(&equal_size, RunLengthWidth::U8); + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Coalesced); + + let moderate_runs: Vec = (0..250) + .flat_map(|run| std::iter::repeat_n((run % 2) as u16, 4)) + .collect(); + let runs = encoded_u16_runs(&moderate_runs, RunLengthWidth::U8); + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Physical); + + let split_constant = vec![7u16; 5000]; + let runs = encoded_u16_runs(&split_constant, RunLengthWidth::U8); + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Coalesced); + + let high_density: Vec = (0..70_000).map(|index| (index % 2) as u16).collect(); + let runs = encoded_u16_runs(&high_density, RunLengthWidth::U32); + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Dense); + } + + #[test] + fn physical_runs_detach_from_large_encoded_frame() { + let levels: Vec = (0..250) + .flat_map(|run| std::iter::repeat_n((run % 2) as u16, 4)) + .collect(); + let frame = encoded_u16_frame(&levels, RunLengthWidth::U8); + let frame_offset = 4096; + let mut allocation = vec![0; frame_offset + frame.len() + 1_000_000]; + allocation[frame_offset..frame_offset + frame.len()].copy_from_slice(frame.as_ref()); + let frame = LanceBuffer::from(allocation).slice_with_length(frame_offset, frame.len()); + let runs = RleDecompressor::with_run_length_width(16, RunLengthWidth::U8) + .decode_u16_runs(frame, levels.len() as u64) + .unwrap(); + + assert_eq!(LazyLevels::select_plan(&runs), LevelPlan::Physical); + let cached = LazyLevels::from_rle_runs(runs).unwrap(); + assert!( + matches!(cached, LazyLevels::Runs(ref runs) if matches!(runs.as_ref(), RunStorage::Physical(_))) + ); + assert_eq!(cached.len(), levels.len()); + assert!(cached.deep_size() < 4096); + } + + #[test] + fn complex_all_null_levels_reject_invalid_values_and_lengths() { + let invalid_levels = vec![0u16, 3]; + for levels in [ + LazyLevels::Dense(ScalarBuffer::from(invalid_levels.clone())), + physical_levels(&invalid_levels), + coalesced_levels(&invalid_levels), + ] { + let error = validate_complex_all_null_levels(&None, &Some(levels), 0, 2).unwrap_err(); + assert!(matches!(error, lance_core::Error::InvalidInput { .. })); + assert!(error.to_string().contains("Invalid definition level 3")); + } + + let rep = Some(LazyLevels::Dense(ScalarBuffer::from(vec![0u16; 2]))); + let def = Some(LazyLevels::Dense(ScalarBuffer::from(vec![0u16]))); + let error = validate_complex_all_null_levels(&rep, &def, 0, 0).unwrap_err(); + assert!(matches!(error, lance_core::Error::InvalidInput { .. })); + assert!( + error + .to_string() + .contains("repetition has 2, definition has 1") + ); + } + + #[test] + fn block_levels_reject_malformed_payload_size() { + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(vec![0]), + bits_per_value: 16, + num_values: 1, + block_info: BlockInfo::new(), + }); + let error = dense_levels_from_block(block, 1, "definition").unwrap_err(); + assert!(matches!(error, lance_core::Error::InvalidInput { .. })); + assert!( + error + .to_string() + .contains("expected 2 bytes for 1 values, got 1") + ); + } + + #[test] + fn complex_all_null_level_codec_validates_rle_metadata() { + let encoding = pb21::CompressiveEncoding { + compression: Some(Compression::Rle(Box::new(pb21::Rle { + values: None, + run_lengths: Some(Box::new(ProtobufUtils21::flat(8, None))), + }))), + }; + + let error = LevelCodec::try_new(Some(&encoding), &DefaultDecompressionStrategy::default()) + .unwrap_err(); + assert!(matches!(error, lance_core::Error::InvalidInput { .. })); + assert!( + error + .to_string() + .contains("RLE compression missing values encoding") + ); + } + + // https://github.com/lance-format/lance/issues/6681 + #[tokio::test] + async fn test_sparse_boolean_list_roundtrip() { + use arrow_array::builder::{BooleanBuilder, ListBuilder}; + + let mut list_builder = ListBuilder::new(BooleanBuilder::new()); + for i in 0..1000i32 { + if i % 64 == 0 { + // Alternate true/false so the array is not constant (constant path avoids the bug). + list_builder.values().append_value(i % 128 == 0); + list_builder.append(true); + } else { + list_builder.append(false); + } + } + let list_array = Arc::new(list_builder.finish()); + + let test_cases = TestCases::default().with_structural_encodings(); + check_round_trip_encoding_of_data(vec![list_array], &test_cases, HashMap::new()).await; + } + + fn truncated_tail_details() -> std::sync::Arc { + use crate::compression::VariablePerValueDecompressor; + use crate::encodings::physical::binary::VariableDecoder; + use crate::repdef::{ControlWordParser, DefinitionInterpretation}; + use std::sync::Arc; + Arc::new(super::FullZipDecodeDetails { + value_decompressor: super::PerValueDecompressor::Variable(Arc::new( + VariableDecoder::default(), + ) + as Arc), + def_meaning: vec![DefinitionInterpretation::NullableItem].into(), + ctrl_word_parser: ControlWordParser::new(0, 0), + max_rep: 0, + max_visible_def: 0, + }) + } + + fn decode_variable_full_zip( + buf: Vec, + bits_per_offset: u8, + ) -> lance_core::Result { + use std::collections::VecDeque; + let mut data = VecDeque::new(); + data.push_back(crate::buffer::LanceBuffer::from(buf)); + super::VariableFullZipDecoder::new( + truncated_tail_details(), + data, + 1, + bits_per_offset, + bits_per_offset, + ) + } + + /// A well-formed length prefix decodes without incident, for both widths. + #[test] + fn variable_full_zip_wellformed_length_prefix() { + assert!(decode_variable_full_zip(0u32.to_le_bytes().to_vec(), 32).is_ok()); + assert!(decode_variable_full_zip(0u64.to_le_bytes().to_vec(), 64).is_ok()); + } + + /// A page whose item walk ends with a partial length prefix must surface a + /// corrupt-file error rather than read past the end of the buffer. + /// + /// This asserts the error variant and message rather than merely expecting a + /// panic: before the length prefix was bounds checked, the read was + /// `get_unchecked` behind a `debug_assert!`, so a debug build panicked here + /// (which a `#[should_panic]` test would have accepted as a pass) while a + /// release build read up to 8 bytes out of a 4 byte allocation. + #[test] + fn variable_full_zip_truncated_length_prefix_is_corrupt_file() { + use lance_core::Error; + + for (bits, buf_len) in [(32u8, 3usize), (64u8, 4usize)] { + let err = decode_variable_full_zip(vec![0xAA; buf_len], bits) + .expect_err("a truncated length prefix must not decode"); + assert!( + matches!(err, Error::CorruptFile { .. }), + "expected CorruptFile for a {}-bit prefix with {} byte(s), got: {:?}", + bits, + buf_len, + err + ); + let msg = err.to_string(); + assert!( + msg.contains("truncated length prefix"), + "error should say what is wrong, got: {msg}" + ); + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/blob.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/blob.rs new file mode 100644 index 000000000..52a7039b2 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/blob.rs @@ -0,0 +1,532 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Routines for decoding blob data +//! +//! The blob structural encoding is a structural encoding where the values (blobs) are stored +//! out-of-line in the file. The page contains the descriptions, encoded using some other layout. + +use std::{collections::VecDeque, ops::Range, sync::Arc}; + +use arrow_array::{Array, UInt64Array, cast::AsArray, make_array}; +use bytes::Bytes; +use futures::{FutureExt, future::BoxFuture}; + +use lance_core::{ + Error, Result, cache::DeepSizeOf, datatypes::BLOB_DESC_TYPE, error::LanceOptionExt, +}; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, VariableWidthBlock}, + decoder::{DecodePageTask, DecodedPage, StructuralPageDecoder}, + encodings::logical::primitive::{CachedPageData, PageLoadTask, StructuralPageScheduler}, + repdef::{DefinitionInterpretation, RepDefUnraveler}, +}; + +/// How many bytes to target in each unloaded / loaded shard. A larger value means +/// we buffer more data in memory / make bigger requests to the I/O scheduler while +/// a smaller value means more requests to the I/O scheduler. +/// +/// This is probably a reasonable default for most cases. +pub const TARGET_SHARD_SIZE: u64 = 32 * 1024 * 1024; + +#[derive(Debug)] +pub(super) struct BlobDescriptionPageScheduler { + inner_scheduler: Box, + def_meaning: Arc<[DefinitionInterpretation]>, +} + +impl BlobDescriptionPageScheduler { + pub fn new( + inner_scheduler: Box, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Self { + Self { + inner_scheduler, + def_meaning, + } + } + + fn wrap_decoder_fut( + decoder_fut: BoxFuture<'static, Result>>, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> BoxFuture<'static, Result>> { + async move { + let decoder = decoder_fut.await?; + Ok( + Box::new(BlobDescriptionPageDecoder::new(decoder, def_meaning)) + as Box, + ) + } + .boxed() + } +} + +impl StructuralPageScheduler for BlobDescriptionPageScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + self.inner_scheduler.initialize(io) + } + + fn load(&mut self, data: &Arc) { + self.inner_scheduler.load(data); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + let tasks = self.inner_scheduler.schedule_ranges(ranges, io)?; + Ok(tasks + .into_iter() + .map(|task| PageLoadTask { + decoder_fut: Self::wrap_decoder_fut(task.decoder_fut, self.def_meaning.clone()), + num_rows: task.num_rows, + }) + .collect()) + } +} + +#[derive(Debug)] +struct BlobDescriptionPageDecoder { + inner: Box, + def_meaning: Arc<[DefinitionInterpretation]>, +} + +impl BlobDescriptionPageDecoder { + fn new( + inner: Box, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Self { + Self { inner, def_meaning } + } +} + +impl StructuralPageDecoder for BlobDescriptionPageDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + Ok(Box::new(BlobDescriptionDecodePageTask::new( + self.inner.drain(num_rows)?, + self.def_meaning.clone(), + ))) + } + + fn num_rows(&self) -> u64 { + self.inner.num_rows() + } +} + +#[derive(Debug)] +struct BlobDescriptionDecodePageTask { + inner: Box, + def_meaning: Arc<[DefinitionInterpretation]>, +} + +impl BlobDescriptionDecodePageTask { + fn new(inner: Box, def_meaning: Arc<[DefinitionInterpretation]>) -> Self { + Self { inner, def_meaning } + } +} + +impl DecodePageTask for BlobDescriptionDecodePageTask { + fn decode(self: Box) -> Result { + let decoded = self.inner.decode()?; + let num_values = decoded.data.num_values(); + + // Need to extract out the repdef information + let DataBlock::Struct(descriptions) = &decoded.data else { + return Err(Error::internal( + "Expected struct data block for descriptions", + )); + }; + let mut description_children = descriptions.children.iter(); + let DataBlock::FixedWidth(positions) = description_children.next().expect_ok()? else { + return Err(Error::internal( + "Expected fixed width data block for positions", + )); + }; + let DataBlock::FixedWidth(sizes) = description_children.next().expect_ok()? else { + return Err(Error::internal("Expected fixed width data block for sizes")); + }; + let positions = positions.data.borrow_to_typed_slice::(); + let sizes = sizes.data.borrow_to_typed_slice::(); + + let mut rep = Vec::with_capacity(num_values as usize); + let mut def = Vec::with_capacity(num_values as usize); + + for (position, size) in positions.iter().copied().zip(sizes.iter().copied()) { + if size == 0 { + if position == 0 { + rep.push(0); + def.push(0); + } else { + let repval = (position & 0xFFFF) as u16; + let defval = ((position >> 16) & 0xFFFF) as u16; + rep.push(repval); + def.push(defval); + } + } else { + rep.push(0); + def.push(0); + } + } + + let rep = if rep.iter().any(|r| *r != 0) { + Some(rep) + } else { + None + }; + let def = if self.def_meaning.len() > 1 + || self.def_meaning[0] != DefinitionInterpretation::AllValidItem + { + Some(def) + } else { + None + }; + + let repdef = + RepDefUnraveler::new(rep, def, self.def_meaning.clone(), positions.len() as u64); + + Ok(DecodedPage { + data: decoded.data, + repdef, + }) + } +} + +struct BlobCacheableState { + positions: Arc, + sizes: Arc, + inner_state: Arc, +} + +impl DeepSizeOf for BlobCacheableState { + fn deep_size_of_children(&self, context: &mut lance_core::deepsize::Context) -> usize { + (self.positions.as_ref() as &dyn arrow_array::Array).deep_size_of_children(context) + + (self.sizes.as_ref() as &dyn arrow_array::Array).deep_size_of_children(context) + + self.inner_state.deep_size_of_children(context) + } +} + +impl CachedPageData for BlobCacheableState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +#[derive(Debug)] +pub(super) struct BlobPageScheduler { + inner_scheduler: Box, + row_number: u64, + num_rows: u64, + def_meaning: Arc<[DefinitionInterpretation]>, + positions: Option>, + sizes: Option>, +} + +impl BlobPageScheduler { + pub fn new( + inner_scheduler: Box, + row_number: u64, + num_rows: u64, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Self { + Self { + inner_scheduler, + row_number, + num_rows, + def_meaning, + positions: None, + sizes: None, + } + } + + fn create_page_load_task( + ranges_to_read: Vec>, + mut loaded_blobs: Vec, + first_row_number: u64, + io: &dyn EncodingsIo, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Result { + let num_rows = loaded_blobs.len() as u64; + let read_fut = io.submit_request(ranges_to_read, first_row_number); + let decoder_fut = async move { + let bytes = read_fut.await?; + let mut bytes_iter = bytes.into_iter(); + for blob in loaded_blobs.iter_mut() { + // Empty values have def == 0 too but scheduled no read; their + // bytes were set at scheduling time. + if blob.def == 0 && blob.bytes.is_none() { + blob.set_bytes(bytes_iter.next().expect_ok()?); + } + } + debug_assert!(bytes_iter.next().is_none()); + Ok(Box::new(BlobPageDecoder::new(loaded_blobs, def_meaning)) + as Box) + } + .boxed(); + Ok(PageLoadTask { + decoder_fut, + num_rows, + }) + } +} + +impl StructuralPageScheduler for BlobPageScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + let io = io.clone(); + let num_rows = self.num_rows; + async move { + let cached = self.inner_scheduler.initialize(&io).await?; + let mut desc_decoders = self.inner_scheduler.schedule_ranges(&[0..num_rows], &io)?; + if desc_decoders.len() != 1 { + // This can't happen yet today so being a little lazy but if it did happen we just + // need to concatenate the descriptions. I'm guessing by then we might be doing something + // different than "load all descriptors in initialize" anyways. + return Err(Error::not_supported_source( + "Expected exactly one descriptor decoder".into(), + )); + } + let desc_decoder_task = desc_decoders.pop().unwrap(); + let mut desc_decoder = desc_decoder_task.decoder_fut.await?; + + let descs = desc_decoder.drain(desc_decoder_task.num_rows)?; + let descs = descs.decode()?; + let descs = make_array(descs.data.into_arrow(BLOB_DESC_TYPE.clone(), true)?); + let descs = descs.as_struct(); + let positions = Arc::new( + descs + .column(0) + .as_any() + .downcast_ref::() + .unwrap() + .clone(), + ); + let sizes = Arc::new( + descs + .column(1) + .as_any() + .downcast_ref::() + .unwrap() + .clone(), + ); + self.positions = Some(positions.clone()); + self.sizes = Some(sizes.clone()); + let state = Arc::new(BlobCacheableState { + inner_state: cached, + positions, + sizes, + }); + Ok(state as Arc) + } + .boxed() + } + + fn load(&mut self, data: &Arc) { + let blob_state = data + .clone() + .as_arc_any() + .downcast::() + .unwrap(); + self.positions = Some(blob_state.positions.clone()); + self.sizes = Some(blob_state.sizes.clone()); + self.inner_scheduler.load(&blob_state.inner_state); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + let num_rows: u64 = ranges.iter().map(|r| r.end - r.start).sum(); + + let positions = self.positions.as_ref().expect_ok()?; + let sizes = self.sizes.as_ref().expect_ok()?; + + let mut page_load_tasks = Vec::new(); + let mut bytes_so_far = 0; + let mut ranges_to_read = Vec::with_capacity(num_rows as usize); + let mut loaded_blobs = Vec::with_capacity(num_rows as usize); + let mut first_row_number = None; + for range in ranges { + for row in range.start..range.end { + if first_row_number.is_none() { + first_row_number = Some(row + self.row_number); + } + let position = positions.value(row as usize); + let size = sizes.value(row as usize); + + if size == 0 { + let rep = (position & 0xFFFF) as u16; + let def = ((position >> 16) & 0xFFFF) as u16; + let mut blob = LoadedBlob::new(rep, def); + if def == 0 { + // A size-0 descriptor with definition level 0 is a + // valid, empty value (nulls carry their non-zero + // packed rep/def levels in `position`). No read is + // scheduled for it, so it gets its zero-length bytes + // here rather than consuming another blob's read + // result in the load task. + blob.set_bytes(Bytes::new()); + } + loaded_blobs.push(blob); + } else { + loaded_blobs.push(LoadedBlob::new(0, 0)); + ranges_to_read.push(position..(position + size)); + bytes_so_far += size; + } + + if bytes_so_far >= TARGET_SHARD_SIZE { + let page_load_task = Self::create_page_load_task( + std::mem::take(&mut ranges_to_read), + std::mem::take(&mut loaded_blobs), + first_row_number.unwrap(), + io.as_ref(), + self.def_meaning.clone(), + )?; + page_load_tasks.push(page_load_task); + bytes_so_far = 0; + first_row_number = None; + } + } + } + if !loaded_blobs.is_empty() { + let page_load_task = Self::create_page_load_task( + std::mem::take(&mut ranges_to_read), + std::mem::take(&mut loaded_blobs), + first_row_number.unwrap(), + io.as_ref(), + self.def_meaning.clone(), + )?; + page_load_tasks.push(page_load_task); + } + + Ok(page_load_tasks) + } +} + +#[derive(Debug)] +struct LoadedBlob { + bytes: Option, + rep: u16, + def: u16, +} + +impl LoadedBlob { + fn new(rep: u16, def: u16) -> Self { + Self { + bytes: None, + rep, + def, + } + } + + fn set_bytes(&mut self, bytes: Bytes) { + self.bytes = Some(bytes); + } +} + +#[derive(Debug)] +struct BlobPageDecoder { + blobs: VecDeque, + def_meaning: Arc<[DefinitionInterpretation]>, + num_rows: u64, +} + +impl BlobPageDecoder { + fn new(blobs: Vec, def_meaning: Arc<[DefinitionInterpretation]>) -> Self { + Self { + num_rows: blobs.len() as u64, + blobs: blobs.into_iter().collect(), + def_meaning, + } + } +} + +impl StructuralPageDecoder for BlobPageDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let blobs = self.blobs.drain(0..num_rows as usize).collect::>(); + Ok(Box::new(BlobDecodePageTask::new( + blobs, + self.def_meaning.clone(), + ))) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +#[derive(Debug)] +struct BlobDecodePageTask { + blobs: Vec, + def_meaning: Arc<[DefinitionInterpretation]>, +} + +impl BlobDecodePageTask { + fn new(blobs: Vec, def_meaning: Arc<[DefinitionInterpretation]>) -> Self { + Self { blobs, def_meaning } + } +} + +impl DecodePageTask for BlobDecodePageTask { + fn decode(self: Box) -> Result { + let num_values = self.blobs.len() as u64; + let num_bytes = self + .blobs + .iter() + .filter_map(|b| b.bytes.as_ref()) + .map(|b| b.len()) + .sum::(); + let mut buffer = Vec::with_capacity(num_bytes); + let mut offsets = Vec::with_capacity(num_values as usize + 1); + let mut rep = Vec::with_capacity(num_values as usize); + let mut def = Vec::with_capacity(num_values as usize); + offsets.push(0_u64); + for blob in self.blobs { + rep.push(blob.rep); + def.push(blob.def); + if let Some(bytes) = blob.bytes { + offsets.push(offsets.last().unwrap() + bytes.len() as u64); + buffer.extend_from_slice(&bytes); + } else { + // Null / emptyvalue + offsets.push(*offsets.last().unwrap()); + } + } + let offsets = LanceBuffer::reinterpret_vec(offsets); + let data = LanceBuffer::from(buffer); + let data_block = DataBlock::VariableWidth(VariableWidthBlock { + data, + offsets, + bits_per_offset: 64, + num_values, + block_info: BlockInfo::new(), + }); + + let rep = if rep.iter().any(|r| *r != 0) { + Some(rep) + } else { + None + }; + let def = if self.def_meaning.len() > 1 + || self.def_meaning[0] != DefinitionInterpretation::AllValidItem + { + Some(def) + } else { + None + }; + + Ok(DecodedPage { + data: data_block, + repdef: RepDefUnraveler::new(rep, def, self.def_meaning, num_values), + }) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/chunk_index.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/chunk_index.rs new file mode 100644 index 000000000..19fb0fdb0 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/chunk_index.rs @@ -0,0 +1,536 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Compact per-page chunk index for the mini-block structural encoding. +//! +//! The chunk index is stored on disk in an extremely compressed form that +//! requires a lot of CPU to work with. However, extracting it out to its full +//! width can be RAM-intensive. As a compromise we extract into a prefix-sum +//! array that we fit into `u32` if possible and we avoid storing per-block row +//! counts when those are redundant. The scheduler looks chunks up by index +//! (byte range, leaf value count) and by row (which chunk holds a row). +//! +//! ```text +//! MiniBlockChunkIndex +//! |- base: u64 absolute file position of the value buffer +//! |- byte_starts: PrefixSums cumulative chunk byte sizes (all pages) +//! `- rows: RowMapping +//! |- UniformFlat { .. } flat page, uniform leaf chunking (arithmetic) +//! |- Flat { value_starts } flat page, non-uniform leaf chunking +//! `- Nested { row_starts, .. } repetition present; rows tracked as prefix sums +//! ``` + +use std::ops::Range; + +use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder}; +use lance_core::cache::{Context, DeepSizeOf}; + +/// Cumulative (prefix-sum) array of length `num_chunks + 1` (entry `0` is `0`, +/// the last entry is the grand total). Stored as `u32` when the total fits, +/// else `u64`. +#[derive(Debug, DeepSizeOf)] +pub enum PrefixSums { + U32(Vec), + U64(Vec), +} + +impl PrefixSums { + /// Builds the cumulative array from per-chunk `deltas`. `total` selects the + /// storage width (callers must pass the true sum); `num_chunks` only pre-sizes. + pub fn from_deltas(deltas: impl Iterator, num_chunks: usize, total: u64) -> Self { + if total <= u32::MAX as u64 { + let mut values = Vec::with_capacity(num_chunks + 1); + let mut acc = 0u32; + values.push(0); + for delta in deltas { + acc += delta as u32; + values.push(acc); + } + debug_assert_eq!(values.len(), num_chunks + 1); + debug_assert_eq!(acc as u64, total); + Self::U32(values) + } else { + let mut values = Vec::with_capacity(num_chunks + 1); + let mut acc = 0u64; + values.push(0); + for delta in deltas { + acc += delta; + values.push(acc); + } + debug_assert_eq!(values.len(), num_chunks + 1); + debug_assert_eq!(acc, total); + Self::U64(values) + } + } + + /// Builds a `PrefixSums` from an already-cumulative array (`[0, .., total]`), + /// narrowing to `u32` when the total fits. Avoids the deltas buffer + /// [`Self::from_deltas`] would need. + fn from_prefix(prefix: Vec) -> Self { + debug_assert!(!prefix.is_empty()); + debug_assert_eq!(prefix[0], 0); + let total = prefix.last().copied().unwrap_or(0); + if total <= u32::MAX as u64 { + Self::U32(prefix.into_iter().map(|v| v as u32).collect()) + } else { + Self::U64(prefix) + } + } + + /// Cumulative value at position `i` (i.e. the start of chunk `i`). + pub fn get(&self, i: usize) -> u64 { + match self { + Self::U32(values) => values[i] as u64, + Self::U64(values) => values[i], + } + } + + /// Start and end of chunk `i` (positions `i`, `i + 1`) behind one width + /// match -- halves the branching of two `get` calls on the hot per-chunk path. + pub fn get_pair(&self, i: usize) -> (u64, u64) { + match self { + Self::U32(values) => (values[i] as u64, values[i + 1] as u64), + Self::U64(values) => (values[i], values[i + 1]), + } + } + + /// Number of chunks (array length minus the trailing total). + pub fn num_chunks(&self) -> usize { + match self { + Self::U32(values) => values.len() - 1, + Self::U64(values) => values.len() - 1, + } + } + + /// Size of chunk `i` (the delta between consecutive cumulative values). + pub fn delta(&self, i: usize) -> u64 { + let (start, end) = self.get_pair(i); + end - start + } + + /// Index of the chunk whose half-open span `[get(i), get(i+1))` contains + /// `value`. On an exact hit against a chunk start, returns the *first* chunk + /// with that start (chunks can share a start row). + pub fn find(&self, value: u64) -> usize { + // Match the width once, then binary-search only the starts (not the + // trailing total). `partition_point` already yields the first of any + // duplicated starts; the `idx - 1` fallback is safe since `get(0) == 0`. + match self { + Self::U32(values) => { + let starts = &values[..values.len() - 1]; + let idx = starts.partition_point(|&start| (start as u64) < value); + if idx < starts.len() && starts[idx] as u64 == value { + idx + } else { + idx - 1 + } + } + Self::U64(values) => { + let starts = &values[..values.len() - 1]; + let idx = starts.partition_point(|&start| start < value); + if idx < starts.len() && starts[idx] == value { + idx + } else { + idx - 1 + } + } + } + } +} + +/// Leaf value counts per chunk, needed to decode. Tracked only for nested +/// pages; flat pages read items off the row mapping (rows == items). +#[derive(Debug, DeepSizeOf)] +pub enum ItemCounts { + /// Every non-last chunk holds the same number of values. + Uniform { + values_per_chunk: u64, + last_chunk_values: u64, + }, + /// `log2` of each chunk's value count, stored as one byte per chunk rather + /// than the full count because this index stays cached in RAM; the last + /// chunk is handled via `last_chunk_values`. + PerChunkLog { + logs: Vec, + last_chunk_values: u64, + }, +} + +impl ItemCounts { + fn get(&self, i: usize, num_chunks: usize) -> u64 { + match self { + Self::Uniform { + values_per_chunk, + last_chunk_values, + } => { + if i == num_chunks - 1 { + *last_chunk_values + } else { + *values_per_chunk + } + } + Self::PerChunkLog { + logs, + last_chunk_values, + } => { + if i == num_chunks - 1 { + *last_chunk_values + } else { + 1u64 << logs[i] + } + } + } + } +} + +/// How row ranges map onto chunks. +/// +/// Flat pages have row == value index and no preamble/trailer. Nested pages +/// track rows separately from leaf items and store a trailer bit per chunk; a +/// chunk's preamble is the previous chunk's trailer. +#[derive(Debug)] +pub enum RowMapping { + /// Flat page whose non-last chunks all hold `values_per_chunk` values, so + /// row->chunk is pure arithmetic. + UniformFlat { + values_per_chunk: u64, + last_chunk_values: u64, + num_chunks: usize, + }, + /// Flat page with non-uniform chunk sizes; `value_starts` are the cumulative + /// value counts (final entry == number of items in the page). + Flat { value_starts: PrefixSums }, + /// Nested page. `row_starts` are cumulative row counts (final entry == + /// number of rows in the page); `has_trailer[i]` is set when chunk `i` ends + /// with a partial list. + Nested { + row_starts: PrefixSums, + has_trailer: BooleanBuffer, + item_counts: ItemCounts, + }, +} + +impl DeepSizeOf for RowMapping { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + match self { + Self::UniformFlat { .. } => 0, + Self::Flat { value_starts } => value_starts.deep_size_of_children(context), + Self::Nested { + row_starts, + has_trailer, + item_counts, + } => { + row_starts.deep_size_of_children(context) + + has_trailer.len().div_ceil(8) + + item_counts.deep_size_of_children(context) + } + } + } +} + +/// Compact per-page chunk index that avoids fully materializing the repetition +/// index into u64's to save RAM. See the module docs for the layout. +#[derive(Debug)] +pub struct MiniBlockChunkIndex { + base: u64, + byte_starts: PrefixSums, + rows: RowMapping, +} + +impl MiniBlockChunkIndex { + pub fn new(base: u64, byte_starts: PrefixSums, rows: RowMapping) -> Self { + Self { + base, + byte_starts, + rows, + } + } + + /// Number of chunks in the page. + pub fn num_chunks(&self) -> usize { + self.byte_starts.num_chunks() + } + + /// Absolute byte range of chunk `i` within the file. + pub fn byte_range(&self, i: usize) -> Range { + let (start, end) = self.byte_starts.get_pair(i); + (self.base + start)..(self.base + end) + } + + /// Number of leaf values in chunk `i` (passed to the value decompressor). + pub fn items_in_chunk(&self, i: usize) -> u64 { + let num_chunks = self.num_chunks(); + match &self.rows { + RowMapping::UniformFlat { + values_per_chunk, + last_chunk_values, + .. + } => { + if i == num_chunks - 1 { + *last_chunk_values + } else { + *values_per_chunk + } + } + RowMapping::Flat { value_starts } => value_starts.delta(i), + RowMapping::Nested { item_counts, .. } => item_counts.get(i, num_chunks), + } + } + + /// Index of the chunk that contains `row`. + pub fn find_chunk(&self, row: u64) -> usize { + match &self.rows { + RowMapping::UniformFlat { + values_per_chunk, + num_chunks, + .. + } => ((row / values_per_chunk) as usize).min(num_chunks - 1), + RowMapping::Flat { value_starts } => value_starts.find(row), + RowMapping::Nested { row_starts, .. } => row_starts.find(row), + } + } + + /// First row (relative to the page) that begins in chunk `i`. + pub fn first_row(&self, i: usize) -> u64 { + match &self.rows { + RowMapping::UniformFlat { + values_per_chunk, .. + } => i as u64 * values_per_chunk, + RowMapping::Flat { value_starts } => value_starts.get(i), + RowMapping::Nested { row_starts, .. } => row_starts.get(i), + } + } + + /// Number of rows that start in chunk `i`, including a trailer but not a + /// preamble (the previous `starts_including_trailer`). + pub fn rows_in_chunk(&self, i: usize) -> u64 { + let num_chunks = self.num_chunks(); + match &self.rows { + RowMapping::UniformFlat { + values_per_chunk, + last_chunk_values, + .. + } => { + if i == num_chunks - 1 { + *last_chunk_values + } else { + *values_per_chunk + } + } + RowMapping::Flat { value_starts } => value_starts.delta(i), + RowMapping::Nested { row_starts, .. } => row_starts.delta(i), + } + } + + /// Whether chunk `i` begins with a preamble (a continuation of the previous + /// chunk's list). Always false for flat pages; for nested pages this is the + /// previous chunk's trailer. + pub fn has_preamble(&self, i: usize) -> bool { + match &self.rows { + RowMapping::Nested { has_trailer, .. } => i > 0 && has_trailer.value(i - 1), + _ => false, + } + } + + /// Whether chunk `i` ends with a trailer (a partial list continued in the + /// next chunk). Always false for flat pages. + pub fn has_trailer(&self, i: usize) -> bool { + match &self.rows { + RowMapping::Nested { has_trailer, .. } => has_trailer.value(i), + _ => false, + } + } + + /// Name of the active row-mapping variant, used to assert detection in tests. + #[cfg(test)] + pub fn row_mapping_debug(&self) -> &'static str { + match &self.rows { + RowMapping::UniformFlat { .. } => "uniform_flat", + RowMapping::Flat { .. } => "flat", + RowMapping::Nested { .. } => "nested", + } + } + + /// Builds a nested index from raw repetition-index bytes, using placeholder + /// byte offsets and item counts. Only the row axis is populated, which is + /// all the scheduler exercises. + #[cfg(test)] + pub fn new_nested_for_test(rep_bytes: &[u8], stride: usize) -> Self { + let (row_starts, has_trailer) = parse_nested_rep(rep_bytes, stride); + let num_chunks = row_starts.num_chunks(); + let byte_starts = PrefixSums::from_deltas( + std::iter::repeat_n(8u64, num_chunks), + num_chunks, + 8 * num_chunks as u64, + ); + Self { + base: 0, + byte_starts, + rows: RowMapping::Nested { + row_starts, + has_trailer, + item_counts: ItemCounts::Uniform { + values_per_chunk: 1, + last_chunk_values: 1, + }, + }, + } + } +} + +impl DeepSizeOf for MiniBlockChunkIndex { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + self.byte_starts.deep_size_of_children(context) + self.rows.deep_size_of_children(context) + } +} + +/// Parses a mini-block repetition index into the compact nested row mapping. +/// +/// Bytes are `u64`s in groups of `stride`; the first two are `ends` (lists +/// finishing in the chunk) and `partial` (leftover items). Only cumulative row +/// starts and a trailer bit are kept: `has_preamble[i] = has_trailer[i-1]` and +/// `starts_including_trailer = ends + has_trailer - has_preamble`. +pub fn parse_nested_rep(rep_bytes: &[u8], stride: usize) -> (PrefixSums, BooleanBuffer) { + // Read the two `u64`s per group straight from the little-endian bytes rather + // than copying the buffer to reinterpret it. The caller guarantees + // `rep_bytes.len() % 8 == 0`, so the 8-byte windows stay in bounds. + const WORD: usize = std::mem::size_of::(); + let read_word = |word_idx: usize| -> u64 { + let byte = word_idx * WORD; + u64::from_le_bytes(rep_bytes[byte..byte + WORD].try_into().unwrap()) + }; + let num_chunks = (rep_bytes.len() / WORD) / stride; + + let mut has_trailer_builder = BooleanBufferBuilder::new(num_chunks); + // Accumulate the cumulative row starts in a single pass (entry 0 is 0, the + // trailing entry is the total) so there is no separate deltas buffer. + let mut row_starts = Vec::with_capacity(num_chunks + 1); + row_starts.push(0u64); + let mut acc = 0u64; + let mut chunk_has_preamble = false; + + for i in 0..num_chunks { + let base_idx = i * stride; + let ends = read_word(base_idx); + let partial = read_word(base_idx + 1); + + let has_trailer = partial > 0; + let starts_including_trailer = ends + (has_trailer as u64) - (chunk_has_preamble as u64); + + has_trailer_builder.append(has_trailer); + acc += starts_including_trailer; + row_starts.push(acc); + + chunk_has_preamble = has_trailer; + } + + ( + PrefixSums::from_prefix(row_starts), + has_trailer_builder.finish(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::buffer::LanceBuffer; + + /// Reference decode: the previous per-block repetition index, used as an + /// oracle for the compact `parse_nested_rep`. + struct RefBlock { + first_row: u64, + starts_including_trailer: u64, + has_preamble: bool, + has_trailer: bool, + } + + fn reference_decode(rep_bytes: &[u8], stride: usize) -> Vec { + let buffer = LanceBuffer::from(rep_bytes.to_vec()); + let u64_slice = buffer.borrow_to_typed_slice::(); + let n = u64_slice.len() / stride; + let mut blocks = Vec::with_capacity(n); + let mut chunk_has_preamble = false; + let mut offset = 0u64; + for i in 0..n { + let base_idx = i * stride; + let ends = u64_slice[base_idx]; + let partial = u64_slice[base_idx + 1]; + let has_trailer = partial > 0; + let starts_including_trailer = + ends + (has_trailer as u64) - (chunk_has_preamble as u64); + blocks.push(RefBlock { + first_row: offset, + starts_including_trailer, + has_preamble: chunk_has_preamble, + has_trailer, + }); + chunk_has_preamble = has_trailer; + offset += starts_including_trailer; + } + blocks + } + + fn rep_bytes(values: &[u64]) -> Vec { + values.iter().flat_map(|v| v.to_le_bytes()).collect() + } + + #[test] + fn test_prefix_sums_u32() { + let sums = PrefixSums::from_deltas([2u64, 3, 5].into_iter(), 3, 10); + assert!(matches!(sums, PrefixSums::U32(_))); + assert_eq!(sums.num_chunks(), 3); + assert_eq!(sums.get(0), 0); + assert_eq!(sums.get(1), 2); + assert_eq!(sums.get(3), 10); + assert_eq!(sums.delta(1), 3); + } + + #[test] + fn test_prefix_sums_u64_selected_by_total() { + let big = u32::MAX as u64 + 1; + let sums = PrefixSums::from_deltas([big].into_iter(), 1, big); + assert!(matches!(sums, PrefixSums::U64(_))); + assert_eq!(sums.get(1), big); + } + + #[test] + fn test_prefix_sums_find() { + // Chunk starts: 0, 5, 5, 12 (a zero-width chunk creates a duplicate start) + let sums = PrefixSums::from_deltas([5u64, 0, 7].into_iter(), 3, 12); + // Inside the first chunk + assert_eq!(sums.find(3), 0); + // Exact match on a duplicated start returns the first such chunk + assert_eq!(sums.find(5), 1); + // Inside the last chunk + assert_eq!(sums.find(11), 2); + // Start of the first chunk + assert_eq!(sums.find(0), 0); + } + + #[test] + fn test_parse_nested_rep_matches_reference() { + let cases: Vec> = vec![ + vec![5, 2, 3, 0, 4, 7, 2, 0], + vec![5, 2, 3, 3, 20, 0], + vec![0, 5, 0, 3, 10, 0], + vec![1, 0], + ]; + for values in cases { + let bytes = rep_bytes(&values); + let reference = reference_decode(&bytes, 2); + let (row_starts, has_trailer) = parse_nested_rep(&bytes, 2); + assert_eq!(row_starts.num_chunks(), reference.len()); + for (i, block) in reference.iter().enumerate() { + assert_eq!(row_starts.get(i), block.first_row, "first_row[{i}]"); + assert_eq!( + row_starts.delta(i), + block.starts_including_trailer, + "starts_including_trailer[{i}]" + ); + assert_eq!(has_trailer.value(i), block.has_trailer, "has_trailer[{i}]"); + let derived_preamble = i > 0 && has_trailer.value(i - 1); + assert_eq!(derived_preamble, block.has_preamble, "has_preamble[{i}]"); + } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/constant.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/constant.rs new file mode 100644 index 000000000..f9288dd87 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/constant.rs @@ -0,0 +1,502 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{any::Any, collections::VecDeque, ops::Range, sync::Arc}; + +use arrow_array::{Array, ArrayRef, new_empty_array}; +use arrow_buffer::ScalarBuffer; +use arrow_schema::DataType; +use bytes::Bytes; +use futures::FutureExt; +use futures::future::BoxFuture; + +use lance_core::{ + Error, Result, + cache::{Context, DeepSizeOf}, +}; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + decoder::PageEncoding, + encoder::EncodedPage, + encodings::logical::primitive::{CachedPageData, PageLoadTask}, + format::ProtobufUtils21, + repdef::{DefinitionInterpretation, RepDefUnraveler}, +}; + +pub(crate) fn encode_constant_page( + column_idx: u32, + scalar: ArrayRef, + repdef: crate::repdef::SerializedRepDefs, + row_number: u64, + num_rows: u64, +) -> Result { + let inline_value = lance_arrow::scalar::try_inline_value(&scalar); + let value_buffer = if inline_value.is_some() { + None + } else { + Some(LanceBuffer::from( + lance_arrow::scalar::encode_scalar_value_buffer(&scalar)?, + )) + }; + + let description = ProtobufUtils21::constant_layout(&repdef.def_meaning, inline_value); + + let has_repdef = repdef.repetition_levels.is_some() || repdef.definition_levels.is_some(); + + let data = if !has_repdef { + value_buffer.into_iter().collect::>() + } else { + let rep_bytes = repdef + .repetition_levels + .as_ref() + .map(|rep| LanceBuffer::reinterpret_slice(rep.clone())) + .unwrap_or_else(LanceBuffer::empty); + let def_bytes = repdef + .definition_levels + .as_ref() + .map(|def| LanceBuffer::reinterpret_slice(def.clone())) + .unwrap_or_else(LanceBuffer::empty); + + match value_buffer { + Some(value_buffer) => vec![value_buffer, rep_bytes, def_bytes], + None => vec![rep_bytes, def_bytes], + } + }; + + Ok(EncodedPage { + column_idx, + data, + description: PageEncoding::Structural(description), + num_rows, + row_number, + }) +} + +#[derive(Debug)] +struct CachedConstantState { + scalar: ArrayRef, + rep: Option>, + def: Option>, +} + +impl DeepSizeOf for CachedConstantState { + fn deep_size_of_children(&self, _ctx: &mut Context) -> usize { + self.scalar.get_buffer_memory_size() + + self.rep.as_ref().map(|buf| buf.len() * 2).unwrap_or(0) + + self.def.as_ref().map(|buf| buf.len() * 2).unwrap_or(0) + } +} + +impl CachedPageData for CachedConstantState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +#[derive(Debug, Clone)] +enum ScalarSource { + Inline(Vec), + ValueBuffer(usize), +} + +#[derive(Debug)] +pub struct ConstantPageScheduler { + buffer_offsets_and_sizes: Arc<[(u64, u64)]>, + scalar_source: ScalarSource, + rep_buf_idx: Option, + def_buf_idx: Option, + data_type: DataType, + def_meaning: Arc<[DefinitionInterpretation]>, + max_rep: u16, + max_visible_def: u16, + repdef: Option>, +} + +impl ConstantPageScheduler { + pub fn try_new( + buffer_offsets_and_sizes: Arc<[(u64, u64)]>, + inline_value: Option, + data_type: DataType, + def_meaning: Arc<[DefinitionInterpretation]>, + ) -> Result { + let max_rep = def_meaning.iter().filter(|d| d.is_list()).count() as u16; + let max_visible_def = def_meaning + .iter() + .take_while(|d| !d.is_list()) + .map(|d| d.num_def_levels()) + .sum(); + + let (scalar_source, rep_buf_idx, def_buf_idx) = + match (inline_value, buffer_offsets_and_sizes.len()) { + (Some(inline), 0) => (ScalarSource::Inline(inline.to_vec()), None, None), + (Some(inline), 2) => (ScalarSource::Inline(inline.to_vec()), Some(0), Some(1)), + (None, 1) => (ScalarSource::ValueBuffer(0), None, None), + (None, 3) => (ScalarSource::ValueBuffer(0), Some(1), Some(2)), + (Some(_inline), 1) => { + return Err(Error::invalid_input(format!( + "Invalid constant layout: inline_value present with {} buffers", + 1 + ))); + } + (Some(_inline), 3) => { + return Err(Error::invalid_input( + "Invalid constant layout: inline_value present with 3 buffers", + )); + } + (None, 0) => { + return Err(Error::invalid_input( + "Invalid constant layout: missing scalar source", + )); + } + (None, 2) => { + return Err(Error::invalid_input( + "Invalid constant layout: ambiguous (2 buffers and no inline_value)", + )); + } + (Some(_), n) => { + return Err(Error::invalid_input(format!( + "Invalid constant layout: inline_value present with {} buffers", + n + ))); + } + (None, n) => { + return Err(Error::invalid_input(format!( + "Invalid constant layout: unexpected buffer count {}", + n + ))); + } + }; + + Ok(Self { + buffer_offsets_and_sizes, + scalar_source, + rep_buf_idx, + def_buf_idx, + data_type, + def_meaning, + max_rep, + max_visible_def, + repdef: None, + }) + } +} + +impl crate::encodings::logical::primitive::StructuralPageScheduler for ConstantPageScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + let rep_range = self + .rep_buf_idx + .and_then(|idx| self.buffer_offsets_and_sizes.get(idx).copied()) + .filter(|(_, len)| *len > 0) + .map(|(pos, len)| pos..pos + len); + + let def_range = self + .def_buf_idx + .and_then(|idx| self.buffer_offsets_and_sizes.get(idx).copied()) + .filter(|(_, len)| *len > 0) + .map(|(pos, len)| pos..pos + len); + + let scalar_range = match self.scalar_source { + ScalarSource::ValueBuffer(idx) => { + let (pos, len) = self.buffer_offsets_and_sizes[idx]; + Some(pos..pos + len) + } + ScalarSource::Inline(_) => None, + }; + + let mut reads = Vec::with_capacity(3); + if let Some(r) = scalar_range { + reads.push(r); + } + if let Some(r) = rep_range.clone() { + reads.push(r); + } + if let Some(r) = def_range.clone() { + reads.push(r); + } + + if reads.is_empty() { + let ScalarSource::Inline(inline) = &self.scalar_source else { + return std::future::ready(Err(Error::invalid_input( + "Invalid constant layout: missing scalar source", + ))) + .boxed(); + }; + + let scalar = match lance_arrow::scalar::decode_scalar_from_inline_value( + &self.data_type, + inline.as_slice(), + ) { + Ok(s) => s, + Err(e) => return std::future::ready(Err(e.into())).boxed(), + }; + let cached = Arc::new(CachedConstantState { + scalar, + rep: None, + def: None, + }); + self.repdef = Some(cached.clone()); + return std::future::ready(Ok(cached as Arc)).boxed(); + } + + let data = io.submit_request(reads, 0); + let scalar_source = self.scalar_source.clone(); + let data_type = self.data_type.clone(); + async move { + let mut data_iter = data.await?.into_iter(); + + let scalar = match scalar_source { + ScalarSource::Inline(inline) => { + lance_arrow::scalar::decode_scalar_from_inline_value(&data_type, &inline)? + } + ScalarSource::ValueBuffer(_) => { + let bytes = data_iter.next().unwrap(); + let buf = LanceBuffer::from_bytes(bytes, 1); + lance_arrow::scalar::decode_scalar_from_value_buffer(&data_type, buf.as_ref())? + } + }; + + let rep = rep_range.map(|_| { + let rep = data_iter.next().unwrap(); + let rep = LanceBuffer::from_bytes(rep, 2); + rep.borrow_to_typed_slice::() + }); + + let def = def_range.map(|_| { + let def = data_iter.next().unwrap(); + let def = LanceBuffer::from_bytes(def, 2); + def.borrow_to_typed_slice::() + }); + + let cached = Arc::new(CachedConstantState { scalar, rep, def }); + self.repdef = Some(cached.clone()); + Ok(cached as Arc) + } + .boxed() + } + + fn load(&mut self, data: &Arc) { + self.repdef = Some( + data.clone() + .as_arc_any() + .downcast::() + .unwrap(), + ); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + _io: &Arc, + ) -> Result> { + let num_rows = ranges.iter().map(|r| r.end - r.start).sum::(); + let decoder = Box::new(ConstantPageDecoder { + ranges: VecDeque::from_iter(ranges.iter().cloned()), + scalar: self.repdef.as_ref().unwrap().scalar.clone(), + rep: self.repdef.as_ref().unwrap().rep.clone(), + def: self.repdef.as_ref().unwrap().def.clone(), + def_meaning: self.def_meaning.clone(), + max_rep: self.max_rep, + max_visible_def: self.max_visible_def, + cursor_row: 0, + cursor_level: 0, + num_rows, + }) + as Box; + Ok(vec![PageLoadTask { + decoder_fut: std::future::ready(Ok(decoder)).boxed(), + num_rows, + }]) + } +} + +#[derive(Debug)] +struct ConstantPageDecoder { + ranges: VecDeque>, + scalar: ArrayRef, + rep: Option>, + def: Option>, + def_meaning: Arc<[DefinitionInterpretation]>, + max_rep: u16, + max_visible_def: u16, + cursor_row: u64, + cursor_level: usize, + num_rows: u64, +} + +impl ConstantPageDecoder { + fn drain_ranges(&mut self, num_rows: u64) -> Vec> { + let mut rows_desired = num_rows; + let mut ranges = Vec::with_capacity(self.ranges.len()); + while rows_desired > 0 { + let front = self.ranges.front_mut().unwrap(); + let avail = front.end - front.start; + if avail > rows_desired { + ranges.push(front.start..front.start + rows_desired); + front.start += rows_desired; + rows_desired = 0; + } else { + ranges.push(self.ranges.pop_front().unwrap()); + rows_desired -= avail; + } + } + ranges + } + + fn take_row(&mut self) -> Result<(Range, u64)> { + let start = self.cursor_level; + let end = if let Some(rep) = &self.rep { + if start >= rep.len() { + return Err(Error::internal( + "Invalid constant layout: repetition buffer too short", + )); + } + if rep[start] != self.max_rep { + return Err(Error::internal( + "Invalid constant layout: row did not start at max_rep", + )); + } + let mut end = start + 1; + while end < rep.len() && rep[end] != self.max_rep { + end += 1; + } + end + } else { + start + 1 + }; + + let visible = if let Some(def) = &self.def { + def[start..end] + .iter() + .filter(|d| **d <= self.max_visible_def) + .count() as u64 + } else { + (end - start) as u64 + }; + + self.cursor_level = end; + self.cursor_row += 1; + Ok((start..end, visible)) + } + + fn skip_to_row(&mut self, target_row: u64) -> Result<()> { + while self.cursor_row < target_row { + self.take_row()?; + } + Ok(()) + } +} + +impl crate::encodings::logical::primitive::StructuralPageDecoder for ConstantPageDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + let drained_ranges = self.drain_ranges(num_rows); + + let mut level_slices: Vec> = Vec::new(); + let mut visible_items_total: u64 = 0; + + for range in drained_ranges { + self.skip_to_row(range.start)?; + for _ in range.start..range.end { + let (level_range, visible) = self.take_row()?; + visible_items_total += visible; + if let Some(last) = level_slices.last_mut() + && last.end == level_range.start + { + last.end = level_range.end; + continue; + } + level_slices.push(level_range); + } + } + + Ok(Box::new(DecodeConstantTask { + scalar: self.scalar.clone(), + rep: self.rep.clone(), + def: self.def.clone(), + level_slices, + visible_items_total, + def_meaning: self.def_meaning.clone(), + max_visible_def: self.max_visible_def, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +#[derive(Debug)] +struct DecodeConstantTask { + scalar: ArrayRef, + rep: Option>, + def: Option>, + level_slices: Vec>, + visible_items_total: u64, + def_meaning: Arc<[DefinitionInterpretation]>, + max_visible_def: u16, +} + +impl DecodeConstantTask { + fn slice_levels( + levels: &Option>, + slices: &[Range], + ) -> Option> { + levels.as_ref().map(|levels| { + let total = slices.iter().map(|r| r.end - r.start).sum(); + let mut out = Vec::with_capacity(total); + for r in slices { + out.extend(levels[r.start..r.end].iter().copied()); + } + out + }) + } + + fn materialize_values(&self, num_values: u64) -> Result { + if num_values == 0 { + return Ok(new_empty_array(self.scalar.data_type())); + } + + if let DataType::Struct(fields) = self.scalar.data_type() + && fields.is_empty() + { + return Ok(Arc::new(arrow_array::StructArray::new_empty_fields( + num_values as usize, + None, + )) as ArrayRef); + } + + let indices = arrow_array::UInt64Array::from(vec![0u64; num_values as usize]); + Ok(arrow_select::take::take( + self.scalar.as_ref(), + &indices, + None, + )?) + } +} + +impl crate::decoder::DecodePageTask for DecodeConstantTask { + fn decode(self: Box) -> Result { + let rep = Self::slice_levels(&self.rep, &self.level_slices); + let def = Self::slice_levels(&self.def, &self.level_slices); + + let visible_items_total = if let Some(def) = &def { + def.iter().filter(|d| **d <= self.max_visible_def).count() as u64 + } else { + self.visible_items_total + }; + + let values = self.materialize_values(visible_items_total)?; + let data = crate::data::DataBlock::from_array(values); + let unraveler = + RepDefUnraveler::new(rep, def, self.def_meaning.clone(), visible_items_total); + + Ok(crate::decoder::DecodedPage { + data, + repdef: unraveler, + }) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/dict.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/dict.rs new file mode 100644 index 000000000..30d79ec72 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/dict.rs @@ -0,0 +1,573 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{collections::HashMap, sync::Arc}; + +/// Bits per value for FixedWidth dictionary values (legacy default for 128-bit values) +pub const DICT_FIXED_WIDTH_BITS_PER_VALUE: u64 = 128; +/// Bits per index for dictionary indices (always i32) +pub const DICT_INDICES_BITS_PER_VALUE: u64 = 32; + +use arrow_array::{ + Array, DictionaryArray, PrimitiveArray, UInt64Array, + cast::AsArray, + types::{ + ArrowDictionaryKeyType, Int8Type, Int16Type, Int32Type, Int64Type, UInt8Type, UInt16Type, + UInt32Type, UInt64Type, + }, +}; +use arrow_buffer::ArrowNativeType; +use arrow_schema::DataType; +use arrow_select::take::TakeOptions; +use lance_core::{Error, Result, error::LanceOptionExt, utils::hash::U8SliceKey}; + +use crate::{ + buffer::LanceBuffer, + data::{BlockInfo, DataBlock, FixedWidthDataBlock, VariableWidthBlock}, + statistics::{ComputeStat, GetStat, Stat}, +}; + +// Helper function for normalize_dict_nulls +fn normalize_dict_nulls_impl( + array: Arc, +) -> Result> { + // TODO: Fast path when there is only one null index? (common case) + + let dict_array = array.as_dictionary_opt::().expect_ok()?; + + if dict_array.values().null_count() == 0 { + return Ok(array); + } + + let mut mapping = vec![None; dict_array.values().len()]; + let mut skipped = 0; + let mut valid_indices = Vec::with_capacity(dict_array.values().len()); + for (old_idx, is_valid) in dict_array.values().nulls().expect_ok()?.iter().enumerate() { + if is_valid { + // Should be safe since we are only decreasing K values (e.g. won't overflow u8 keys into u16) + mapping[old_idx] = Some(K::Native::from_usize(old_idx - skipped).expect_ok()?); + valid_indices.push(old_idx as u64); + } else { + skipped += 1; + mapping[old_idx] = None; + } + } + + let mut keys_builder = PrimitiveArray::::builder(dict_array.keys().len()); + for key in dict_array.keys().iter() { + if let Some(key) = key { + if let Some(mapped) = mapping[key.to_usize().expect_ok()?] { + // Valid item + keys_builder.append_value(mapped); + } else { + // Null via values + keys_builder.append_null(); + } + } else { + // Null via keys + keys_builder.append_null(); + } + } + let keys = keys_builder.finish(); + + let valid_indices = UInt64Array::from(valid_indices); + let values = arrow_select::take::take( + dict_array.values(), + &valid_indices, + Some(TakeOptions { + check_bounds: false, + }), + )?; + + Ok(Arc::new(DictionaryArray::new(keys, values)) as Arc) +} + +/// In Arrow a dictionary array can have nulls in two different places: +/// 1. The keys can be null +/// 2. The values can be null +/// +/// We want to normalize this so that all nulls are in the keys. This way we can store +/// the nulls with the keys as rep-def values the same as any other array. +pub fn normalize_dict_nulls(array: Arc) -> Result> { + match array.data_type() { + DataType::Dictionary(key_type, _) => match key_type.as_ref() { + DataType::UInt8 => normalize_dict_nulls_impl::(array), + DataType::UInt16 => normalize_dict_nulls_impl::(array), + DataType::UInt32 => normalize_dict_nulls_impl::(array), + DataType::UInt64 => normalize_dict_nulls_impl::(array), + DataType::Int8 => normalize_dict_nulls_impl::(array), + DataType::Int16 => normalize_dict_nulls_impl::(array), + DataType::Int32 => normalize_dict_nulls_impl::(array), + DataType::Int64 => normalize_dict_nulls_impl::(array), + _ => Err(Error::not_supported_source( + format!("Unsupported dictionary key type: {}", key_type).into(), + )), + }, + _ => Err(Error::internal(format!( + "Data type is not a dictionary: {}", + array.data_type() + ))), + } +} + +fn dict_encode_variable_width( + variable_width_data_block: &VariableWidthBlock, + bits_per_offset: u8, + max_dict_entries: u32, + max_encoded_size: usize, +) -> Option<(DataBlock, DataBlock)> +where + T: ArrowNativeType, + usize: TryFrom, +{ + use std::collections::hash_map::Entry; + let mut map = HashMap::new(); + let offsets = variable_width_data_block + .offsets + .borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + + let max_len = variable_width_data_block + .get_stat(Stat::MaxLength) + .expect("VariableWidth DataBlock should have valid `Stat::MaxLength` statistics"); + let max_len = max_len.as_primitive::().value(0); + + let max_dict_data_len = variable_width_data_block.data.len(); + let max_len: usize = max_len.try_into().unwrap_or(usize::MAX); + let dict_data_capacity = max_len + .saturating_mul(32) + .max(1024) + .min(max_dict_data_len) + .min(max_encoded_size); + + let mut dictionary_buffer: Vec = Vec::with_capacity(dict_data_capacity); + let mut dictionary_offsets_buffer = vec![T::default()]; + let mut curr_idx = 0; + let mut indices_buffer = Vec::with_capacity(variable_width_data_block.num_values as usize); + let bytes_per_offset = (bits_per_offset / 8) as usize; + + for window in offsets.windows(2) { + let start = usize::try_from(window[0]).ok()?; + let end = usize::try_from(window[1]).ok()?; + if start > end || end > variable_width_data_block.data.len() { + return None; + } + + let key = &variable_width_data_block.data[start..end]; + + let idx = match map.entry(U8SliceKey(key)) { + Entry::Occupied(entry) => *entry.get(), + Entry::Vacant(entry) => { + if max_dict_entries == 0 || curr_idx as u32 >= max_dict_entries { + return None; + } + if curr_idx == i32::MAX { + return None; + } + dictionary_buffer.extend_from_slice(key); + let dict_offset = T::from_usize(dictionary_buffer.len())?; + dictionary_offsets_buffer.push(dict_offset); + let idx = curr_idx; + entry.insert(idx); + curr_idx += 1; + idx + } + }; + + indices_buffer.push(idx); + + let indices_bytes = indices_buffer + .len() + .saturating_mul(DICT_INDICES_BITS_PER_VALUE as usize / 8); + let offsets_bytes = dictionary_offsets_buffer + .len() + .saturating_mul(bytes_per_offset); + let encoded_size = dictionary_buffer + .len() + .saturating_add(indices_bytes) + .saturating_add(offsets_bytes); + if encoded_size > max_encoded_size { + return None; + } + } + + let mut dictionary_data_block = DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::reinterpret_vec(dictionary_buffer), + offsets: LanceBuffer::reinterpret_vec(dictionary_offsets_buffer), + bits_per_offset, + num_values: curr_idx as u64, + block_info: BlockInfo::default(), + }); + dictionary_data_block.compute_stat(); + + let mut indices_data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(indices_buffer), + bits_per_value: DICT_INDICES_BITS_PER_VALUE, + num_values: variable_width_data_block.num_values, + block_info: BlockInfo::default(), + }); + indices_data_block.compute_stat(); + + Some((indices_data_block, dictionary_data_block)) +} + +/// Dictionary encodes a data block +/// +/// Currently only supported for some common cases (string / binary / 64-bit / 128-bit) +/// +/// Returns a block of indices (will always be a fixed width data block) and a block of dictionary +pub fn dictionary_encode( + data_block: &DataBlock, + max_dict_entries: u32, + max_encoded_size: usize, +) -> Option<(DataBlock, DataBlock)> { + match data_block { + DataBlock::FixedWidth(fixed_width_data_block) => { + use std::collections::hash_map::Entry; + + let bytes_per_value = match fixed_width_data_block.bits_per_value { + 64 => 8usize, + 128 => 16usize, + _ => return None, + }; + + match fixed_width_data_block.bits_per_value { + 64 => { + let mut map = HashMap::new(); + let u64_slice = fixed_width_data_block.data.borrow_to_typed_slice::(); + let u64_slice = u64_slice.as_ref(); + let mut dictionary_buffer = + Vec::with_capacity((fixed_width_data_block.num_values as usize).min(1024)); + let mut indices_buffer = + Vec::with_capacity(fixed_width_data_block.num_values as usize); + let mut curr_idx: i32 = 0; + + for &value in u64_slice.iter() { + let idx = match map.entry(value) { + Entry::Occupied(entry) => *entry.get(), + Entry::Vacant(entry) => { + if max_dict_entries == 0 || curr_idx as u32 >= max_dict_entries { + return None; + } + if curr_idx == i32::MAX { + return None; + } + dictionary_buffer.push(value); + let idx = curr_idx; + entry.insert(idx); + curr_idx += 1; + idx + } + }; + indices_buffer.push(idx); + let dict_bytes = dictionary_buffer.len().saturating_mul(bytes_per_value); + let indices_bytes = indices_buffer + .len() + .saturating_mul(DICT_INDICES_BITS_PER_VALUE as usize / 8); + let encoded_size = dict_bytes.saturating_add(indices_bytes); + if encoded_size > max_encoded_size { + return None; + } + } + + let mut dictionary_data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(dictionary_buffer), + bits_per_value: 64, + num_values: curr_idx as u64, + block_info: BlockInfo::default(), + }); + dictionary_data_block.compute_stat(); + let mut indices_data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(indices_buffer), + bits_per_value: DICT_INDICES_BITS_PER_VALUE, + num_values: fixed_width_data_block.num_values, + block_info: BlockInfo::default(), + }); + indices_data_block.compute_stat(); + + Some((indices_data_block, dictionary_data_block)) + } + 128 => { + // TODO: a follow up PR to support `FixedWidth DataBlock with bits_per_value == 256`. + let mut map = HashMap::new(); + let u128_slice = fixed_width_data_block.data.borrow_to_typed_slice::(); + let u128_slice = u128_slice.as_ref(); + let mut dictionary_buffer = + Vec::with_capacity((fixed_width_data_block.num_values as usize).min(1024)); + let mut indices_buffer = + Vec::with_capacity(fixed_width_data_block.num_values as usize); + let mut curr_idx: i32 = 0; + + for &value in u128_slice.iter() { + let idx = match map.entry(value) { + Entry::Occupied(entry) => *entry.get(), + Entry::Vacant(entry) => { + if max_dict_entries == 0 || curr_idx as u32 >= max_dict_entries { + return None; + } + if curr_idx == i32::MAX { + return None; + } + dictionary_buffer.push(value); + let idx = curr_idx; + entry.insert(idx); + curr_idx += 1; + idx + } + }; + indices_buffer.push(idx); + let dict_bytes = dictionary_buffer.len().saturating_mul(bytes_per_value); + let indices_bytes = indices_buffer + .len() + .saturating_mul(DICT_INDICES_BITS_PER_VALUE as usize / 8); + let encoded_size = dict_bytes.saturating_add(indices_bytes); + if encoded_size > max_encoded_size { + return None; + } + } + + let mut dictionary_data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(dictionary_buffer), + bits_per_value: DICT_FIXED_WIDTH_BITS_PER_VALUE, + num_values: curr_idx as u64, + block_info: BlockInfo::default(), + }); + dictionary_data_block.compute_stat(); + let mut indices_data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(indices_buffer), + bits_per_value: DICT_INDICES_BITS_PER_VALUE, + num_values: fixed_width_data_block.num_values, + block_info: BlockInfo::default(), + }); + indices_data_block.compute_stat(); + + Some((indices_data_block, dictionary_data_block)) + } + _ => None, + } + } + DataBlock::VariableWidth(variable_width_data_block) => { + match variable_width_data_block.bits_per_offset { + 32 => dict_encode_variable_width::( + variable_width_data_block, + 32, + max_dict_entries, + max_encoded_size, + ), + 64 => dict_encode_variable_width::( + variable_width_data_block, + 64, + max_dict_entries, + max_encoded_size, + ), + _ => None, + } + } + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + buffer::LanceBuffer, + data::{BlockInfo, FixedWidthDataBlock}, + }; + use arrow_array::{Array, StringArray}; + use std::sync::Arc; + + #[test] + fn test_dictionary_encode_abort_fixed_width() { + // Create a u128 block with very high cardinality where dict encoding + // would result in larger data (dictionary overhead + indices > original) + let num_values = 120u64; + + // Create actual data: each value is unique u128 so dictionary encode will not be helpful + let mut data = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + data.push(i as u128); + } + + let mut data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: DICT_FIXED_WIDTH_BITS_PER_VALUE, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }); + + // Compute stats naturally + data_block.compute_stat(); + + // Dictionary encoding should abort and return None + let max_encoded_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + let result = dictionary_encode(&data_block, 1000, max_encoded_size); + assert!( + result.is_none(), + "Dictionary encoding should abort for high cardinality u128 data" + ); + } + + #[test] + fn test_dictionary_encode_success_fixed_width() { + // Create a u128 block with low cardinality where dict encoding helps + let num_values = 120u64; + let cardinality = 3u64; + + // Create data with few unique u128 values + let mut data = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + data.push((i % cardinality) as u128); + } + + let mut data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: DICT_FIXED_WIDTH_BITS_PER_VALUE, + data: LanceBuffer::reinterpret_vec(data), + num_values, + block_info: BlockInfo::default(), + }); + + // Compute stats naturally + data_block.compute_stat(); + + // Dictionary encoding should succeed and return Some + let max_encoded_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + let result = dictionary_encode(&data_block, 1000, max_encoded_size); + assert!( + result.is_some(), + "Dictionary encoding should succeed for low cardinality u128 data" + ); + + if let Some((indices, dictionary)) = result { + // Verify indices block + if let DataBlock::FixedWidth(indices_block) = indices { + assert_eq!(indices_block.num_values, num_values); + assert_eq!(indices_block.bits_per_value, DICT_INDICES_BITS_PER_VALUE); + } else { + panic!("Expected FixedWidth indices block"); + } + + // Verify dictionary block + if let DataBlock::FixedWidth(dict_block) = dictionary { + assert_eq!(dict_block.num_values, cardinality); + assert_eq!(dict_block.bits_per_value, DICT_FIXED_WIDTH_BITS_PER_VALUE); + } else { + panic!("Expected FixedWidth dictionary block"); + } + } + } + + #[test] + fn test_dictionary_encode_abort_variable_width() { + // Create a variable-width block with high cardinality where dict encoding + // won't provide sufficient benefit + let num_values = 120u64; + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + values.push(format!("unique_value_{:04}", i)); + } + let array = StringArray::from(values); + // from_array already computes stats + let data_block = DataBlock::from_array(Arc::new(array) as Arc); + + // Dictionary encoding should abort and return None + let max_encoded_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + let result = dictionary_encode(&data_block, 10, max_encoded_size); + assert!( + result.is_none(), + "Dictionary encoding should abort for high cardinality string data" + ); + } + + #[test] + fn test_dictionary_encode_success_low_cardinality() { + // Create a variable-width block with low cardinality where dict encoding helps + let num_values = 120u64; + let cardinality = 3u64; + + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + values.push(format!("value_{}", i % cardinality)); + } + + let array = StringArray::from(values); + let data_block = DataBlock::from_array(Arc::new(array) as Arc); + + // Dictionary encoding should succeed and return Some + let max_encoded_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + let result = dictionary_encode(&data_block, 100, max_encoded_size); + assert!( + result.is_some(), + "Dictionary encoding should succeed for low cardinality data" + ); + + if let Some((indices, dictionary)) = result { + // Verify indices block + if let DataBlock::FixedWidth(indices_block) = indices { + assert_eq!(indices_block.num_values, num_values); + assert_eq!(indices_block.bits_per_value, DICT_INDICES_BITS_PER_VALUE); + } else { + panic!("Expected FixedWidth indices block"); + } + + // Verify dictionary block + if let DataBlock::VariableWidth(dict_block) = dictionary { + assert_eq!(dict_block.num_values, cardinality); + } else { + panic!("Expected VariableWidth dictionary block"); + } + } + } + + #[test] + fn test_dictionary_encode_invalid_offset_width_returns_none() { + let array = StringArray::from(vec!["a", "b", "c", "a"]); + let data_block = DataBlock::from_array(Arc::new(array) as Arc); + let invalid_block = match data_block { + DataBlock::VariableWidth(mut var) => { + var.bits_per_offset = 16; + DataBlock::VariableWidth(var) + } + other => panic!("Expected VariableWidth data block, got {:?}", other), + }; + let max_encoded_size = usize::try_from(invalid_block.data_size()).unwrap_or(usize::MAX); + assert!(dictionary_encode(&invalid_block, 100, max_encoded_size).is_none()); + } + + #[test] + fn test_dictionary_encode_respects_size_limit() { + let num_values = 10_000u64; + let cardinality = 50u64; + + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + values.push(format!("value_{:08}", i % cardinality)); + } + + let array = StringArray::from(values); + let data_block = DataBlock::from_array(Arc::new(array) as Arc); + + let full_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + let too_small_limit = full_size / 10; + assert!(dictionary_encode(&data_block, 1000, too_small_limit).is_none()); + assert!(dictionary_encode(&data_block, 1000, full_size).is_some()); + } + + #[test] + fn test_dictionary_encode_respects_entry_limit() { + let num_values = 10_000u64; + let cardinality = 200u64; + + let mut values = Vec::with_capacity(num_values as usize); + for i in 0..num_values { + values.push(format!("value_{:08}", i % cardinality)); + } + + let array = StringArray::from(values); + let data_block = DataBlock::from_array(Arc::new(array) as Arc); + + let max_encoded_size = usize::try_from(data_block.data_size()).unwrap_or(usize::MAX); + assert!(dictionary_encode(&data_block, 10, max_encoded_size).is_none()); + assert!(dictionary_encode(&data_block, 500, max_encoded_size).is_some()); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/fullzip.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/fullzip.rs new file mode 100644 index 000000000..aaafa1887 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/fullzip.rs @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Routines for encoding and decoding full-zip data +//! +//! Full-zip is one of the two structural encodings in Lance 2.1. +//! In this approach the various compressed buffers are zipped +//! together so that all parts of a value are stored contiguously in memory. +//! +//! This requires transparent compression and is most suitable for +//! large data types. + +use crate::{ + data::{DataBlock, FixedWidthDataBlock, VariableWidthBlock}, + format::pb21::CompressiveEncoding, +}; + +use lance_core::Result; + +/// Per-value compression must either: +/// +/// A single buffer of fixed-width values +/// A single buffer of value data and a buffer of offsets +/// +/// TODO: In the future we may allow metadata buffers +#[derive(Debug)] +pub enum PerValueDataBlock { + Fixed(FixedWidthDataBlock), + Variable(VariableWidthBlock), +} + +impl PerValueDataBlock { + pub fn data_size(&self) -> u64 { + match self { + Self::Fixed(fixed) => fixed.data_size(), + Self::Variable(variable) => variable.data_size(), + } + } +} + +/// Trait for compression algorithms that are suitable for use in the zipped structural encoding +/// +/// This compression must return either a FixedWidthDataBlock or a VariableWidthBlock. This is because +/// we need to zip the data and those are the only two blocks we know how to zip today. +/// +/// In addition, the compressed data must be able to be decompressed in a random-access fashion. +/// This means that the decompression algorithm must be able to decompress any value without +/// decompressing all values before it. +pub trait PerValueCompressor: std::fmt::Debug + Send + Sync { + /// Compress the data into a single buffer + /// + /// Also returns a description of the compression that can be used to decompress when reading the data back + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)>; +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/layout.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/layout.rs new file mode 100644 index 000000000..95df12ede --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/layout.rs @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use lance_core::Result; + +use crate::repdef::MiniBlockRepDefBudget; + +/// Runs automatic sparse planning only after the dense mini-block budget makes it useful. +pub(super) fn select_automatic_sparse( + requested_encoding: Option<&str>, + dense_budget: &MiniBlockRepDefBudget, + candidate: impl FnOnce() -> Result>, +) -> Result> { + if requested_encoding.is_some() + || !matches!( + dense_budget, + MiniBlockRepDefBudget::RequiresPageSplit(_) + | MiniBlockRepDefBudget::SingleRowOverBudget(_) + ) + { + return Ok(None); + } + candidate() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::constants::{ + STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_SPARSE, + }; + + #[test] + fn within_budget_does_not_construct_sparse_candidate() { + let selected = + select_automatic_sparse::<()>(None, &MiniBlockRepDefBudget::WithinBudget, || { + panic!("within-budget pages must not construct sparse candidates") + }) + .unwrap(); + assert!(selected.is_none()); + } + + #[test] + fn over_budget_selects_only_eligible_candidates() { + let split = MiniBlockRepDefBudget::RequiresPageSplit(Vec::new()); + let selected = select_automatic_sparse(None, &split, || Ok(Some(42))).unwrap(); + assert_eq!(selected, Some(42)); + + let ineligible = select_automatic_sparse::<()>(None, &split, || Ok(None)).unwrap(); + assert!(ineligible.is_none()); + + let unsplittable = MiniBlockRepDefBudget::SingleRowOverBudget(70_000); + let selected = select_automatic_sparse(None, &unsplittable, || Ok(Some(7))).unwrap(); + assert_eq!(selected, Some(7)); + } + + #[test] + fn explicit_modes_and_lance_2_2_do_not_auto_select() { + let split = MiniBlockRepDefBudget::RequiresPageSplit(Vec::new()); + for requested in [ + STRUCTURAL_ENCODING_MINIBLOCK, + STRUCTURAL_ENCODING_FULLZIP, + STRUCTURAL_ENCODING_SPARSE, + ] { + let selected = select_automatic_sparse::<()>(Some(requested), &split, || { + panic!("explicit modes must not invoke automatic sparse planning") + }) + .unwrap(); + assert!(selected.is_none()); + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/miniblock.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/miniblock.rs new file mode 100644 index 000000000..0ee408a85 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/miniblock.rs @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Routines for encoding and decoding miniblock data +//! +//! Miniblock encoding is one of the two structural encodings in Lance 2.1. +//! In this approach the data is compressed into a series of chunks put into +//! a single buffer. +//! +//! A chunk must be encoded or decoded as a unit. There is a small amount of +//! chunk metadata such as the number and size of each buffer in the chunk. +//! +//! Any form of compression can be used since we are compressing and decompressing +//! entire chunks. +use crate::{buffer::LanceBuffer, data::DataBlock, format::pb21::CompressiveEncoding}; + +use lance_core::Result; + +pub const MAX_MINIBLOCK_BYTES: u64 = 8 * 1024 - 6; + +const DEFAULT_MAX_MINIBLOCK_VALUES: u64 = 4096; +/// Maximum number of values that any mini-block decoder accepts from page metadata. +pub(crate) const MAX_CONFIGURABLE_MINIBLOCK_VALUES: u64 = 32768; + +fn parse_max_miniblock_values() -> u64 { + let val = std::env::var("LANCE_MINIBLOCK_MAX_VALUES") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(DEFAULT_MAX_MINIBLOCK_VALUES); + val.clamp(1, MAX_CONFIGURABLE_MINIBLOCK_VALUES) +} + +pub static MAX_MINIBLOCK_VALUES: std::sync::LazyLock = + std::sync::LazyLock::new(parse_max_miniblock_values); + +/// Maximum number of rep/def levels the structural planner should place into +/// a single mini-block chunk. +pub fn max_repdef_levels_per_chunk(bits_per_level: u64) -> u64 { + debug_assert!(bits_per_level > 0); + const REPDEF_BUDGET_BITS: u64 = 16 * 1024 * 8; + let budgeted_levels = REPDEF_BUDGET_BITS / bits_per_level; + budgeted_levels.min(u16::MAX as u64) +} + +/// Page data that has been compressed into a series of chunks put into +/// a single buffer. +#[derive(Debug)] +pub struct MiniBlockCompressed { + /// The buffers of compressed data + pub data: Vec, + /// Describes the size of each chunk + pub chunks: Vec, + /// The number of values in the entire page + pub num_values: u64, +} + +/// Per-page framing details that can affect a mini-block compressor's choice. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct MiniBlockCompressionContext { + common_chunk_buffers: u64, + support_large_chunk: bool, + allow_generic_offsets: bool, +} + +impl MiniBlockCompressionContext { + /// Creates the framing context supplied by the owning mini-block page. + pub fn new( + common_chunk_buffers: u64, + support_large_chunk: bool, + allow_generic_offsets: bool, + ) -> Self { + Self { + common_chunk_buffers, + support_large_chunk, + allow_generic_offsets, + } + } +} + +/// Describes the size of a mini-block chunk of data +/// +/// Mini-block chunks are designed to be small (just a few disk sectors) +/// and contain a power-of-two number of values (except for the last chunk) +/// +/// By default we limit a chunk to 4Ki values and slightly less than +/// 8KiB of compressed value data. The byte budget remains the primary +/// constraint, so only encodings that compress many values into that +/// budget can use larger value counts when explicitly configured. +/// +/// The maximum number of values per chunk can be configured via the +/// `LANCE_MINIBLOCK_MAX_VALUES` environment variable. This is only +/// useful in extremely bandwidth-limited environments; the default is +/// appropriate for local disks and same-region cloud object storage. +#[derive(Debug)] +pub struct MiniBlockChunk { + // The size in bytes of each buffer in the chunk. + // + // In Lance 2.1, the chunk size is limited to 32KiB, so only 16-bits are used. + // Since Lance 2.2, the chunk size uses u32 to support larger chunk size + pub buffer_sizes: Vec, + // The log (base 2) of the number of values in the chunk. If this is the final chunk + // then this should be 0 (the number of values will be calculated by subtracting the + // size of all other chunks from the total size of the page) + // + // For example, 1 would mean there are 2 values in the chunk and 15 would mean there + // are 32Ki values in the chunk. + // + // This must be <= log2(MAX_MINIBLOCK_VALUES) (i.e. <= 12 at the default of 4096) + pub log_num_values: u8, +} + +impl MiniBlockChunk { + /// Gets the number of values in this block + /// + /// This requires `vals_in_prev_blocks` and `total_num_values` because the + /// last block in a page is a special case which stores 0 for log_num_values + /// and, in that case, the number of values is determined by subtracting + /// `vals_in_prev_blocks` from `total_num_values` + pub fn num_values(&self, vals_in_prev_blocks: u64, total_num_values: u64) -> u64 { + if self.log_num_values == 0 { + total_num_values - vals_in_prev_blocks + } else { + 1 << self.log_num_values + } + } +} + +/// Trait for compression algorithms that are suitable for use in the miniblock structural encoding +/// +/// These compression algorithms should be capable of encoding the data into small chunks +/// where each chunk (except the last) has 2^N values (N can vary between chunks) +pub trait MiniBlockCompressor: std::fmt::Debug + Send + Sync { + /// Compress a `page` of data into multiple chunks + /// + /// See [`MiniBlockCompressed`] for details on how chunks should be sized. + /// + /// This method also returns a description of the encoding applied that will be + /// used at decode time to read the data. + fn compress( + &self, + context: MiniBlockCompressionContext, + page: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)>; +} + +#[cfg(test)] +mod tests { + use serial_test::serial; + + use super::*; + + #[test] + #[serial] + fn test_parse_default() { + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + assert_eq!(parse_max_miniblock_values(), 4096); + } + + #[test] + #[serial] + fn test_parse_custom_value() { + unsafe { std::env::set_var("LANCE_MINIBLOCK_MAX_VALUES", "256") }; + assert_eq!(parse_max_miniblock_values(), 256); + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + } + + #[test] + #[serial] + fn test_parse_can_raise_to_32k() { + unsafe { std::env::set_var("LANCE_MINIBLOCK_MAX_VALUES", "32768") }; + assert_eq!(parse_max_miniblock_values(), 32768); + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + } + + #[test] + #[serial] + fn test_parse_clamps_zero_to_one() { + unsafe { std::env::set_var("LANCE_MINIBLOCK_MAX_VALUES", "0") }; + assert_eq!(parse_max_miniblock_values(), 1); + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + } + + #[test] + #[serial] + fn test_parse_clamps_above_max() { + unsafe { std::env::set_var("LANCE_MINIBLOCK_MAX_VALUES", "99999") }; + assert_eq!( + parse_max_miniblock_values(), + MAX_CONFIGURABLE_MINIBLOCK_VALUES + ); + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + } + + #[test] + #[serial] + fn test_parse_invalid_falls_back_to_default() { + unsafe { std::env::set_var("LANCE_MINIBLOCK_MAX_VALUES", "not_a_number") }; + assert_eq!(parse_max_miniblock_values(), DEFAULT_MAX_MINIBLOCK_VALUES); + unsafe { std::env::remove_var("LANCE_MINIBLOCK_MAX_VALUES") }; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse.rs new file mode 100644 index 000000000..25cb85f49 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse.rs @@ -0,0 +1,5614 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use super::*; +use arrow_array::new_empty_array; +use arrow_buffer::ArrowNativeType; + +fn invalid_enum( + value: std::result::Result, + label: &str, +) -> Result { + value.map_err(|error| { + Error::invalid_input_source( + format!("Sparse structural {label} has an invalid enum value: {error}").into(), + ) + }) +} + +pub(super) mod writer; + +fn usize_from_u64(value: u64, label: &str) -> Result { + usize::try_from(value).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} {value} exceeds usize::MAX").into(), + ) + }) +} + +/// Native sparse structural representation used by the 2.3 sparse layout. +/// +/// Layers are stored from outer-most to inner-most, matching the order Arrow structural +/// encoders record offsets and validity. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SparseStructuralPlan { + pub(crate) layers: Vec, + pub(crate) num_items: u64, + pub(crate) num_visible_items: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SparsePositionSet { + Empty, + All { len: u64 }, + Range { start: u64, len: u64 }, + Explicit(Vec), +} + +impl SparsePositionSet { + pub(crate) fn from_positions( + positions: Vec, + domain_len: u64, + label: &str, + ) -> Result { + if positions.is_empty() { + return Ok(Self::Empty); + } + for window in positions.windows(2) { + let [previous, current] = window else { + continue; + }; + if previous >= current { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} positions must be strictly increasing") + .into(), + )); + } + } + if let Some(position) = positions.iter().find(|position| **position >= domain_len) { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} position {} is outside layer with {} slots", + position, domain_len + ) + .into(), + )); + } + + let len = u64::try_from(positions.len()).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} position count exceeds u64::MAX").into(), + ) + })?; + let first = positions.first().copied().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} positions are unexpectedly empty").into(), + ) + })?; + let last = positions.last().copied().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} positions are unexpectedly empty").into(), + ) + })?; + if first == 0 && len == domain_len && domain_len > 0 && last == domain_len - 1 { + return Ok(Self::All { len: domain_len }); + } + if last - first + 1 == len { + return Ok(Self::Range { start: first, len }); + } + Ok(Self::Explicit(positions)) + } + + pub(crate) fn empty() -> Self { + Self::Empty + } + + pub(crate) fn all(len: u64) -> Self { + if len == 0 { + Self::Empty + } else { + Self::All { len } + } + } + + pub(crate) fn range(start: u64, len: u64) -> Self { + if len == 0 { + Self::Empty + } else { + Self::Range { start, len } + } + } + + pub(crate) fn len(&self) -> u64 { + match self { + Self::Empty => 0, + Self::All { len } | Self::Range { len, .. } => *len, + Self::Explicit(positions) => positions.len() as u64, + } + } + + pub(crate) fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub(crate) fn deep_size(&self) -> usize { + match self { + Self::Explicit(positions) => positions.len() * std::mem::size_of::(), + Self::Empty | Self::All { .. } | Self::Range { .. } => 0, + } + } + + pub(crate) fn materialize(&self) -> Result> { + match self { + Self::Empty => Ok(Vec::new()), + Self::All { len } => Self::materialize_range(0, *len), + Self::Range { start, len } => Self::materialize_range(*start, *len), + Self::Explicit(positions) => Ok(positions.clone()), + } + } + + fn materialize_range(start: u64, len: u64) -> Result> { + let len_usize = usize::try_from(len).map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse structural position range length {} exceeds usize::MAX", + len + ) + .into(), + ) + })?; + let end = start.checked_add(len).ok_or_else(|| { + Error::invalid_input_source("Sparse structural position range overflows".into()) + })?; + let mut positions = Vec::with_capacity(len_usize); + positions.extend(start..end); + Ok(positions) + } + + fn contains(&self, position: u64) -> bool { + match self { + Self::Empty => false, + Self::All { len } => position < *len, + Self::Range { start, len } => { + position >= *start && position < start.saturating_add(*len) + } + Self::Explicit(positions) => positions.binary_search(&position).is_ok(), + } + } + + fn is_subset_of(&self, other: &Self, domain_len: u64) -> Result { + self.validate_domain(domain_len, "subset")?; + other.validate_domain(domain_len, "superset")?; + Ok(match self { + Self::Empty => true, + Self::All { .. } => other.len() == domain_len, + Self::Range { start, len } => { + let end = start.checked_add(*len).ok_or_else(|| { + Error::invalid_input_source("Sparse structural subset range overflows".into()) + })?; + match other { + Self::All { .. } => true, + Self::Range { + start: other_start, + len: other_len, + } => { + let other_end = other_start.saturating_add(*other_len); + *start >= *other_start && end <= other_end + } + Self::Explicit(positions) => { + let first = positions.partition_point(|position| *position < *start); + let last = positions.partition_point(|position| *position < end); + u64::try_from(last.saturating_sub(first)).ok() == Some(*len) + } + Self::Empty => false, + } + } + Self::Explicit(positions) => positions.iter().all(|position| other.contains(*position)), + }) + } + + fn is_disjoint(&self, other: &Self, domain_len: u64) -> Result { + self.validate_domain(domain_len, "first disjoint set")?; + other.validate_domain(domain_len, "second disjoint set")?; + let (smaller, larger) = if self.len() <= other.len() { + (self, other) + } else { + (other, self) + }; + Ok(match smaller { + Self::Empty => true, + Self::All { .. } => larger.is_empty(), + Self::Range { start, len } => { + let end = start.saturating_add(*len); + match larger { + Self::Empty => true, + Self::All { .. } => false, + Self::Range { + start: other_start, + len: other_len, + } => end <= *other_start || other_start.saturating_add(*other_len) <= *start, + Self::Explicit(positions) => { + let index = positions.partition_point(|position| *position < *start); + positions.get(index).is_none_or(|position| *position >= end) + } + } + } + Self::Explicit(positions) => { + positions.iter().all(|position| !larger.contains(*position)) + } + }) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SparseValidityMeaning { + NullPositions, + ValidPositions, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SparseValiditySet { + pub(crate) meaning: SparseValidityMeaning, + pub(crate) positions: SparsePositionSet, +} + +impl SparseValiditySet { + pub(crate) fn deep_size(&self) -> usize { + self.positions.deep_size() + } + + fn contains_only_valid_positions( + &self, + positions: &SparsePositionSet, + num_slots: u64, + ) -> Result { + match self.meaning { + SparseValidityMeaning::NullPositions => { + positions.is_disjoint(&self.positions, num_slots) + } + SparseValidityMeaning::ValidPositions => { + positions.is_subset_of(&self.positions, num_slots) + } + } + } + + fn append_to(&self, validity: &mut BooleanBufferBuilder, num_slots: u64) -> Result<()> { + self.positions.validate_domain(num_slots, "validity")?; + let num_slots_usize = usize_from_u64(num_slots, "validity slot count")?; + match (self.meaning, &self.positions) { + (SparseValidityMeaning::NullPositions, SparsePositionSet::Empty) => { + validity.append_n(num_slots_usize, true); + } + (SparseValidityMeaning::ValidPositions, SparsePositionSet::Empty) => { + validity.append_n(num_slots_usize, false); + } + (SparseValidityMeaning::NullPositions, SparsePositionSet::All { .. }) => { + validity.append_n(num_slots_usize, false); + } + (SparseValidityMeaning::ValidPositions, SparsePositionSet::All { .. }) => { + validity.append_n(num_slots_usize, true); + } + (meaning, SparsePositionSet::Range { start, len }) => { + let range_end = start.checked_add(*len).ok_or_else(|| { + Error::invalid_input_source("Sparse structural validity range overflows".into()) + })?; + let default_valid = matches!(meaning, SparseValidityMeaning::NullPositions); + let range_valid = !default_valid; + validity.append_n( + usize_from_u64(*start, "validity range start")?, + default_valid, + ); + validity.append_n(usize_from_u64(*len, "validity range length")?, range_valid); + validity.append_n( + usize_from_u64(num_slots - range_end, "validity range tail")?, + default_valid, + ); + } + (_, SparsePositionSet::Explicit(_)) => { + let mut cursor = SparseValidityCursor::new(self, num_slots, "validity")?; + for slot in 0..num_slots { + validity.append(cursor.is_valid(slot)?); + } + cursor.finish()?; + } + } + Ok(()) + } +} + +impl SparsePositionSet { + fn validate_domain(&self, domain_len: u64, label: &str) -> Result<()> { + match self { + Self::Empty => {} + Self::All { len } => { + if *len != domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} all set length {} does not match domain {}", + len, domain_len + ) + .into(), + )); + } + } + Self::Range { start, len } => { + let end = start.checked_add(*len).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} range overflows").into(), + ) + })?; + if end > domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} range {}..{} is outside domain {}", + start, end, domain_len + ) + .into(), + )); + } + } + Self::Explicit(positions) => { + for window in positions.windows(2) { + let [previous, current] = window else { + continue; + }; + if previous >= current { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} positions must be strictly increasing" + ) + .into(), + )); + } + } + if let Some(position) = positions.last() + && *position >= domain_len + { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} position {} is outside layer with {} slots", + position, domain_len + ) + .into(), + )); + } + } + } + Ok(()) + } +} + +struct SparsePositionSetCursor<'a> { + set: &'a SparsePositionSet, + explicit: Option>>, +} + +impl<'a> SparsePositionSetCursor<'a> { + fn new(set: &'a SparsePositionSet, domain_len: u64, label: &str) -> Result { + set.validate_domain(domain_len, label)?; + let explicit = match set { + SparsePositionSet::Explicit(positions) => Some(positions.iter().peekable()), + _ => None, + }; + Ok(Self { set, explicit }) + } + + fn contains(&mut self, slot: u64) -> Result { + Ok(match self.set { + SparsePositionSet::Empty => false, + SparsePositionSet::All { .. } => true, + SparsePositionSet::Range { start, len } => { + slot >= *start && slot < start.saturating_add(*len) + } + SparsePositionSet::Explicit(_) => { + let iter = self.explicit.as_mut().ok_or_else(|| { + Error::internal("Sparse structural explicit cursor is missing".to_string()) + })?; + if let Some(position) = iter.peek() + && **position < slot + { + return Err(Error::invalid_input_source( + format!( + "Sparse structural explicit position {} was skipped before slot {}", + **position, slot + ) + .into(), + )); + } + if iter.peek().is_some_and(|position| **position == slot) { + iter.next(); + true + } else { + false + } + } + }) + } + + fn finish(&mut self) -> Result<()> { + if let Some(iter) = self.explicit.as_mut() + && let Some(position) = iter.next() + { + return Err(Error::invalid_input_source( + format!( + "Sparse structural explicit position {} was not consumed", + position + ) + .into(), + )); + } + Ok(()) + } +} + +struct SparseValidityCursor<'a> { + meaning: SparseValidityMeaning, + positions: SparsePositionSetCursor<'a>, +} + +impl<'a> SparseValidityCursor<'a> { + fn new(validity: &'a SparseValiditySet, domain_len: u64, label: &str) -> Result { + Ok(Self { + meaning: validity.meaning, + positions: SparsePositionSetCursor::new(&validity.positions, domain_len, label)?, + }) + } + + fn is_valid(&mut self, slot: u64) -> Result { + let is_stored = self.positions.contains(slot)?; + Ok(match self.meaning { + SparseValidityMeaning::NullPositions => !is_stored, + SparseValidityMeaning::ValidPositions => is_stored, + }) + } + + fn finish(&mut self) -> Result<()> { + self.positions.finish() + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SparseCountSet { + Empty, + Constant { + value: u64, + len: u64, + }, + Explicit { + counts: Arc<[u64]>, + offsets: Arc<[u64]>, + }, +} + +impl SparseCountSet { + pub(crate) fn from_counts(counts: Vec) -> Result { + if counts.is_empty() { + return Ok(Self::Empty); + } + if let Some(first) = counts.first().copied() + && counts.iter().all(|count| *count == first) + { + return Ok(Self::Constant { + value: first, + len: counts.len() as u64, + }); + } + let offsets = offsets_from_counts(&counts)?; + Ok(Self::Explicit { + counts: counts.into(), + offsets: offsets.into(), + }) + } + + pub(crate) fn constant(value: u64, len: u64) -> Self { + if len == 0 { + Self::Empty + } else { + Self::Constant { value, len } + } + } + + pub(crate) fn len(&self) -> u64 { + match self { + Self::Empty => 0, + Self::Constant { len, .. } => *len, + Self::Explicit { counts, .. } => counts.len() as u64, + } + } + + pub(crate) fn deep_size(&self) -> usize { + match self { + Self::Explicit { counts, offsets } => { + counts.len() * std::mem::size_of::() + + offsets.len() * std::mem::size_of::() + } + Self::Empty | Self::Constant { .. } => 0, + } + } + + pub(crate) fn materialize(&self) -> Result> { + match self { + Self::Empty => Ok(Vec::new()), + Self::Constant { value, len } => { + let len = usize::try_from(*len).map_err(|_| { + Error::invalid_input_source( + "Sparse structural count set length exceeds usize::MAX".into(), + ) + })?; + Ok(vec![*value; len]) + } + Self::Explicit { counts, .. } => Ok(counts.to_vec()), + } + } + + pub(crate) fn sum(&self) -> Result { + match self { + Self::Empty => Ok(0), + Self::Constant { value, len } => value.checked_mul(*len).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural constant count sum overflows: value={}, len={}", + value, len + ) + .into(), + ) + }), + Self::Explicit { offsets, .. } => offsets.last().copied().ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural explicit count offsets are empty".into(), + ) + }), + } + } + + fn validate_positive(&self) -> Result<()> { + let has_zero = match self { + Self::Empty => false, + Self::Constant { value, .. } => *value == 0, + Self::Explicit { counts, .. } => counts.contains(&0), + }; + if has_zero { + return Err(Error::invalid_input_source( + "Sparse structural non-empty list count is zero".into(), + )); + } + Ok(()) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SparseStructuralLayerPlan { + Validity { + num_slots: u64, + validity: SparseValiditySet, + }, + List { + num_slots: u64, + num_child_slots: u64, + non_empty_positions: SparsePositionSet, + counts: SparseCountSet, + validity: SparseValiditySet, + }, + FixedSizeList { + num_slots: u64, + dimension: u64, + validity: SparseValiditySet, + }, +} + +impl SparseStructuralPlan { + fn expected_num_items( + layers: &[SparseStructuralLayerPlan], + num_visible_items: u64, + ) -> Result { + layers.iter().try_fold(num_visible_items, |items, layer| { + let additional = match layer { + SparseStructuralLayerPlan::List { + num_slots, + non_empty_positions, + .. + } => num_slots + .checked_sub(non_empty_positions.len()) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list has more non-empty positions than slots".into(), + ) + })?, + SparseStructuralLayerPlan::Validity { .. } + | SparseStructuralLayerPlan::FixedSizeList { .. } => 0, + }; + items.checked_add(additional).ok_or_else(|| { + Error::invalid_input_source("Sparse structural item count overflows".into()) + }) + }) + } + + fn validate(&self, row_domain: u64) -> Result<()> { + usize_from_u64(self.num_visible_items, "visible item count")?; + let expected_num_items = Self::expected_num_items(&self.layers, self.num_visible_items)?; + if self.num_items != expected_num_items { + return Err(Error::invalid_input_source( + format!( + "Sparse structural item count {} does not match the {} items implied by its layers", + self.num_items, expected_num_items + ) + .into(), + )); + } + let mut expected_slots = row_domain; + for (layer_index, layer) in self.layers.iter().enumerate() { + let (num_slots, num_child_slots, validity) = match layer { + SparseStructuralLayerPlan::Validity { + num_slots, + validity, + } => (*num_slots, *num_slots, validity), + SparseStructuralLayerPlan::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + } => { + counts.validate_positive()?; + if non_empty_positions.len() != counts.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list layer {} has {} non-empty positions but {} counts", + layer_index, + non_empty_positions.len(), + counts.len() + ) + .into(), + )); + } + if counts.sum()? != *num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list layer {} count sum does not match {} child slots", + layer_index, num_child_slots + ) + .into(), + )); + } + if !validity.contains_only_valid_positions(non_empty_positions, *num_slots)? { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list layer {} contains a non-empty null slot", + layer_index + ) + .into(), + )); + } + (*num_slots, *num_child_slots, validity) + } + SparseStructuralLayerPlan::FixedSizeList { + num_slots, + dimension, + validity, + } => { + if *dimension == 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list layer {} has dimension zero", + layer_index + ) + .into(), + )); + } + let num_child_slots = num_slots.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural fixed-size-list child domain overflows".into(), + ) + })?; + (*num_slots, num_child_slots, validity) + } + }; + usize_from_u64(num_slots, "layer slot count")?; + usize_from_u64(num_child_slots, "layer child slot count")?; + if num_slots != expected_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural layer {} has {} slots, expected {}", + layer_index, num_slots, expected_slots + ) + .into(), + )); + } + validity.positions.validate_domain(num_slots, "validity")?; + expected_slots = num_child_slots; + } + if expected_slots != self.num_visible_items { + return Err(Error::invalid_input_source( + format!( + "Sparse structural terminal domain has {} slots, expected {} visible items", + expected_slots, self.num_visible_items + ) + .into(), + )); + } + Ok(()) + } +} + +#[derive(Debug)] +pub struct SparseStructuralUnraveler { + layers: Vec, + next_layer: usize, + pending_fixed_size_list: bool, +} + +impl SparseStructuralUnraveler { + pub(crate) fn new(plan: SparseStructuralPlan) -> Self { + let next_layer = plan.layers.len(); + Self { + layers: plan.layers, + next_layer, + pending_fixed_size_list: false, + } + } + + fn current_layer(&self) -> Option<&SparseStructuralLayerPlan> { + self.next_layer + .checked_sub(1) + .and_then(|idx| self.layers.get(idx)) + } + + fn consume_current_layer(&mut self) -> Result<()> { + self.next_layer = self.next_layer.checked_sub(1).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural metadata has fewer layers than the Arrow schema".into(), + ) + })?; + Ok(()) + } + + pub(crate) fn ensure_exhausted(&self) -> Result<()> { + if self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse structural metadata has an unconsumed fixed-size-list layer".into(), + )); + } + if self.next_layer != 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural metadata has {} unconsumed layer(s)", + self.next_layer + ) + .into(), + )); + } + Ok(()) + } + + pub(crate) fn is_all_valid(&self) -> bool { + match self.current_layer() { + Some(SparseStructuralLayerPlan::Validity { + num_slots, + validity, + }) + | Some(SparseStructuralLayerPlan::FixedSizeList { + num_slots, + validity, + .. + }) + | Some(SparseStructuralLayerPlan::List { + num_slots, + validity, + .. + }) => match validity.meaning { + SparseValidityMeaning::NullPositions => validity.positions.is_empty(), + SparseValidityMeaning::ValidPositions => validity.positions.len() == *num_slots, + }, + None => true, + } + } + + pub(crate) fn max_lists(&self) -> Result { + match self.current_layer() { + Some(SparseStructuralLayerPlan::List { num_slots, .. }) => { + usize_from_u64(*num_slots, "list slot count") + } + _ => Ok(0), + } + } + + pub(crate) fn skip_validity(&mut self) -> Result<()> { + match self.current_layer() { + Some(SparseStructuralLayerPlan::Validity { .. }) => { + if self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list schema does not match a validity layer".into(), + )); + } + self.consume_current_layer()?; + } + Some(SparseStructuralLayerPlan::FixedSizeList { .. }) => { + if !self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list layer does not match the Arrow schema".into(), + )); + } + self.pending_fixed_size_list = false; + self.consume_current_layer()?; + } + None => { + return Err(Error::invalid_input_source( + "Sparse structural metadata has fewer layers than the Arrow schema".into(), + )); + } + Some(SparseStructuralLayerPlan::List { .. }) => { + return Err(Error::invalid_input_source( + "Sparse structural list layer does not match an Arrow validity layer".into(), + )); + } + } + Ok(()) + } + + pub(crate) fn unravel_validity(&mut self, validity: &mut BooleanBufferBuilder) -> Result<()> { + match self.current_layer() { + Some(SparseStructuralLayerPlan::Validity { + num_slots, + validity: layer_validity, + }) => { + if self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list schema does not match a validity layer".into(), + )); + } + layer_validity.append_to(validity, *num_slots)?; + self.consume_current_layer()?; + } + Some(SparseStructuralLayerPlan::FixedSizeList { + num_slots, + validity: layer_validity, + .. + }) => { + if !self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list layer does not match the Arrow schema".into(), + )); + } + layer_validity.append_to(validity, *num_slots)?; + self.pending_fixed_size_list = false; + self.consume_current_layer()?; + } + None => { + return Err(Error::invalid_input_source( + "Sparse structural metadata has fewer layers than the Arrow schema".into(), + )); + } + Some(SparseStructuralLayerPlan::List { .. }) => { + return Err(Error::invalid_input_source( + "Sparse structural list layer does not match an Arrow validity layer".into(), + )); + } + } + Ok(()) + } + + pub(crate) fn decimate(&mut self, dimension: usize) -> Result<()> { + if self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list layer was decimated more than once".into(), + )); + } + let Some(SparseStructuralLayerPlan::FixedSizeList { + dimension: actual_dimension, + .. + }) = self.current_layer() + else { + return Err(Error::invalid_input_source( + "Sparse structural layer does not match an Arrow fixed-size-list layer".into(), + )); + }; + if usize_from_u64(*actual_dimension, "fixed-size-list dimension")? != dimension { + return Err(Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list dimension {} does not match Arrow dimension {}", + actual_dimension, dimension + ) + .into(), + )); + } + self.pending_fixed_size_list = true; + Ok(()) + } + + fn to_offset(value: u64) -> Result { + let value = usize::try_from(value).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural offset {} exceeds usize::MAX", value).into(), + ) + })?; + T::from_usize(value).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural offset does not fit the Arrow offset type".into(), + ) + }) + } + + pub(crate) fn unravel_offsets( + &mut self, + offsets: &mut Vec, + validity: Option<&mut BooleanBufferBuilder>, + ) -> Result<()> { + let Some(SparseStructuralLayerPlan::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity: layer_validity, + .. + }) = self.current_layer() + else { + return Err(Error::invalid_input_source( + "Sparse structural layer does not match an Arrow list layer".into(), + )); + }; + if self.pending_fixed_size_list { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list schema does not match an Arrow list layer".into(), + )); + } + + if non_empty_positions.len() != counts.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list has {} non-empty positions but {} counts", + non_empty_positions.len(), + counts.len() + ) + .into(), + )); + } + let actual_child_slots = counts.sum()?; + if actual_child_slots != *num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list count sum {} does not match child slots {}", + actual_child_slots, num_child_slots + ) + .into(), + )); + } + + let mut current_offset = offsets + .last() + .map(|offset| offset.as_usize() as u64) + .unwrap_or(0); + if offsets.is_empty() { + offsets.push(Self::to_offset(current_offset)?); + } + + if non_empty_positions.is_empty() { + if let Some(validity) = validity { + layer_validity.append_to(validity, *num_slots)?; + } + let offset = Self::to_offset(current_offset)?; + let new_len = offsets + .len() + .checked_add(usize_from_u64(*num_slots, "list slot count")?) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list offset length overflows usize".into(), + ) + })?; + offsets.resize(new_len, offset); + self.consume_current_layer()?; + return Ok(()); + } + + let non_empty_positions = non_empty_positions.materialize()?; + let counts = counts.materialize()?; + let mut non_empty_iter = non_empty_positions + .iter() + .copied() + .zip(counts.iter().copied()) + .peekable(); + let mut validity_cursor = + SparseValidityCursor::new(layer_validity, *num_slots, "list validity")?; + let mut validity = validity; + for slot in 0..*num_slots { + let is_valid = validity_cursor.is_valid(slot)?; + if let Some(validity) = validity.as_mut() { + validity.append(is_valid); + } + + if non_empty_iter + .peek() + .is_some_and(|(non_empty_pos, _)| *non_empty_pos == slot) + { + let (_, count) = non_empty_iter.next().ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list position is missing its child count".into(), + ) + })?; + if !is_valid { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list slot {} is both invalid and non-empty", + slot + ) + .into(), + )); + } + current_offset = current_offset.checked_add(count).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list offsets overflow u64".into(), + ) + })?; + } + offsets.push(Self::to_offset(current_offset)?); + } + validity_cursor.finish()?; + if let Some((extra_pos, _)) = non_empty_iter.next() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural non-empty position {} is outside layer with {} slots", + extra_pos, num_slots + ) + .into(), + )); + } + + self.consume_current_layer()?; + Ok(()) + } +} + +#[derive(Debug, Clone)] +enum SparsePositionSetDecoder { + Empty, + All { + len: u64, + }, + Range { + start: u64, + len: u64, + }, + Explicit { + decompressor: Arc, + encoding: CompressiveEncoding, + count: u64, + domain_len: u64, + }, +} + +#[derive(Debug, Clone)] +enum SparseCountSetDecoder { + Empty, + Constant { + value: u64, + len: u64, + }, + Explicit { + decompressor: Arc, + encoding: CompressiveEncoding, + count: u64, + }, +} + +#[derive(Debug, Clone)] +enum SparseLayerDecompressors { + Validity { + num_slots: u64, + validity: SparseValiditySetDecoder, + }, + List { + num_slots: u64, + num_child_slots: u64, + non_empty_positions: SparsePositionSetDecoder, + counts: SparseCountSetDecoder, + validity: SparseValiditySetDecoder, + }, + FixedSizeList { + num_slots: u64, + num_child_slots: u64, + dimension: u64, + validity: SparseValiditySetDecoder, + }, +} + +#[derive(Debug, Clone)] +struct SparseValiditySetDecoder { + meaning: SparseValidityMeaning, + positions: SparsePositionSetDecoder, +} + +#[derive(Debug)] +struct SparseStructuralCacheableState { + chunk_meta: Vec, + chunk_value_offsets: Arc<[u64]>, + plan: SparseStructuralPlan, + row_domain: u64, +} + +impl DeepSizeOf for SparseStructuralCacheableState { + fn deep_size_of_children(&self, _context: &mut Context) -> usize { + let structural_size = self + .plan + .layers + .iter() + .map(|layer| match layer { + SparseStructuralLayerPlan::Validity { validity, .. } => validity.deep_size(), + SparseStructuralLayerPlan::List { + non_empty_positions, + counts, + validity, + .. + } => non_empty_positions.deep_size() + counts.deep_size() + validity.deep_size(), + SparseStructuralLayerPlan::FixedSizeList { validity, .. } => validity.deep_size(), + }) + .sum::(); + self.chunk_meta.len() * std::mem::size_of::() + + self.chunk_value_offsets.len() * std::mem::size_of::() + + structural_size + } +} + +impl CachedPageData for SparseStructuralCacheableState { + fn as_arc_any(self: Arc) -> Arc { + self + } +} + +#[derive(Debug)] +pub(super) struct SparseStructuralScheduler { + buffer_offsets_and_sizes: Vec<(u64, u64)>, + priority: u64, + row_domain: u64, + row_scale: u64, + num_items: u64, + num_visible_items: u64, + num_buffers: u64, + value_encoding: CompressiveEncoding, + value_decompressor: Arc, + layer_decompressors: Vec, + data_type: DataType, + page_meta: Option>, + has_large_chunk: bool, +} + +impl SparseStructuralScheduler { + fn require_layer( + layer: &pb21::SparseStructuralLayer, + ) -> Result<&pb21::sparse_structural_layer::Layer> { + layer.layer.as_ref().ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural layer is missing its layer variant".into(), + ) + }) + } + + fn layer_num_slots(layer: &pb21::SparseStructuralLayer) -> Result { + Ok(match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::Validity(layer) => layer.num_slots, + pb21::sparse_structural_layer::Layer::List(layer) => layer.num_slots, + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => layer.num_slots, + }) + } + + fn layer_num_child_slots(layer: &pb21::SparseStructuralLayer) -> Result { + Ok(match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::Validity(layer) => layer.num_slots, + pb21::sparse_structural_layer::Layer::List(layer) => layer.num_child_slots, + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => layer + .num_slots + .checked_mul(layer.dimension) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count overflows: slots={}, dimension={}", + layer.num_slots, layer.dimension + ) + .into(), + ) + })?, + }) + } + + pub(super) fn try_new( + buffer_offsets_and_sizes: &[(u64, u64)], + priority: u64, + encoded_row_domain: u64, + data_type: DataType, + layout: &pb21::SparseLayout, + decompressors: &dyn DecompressionStrategy, + ) -> Result { + let value_compression = layout.value_compression.as_ref().ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing value compression".into()) + })?; + let value_buffer_count = Self::validate_value_encoding(value_compression)?; + if layout.num_buffers != value_buffer_count { + return Err(Error::invalid_input_source( + format!( + "Sparse layout declares {} value buffers, but its compression descriptor requires {}", + layout.num_buffers, value_buffer_count + ) + .into(), + )); + } + let row_domain = match layout.structural_layers.first() { + Some(layer) => Self::layer_num_slots(layer)?, + None => encoded_row_domain, + }; + Self::validate_domain_chain( + &layout.structural_layers, + row_domain, + layout.num_items, + layout.num_visible_items, + )?; + let expected_buffers = 2 + Self::structural_buffer_count(&layout.structural_layers)?; + if buffer_offsets_and_sizes.len() != expected_buffers { + return Err(Error::invalid_input_source( + format!( + "Sparse layout has {} buffers, expected {}", + buffer_offsets_and_sizes.len(), + expected_buffers + ) + .into(), + )); + } + Self::validate_page_buffers(buffer_offsets_and_sizes, layout.num_visible_items)?; + let row_scale = + layout + .structural_layers + .iter() + .try_fold(1_u64, |scale, layer| -> Result { + match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => { + scale.checked_mul(layer.dimension).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural fixed-size-list row scale overflows".into(), + ) + }) + } + pb21::sparse_structural_layer::Layer::Validity(_) + | pb21::sparse_structural_layer::Layer::List(_) => Ok(scale), + } + })?; + let expected_encoded_row_domain = row_domain.checked_mul(row_scale).ok_or_else(|| { + Error::invalid_input_source("Sparse structural encoded row domain overflows".into()) + })?; + if encoded_row_domain != expected_encoded_row_domain { + return Err(Error::invalid_input_source( + format!( + "Sparse structural encoded row domain {} does not match outer domain {} * fixed-size-list scale {}", + encoded_row_domain, row_domain, row_scale + ) + .into(), + )); + } + let value_decompressor = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + decompressors.create_miniblock_decompressor(value_compression, decompressors) + })) + .map_err(|_| { + Error::invalid_input_source( + "Sparse value compression descriptor caused decompressor construction to panic" + .into(), + ) + })? + .map_err(|error| { + Error::invalid_input_source( + format!("Sparse value decompressor construction failed: {error}").into(), + ) + })?; + let layer_decompressors = layout + .structural_layers + .iter() + .map(|layer| Self::layer_decompressors(layer, decompressors)) + .collect::>>()?; + + Ok(Self { + buffer_offsets_and_sizes: buffer_offsets_and_sizes.to_vec(), + priority, + row_domain, + row_scale, + num_items: layout.num_items, + num_visible_items: layout.num_visible_items, + num_buffers: layout.num_buffers, + value_encoding: value_compression.clone(), + value_decompressor: value_decompressor.into(), + layer_decompressors, + data_type, + page_meta: None, + has_large_chunk: layout.has_large_chunk, + }) + } + + fn validate_compression<'a>( + compression: &'a CompressiveEncoding, + label: &str, + ) -> Result<&'a CompressiveEncoding> { + compression.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} is missing compression details").into(), + ) + })?; + Ok(compression) + } + + fn validate_buffer_compression( + compression: Option<&pb21::BufferCompression>, + label: &str, + ) -> Result<()> { + let Some(compression) = compression else { + return Ok(()); + }; + match invalid_enum( + pb21::CompressionScheme::try_from(compression.scheme), + "compression scheme", + )? { + pb21::CompressionScheme::CompressionAlgorithmUnspecified => { + Err(Error::invalid_input_source( + format!("Sparse structural {label} buffer compression is unspecified").into(), + )) + } + pb21::CompressionScheme::CompressionAlgorithmLz4 + | pb21::CompressionScheme::CompressionAlgorithmZstd => Ok(()), + } + } + + fn encoding_contains_general(root: &CompressiveEncoding) -> bool { + use pb21::compressive_encoding::Compression; + + let mut stack = vec![root]; + while let Some(encoding) = stack.pop() { + let Some(compression) = encoding.compression.as_ref() else { + continue; + }; + match compression { + Compression::General(_) => return true, + Compression::Variable(variable) => { + stack.extend(variable.offsets.as_deref()); + } + Compression::OutOfLineBitpacking(bitpacking) => { + stack.extend(bitpacking.values.as_deref()); + } + Compression::Fsst(fsst) => stack.extend(fsst.values.as_deref()), + Compression::Dictionary(dictionary) => { + stack.extend(dictionary.indices.as_deref()); + stack.extend(dictionary.items.as_deref()); + } + Compression::Rle(rle) => { + stack.extend(rle.values.as_deref()); + stack.extend(rle.run_lengths.as_deref()); + } + Compression::ByteStreamSplit(split) => { + stack.extend(split.values.as_deref()); + } + Compression::FixedSizeList(fsl) => stack.extend(fsl.values.as_deref()), + Compression::PackedStruct(packed) => stack.extend(packed.values.as_deref()), + Compression::VariablePackedStruct(packed) => { + stack.extend( + packed + .fields + .iter() + .filter_map(|field| field.value.as_ref()), + ); + } + Compression::Flat(_) + | Compression::Constant(_) + | Compression::InlineBitpacking(_) => {} + } + } + false + } + + fn require_encoding<'a>( + encoding: &'a Option>, + label: &str, + ) -> Result<&'a CompressiveEncoding> { + encoding.as_deref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} encoding is required").into(), + ) + }) + } + + fn validate_flat(flat: &pb21::Flat, label: &str) -> Result<()> { + if flat.bits_per_value == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} flat bit width is zero").into(), + )); + } + if flat.data.is_some() { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} uses unsupported leaf buffer compression") + .into(), + )); + } + Ok(()) + } + + fn validate_value_encoding(encoding: &CompressiveEncoding) -> Result { + use pb21::compressive_encoding::Compression; + + let compression = encoding.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + "Sparse value compression is missing compression details".into(), + ) + })?; + match compression { + Compression::Flat(flat) => { + Self::validate_flat(flat, "value")?; + Ok(1) + } + Compression::InlineBitpacking(bitpacking) => { + if !matches!(bitpacking.uncompressed_bits_per_value, 8 | 16 | 32 | 64) { + return Err(Error::invalid_input_source( + format!( + "Sparse inline bitpacking width {} is not supported", + bitpacking.uncompressed_bits_per_value + ) + .into(), + )); + } + if bitpacking.values.is_some() { + return Err(Error::invalid_input_source( + "Sparse inline bitpacking uses unsupported leaf buffer compression".into(), + )); + } + Ok(1) + } + Compression::Variable(variable) => { + let offsets = variable.offsets.as_deref().ok_or_else(|| { + Error::invalid_input_source( + "Sparse variable compression is missing offsets".into(), + ) + })?; + let Some(Compression::Flat(offsets)) = offsets.compression.as_ref() else { + return Err(Error::invalid_input_source( + "Sparse variable offsets must use flat compression".into(), + )); + }; + Self::validate_flat(offsets, "variable offsets")?; + if !matches!(offsets.bits_per_value, 32 | 64) { + return Err(Error::invalid_input_source( + format!( + "Sparse variable offset width {} is not supported", + offsets.bits_per_value + ) + .into(), + )); + } + if variable.values.is_some() { + return Err(Error::invalid_input_source( + "Sparse variable values use unsupported leaf buffer compression".into(), + )); + } + Ok(1) + } + Compression::Fsst(fsst) => { + if fsst.symbol_table.is_empty() { + return Err(Error::invalid_input_source( + "Sparse FSST compression has an empty symbol table".into(), + )); + } + let values = Self::require_encoding(&fsst.values, "FSST values")?; + if !matches!(values.compression.as_ref(), Some(Compression::Variable(_))) { + return Err(Error::invalid_input_source( + "Sparse FSST values must use variable compression".into(), + )); + } + Self::validate_value_encoding(values) + } + Compression::ByteStreamSplit(split) => { + let values = Self::require_encoding(&split.values, "byte-stream-split values")?; + let Some(Compression::Flat(flat)) = values.compression.as_ref() else { + return Err(Error::invalid_input_source( + "Sparse byte-stream-split values must use flat compression".into(), + )); + }; + Self::validate_flat(flat, "byte-stream-split values")?; + if !matches!(flat.bits_per_value, 32 | 64) { + return Err(Error::invalid_input_source( + format!( + "Sparse byte-stream-split width {} is not supported", + flat.bits_per_value + ) + .into(), + )); + } + Ok(1) + } + Compression::FixedSizeList(fsl) => Self::validate_fsl_value_encoding(fsl), + Compression::PackedStruct(packed) => Self::validate_packed_value_encoding(packed), + Compression::Rle(rle) => { + let values = Self::require_encoding(&rle.values, "RLE values")?; + let lengths = Self::require_encoding(&rle.run_lengths, "RLE run lengths")?; + Self::validate_block_encoding(values, "RLE values")?; + Self::validate_block_encoding(lengths, "RLE run lengths")?; + Ok(2) + } + Compression::General(general) => { + let compression = general.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + "Sparse general compression is missing its buffer compression".into(), + ) + })?; + Self::validate_buffer_compression(Some(compression), "general")?; + Self::validate_value_encoding(Self::require_encoding( + &general.values, + "general values", + )?) + } + Compression::Constant(_) + | Compression::OutOfLineBitpacking(_) + | Compression::Dictionary(_) + | Compression::VariablePackedStruct(_) => Err(Error::invalid_input_source( + "Sparse value compression uses an unsupported mini-block encoding".into(), + )), + } + } + + fn validate_fsl_value_encoding(fsl: &pb21::FixedSizeList) -> Result { + use pb21::compressive_encoding::Compression; + + if fsl.items_per_value == 0 { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list value compression has dimension zero".into(), + )); + } + let values = Self::require_encoding(&fsl.values, "fixed-size-list values")?; + let child_buffers = match values.compression.as_ref() { + Some(Compression::Flat(flat)) => { + Self::validate_flat(flat, "fixed-size-list values")?; + 1_u64 + } + Some(Compression::FixedSizeList(inner)) => Self::validate_fsl_value_encoding(inner)?, + _ => { + return Err(Error::invalid_input_source( + "Sparse fixed-size-list values must use fixed-size-list or flat compression" + .into(), + )); + } + }; + child_buffers + .checked_add(u64::from(fsl.has_validity)) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse fixed-size-list value buffer count overflows".into(), + ) + }) + } + + fn validate_packed_value_encoding(packed: &pb21::PackedStruct) -> Result { + use pb21::compressive_encoding::Compression; + + if packed.bits_per_value.is_empty() + || packed + .bits_per_value + .iter() + .any(|bits| *bits == 0 || !bits.is_multiple_of(8)) + { + return Err(Error::invalid_input_source( + "Sparse packed-struct widths must be non-empty positive byte widths".into(), + )); + } + let values = Self::require_encoding(&packed.values, "packed-struct values")?; + let Some(Compression::Flat(flat)) = values.compression.as_ref() else { + return Err(Error::invalid_input_source( + "Sparse packed-struct values must use flat compression".into(), + )); + }; + Self::validate_flat(flat, "packed-struct values")?; + let total_bits = packed.bits_per_value.iter().try_fold(0_u64, |sum, bits| { + sum.checked_add(*bits).ok_or_else(|| { + Error::invalid_input_source("Sparse packed-struct bit width sum overflows".into()) + }) + })?; + if total_bits != flat.bits_per_value { + return Err(Error::invalid_input_source( + format!( + "Sparse packed-struct child widths sum to {}, but values use {} bits", + total_bits, flat.bits_per_value + ) + .into(), + )); + } + Ok(1) + } + + fn validate_block_encoding(encoding: &CompressiveEncoding, label: &str) -> Result<()> { + use pb21::compressive_encoding::Compression; + + match encoding.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} is missing compression details").into(), + ) + })? { + Compression::Flat(flat) => Self::validate_flat(flat, label), + Compression::InlineBitpacking(bitpacking) => { + if !matches!(bitpacking.uncompressed_bits_per_value, 8 | 16 | 32 | 64) { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} inline bitpacking width {} is unsupported", + bitpacking.uncompressed_bits_per_value + ) + .into(), + )); + } + if bitpacking.values.is_some() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} uses unsupported leaf buffer compression" + ) + .into(), + )); + } + Ok(()) + } + Compression::OutOfLineBitpacking(bitpacking) => { + if !matches!(bitpacking.uncompressed_bits_per_value, 8 | 16 | 32 | 64) { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} out-of-line bitpacking width {} is unsupported", + bitpacking.uncompressed_bits_per_value + ) + .into(), + )); + } + let values = Self::require_encoding(&bitpacking.values, label)?; + let Some(Compression::Flat(flat)) = values.compression.as_ref() else { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} bitpacked values must be flat").into(), + )); + }; + Self::validate_flat(flat, label) + } + Compression::Constant(constant) => { + if constant + .value + .as_ref() + .is_some_and(|value| value.len() != 8) + { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} constant must be 64 bits").into(), + )); + } + Ok(()) + } + Compression::General(general) => { + let compression = general.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} general compression is missing config") + .into(), + ) + })?; + Self::validate_buffer_compression(Some(compression), label)?; + Self::validate_block_encoding( + Self::require_encoding(&general.values, label)?, + label, + ) + } + Compression::Rle(rle) => { + Self::validate_block_encoding( + Self::require_encoding(&rle.values, "RLE values")?, + "RLE values", + )?; + Self::validate_block_encoding( + Self::require_encoding(&rle.run_lengths, "RLE run lengths")?, + "RLE run lengths", + ) + } + _ => Err(Error::invalid_input_source( + format!("Sparse structural {label} uses an unsupported block encoding").into(), + )), + } + } + + fn validate_domain_chain( + layers: &[pb21::SparseStructuralLayer], + row_domain: u64, + num_items: u64, + num_visible_items: u64, + ) -> Result<()> { + let expected_num_items = + layers + .iter() + .try_fold(num_visible_items, |items, layer| -> Result { + let additional = match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::List(layer) => { + let positions = Self::require_position_set( + &layer.non_empty_positions, + "list non-empty", + )?; + let num_non_empty = Self::position_cardinality( + positions, + layer.num_slots, + "list non-empty positions", + )?; + layer.num_slots.checked_sub(num_non_empty).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list has more non-empty positions than slots" + .into(), + ) + })? + } + pb21::sparse_structural_layer::Layer::Validity(_) + | pb21::sparse_structural_layer::Layer::FixedSizeList(_) => 0, + }; + items.checked_add(additional).ok_or_else(|| { + Error::invalid_input_source("Sparse structural item count overflows".into()) + }) + })?; + if num_items != expected_num_items { + return Err(Error::invalid_input_source( + format!( + "Sparse layout has {} structural items, but its layers imply {}", + num_items, expected_num_items + ) + .into(), + )); + } + let mut expected_slots = row_domain; + for (layer_index, layer) in layers.iter().enumerate() { + let num_slots = Self::layer_num_slots(layer)?; + let num_child_slots = Self::layer_num_child_slots(layer)?; + usize_from_u64(num_slots, "layer slot count")?; + usize_from_u64(num_child_slots, "layer child slot count")?; + if num_slots != expected_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural layer {} has {} slots, expected {} from the outer domain", + layer_index, num_slots, expected_slots + ) + .into(), + )); + } + expected_slots = num_child_slots; + } + if expected_slots != num_visible_items { + return Err(Error::invalid_input_source( + format!( + "Sparse structural terminal domain has {} slots, expected {} visible items", + expected_slots, num_visible_items + ) + .into(), + )); + } + Ok(()) + } + + fn metadata_buffer(&self) -> Result<(u64, u64)> { + self.buffer_offsets_and_sizes + .first() + .copied() + .ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing metadata buffer".into()) + }) + } + + fn value_buffer(&self) -> Result<(u64, u64)> { + self.buffer_offsets_and_sizes + .get(1) + .copied() + .ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing value buffer".into()) + }) + } + + fn checked_buffer_range(position: u64, size: u64, label: &str) -> Result> { + let end = position.checked_add(size).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} buffer range overflows").into(), + ) + })?; + Ok(position..end) + } + + fn validate_page_buffers( + buffer_offsets_and_sizes: &[(u64, u64)], + num_visible_items: u64, + ) -> Result<()> { + let (_, metadata_size) = buffer_offsets_and_sizes.first().copied().ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing metadata buffer".into()) + })?; + if !metadata_size.is_multiple_of(8) { + return Err(Error::invalid_input_source( + format!( + "Sparse layout metadata buffer has {metadata_size} bytes, which is not a multiple of 8" + ) + .into(), + )); + } + let chunk_count = metadata_size / 8; + if num_visible_items == 0 { + if chunk_count != 0 { + return Err(Error::invalid_input_source( + "Sparse layout with no visible items must have empty chunk metadata".into(), + )); + } + } else { + let min_chunks = + num_visible_items.div_ceil(miniblock::MAX_CONFIGURABLE_MINIBLOCK_VALUES); + if chunk_count < min_chunks || chunk_count > num_visible_items { + return Err(Error::invalid_input_source( + format!( + "Sparse layout metadata declares {chunk_count} chunks for {num_visible_items} visible items, expected {min_chunks}..={num_visible_items}" + ) + .into(), + )); + } + } + + let (_, value_size) = buffer_offsets_and_sizes.get(1).copied().ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing value buffer".into()) + })?; + if (num_visible_items == 0) != (value_size == 0) { + return Err(Error::invalid_input_source( + format!( + "Sparse layout value buffer has {value_size} bytes for {num_visible_items} visible items" + ) + .into(), + )); + } + + for (position, size) in buffer_offsets_and_sizes.iter().skip(2) { + Self::checked_buffer_range(*position, *size, "structural")?; + } + + for (position, size) in buffer_offsets_and_sizes.iter().take(2) { + Self::checked_buffer_range(*position, *size, "page")?; + } + Ok(()) + } + + fn require_position_set<'a>( + set: &'a Option, + label: &str, + ) -> Result<&'a pb21::SparsePositionSet> { + set.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} position set is required").into(), + ) + }) + } + + fn require_count_set<'a>( + set: &'a Option, + label: &str, + ) -> Result<&'a pb21::SparseCountSet> { + set.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} count set is required").into(), + ) + }) + } + + fn require_validity_set<'a>( + set: &'a Option, + label: &str, + ) -> Result<&'a pb21::SparseValiditySet> { + set.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} validity set is required").into(), + ) + }) + } + + fn validity_meaning( + validity_set: &pb21::SparseValiditySet, + label: &str, + ) -> Result { + match invalid_enum( + pb21::sparse_validity_set::Meaning::try_from(validity_set.meaning), + "validity meaning", + )? { + pb21::sparse_validity_set::Meaning::SparseValidityUnspecified => { + Err(Error::invalid_input_source( + format!("Sparse structural {label} meaning is unspecified").into(), + )) + } + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions => { + Ok(SparseValidityMeaning::NullPositions) + } + pb21::sparse_validity_set::Meaning::SparseValidityValidPositions => { + Ok(SparseValidityMeaning::ValidPositions) + } + } + } + + fn validity_buffer_count( + validity_set: &pb21::SparseValiditySet, + domain_len: u64, + label: &str, + ) -> Result<(usize, SparseValidityMeaning, u64)> { + let meaning = Self::validity_meaning(validity_set, label)?; + let position_set = validity_set.positions.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} positions are required").into(), + ) + })?; + let cardinality = Self::position_cardinality(position_set, domain_len, label)?; + let buffer_count = Self::position_buffer_count(position_set, domain_len, label)?; + Ok((buffer_count, meaning, cardinality)) + } + + fn position_cardinality( + position_set: &pb21::SparsePositionSet, + domain_len: u64, + label: &str, + ) -> Result { + let positions = position_set.positions.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} position set is missing its variant").into(), + ) + })?; + let cardinality = position_set.num_positions; + if cardinality > domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} cardinality {} exceeds domain {}", + cardinality, domain_len + ) + .into(), + )); + } + match positions { + pb21::sparse_position_set::Positions::Empty(_) => { + if cardinality != 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} empty set has cardinality {}", + cardinality + ) + .into(), + )); + } + } + pb21::sparse_position_set::Positions::All(_) => { + if domain_len == 0 || cardinality != domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} all set has cardinality {}, expected {}", + cardinality, domain_len + ) + .into(), + )); + } + } + pb21::sparse_position_set::Positions::Range(range) => { + let end = range.start.checked_add(range.length).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} range overflows").into(), + ) + })?; + if range.length == 0 || range.length != cardinality || end > domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} range {}..{} does not match cardinality {} in domain {}", + range.start, end, cardinality, domain_len + ) + .into(), + )); + } + } + pb21::sparse_position_set::Positions::Explicit(compression) => { + if cardinality == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} has compression but no values").into(), + )); + } + Self::validate_compression(compression, label)?; + } + } + Ok(cardinality) + } + + fn position_buffer_count( + position_set: &pb21::SparsePositionSet, + domain_len: u64, + label: &str, + ) -> Result { + Self::position_cardinality(position_set, domain_len, label)?; + let positions = position_set.positions.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} position set is missing its variant").into(), + ) + })?; + Ok(usize::from(matches!( + positions, + pb21::sparse_position_set::Positions::Explicit(_) + ))) + } + + fn count_buffer_count( + count_set: &pb21::SparseCountSet, + cardinality: u64, + label: &str, + ) -> Result { + let counts = count_set.counts.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} count set is missing its variant").into(), + ) + })?; + match counts { + pb21::sparse_count_set::Counts::Empty(_) => { + if cardinality != 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} empty count set has cardinality {}", + cardinality + ) + .into(), + )); + } + Ok(0) + } + pb21::sparse_count_set::Counts::Constant(constant) => { + if cardinality == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} constant count has no values").into(), + )); + } + if constant.value == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} constant count is zero").into(), + )); + } + Ok(0) + } + pb21::sparse_count_set::Counts::Explicit(compression) => { + if cardinality == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} has compression but no values").into(), + )); + } + Self::validate_compression(compression, label)?; + Ok(1) + } + } + } + + fn count_set_child_slots( + count_set: &pb21::SparseCountSet, + cardinality: u64, + label: &str, + ) -> Result> { + let counts = count_set.counts.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} count set is missing its variant").into(), + ) + })?; + match counts { + pb21::sparse_count_set::Counts::Empty(_) => Ok(Some(0)), + pb21::sparse_count_set::Counts::Constant(constant) => constant + .value + .checked_mul(cardinality) + .map(Some) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} constant count sum overflows: value={}, len={}", + constant.value, cardinality + ) + .into(), + ) + }), + pb21::sparse_count_set::Counts::Explicit(_) => Ok(None), + } + } + + fn create_position_decompressor( + compression: &CompressiveEncoding, + label: &str, + decompressors: &dyn DecompressionStrategy, + ) -> Result> { + let compression = Self::validate_compression(compression, label)?; + Self::validate_block_encoding(compression, label)?; + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + decompressors.create_block_decompressor(compression) + })) + .map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse structural {label} descriptor caused decompressor construction to panic" + ) + .into(), + ) + })? + .map(Arc::from) + .map_err(|error| { + Error::invalid_input_source( + format!("Sparse structural {label} decompressor construction failed: {error}") + .into(), + ) + }) + } + + fn position_set_decoder( + position_set: &pb21::SparsePositionSet, + domain_len: u64, + label: &str, + decompressors: &dyn DecompressionStrategy, + ) -> Result<(SparsePositionSetDecoder, u64)> { + let cardinality = Self::position_cardinality(position_set, domain_len, label)?; + let positions = position_set.positions.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} position set is missing its variant").into(), + ) + })?; + Ok(( + match positions { + pb21::sparse_position_set::Positions::Empty(_) => SparsePositionSetDecoder::Empty, + pb21::sparse_position_set::Positions::All(_) => { + SparsePositionSetDecoder::All { len: domain_len } + } + pb21::sparse_position_set::Positions::Range(range) => { + SparsePositionSetDecoder::Range { + start: range.start, + len: range.length, + } + } + pb21::sparse_position_set::Positions::Explicit(compression) => { + SparsePositionSetDecoder::Explicit { + decompressor: Self::create_position_decompressor( + compression, + label, + decompressors, + )?, + encoding: compression.clone(), + count: cardinality, + domain_len, + } + } + }, + cardinality, + )) + } + + fn validity_set_decoder( + validity_set: &pb21::SparseValiditySet, + domain_len: u64, + label: &str, + decompressors: &dyn DecompressionStrategy, + ) -> Result<(SparseValiditySetDecoder, u64)> { + let meaning = Self::validity_meaning(validity_set, label)?; + let position_set = validity_set.positions.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} positions are required").into(), + ) + })?; + let (positions, cardinality) = + Self::position_set_decoder(position_set, domain_len, label, decompressors)?; + Ok((SparseValiditySetDecoder { meaning, positions }, cardinality)) + } + + fn num_valid_slots( + meaning: SparseValidityMeaning, + cardinality: u64, + num_slots: u64, + label: &str, + ) -> Result { + match meaning { + SparseValidityMeaning::NullPositions => { + num_slots.checked_sub(cardinality).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} null cardinality {} exceeds slots {}", + cardinality, num_slots + ) + .into(), + ) + }) + } + SparseValidityMeaning::ValidPositions => Ok(cardinality), + } + } + + fn count_set_decoder( + count_set: &pb21::SparseCountSet, + cardinality: u64, + label: &str, + decompressors: &dyn DecompressionStrategy, + ) -> Result { + Self::count_buffer_count(count_set, cardinality, label)?; + let counts = count_set.counts.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} count set is missing its variant").into(), + ) + })?; + Ok(match counts { + pb21::sparse_count_set::Counts::Empty(_) => SparseCountSetDecoder::Empty, + pb21::sparse_count_set::Counts::Constant(constant) => SparseCountSetDecoder::Constant { + value: constant.value, + len: cardinality, + }, + pb21::sparse_count_set::Counts::Explicit(compression) => { + SparseCountSetDecoder::Explicit { + decompressor: Self::create_position_decompressor( + compression, + label, + decompressors, + )?, + encoding: compression.clone(), + count: cardinality, + } + } + }) + } + + fn add_buffer_count(count: &mut usize, additional: usize) -> Result<()> { + *count = count.checked_add(additional).ok_or_else(|| { + Error::invalid_input_source("Sparse structural buffer count overflows".into()) + })?; + Ok(()) + } + + fn structural_buffer_count(layers: &[pb21::SparseStructuralLayer]) -> Result { + layers + .iter() + .try_fold(0_usize, |mut count, layer| -> Result { + match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::Validity(layer) => { + let (validity_buffers, _, _) = Self::validity_buffer_count( + Self::require_validity_set(&layer.validity, "validity")?, + layer.num_slots, + "validity positions", + )?; + Self::add_buffer_count(&mut count, validity_buffers)?; + } + pb21::sparse_structural_layer::Layer::List(layer) => { + let non_empty_positions = Self::require_position_set( + &layer.non_empty_positions, + "list non-empty", + )?; + let num_non_empty = Self::position_cardinality( + non_empty_positions, + layer.num_slots, + "list non-empty positions", + )?; + let non_empty_buffers = Self::position_buffer_count( + non_empty_positions, + layer.num_slots, + "list non-empty positions", + )?; + Self::add_buffer_count(&mut count, non_empty_buffers)?; + let (validity_buffers, validity_meaning, validity_cardinality) = + Self::validity_buffer_count( + Self::require_validity_set(&layer.validity, "list")?, + layer.num_slots, + "list validity positions", + )?; + Self::add_buffer_count(&mut count, validity_buffers)?; + let num_valid_slots = Self::num_valid_slots( + validity_meaning, + validity_cardinality, + layer.num_slots, + "list validity", + )?; + if num_non_empty > num_valid_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list has {} non-empty slots but only {} valid slots", + num_non_empty, num_valid_slots + ) + .into(), + )); + } + let counts = Self::require_count_set(&layer.counts, "list counts")?; + let count_buffers = + Self::count_buffer_count(counts, num_non_empty, "list counts")?; + Self::add_buffer_count(&mut count, count_buffers)?; + if let Some(child_slots) = + Self::count_set_child_slots(counts, num_non_empty, "list counts")? + && child_slots != layer.num_child_slots + { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list count sum {} does not match child slots {}", + child_slots, layer.num_child_slots + ) + .into(), + )); + } + } + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => { + if layer.dimension == 0 { + return Err(Error::invalid_input_source( + "Sparse structural fixed-size-list dimension is zero".into(), + )); + } + layer.num_slots.checked_mul(layer.dimension).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count overflows: slots={}, dimension={}", + layer.num_slots, layer.dimension + ) + .into(), + ) + })?; + let (validity_buffers, _, _) = Self::validity_buffer_count( + Self::require_validity_set(&layer.validity, "fixed-size-list")?, + layer.num_slots, + "fixed-size-list validity positions", + )?; + Self::add_buffer_count(&mut count, validity_buffers)?; + } + } + Ok(count) + }) + } + + fn layer_decompressors( + layer: &pb21::SparseStructuralLayer, + decompressors: &dyn DecompressionStrategy, + ) -> Result { + Ok(match Self::require_layer(layer)? { + pb21::sparse_structural_layer::Layer::Validity(layer) => { + let (validity, _) = Self::validity_set_decoder( + Self::require_validity_set(&layer.validity, "validity")?, + layer.num_slots, + "validity positions", + decompressors, + )?; + SparseLayerDecompressors::Validity { + num_slots: layer.num_slots, + validity, + } + } + pb21::sparse_structural_layer::Layer::List(layer) => { + let non_empty_positions = + Self::require_position_set(&layer.non_empty_positions, "list non-empty")?; + let (non_empty_positions, num_non_empty) = Self::position_set_decoder( + non_empty_positions, + layer.num_slots, + "list non-empty positions", + decompressors, + )?; + let counts = Self::count_set_decoder( + Self::require_count_set(&layer.counts, "list counts")?, + num_non_empty, + "list counts", + decompressors, + )?; + let (validity, _) = Self::validity_set_decoder( + Self::require_validity_set(&layer.validity, "list")?, + layer.num_slots, + "list validity positions", + decompressors, + )?; + SparseLayerDecompressors::List { + num_slots: layer.num_slots, + num_child_slots: layer.num_child_slots, + non_empty_positions, + counts, + validity, + } + } + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => { + let (validity, _) = Self::validity_set_decoder( + Self::require_validity_set(&layer.validity, "fixed-size-list")?, + layer.num_slots, + "fixed-size-list validity positions", + decompressors, + )?; + SparseLayerDecompressors::FixedSizeList { + num_slots: layer.num_slots, + num_child_slots: layer.num_slots.checked_mul(layer.dimension).ok_or_else( + || { + Error::invalid_input_source( + "Sparse structural fixed-size-list child slot count overflows" + .into(), + ) + }, + )?, + dimension: layer.dimension, + validity, + } + } + }) + } + + fn parse_chunk_meta(&self, meta_bytes: Bytes) -> Result> { + if !meta_bytes.len().is_multiple_of(8) { + return Err(Error::invalid_input_source( + format!( + "Sparse layout metadata length {} is not a multiple of 8", + meta_bytes.len() + ) + .into(), + )); + } + + let (value_buf_position, value_buf_size) = self.value_buffer()?; + let value_buf_end = value_buf_position + .checked_add(value_buf_size) + .ok_or_else(|| { + Error::invalid_input_source("Sparse layout value buffer range overflows".into()) + })?; + let mut rows_counter = 0_u64; + let mut offset_bytes = value_buf_position; + let mut chunk_meta = Vec::with_capacity(meta_bytes.len() / 8); + for chunk in meta_bytes.chunks_exact(8) { + let entry: [u8; 8] = chunk.try_into().map_err(|_| { + Error::invalid_input_source( + "Sparse layout chunk metadata entry is not 8 bytes".into(), + ) + })?; + let divided_bytes_minus_one = u32::from_le_bytes( + entry + .get(..4) + .and_then(|bytes| bytes.try_into().ok()) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse layout chunk byte-size field is malformed".into(), + ) + })?, + ); + let num_values = u64::from(u32::from_le_bytes( + entry + .get(4..) + .and_then(|bytes| bytes.try_into().ok()) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse layout chunk value-count field is malformed".into(), + ) + })?, + )); + if num_values == 0 { + return Err(Error::invalid_input_source( + "Sparse layout contains an empty value chunk".into(), + )); + } + if num_values > miniblock::MAX_CONFIGURABLE_MINIBLOCK_VALUES { + return Err(Error::invalid_input_source( + format!( + "Sparse layout value chunk has {} values, exceeding the mini-block limit {}", + num_values, + miniblock::MAX_CONFIGURABLE_MINIBLOCK_VALUES + ) + .into(), + )); + } + let num_bytes = u64::from(divided_bytes_minus_one) + .checked_add(1) + .and_then(|units| units.checked_mul(MINIBLOCK_ALIGNMENT as u64)) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse layout value chunk byte size overflows".into(), + ) + })?; + rows_counter = rows_counter.checked_add(num_values).ok_or_else(|| { + Error::invalid_input_source("Sparse layout visible item count overflows".into()) + })?; + chunk_meta.push(ChunkMeta { + num_values, + chunk_size_bytes: num_bytes, + offset_bytes, + }); + offset_bytes = offset_bytes.checked_add(num_bytes).ok_or_else(|| { + Error::invalid_input_source("Sparse layout value chunk byte range overflows".into()) + })?; + } + if rows_counter != self.num_visible_items { + return Err(Error::invalid_input_source( + format!( + "Sparse layout visible item count mismatch: metadata has {}, layout has {}", + rows_counter, self.num_visible_items + ) + .into(), + )); + } + if offset_bytes != value_buf_end { + return Err(Error::invalid_input_source( + format!( + "Sparse layout chunk metadata describes {} value bytes, but value buffer has {} bytes", + offset_bytes - value_buf_position, + value_buf_size + ) + .into(), + )); + } + Ok(chunk_meta) + } + + fn validate_general_buffer_header( + general: &pb21::General, + data: &[u8], + label: &str, + ) -> Result<()> { + let compression = general.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} general compression is missing config").into(), + ) + })?; + Self::validate_buffer_compression(Some(compression), label)?; + let values = Self::require_encoding(&general.values, label)?; + if Self::encoding_contains_general(values) { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} contains nested general compression, which is unsupported" + ) + .into(), + )); + } + + let scheme = invalid_enum( + pb21::CompressionScheme::try_from(compression.scheme), + "compression scheme", + )?; + match scheme { + pb21::CompressionScheme::CompressionAlgorithmLz4 => { + data.get(..4).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} LZ4 buffer is missing its length prefix" + ) + .into(), + ) + })?; + } + pb21::CompressionScheme::CompressionAlgorithmZstd => { + data.get(..8).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} Zstd buffer is missing its length prefix" + ) + .into(), + ) + })?; + } + pb21::CompressionScheme::CompressionAlgorithmUnspecified => { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} general compression scheme is unspecified") + .into(), + )); + } + } + Ok(()) + } + + fn validate_general_child_buffer( + encoding: &CompressiveEncoding, + data: &[u8], + label: &str, + ) -> Result<()> { + if let Some(pb21::compressive_encoding::Compression::General(general)) = + encoding.compression.as_ref() + { + Self::validate_general_buffer_header(general, data, label)?; + } + Ok(()) + } + + fn validate_structural_buffer_headers( + encoding: &CompressiveEncoding, + data: &[u8], + label: &str, + ) -> Result<()> { + use pb21::compressive_encoding::Compression; + + match encoding.compression.as_ref() { + Some(Compression::General(general)) => { + Self::validate_general_buffer_header(general, data, label) + } + Some(Compression::Rle(rle)) => { + let values_size = u64::from_le_bytes( + data.get(..8) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} RLE buffer is missing its header" + ) + .into(), + ) + })? + .try_into() + .map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} RLE header is malformed").into(), + ) + })?, + ); + let values_size = usize_from_u64(values_size, "RLE values buffer size")?; + let values_end = 8_usize.checked_add(values_size).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} RLE values range overflows").into(), + ) + })?; + let values_data = data.get(8..values_end).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} RLE values buffer is truncated").into(), + ) + })?; + let lengths_data = data.get(values_end..).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} RLE run-length buffer is missing") + .into(), + ) + })?; + Self::validate_general_child_buffer( + Self::require_encoding(&rle.values, "RLE values")?, + values_data, + "RLE values", + )?; + Self::validate_general_child_buffer( + Self::require_encoding(&rle.run_lengths, "RLE run lengths")?, + lengths_data, + "RLE run lengths", + ) + } + _ => Ok(()), + } + } + + fn decode_u64_values( + decompressor: &dyn BlockDecompressor, + encoding: &CompressiveEncoding, + data: Bytes, + num_values: u64, + label: &str, + ) -> Result> { + Self::validate_structural_buffer_headers(encoding, &data, label)?; + let decoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + decompressor.decompress(LanceBuffer::from_bytes(data, 1), num_values) + })) + .map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} decompression panicked").into(), + ) + })? + .map_err(|error| { + Error::invalid_input_source( + format!("Sparse structural {label} decompression failed: {error}").into(), + ) + })?; + let fixed = decoded.as_fixed_width().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} did not decode to fixed width data").into(), + ) + })?; + if fixed.bits_per_value != 64 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} decoded to {} bits per value, expected 64", + fixed.bits_per_value + ) + .into(), + )); + } + if fixed.num_values != num_values { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} decoded {} values, expected {}", + fixed.num_values, num_values + ) + .into(), + )); + } + let num_values_usize = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} value count exceeds usize::MAX").into(), + ) + })?; + let expected_len = num_values_usize + .checked_mul(std::mem::size_of::()) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} decoded byte length overflows").into(), + ) + })?; + if fixed.data.len() != expected_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} decoded {} bytes, expected {}", + fixed.data.len(), + expected_len + ) + .into(), + )); + } + let values = fixed.data.borrow_to_typed_slice::(); + if values.len() != num_values_usize { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} decoded {} u64 values, expected {}", + values.len(), + num_values + ) + .into(), + )); + } + Ok(values.to_vec()) + } + + fn decode_explicit_positions( + decompressor: &Arc, + encoding: &CompressiveEncoding, + data: Bytes, + num_positions: u64, + num_slots: u64, + label: &str, + ) -> Result { + let deltas = + Self::decode_u64_values(decompressor.as_ref(), encoding, data, num_positions, label)?; + let mut positions = Vec::with_capacity(deltas.len()); + let mut current = 0_u64; + for (idx, delta) in deltas.into_iter().enumerate() { + if idx == 0 { + current = delta; + } else { + if delta == 0 { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} positions must be strictly increasing") + .into(), + )); + } + current = current.checked_add(delta).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} position overflow").into(), + ) + })?; + } + if current >= num_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} position {} is outside layer with {} slots", + current, num_slots + ) + .into(), + )); + } + positions.push(current); + } + SparsePositionSet::from_positions(positions, num_slots, label) + } + + fn decode_position_set( + decoder: &SparsePositionSetDecoder, + buffers: &mut impl Iterator, + label: &str, + ) -> Result { + match decoder { + SparsePositionSetDecoder::Empty => Ok(SparsePositionSet::empty()), + SparsePositionSetDecoder::All { len } => Ok(SparsePositionSet::all(*len)), + SparsePositionSetDecoder::Range { start, len } => { + Ok(SparsePositionSet::range(*start, *len)) + } + SparsePositionSetDecoder::Explicit { + decompressor, + encoding, + count, + domain_len, + } => Self::decode_explicit_positions( + decompressor, + encoding, + Self::next_structural_buffer(buffers, label)?, + *count, + *domain_len, + label, + ), + } + } + + fn decode_validity_set( + decoder: &SparseValiditySetDecoder, + buffers: &mut impl Iterator, + label: &str, + ) -> Result { + Ok(SparseValiditySet { + meaning: decoder.meaning, + positions: Self::decode_position_set(&decoder.positions, buffers, label)?, + }) + } + + fn decode_count_set( + decoder: &SparseCountSetDecoder, + buffers: &mut impl Iterator, + label: &str, + ) -> Result { + match decoder { + SparseCountSetDecoder::Empty => Ok(SparseCountSet::Empty), + SparseCountSetDecoder::Constant { value, len } => { + Ok(SparseCountSet::constant(*value, *len)) + } + SparseCountSetDecoder::Explicit { + decompressor, + encoding, + count, + } => { + let counts = Self::decode_u64_values( + decompressor.as_ref(), + encoding, + Self::next_structural_buffer(buffers, label)?, + *count, + label, + )?; + SparseCountSet::from_counts(counts) + } + } + } + + fn next_structural_buffer( + buffers: &mut impl Iterator, + label: &str, + ) -> Result { + buffers.next().ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} is missing its buffer").into(), + ) + }) + } + + fn decode_layer( + layer: &SparseLayerDecompressors, + buffers: &mut impl Iterator, + ) -> Result { + Ok(match layer { + SparseLayerDecompressors::Validity { + num_slots, + validity, + } => SparseStructuralLayerPlan::Validity { + num_slots: *num_slots, + validity: Self::decode_validity_set(validity, buffers, "validity positions")?, + }, + SparseLayerDecompressors::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + } => { + let non_empty_positions = Self::decode_position_set( + non_empty_positions, + buffers, + "list non-empty positions", + )?; + let counts = Self::decode_count_set(counts, buffers, "list counts")?; + let validity = + Self::decode_validity_set(validity, buffers, "list validity positions")?; + let actual_child_slots = counts.sum()?; + if actual_child_slots != *num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list count sum {} does not match declared child slots {}", + actual_child_slots, num_child_slots + ) + .into(), + )); + } + SparseStructuralLayerPlan::List { + num_slots: *num_slots, + num_child_slots: *num_child_slots, + non_empty_positions, + counts, + validity, + } + } + SparseLayerDecompressors::FixedSizeList { + num_slots, + num_child_slots, + dimension, + validity, + } => { + let expected_child_slots = num_slots.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count overflows: slots={}, dimension={}", + num_slots, dimension + ) + .into(), + ) + })?; + if expected_child_slots != *num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count {} does not match slots {} * dimension {}", + num_child_slots, num_slots, dimension + ) + .into(), + )); + } + SparseStructuralLayerPlan::FixedSizeList { + num_slots: *num_slots, + dimension: *dimension, + validity: Self::decode_validity_set( + validity, + buffers, + "fixed-size-list validity positions", + )?, + } + } + }) + } + + fn lookup_value_chunks(&self, chunk_indices: &[usize]) -> Result> { + let page_meta = self.page_meta.as_ref().ok_or_else(|| { + Error::internal("Sparse page scheduler has not been initialized".to_string()) + })?; + chunk_indices + .iter() + .map(|&chunk_idx| { + let chunk_meta = page_meta.chunk_meta.get(chunk_idx).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse layout missing value chunk metadata for chunk {chunk_idx}") + .into(), + ) + })?; + let bytes_start = chunk_meta.offset_bytes; + let bytes_end = bytes_start + .checked_add(chunk_meta.chunk_size_bytes) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse layout value chunk {} byte range overflows", + chunk_idx + ) + .into(), + ) + })?; + Ok(LoadedChunk { + byte_range: bytes_start..bytes_end, + items_in_chunk: chunk_meta.num_values, + chunk_idx, + data: LanceBuffer::empty(), + }) + }) + .collect() + } + + fn value_chunk_index(chunk_value_offsets: &[u64], value: u64) -> Result { + let total_values = chunk_value_offsets.last().copied().ok_or_else(|| { + Error::invalid_input_source("Sparse layout has no value chunk offsets".into()) + })?; + if chunk_value_offsets.len() < 2 || value >= total_values { + return Err(Error::invalid_input_source( + format!( + "Sparse layout value index {} is outside {} visible items", + value, total_values + ) + .into(), + )); + } + chunk_value_offsets + .partition_point(|&offset| offset <= value) + .checked_sub(1) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse layout value index {value} is before the first chunk").into(), + ) + }) + } + + fn value_chunk_range( + chunk_value_offsets: &[u64], + value_range: Range, + ) -> Result> { + if value_range.is_empty() { + return Ok(0..0); + } + let total_values = chunk_value_offsets.last().copied().ok_or_else(|| { + Error::invalid_input_source("Sparse layout has no value chunk offsets".into()) + })?; + if value_range.start > value_range.end || value_range.end > total_values { + return Err(Error::invalid_input_source( + format!( + "Sparse layout value range {}..{} is outside {} visible items", + value_range.start, value_range.end, total_values + ) + .into(), + )); + } + let start = Self::value_chunk_index(chunk_value_offsets, value_range.start)?; + let end = chunk_value_offsets + .partition_point(|&offset| offset < value_range.end) + .max(start + 1); + Ok(start..end) + } +} + +impl StructuralPageScheduler for SparseStructuralScheduler { + fn initialize<'a>( + &'a mut self, + io: &Arc, + ) -> BoxFuture<'a, Result>> { + let (meta_buf_position, meta_buf_size) = match self.metadata_buffer() { + Ok(buffer) => buffer, + Err(err) => return std::future::ready(Err(err)).boxed(), + }; + let required_ranges = match (|| -> Result>> { + let mut required_ranges = Vec::new(); + required_ranges.push(Self::checked_buffer_range( + meta_buf_position, + meta_buf_size, + "metadata", + )?); + for (position, size) in self.buffer_offsets_and_sizes.iter().skip(2) { + required_ranges.push(Self::checked_buffer_range(*position, *size, "structural")?); + } + Ok(required_ranges) + })() { + Ok(ranges) => ranges, + Err(err) => return std::future::ready(Err(err)).boxed(), + }; + let io_req = io.submit_request(required_ranges, 0); + + async move { + let mut buffers = io_req.await?.into_iter(); + let meta_bytes = buffers.next().ok_or_else(|| { + Error::invalid_input_source("Sparse layout is missing chunk metadata buffer".into()) + })?; + + let chunk_meta = self.parse_chunk_meta(meta_bytes)?; + let mut chunk_value_offsets = Vec::with_capacity(chunk_meta.len() + 1); + let mut value_offset = 0_u64; + chunk_value_offsets.push(value_offset); + for chunk in &chunk_meta { + value_offset = value_offset.checked_add(chunk.num_values).ok_or_else(|| { + Error::invalid_input_source( + "Sparse layout visible item offset overflows".into(), + ) + })?; + chunk_value_offsets.push(value_offset); + } + + let layers = self + .layer_decompressors + .iter() + .map(|layer| Self::decode_layer(layer, &mut buffers)) + .collect::>>()?; + let plan = SparseStructuralPlan { + layers, + num_items: self.num_items, + num_visible_items: self.num_visible_items, + }; + plan.validate(self.row_domain)?; + if buffers.next().is_some() { + return Err(Error::invalid_input_source( + "Sparse layout has unused structural buffers".into(), + )); + } + + let page_meta = Arc::new(SparseStructuralCacheableState { + chunk_meta, + chunk_value_offsets: chunk_value_offsets.into(), + plan, + row_domain: self.row_domain, + }); + self.page_meta = Some(page_meta.clone()); + Ok(page_meta as Arc) + } + .boxed() + } + + fn load(&mut self, data: &Arc) { + self.page_meta = data + .clone() + .as_arc_any() + .downcast::() + .ok(); + } + + fn schedule_ranges( + &self, + ranges: &[Range], + io: &Arc, + ) -> Result> { + let page_meta = self.page_meta.as_ref().ok_or_else(|| { + Error::internal("Sparse page scheduler has not been initialized".to_string()) + })?; + let encoded_row_domain = page_meta + .row_domain + .checked_mul(self.row_scale) + .ok_or_else(|| { + Error::invalid_input_source("Sparse structural encoded row domain overflows".into()) + })?; + let num_rows = validate_slice_ranges(ranges, encoded_row_domain, "row")?; + let ranges = ranges + .iter() + .map(|range| { + if !range.start.is_multiple_of(self.row_scale) + || !range.end.is_multiple_of(self.row_scale) + { + return Err(Error::invalid_input_source( + format!( + "Sparse structural encoded row range {}..{} is not aligned to fixed-size-list scale {}", + range.start, range.end, self.row_scale + ) + .into(), + )); + } + Ok((range.start / self.row_scale)..(range.end / self.row_scale)) + }) + .collect::>>()?; + + let mut chunks_needed = Vec::new(); + let selection = slice_sparse_plan(&page_meta.plan, &ranges, page_meta.row_domain)?; + for value_range in &selection.leaf_ranges { + chunks_needed.extend(Self::value_chunk_range( + &page_meta.chunk_value_offsets, + value_range.clone(), + )?); + } + chunks_needed.sort_unstable(); + chunks_needed.dedup(); + + let mut loaded_chunks = self.lookup_value_chunks(&chunks_needed)?; + let chunk_ranges = loaded_chunks + .iter() + .map(|chunk| chunk.byte_range.clone()) + .collect::>(); + let loaded_chunk_data = io.submit_request(chunk_ranges, self.priority); + let ranges = VecDeque::from(ranges); + let value_decompressor = self.value_decompressor.clone(); + let value_encoding = self.value_encoding.clone(); + let data_type = self.data_type.clone(); + let page_meta = page_meta.clone(); + let num_buffers = self.num_buffers; + let has_large_chunk = self.has_large_chunk; + let row_scale = self.row_scale; + + let res = async move { + let loaded_chunk_data = loaded_chunk_data.await?; + for (loaded_chunk, chunk_data) in loaded_chunks.iter_mut().zip(loaded_chunk_data) { + loaded_chunk.data = LanceBuffer::from_bytes(chunk_data, 1); + } + + Ok(Box::new(SparseStructuralDecoder { + value_decompressor, + value_encoding, + data_type, + page_meta, + loaded_chunks: Arc::new(loaded_chunks), + ranges, + offset_in_current_range: 0, + num_rows, + row_scale, + num_buffers, + has_large_chunk, + }) as Box) + } + .boxed(); + Ok(vec![PageLoadTask { + decoder_fut: res, + num_rows, + }]) + } +} + +#[derive(Debug)] +struct SparseStructuralDecoder { + value_decompressor: Arc, + value_encoding: CompressiveEncoding, + data_type: DataType, + page_meta: Arc, + loaded_chunks: Arc>, + ranges: VecDeque>, + offset_in_current_range: u64, + num_rows: u64, + row_scale: u64, + num_buffers: u64, + has_large_chunk: bool, +} + +impl SparseStructuralDecoder { + fn drain_ranges(&mut self, mut rows_desired: u64) -> Result>> { + if !rows_desired.is_multiple_of(self.row_scale) { + return Err(Error::invalid_input_source( + format!( + "Sparse page decoder drain of {} encoded rows is not aligned to fixed-size-list scale {}", + rows_desired, self.row_scale + ) + .into(), + )); + } + rows_desired /= self.row_scale; + let mut ranges = Vec::new(); + while rows_desired > 0 { + let range = self.ranges.front().ok_or_else(|| { + Error::invalid_input_source( + "Sparse page decoder was asked to drain more rows than were scheduled".into(), + ) + })?; + let start = range + .start + .checked_add(self.offset_in_current_range) + .ok_or_else(|| { + Error::invalid_input_source("Sparse page decoder row offset overflows".into()) + })?; + let rows_available = range.end.checked_sub(start).ok_or_else(|| { + Error::invalid_input_source( + "Sparse page decoder range offset exceeds the scheduled range".into(), + ) + })?; + let rows_to_take = rows_available.min(rows_desired); + let end = start.checked_add(rows_to_take).ok_or_else(|| { + Error::invalid_input_source("Sparse page decoder row range overflows".into()) + })?; + ranges.push(start..end); + rows_desired -= rows_to_take; + self.offset_in_current_range += rows_to_take; + if self.offset_in_current_range == range.end - range.start { + self.offset_in_current_range = 0; + self.ranges.pop_front(); + } + } + Ok(ranges) + } +} + +impl StructuralPageDecoder for SparseStructuralDecoder { + fn drain(&mut self, num_rows: u64) -> Result> { + Ok(Box::new(DecodeSparseStructuralTask { + row_ranges: self.drain_ranges(num_rows)?, + value_decompressor: self.value_decompressor.clone(), + value_encoding: self.value_encoding.clone(), + data_type: self.data_type.clone(), + page_meta: self.page_meta.clone(), + loaded_chunks: self.loaded_chunks.clone(), + num_buffers: self.num_buffers, + has_large_chunk: self.has_large_chunk, + })) + } + + fn num_rows(&self) -> u64 { + self.num_rows + } +} + +#[derive(Debug)] +struct DecodeSparseStructuralTask { + row_ranges: Vec>, + value_decompressor: Arc, + value_encoding: CompressiveEncoding, + data_type: DataType, + page_meta: Arc, + loaded_chunks: Arc>, + num_buffers: u64, + has_large_chunk: bool, +} + +impl DecodeSparseStructuralTask { + fn read_chunk_size( + buf: &[u8], + offset: &mut usize, + width: usize, + chunk_idx: usize, + ) -> Result { + let end = offset.checked_add(width).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural value chunk {chunk_idx} size header overflows").into(), + ) + })?; + let bytes = buf.get(*offset..end).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural value chunk {chunk_idx} has a truncated size header") + .into(), + ) + })?; + let size = match width { + 2 => u32::from(u16::from_le_bytes(bytes.try_into().map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural value chunk {chunk_idx} has a malformed u16 size") + .into(), + ) + })?)), + 4 => u32::from_le_bytes(bytes.try_into().map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural value chunk {chunk_idx} has a malformed u32 size") + .into(), + ) + })?), + _ => { + return Err(Error::internal(format!( + "Unsupported sparse value chunk size width {width}" + ))); + } + }; + *offset = end; + Ok(size) + } + + fn expected_fixed_bytes(num_values: u64, bits_per_value: u64, label: &str) -> Result { + let bits = num_values.checked_mul(bits_per_value).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} decoded bit length overflows").into(), + ) + })?; + usize_from_u64(bits.div_ceil(8), label) + } + + fn validate_fixed_buffer( + buffer: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + label: &str, + ) -> Result<()> { + let expected = Self::expected_fixed_bytes(num_values, bits_per_value, label)?; + if buffer.len() != expected { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} buffer has {} bytes, expected {}", + buffer.len(), + expected + ) + .into(), + )); + } + Ok(()) + } + + fn validate_variable_buffer( + buffer: &LanceBuffer, + num_values: u64, + bits_per_offset: u64, + ) -> Result<()> { + let width = usize_from_u64(bits_per_offset / 8, "variable offset width")?; + let offset_count = num_values.checked_add(1).ok_or_else(|| { + Error::invalid_input_source("Sparse variable offset count overflows".into()) + })?; + let table_len = usize_from_u64(offset_count, "variable offset count")? + .checked_mul(width) + .ok_or_else(|| { + Error::invalid_input_source("Sparse variable offset table size overflows".into()) + })?; + if buffer.len() < table_len { + return Err(Error::invalid_input_source( + format!( + "Sparse variable buffer has {} bytes, smaller than its {}-byte offset table", + buffer.len(), + table_len + ) + .into(), + )); + } + let mut previous = None; + for index in 0..usize_from_u64(offset_count, "variable offset count")? { + let start = index.checked_mul(width).ok_or_else(|| { + Error::invalid_input_source("Sparse variable offset index overflows".into()) + })?; + let end = start.checked_add(width).ok_or_else(|| { + Error::invalid_input_source("Sparse variable offset range overflows".into()) + })?; + let bytes = buffer.as_ref().get(start..end).ok_or_else(|| { + Error::invalid_input_source("Sparse variable offset table is truncated".into()) + })?; + let offset = match width { + 4 => u64::from(u32::from_le_bytes(bytes.try_into().map_err(|_| { + Error::invalid_input_source("Sparse variable u32 offset is malformed".into()) + })?)), + 8 => u64::from_le_bytes(bytes.try_into().map_err(|_| { + Error::invalid_input_source("Sparse variable u64 offset is malformed".into()) + })?), + _ => { + return Err(Error::invalid_input_source( + format!( + "Sparse variable offset width {} is unsupported", + bits_per_offset + ) + .into(), + )); + } + }; + if offset < table_len as u64 || offset > buffer.len() as u64 { + return Err(Error::invalid_input_source( + format!( + "Sparse variable offset {} is outside payload range {}..{}", + offset, + table_len, + buffer.len() + ) + .into(), + )); + } + if previous.is_some_and(|previous| offset < previous) { + return Err(Error::invalid_input_source( + "Sparse variable offsets are not monotonically increasing".into(), + )); + } + previous = Some(offset); + } + Ok(()) + } + + fn validate_fsl_buffers( + fsl: &pb21::FixedSizeList, + buffers: &[LanceBuffer], + num_values: u64, + buffer_index: &mut usize, + ) -> Result<()> { + use pb21::compressive_encoding::Compression; + + let child_values = num_values.checked_mul(fsl.items_per_value).ok_or_else(|| { + Error::invalid_input_source("Sparse fixed-size-list value count overflows".into()) + })?; + if fsl.has_validity { + let validity = buffers.get(*buffer_index).ok_or_else(|| { + Error::invalid_input_source( + "Sparse fixed-size-list value validity buffer is missing".into(), + ) + })?; + Self::validate_fixed_buffer(validity, child_values, 1, "fixed-size-list validity")?; + *buffer_index = buffer_index.checked_add(1).ok_or_else(|| { + Error::invalid_input_source("Sparse fixed-size-list buffer index overflows".into()) + })?; + } + let values = fsl.values.as_deref().ok_or_else(|| { + Error::invalid_input_source("Sparse fixed-size-list value encoding is missing".into()) + })?; + match values.compression.as_ref() { + Some(Compression::FixedSizeList(inner)) => { + Self::validate_fsl_buffers(inner, buffers, child_values, buffer_index) + } + Some(Compression::Flat(flat)) => { + let values = buffers.get(*buffer_index).ok_or_else(|| { + Error::invalid_input_source( + "Sparse fixed-size-list leaf value buffer is missing".into(), + ) + })?; + Self::validate_fixed_buffer( + values, + child_values, + flat.bits_per_value, + "fixed-size-list leaf values", + )?; + *buffer_index = buffer_index.checked_add(1).ok_or_else(|| { + Error::invalid_input_source( + "Sparse fixed-size-list buffer index overflows".into(), + ) + })?; + Ok(()) + } + _ => Err(Error::invalid_input_source( + "Sparse fixed-size-list value encoding is malformed".into(), + )), + } + } + + fn validate_value_buffers(&self, buffers: &[LanceBuffer], num_values: u64) -> Result<()> { + use pb21::compressive_encoding::Compression; + + let compression = self.value_encoding.compression.as_ref().ok_or_else(|| { + Error::invalid_input_source("Sparse value compression is missing".into()) + })?; + match compression { + Compression::Flat(flat) => Self::validate_fixed_buffer( + buffers.first().ok_or_else(|| { + Error::invalid_input_source("Sparse flat value buffer is missing".into()) + })?, + num_values, + flat.bits_per_value, + "flat values", + ), + Compression::InlineBitpacking(bitpacking) => { + let buffer = buffers.first().ok_or_else(|| { + Error::invalid_input_source( + "Sparse inline-bitpacked value buffer is missing".into(), + ) + })?; + if num_values > 1024 { + return Err(Error::invalid_input_source( + format!( + "Sparse inline-bitpacked chunk has {} values, exceeding 1024", + num_values + ) + .into(), + )); + } + let word_bytes = usize_from_u64( + bitpacking.uncompressed_bits_per_value / 8, + "inline bitpacking word width", + )?; + let header = buffer.as_ref().get(..word_bytes).ok_or_else(|| { + Error::invalid_input_source( + "Sparse inline-bitpacked buffer is missing its bit-width header".into(), + ) + })?; + let bit_width = + header + .iter() + .enumerate() + .try_fold(0_u64, |value, (idx, byte)| { + let shift = u32::try_from(idx.checked_mul(8).ok_or_else(|| { + Error::invalid_input_source( + "Sparse inline bit-width shift overflows".into(), + ) + })?) + .map_err(|_| { + Error::invalid_input_source( + "Sparse inline bit-width shift exceeds u32".into(), + ) + })?; + Ok::<_, Error>(value | (u64::from(*byte) << shift)) + })?; + if bit_width > bitpacking.uncompressed_bits_per_value { + return Err(Error::invalid_input_source( + format!( + "Sparse inline bit width {} exceeds uncompressed width {}", + bit_width, bitpacking.uncompressed_bits_per_value + ) + .into(), + )); + } + let payload_bytes = usize_from_u64( + bit_width.checked_mul(1024).ok_or_else(|| { + Error::invalid_input_source( + "Sparse inline-bitpacked payload size overflows".into(), + ) + })? / 8, + "inline-bitpacked payload size", + )?; + let expected = word_bytes.checked_add(payload_bytes).ok_or_else(|| { + Error::invalid_input_source( + "Sparse inline-bitpacked buffer size overflows".into(), + ) + })?; + if buffer.len() != expected { + return Err(Error::invalid_input_source( + format!( + "Sparse inline-bitpacked buffer has {} bytes, expected {}", + buffer.len(), + expected + ) + .into(), + )); + } + Ok(()) + } + Compression::Variable(variable) => { + let offsets = variable + .offsets + .as_deref() + .and_then(|encoding| encoding.compression.as_ref()) + .and_then(|compression| match compression { + Compression::Flat(flat) => Some(flat), + _ => None, + }) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse variable offset encoding is malformed".into(), + ) + })?; + Self::validate_variable_buffer( + buffers.first().ok_or_else(|| { + Error::invalid_input_source( + "Sparse variable value buffer is missing".into(), + ) + })?, + num_values, + offsets.bits_per_value, + ) + } + Compression::Fsst(fsst) => { + let variable = fsst + .values + .as_deref() + .and_then(|encoding| encoding.compression.as_ref()) + .and_then(|compression| match compression { + Compression::Variable(variable) => Some(variable), + _ => None, + }) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse FSST value encoding is malformed".into(), + ) + })?; + let offsets = variable + .offsets + .as_deref() + .and_then(|encoding| encoding.compression.as_ref()) + .and_then(|compression| match compression { + Compression::Flat(flat) => Some(flat), + _ => None, + }) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse FSST offset encoding is malformed".into(), + ) + })?; + Self::validate_variable_buffer( + buffers.first().ok_or_else(|| { + Error::invalid_input_source("Sparse FSST value buffer is missing".into()) + })?, + num_values, + offsets.bits_per_value, + ) + } + Compression::ByteStreamSplit(split) => { + let bits = split + .values + .as_deref() + .and_then(|encoding| encoding.compression.as_ref()) + .and_then(|compression| match compression { + Compression::Flat(flat) => Some(flat.bits_per_value), + _ => None, + }) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse byte-stream-split encoding is malformed".into(), + ) + })?; + Self::validate_fixed_buffer( + buffers.first().ok_or_else(|| { + Error::invalid_input_source( + "Sparse byte-stream-split buffer is missing".into(), + ) + })?, + num_values, + bits, + "byte-stream-split values", + ) + } + Compression::FixedSizeList(fsl) => { + let mut buffer_index = 0; + Self::validate_fsl_buffers(fsl, buffers, num_values, &mut buffer_index)?; + if buffer_index != buffers.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse fixed-size-list descriptor consumed {} of {} buffers", + buffer_index, + buffers.len() + ) + .into(), + )); + } + Ok(()) + } + Compression::PackedStruct(packed) => { + let bits = packed.bits_per_value.iter().try_fold(0_u64, |sum, bits| { + sum.checked_add(*bits).ok_or_else(|| { + Error::invalid_input_source( + "Sparse packed-struct bit width sum overflows".into(), + ) + }) + })?; + Self::validate_fixed_buffer( + buffers.first().ok_or_else(|| { + Error::invalid_input_source( + "Sparse packed-struct value buffer is missing".into(), + ) + })?, + num_values, + bits, + "packed-struct values", + ) + } + Compression::Rle(rle) => { + if buffers.len() != 2 { + return Err(Error::invalid_input_source( + format!( + "Sparse RLE value chunk has {} buffers, expected 2", + buffers.len() + ) + .into(), + )); + } + SparseStructuralScheduler::validate_general_child_buffer( + SparseStructuralScheduler::require_encoding(&rle.values, "RLE values")?, + buffers + .first() + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse RLE value buffer is missing after count validation".into(), + ) + })? + .as_ref(), + "value chunk RLE values", + )?; + SparseStructuralScheduler::validate_general_child_buffer( + SparseStructuralScheduler::require_encoding( + &rle.run_lengths, + "RLE run lengths", + )?, + buffers + .get(1) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse RLE run-length buffer is missing after count validation" + .into(), + ) + })? + .as_ref(), + "value chunk RLE run lengths", + ) + } + Compression::General(general) => { + let buffer = buffers.first().ok_or_else(|| { + Error::invalid_input_source( + "Sparse general-compressed value chunk is missing its first buffer".into(), + ) + })?; + SparseStructuralScheduler::validate_general_buffer_header( + general, + buffer.as_ref(), + "value chunk", + ) + } + _ => Err(Error::invalid_input_source( + "Sparse value chunk uses an unsupported compression descriptor".into(), + )), + } + } + + fn loaded_chunk(&self, chunk_idx: usize) -> Result<&LoadedChunk> { + let index = self + .loaded_chunks + .binary_search_by_key(&chunk_idx, |chunk| chunk.chunk_idx) + .map_err(|_| { + Error::internal(format!( + "Sparse structural decode missing loaded value chunk {}", + chunk_idx + )) + })?; + self.loaded_chunks.get(index).ok_or_else(|| { + Error::internal(format!( + "Sparse structural loaded chunk index {} is missing", + index + )) + }) + } + + fn decode_value_chunk(&self, chunk: &LoadedChunk) -> Result { + let buf = &chunk.data; + if buf.len() < 2 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} is too small for its header: {} bytes", + chunk.chunk_idx, + buf.len() + ) + .into(), + )); + } + let num_levels = u16::from_le_bytes( + buf.as_ref() + .get(..2) + .and_then(|bytes| bytes.try_into().ok()) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} has a malformed level header", + chunk.chunk_idx + ) + .into(), + ) + })?, + ); + let mut offset: usize = 2; + if num_levels != 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk unexpectedly contains {} rep/def levels", + num_levels + ) + .into(), + )); + } + + let size_width = if self.has_large_chunk { 4 } else { 2 }; + let num_buffers = usize::try_from(self.num_buffers).map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk has too many buffers: {}", + self.num_buffers + ) + .into(), + ) + })?; + let sizes_len = num_buffers.checked_mul(size_width).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural value chunk buffer-size header overflows".into(), + ) + })?; + let header_len = offset.checked_add(sizes_len).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural value chunk header length overflows".into(), + ) + })?; + if buf.len() < header_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} is too small for {} buffer sizes: {} bytes", + chunk.chunk_idx, + self.num_buffers, + buf.len() + ) + .into(), + )); + } + let buffer_sizes = (0..num_buffers) + .map(|_| Self::read_chunk_size(buf, &mut offset, size_width, chunk.chunk_idx)) + .collect::>>()?; + + offset = offset + .checked_add(pad_bytes::(offset)) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} padded header overflows", + chunk.chunk_idx + ) + .into(), + ) + })?; + if offset > buf.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} is missing padding after its header", + chunk.chunk_idx + ) + .into(), + )); + } + let buffers = buffer_sizes + .into_iter() + .map(|buf_size| { + let buf_size = buf_size as usize; + let end = offset.checked_add(buf_size).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} buffer size overflows", + chunk.chunk_idx + ) + .into(), + ) + })?; + if end > buf.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} buffer extends past chunk end", + chunk.chunk_idx + ) + .into(), + )); + } + let buffer = buf.slice_with_length(offset, buf_size); + offset = end; + offset = offset + .checked_add(pad_bytes::(offset)) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} padded buffer range overflows", + chunk.chunk_idx + ) + .into(), + ) + })?; + if offset > buf.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} padding extends past chunk end", + chunk.chunk_idx + ) + .into(), + )); + } + Ok(buffer) + }) + .collect::>>()?; + + if offset != buf.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} consumed {} of {} bytes", + chunk.chunk_idx, + offset, + buf.len() + ) + .into(), + )); + } + + self.validate_value_buffers(&buffers, chunk.items_in_chunk)?; + + let decoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + self.value_decompressor + .decompress(buffers, chunk.items_in_chunk) + })) + .map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} decompression panicked", + chunk.chunk_idx + ) + .into(), + ) + })? + .map_err(|error| { + Error::invalid_input_source( + format!( + "Sparse structural value chunk {} decompression failed: {error}", + chunk.chunk_idx + ) + .into(), + ) + })?; + if decoded.num_values() != chunk.items_in_chunk { + return Err(Error::invalid_input_source( + format!( + "Sparse structural value chunk {} decoded {} values, expected {}", + chunk.chunk_idx, + decoded.num_values(), + chunk.items_in_chunk + ) + .into(), + )); + } + Ok(decoded) + } + + fn append_value_range( + &self, + value_range: Range, + data_builder: &mut DataBlockBuilder, + chunk_cache: &mut Option<(usize, DataBlock)>, + ) -> Result<()> { + let mut value_start = value_range.start; + while value_start < value_range.end { + let chunk_idx = SparseStructuralScheduler::value_chunk_index( + &self.page_meta.chunk_value_offsets, + value_start, + )?; + let chunk_value_start = *self + .page_meta + .chunk_value_offsets + .get(chunk_idx) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse value chunk {} has no start offset", chunk_idx).into(), + ) + })?; + let chunk_value_end = *self + .page_meta + .chunk_value_offsets + .get(chunk_idx.checked_add(1).ok_or_else(|| { + Error::invalid_input_source("Sparse value chunk index overflows".into()) + })?) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse value chunk {} has no end offset", chunk_idx).into(), + ) + })?; + let take_end = value_range.end.min(chunk_value_end); + if value_start < chunk_value_start || take_end <= value_start { + return Err(Error::invalid_input_source( + format!( + "Sparse value range {}..{} does not make progress in chunk {} covering {}..{}", + value_range.start, + value_range.end, + chunk_idx, + chunk_value_start, + chunk_value_end + ) + .into(), + )); + } + + if !matches!(chunk_cache, Some((cached_idx, _)) if *cached_idx == chunk_idx) { + let chunk = self.loaded_chunk(chunk_idx)?; + *chunk_cache = Some((chunk_idx, self.decode_value_chunk(chunk)?)); + } + let values = &chunk_cache + .as_ref() + .ok_or_else(|| Error::internal("Sparse structural chunk cache is empty"))? + .1; + data_builder.append( + values, + value_start - chunk_value_start..take_end - chunk_value_start, + )?; + value_start = take_end; + } + Ok(()) + } + + fn decode_checked(self) -> Result { + let selection = slice_sparse_plan( + &self.page_meta.plan, + &self.row_ranges, + self.page_meta.row_domain, + )?; + let estimated_size_bytes = self + .loaded_chunks + .iter() + .map(|chunk| chunk.data.len()) + .try_fold(0_usize, |total, len| total.checked_add(len)) + .and_then(|total| total.checked_mul(2)) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural decode size estimate overflows".into(), + ) + })?; + let mut data_builder = DataBlockBuilder::with_capacity_estimate( + u64::try_from(estimated_size_bytes).map_err(|_| { + Error::invalid_input_source("Sparse structural decode size exceeds u64::MAX".into()) + })?, + ); + let mut chunk_cache: Option<(usize, DataBlock)> = None; + let mut appended_values = false; + for value_range in &selection.leaf_ranges { + self.append_value_range(value_range.clone(), &mut data_builder, &mut chunk_cache)?; + appended_values = true; + } + + let data = if appended_values { + data_builder.finish() + } else { + DataBlock::from_array(new_empty_array(&self.data_type)) + }; + let unraveler = RepDefUnraveler::new_sparse(selection.plan); + Ok(DecodedPage { + data, + repdef: unraveler, + }) + } +} + +impl DecodePageTask for DecodeSparseStructuralTask { + fn decode(self: Box) -> Result { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (*self).decode_checked())) + .map_err(|_| { + Error::invalid_input_source( + "Sparse structural page decoding panicked on malformed input".into(), + ) + })? + } +} + +struct SparseStructuralSelection { + plan: SparseStructuralPlan, + leaf_ranges: Vec>, +} + +struct SparsePositionSelection { + positions: SparsePositionSet, + ordinal_ranges: Vec>, +} + +fn validate_slice_ranges(ranges: &[Range], domain_len: u64, label: &str) -> Result { + let mut total = 0_u64; + for range in ranges { + if range.start > range.end || range.end > domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} slice {}..{} is outside domain {}", + range.start, range.end, domain_len + ) + .into(), + )); + } + total = total.checked_add(range.end - range.start).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} slice length overflows").into(), + ) + })?; + } + Ok(total) +} + +fn push_coalesced_range(ranges: &mut Vec>, range: Range) { + if range.is_empty() { + return; + } + if let Some(last) = ranges.last_mut() + && last.end == range.start + { + last.end = range.end; + return; + } + ranges.push(range); +} + +fn position_segments_to_set( + segments: Vec>, + output_domain: u64, + label: &str, +) -> Result { + let segments = coalesce_ranges(segments); + if segments.is_empty() { + return Ok(SparsePositionSet::empty()); + } + if segments.len() == 1 { + let segment = segments.first().ok_or_else(|| { + Error::internal("Sparse structural segment unexpectedly missing".to_string()) + })?; + if segment.start == 0 && segment.end == output_domain { + return Ok(SparsePositionSet::all(output_domain)); + } + return Ok(SparsePositionSet::range( + segment.start, + segment.end - segment.start, + )); + } + + let total_len = segments + .iter() + .map(|range| range.end - range.start) + .try_fold(0_u64, |sum, len| { + sum.checked_add(len).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} segment length overflows").into(), + ) + }) + })?; + let total_len = usize::try_from(total_len).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} segment length exceeds usize::MAX").into(), + ) + })?; + let mut positions = Vec::with_capacity(total_len); + for segment in segments { + positions.extend(segment); + } + SparsePositionSet::from_positions(positions, output_domain, label) +} + +fn select_position_set( + positions: &SparsePositionSet, + ranges: &[Range], + domain_len: u64, + label: &str, +) -> Result { + let output_domain = validate_slice_ranges(ranges, domain_len, label)?; + let mut segments = Vec::new(); + let mut ordinal_ranges = Vec::new(); + let mut output_base = 0_u64; + + match positions { + SparsePositionSet::Empty => {} + SparsePositionSet::All { len } => { + if *len != domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} all set length {} does not match domain {}", + len, domain_len + ) + .into(), + )); + } + for range in ranges { + let range_len = range.end - range.start; + let output_end = output_base.checked_add(range_len).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} output domain overflows").into(), + ) + })?; + push_coalesced_range(&mut segments, output_base..output_end); + push_coalesced_range(&mut ordinal_ranges, range.clone()); + output_base = output_end; + } + } + SparsePositionSet::Range { start, len } => { + let source_end = start.checked_add(*len).ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} range overflows").into(), + ) + })?; + if source_end > domain_len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} range {}..{} is outside domain {}", + start, source_end, domain_len + ) + .into(), + )); + } + for range in ranges { + let intersect_start = range.start.max(*start); + let intersect_end = range.end.min(source_end); + if intersect_start < intersect_end { + let out_start = output_base + .checked_add(intersect_start - range.start) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} output position overflows") + .into(), + ) + })?; + let out_end = output_base + .checked_add(intersect_end - range.start) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} output position overflows") + .into(), + ) + })?; + push_coalesced_range(&mut segments, out_start..out_end); + push_coalesced_range( + &mut ordinal_ranges, + intersect_start - *start..intersect_end - *start, + ); + } + output_base = output_base + .checked_add(range.end - range.start) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} output domain overflows").into(), + ) + })?; + } + } + SparsePositionSet::Explicit(source_positions) => { + let mut out_positions = Vec::new(); + for range in ranges { + let idx_start = + source_positions.partition_point(|position| *position < range.start); + let idx_end = source_positions.partition_point(|position| *position < range.end); + if idx_start < idx_end { + let selected_positions = + source_positions.get(idx_start..idx_end).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} explicit position slice is invalid" + ) + .into(), + ) + })?; + for position in selected_positions { + out_positions.push( + output_base + .checked_add(*position - range.start) + .ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural {label} output position overflows" + ) + .into(), + ) + })?, + ); + } + push_coalesced_range(&mut ordinal_ranges, idx_start as u64..idx_end as u64); + } + output_base = output_base + .checked_add(range.end - range.start) + .ok_or_else(|| { + Error::invalid_input_source( + format!("Sparse structural {label} output domain overflows").into(), + ) + })?; + } + let positions = SparsePositionSet::from_positions(out_positions, output_domain, label)?; + return Ok(SparsePositionSelection { + positions, + ordinal_ranges, + }); + } + } + + Ok(SparsePositionSelection { + positions: position_segments_to_set(segments, output_domain, label)?, + ordinal_ranges, + }) +} + +fn select_validity_set( + validity: &SparseValiditySet, + ranges: &[Range], + domain_len: u64, + label: &str, +) -> Result { + Ok(SparseValiditySet { + meaning: validity.meaning, + positions: select_position_set(&validity.positions, ranges, domain_len, label)?.positions, + }) +} + +fn offsets_from_counts(counts: &[u64]) -> Result> { + let mut offsets = Vec::with_capacity(counts.len() + 1); + let mut offset = 0_u64; + offsets.push(offset); + for count in counts { + offset = offset.checked_add(*count).ok_or_else(|| { + Error::invalid_input_source("Sparse structural list count offsets overflow".into()) + })?; + offsets.push(offset); + } + Ok(offsets) +} + +fn coalesce_ranges(ranges: Vec>) -> Vec> { + let mut coalesced: Vec> = Vec::with_capacity(ranges.len()); + for range in ranges { + if range.is_empty() { + continue; + } + if let Some(last) = coalesced.last_mut() + && last.end == range.start + { + last.end = range.end; + continue; + } + coalesced.push(range); + } + coalesced +} + +fn slice_list_layer( + num_slots: u64, + num_child_slots: u64, + non_empty_positions: &SparsePositionSet, + counts: &SparseCountSet, + validity: &SparseValiditySet, + ranges: &[Range], +) -> Result<(SparseStructuralLayerPlan, Vec>)> { + if non_empty_positions.len() != counts.len() { + return Err(Error::invalid_input_source( + "Sparse structural list has mismatched non-empty positions and counts".into(), + )); + } + let count_sum = counts.sum()?; + if count_sum != num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list count sum {} does not match child slots {}", + count_sum, num_child_slots + ) + .into(), + )); + } + + let non_empty_selection = + select_position_set(non_empty_positions, ranges, num_slots, "list non-empty")?; + let out_counts = select_count_set( + counts, + &non_empty_selection.ordinal_ranges, + non_empty_selection.positions.len(), + )?; + let child_ranges = + child_ranges_from_counts(counts, num_child_slots, &non_empty_selection.ordinal_ranges)?; + let out_validity = select_validity_set(validity, ranges, num_slots, "list validity")?; + let out_num_slots = validate_slice_ranges(ranges, num_slots, "list")?; + let out_num_child_slots = out_counts.sum()?; + Ok(( + SparseStructuralLayerPlan::List { + num_slots: out_num_slots, + num_child_slots: out_num_child_slots, + non_empty_positions: non_empty_selection.positions, + counts: out_counts, + validity: out_validity, + }, + child_ranges, + )) +} + +fn select_count_set( + counts: &SparseCountSet, + ordinal_ranges: &[Range], + selected_len: u64, +) -> Result { + match counts { + SparseCountSet::Empty => { + if selected_len != 0 { + return Err(Error::invalid_input_source( + "Sparse structural selected non-empty positions but counts are empty".into(), + )); + } + Ok(SparseCountSet::Empty) + } + SparseCountSet::Constant { value, len } => { + validate_ordinal_ranges(ordinal_ranges, *len, "constant list counts")?; + Ok(SparseCountSet::constant(*value, selected_len)) + } + SparseCountSet::Explicit { + counts: source_counts, + .. + } => { + validate_ordinal_ranges( + ordinal_ranges, + source_counts.len() as u64, + "explicit list counts", + )?; + let selected_len = usize::try_from(selected_len).map_err(|_| { + Error::invalid_input_source( + "Sparse structural selected count length exceeds usize::MAX".into(), + ) + })?; + let mut out_counts = Vec::with_capacity(selected_len); + for range in ordinal_ranges { + let start = usize::try_from(range.start).map_err(|_| { + Error::invalid_input_source( + "Sparse structural count ordinal exceeds usize::MAX".into(), + ) + })?; + let end = usize::try_from(range.end).map_err(|_| { + Error::invalid_input_source( + "Sparse structural count ordinal exceeds usize::MAX".into(), + ) + })?; + out_counts.extend_from_slice(source_counts.get(start..end).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural explicit count slice is invalid".into(), + ) + })?); + } + SparseCountSet::from_counts(out_counts) + } + } +} + +fn validate_ordinal_ranges(ranges: &[Range], len: u64, label: &str) -> Result<()> { + for range in ranges { + if range.start > range.end || range.end > len { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} ordinal range {}..{} is outside {} values", + range.start, range.end, len + ) + .into(), + )); + } + } + Ok(()) +} + +fn child_ranges_from_counts( + counts: &SparseCountSet, + num_child_slots: u64, + ordinal_ranges: &[Range], +) -> Result>> { + if ordinal_ranges.is_empty() { + return Ok(Vec::new()); + } + let child_ranges = match counts { + SparseCountSet::Empty => { + return Err(Error::invalid_input_source( + "Sparse structural selected non-empty positions but counts are empty".into(), + )); + } + SparseCountSet::Constant { value, len } => { + let expected_child_slots = value.checked_mul(*len).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list constant count sum overflows child slots".into(), + ) + })?; + if expected_child_slots != num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list constant count sum {} does not match child slots {}", + expected_child_slots, num_child_slots + ) + .into(), + )); + } + validate_ordinal_ranges(ordinal_ranges, *len, "constant list counts")?; + ordinal_ranges + .iter() + .map(|range| { + let start = range.start.checked_mul(*value).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list child range start overflows".into(), + ) + })?; + let end = range.end.checked_mul(*value).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list child range end overflows".into(), + ) + })?; + Ok(start..end) + }) + .collect::>>()? + } + SparseCountSet::Explicit { + counts, + offsets: value_offsets, + } => { + let last_offset = *value_offsets.last().ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list count offsets are unexpectedly empty".into(), + ) + })?; + if last_offset != num_child_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list count sum {} does not match child slots {}", + last_offset, num_child_slots + ) + .into(), + )); + } + validate_ordinal_ranges(ordinal_ranges, counts.len() as u64, "explicit list counts")?; + ordinal_ranges + .iter() + .map(|range| { + let start = usize::try_from(range.start).map_err(|_| { + Error::invalid_input_source( + "Sparse structural count ordinal exceeds usize::MAX".into(), + ) + })?; + let end = usize::try_from(range.end).map_err(|_| { + Error::invalid_input_source( + "Sparse structural count ordinal exceeds usize::MAX".into(), + ) + })?; + let start_offset = *value_offsets.get(start).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list start offset is missing".into(), + ) + })?; + let end_offset = *value_offsets.get(end).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list end offset is missing".into(), + ) + })?; + Ok(start_offset..end_offset) + }) + .collect::>>()? + } + }; + Ok(coalesce_ranges(child_ranges)) +} + +fn slice_sparse_plan( + plan: &SparseStructuralPlan, + row_ranges: &[Range], + row_domain: u64, +) -> Result { + plan.validate(row_domain)?; + let mut selected_ranges = row_ranges.to_vec(); + let mut selected_domain = row_domain; + let mut sliced_layers = Vec::with_capacity(plan.layers.len()); + + for layer in &plan.layers { + match layer { + SparseStructuralLayerPlan::Validity { + num_slots, + validity, + } => { + if selected_domain != *num_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural validity slice domain {} does not match {} slots", + selected_domain, num_slots + ) + .into(), + )); + } + let out_num_slots = + validate_slice_ranges(&selected_ranges, *num_slots, "validity")?; + sliced_layers.push(SparseStructuralLayerPlan::Validity { + num_slots: out_num_slots, + validity: select_validity_set( + validity, + &selected_ranges, + *num_slots, + "validity", + )?, + }); + } + SparseStructuralLayerPlan::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + .. + } => { + if selected_domain != *num_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list slice domain {} does not match {} slots", + selected_domain, num_slots + ) + .into(), + )); + } + let (sliced_layer, child_ranges) = slice_list_layer( + *num_slots, + *num_child_slots, + non_empty_positions, + counts, + validity, + &selected_ranges, + )?; + sliced_layers.push(sliced_layer); + selected_ranges = child_ranges; + selected_domain = *num_child_slots; + } + SparseStructuralLayerPlan::FixedSizeList { + num_slots, + dimension, + validity, + } => { + if selected_domain != *num_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list slice domain {} does not match {} slots", + selected_domain, num_slots + ) + .into(), + )); + } + let num_child_slots = num_slots.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count overflows: slots={}, dimension={}", + num_slots, dimension + ) + .into(), + ) + })?; + let out_num_slots = + validate_slice_ranges(&selected_ranges, *num_slots, "fixed-size-list")?; + let child_ranges = selected_ranges + .iter() + .map(|range| { + let start = range.start.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural fixed-size-list child range start overflows" + .into(), + ) + })?; + let end = range.end.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural fixed-size-list child range end overflows" + .into(), + ) + })?; + Ok(start..end) + }) + .collect::>>()?; + sliced_layers.push(SparseStructuralLayerPlan::FixedSizeList { + num_slots: out_num_slots, + dimension: *dimension, + validity: select_validity_set( + validity, + &selected_ranges, + *num_slots, + "fixed-size-list validity", + )?, + }); + selected_ranges = child_ranges; + selected_domain = num_child_slots; + } + } + } + + if selected_domain != plan.num_visible_items { + return Err(Error::invalid_input_source( + format!( + "Sparse structural selected terminal domain {} does not match {} visible items", + selected_domain, plan.num_visible_items + ) + .into(), + )); + } + let num_visible_items = + validate_slice_ranges(&selected_ranges, plan.num_visible_items, "visible value")?; + let num_items = SparseStructuralPlan::expected_num_items(&sliced_layers, num_visible_items)?; + Ok(SparseStructuralSelection { + plan: SparseStructuralPlan { + layers: sliced_layers, + num_items, + num_visible_items, + }, + leaf_ranges: coalesce_ranges(selected_ranges), + }) +} + +#[cfg(test)] +mod tests { + use std::sync::Mutex; + + use crate::{ + compression::DefaultDecompressionStrategy, + encodings::physical::block::{CompressionConfig, CompressionScheme}, + testing::SimulatedScheduler, + }; + + use super::*; + + fn position_set( + positions: pb21::sparse_position_set::Positions, + num_positions: u64, + ) -> Option { + Some(pb21::SparsePositionSet { + positions: Some(positions), + num_positions, + }) + } + + fn position_empty() -> Option { + position_set( + pb21::sparse_position_set::Positions::Empty(pb21::SparsePositionEmpty {}), + 0, + ) + } + + fn position_all(num_positions: u64) -> Option { + position_set( + pb21::sparse_position_set::Positions::All(pb21::SparsePositionAll {}), + num_positions, + ) + } + + fn position_explicit(num_positions: u64) -> Option { + position_set( + pb21::sparse_position_set::Positions::Explicit(ProtobufUtils21::flat(64, None)), + num_positions, + ) + } + + fn general_lz4(values: CompressiveEncoding) -> CompressiveEncoding { + ProtobufUtils21::wrapped(CompressionConfig::new(CompressionScheme::Lz4, None), values) + .unwrap() + } + + fn validity( + meaning: pb21::sparse_validity_set::Meaning, + positions: Option, + ) -> Option { + Some(pb21::SparseValiditySet { + meaning: meaning as i32, + positions, + }) + } + + fn null_positions( + positions: Option, + ) -> Option { + validity( + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions, + positions, + ) + } + + fn count_empty() -> Option { + Some(pb21::SparseCountSet { + counts: Some(pb21::sparse_count_set::Counts::Empty( + pb21::SparseCountEmpty {}, + )), + }) + } + + fn count_constant(value: u64) -> Option { + Some(pb21::SparseCountSet { + counts: Some(pb21::sparse_count_set::Counts::Constant( + pb21::SparseCountConstant { value }, + )), + }) + } + + fn sparse_layout() -> pb21::SparseLayout { + pb21::SparseLayout { + value_compression: Some(ProtobufUtils21::flat(32, None)), + num_buffers: 1, + num_items: 1, + num_visible_items: 1, + has_large_chunk: false, + structural_layers: Vec::new(), + } + } + + fn validity_layer( + num_slots: u64, + validity: Option, + ) -> pb21::SparseStructuralLayer { + pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::Validity( + pb21::SparseValidityLayer { + num_slots, + validity, + }, + )), + } + } + + fn list_layer( + num_slots: u64, + num_child_slots: u64, + non_empty_positions: Option, + counts: Option, + validity: Option, + ) -> pb21::SparseStructuralLayer { + pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::List( + pb21::SparseListLayer { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + }, + )), + } + } + + fn fixed_size_list_layer( + num_slots: u64, + dimension: u64, + validity: Option, + ) -> pb21::SparseStructuralLayer { + pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::FixedSizeList( + pb21::SparseFixedSizeListLayer { + num_slots, + dimension, + validity, + }, + )), + } + } + + fn assert_invalid_input_contains(err: Error, expected: &str) { + let message = err.to_string(); + assert!( + matches!(&err, Error::InvalidInput { .. }), + "expected InvalidInput, got {err:?}" + ); + assert!( + message.contains(expected), + "expected error to contain {expected:?}, got {message}" + ); + } + + #[test] + fn rejects_missing_layer_variants_and_item_count_mismatches() { + let decompressors = DefaultDecompressionStrategy::default(); + + let mut layout = sparse_layout(); + layout + .structural_layers + .push(pb21::SparseStructuralLayer { layer: None }); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "missing its layer variant"); + + let mut layout = sparse_layout(); + layout.num_items = 2; + layout + .structural_layers + .push(validity_layer(1, null_positions(position_empty()))); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "layers imply 1"); + } + + #[test] + fn accepts_large_structural_descriptors() { + let explicit_values = 8_u64 * 1024 * 1024 + 1; + let metadata_size = explicit_values * 8; + let value_position = metadata_size; + let value_size = explicit_values * 16; + let structural_position = value_position + value_size; + let structural_size = explicit_values * std::mem::size_of::() as u64; + let mut layout = sparse_layout(); + layout.num_items = explicit_values; + layout.num_visible_items = explicit_values; + layout.structural_layers.push(validity_layer( + explicit_values, + null_positions(position_explicit(explicit_values)), + )); + + SparseStructuralScheduler::try_new( + &[ + (0, metadata_size), + (value_position, value_size), + (structural_position, structural_size), + ], + 0, + explicit_values, + DataType::Int32, + &layout, + &DefaultDecompressionStrategy::default(), + ) + .unwrap(); + } + + #[test] + fn accepts_deep_supported_value_encodings() { + let encoding = (0..300).fold(ProtobufUtils21::flat(32, None), |values, _| { + ProtobufUtils21::fsl(1, false, values) + }); + + assert_eq!( + SparseStructuralScheduler::validate_value_encoding(&encoding).unwrap(), + 1 + ); + } + + fn null_set(positions: SparsePositionSet) -> SparseValiditySet { + SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions, + } + } + + fn valid_set(positions: SparsePositionSet) -> SparseValiditySet { + SparseValiditySet { + meaning: SparseValidityMeaning::ValidPositions, + positions, + } + } + + #[test] + fn semantic_position_and_count_sets_project_without_materializing_ranges() { + let ranges = [1..4]; + assert_eq!( + select_position_set(&SparsePositionSet::Empty, &ranges, 5, "empty") + .unwrap() + .positions, + SparsePositionSet::Empty + ); + assert_eq!( + select_position_set(&SparsePositionSet::all(5), &ranges, 5, "all") + .unwrap() + .positions, + SparsePositionSet::all(3) + ); + assert_eq!( + select_position_set(&SparsePositionSet::range(1, 3), &ranges, 5, "range") + .unwrap() + .positions, + SparsePositionSet::all(3) + ); + assert_eq!( + select_position_set( + &SparsePositionSet::Explicit(vec![0, 2, 4]), + &[0..1, 4..5], + 5, + "explicit", + ) + .unwrap() + .positions, + SparsePositionSet::all(2) + ); + + assert_eq!( + select_count_set(&SparseCountSet::Empty, &[], 0).unwrap(), + SparseCountSet::Empty + ); + assert_eq!( + select_count_set(&SparseCountSet::constant(2, 3), &[0..1, 2..3], 2,).unwrap(), + SparseCountSet::constant(2, 2) + ); + assert_eq!( + select_count_set( + &SparseCountSet::from_counts(vec![1, 2, 3]).unwrap(), + &[0..1, 2..3], + 2, + ) + .unwrap(), + SparseCountSet::from_counts(vec![1, 3]).unwrap() + ); + } + + #[test] + fn validity_polarities_rebuild_the_same_arrow_domain() { + let mut null_builder = BooleanBufferBuilder::new(4); + null_set(SparsePositionSet::Explicit(vec![1, 3])) + .append_to(&mut null_builder, 4) + .unwrap(); + assert_eq!( + null_builder.finish().iter().collect::>(), + vec![true, false, true, false] + ); + + let mut valid_builder = BooleanBufferBuilder::new(4); + valid_set(SparsePositionSet::range(1, 2)) + .append_to(&mut valid_builder, 4) + .unwrap(); + assert_eq!( + valid_builder.finish().iter().collect::>(), + vec![false, true, true, false] + ); + } + + #[test] + fn schema_layer_mismatches_are_invalid_input() { + let mut missing = SparseStructuralUnraveler::new(SparseStructuralPlan { + layers: Vec::new(), + num_items: 1, + num_visible_items: 1, + }); + let mut validity = BooleanBufferBuilder::new(1); + let err = missing.unravel_validity(&mut validity).unwrap_err(); + assert_invalid_input_contains(err, "fewer layers than the Arrow schema"); + + let mut fixed_size_list = SparseStructuralUnraveler::new(SparseStructuralPlan { + layers: vec![SparseStructuralLayerPlan::FixedSizeList { + num_slots: 2, + dimension: 2, + validity: null_set(SparsePositionSet::empty()), + }], + num_items: 4, + num_visible_items: 4, + }); + let mut validity = BooleanBufferBuilder::new(2); + let err = fixed_size_list.unravel_validity(&mut validity).unwrap_err(); + assert_invalid_input_contains(err, "does not match the Arrow schema"); + + let extra = SparseStructuralUnraveler::new(SparseStructuralPlan { + layers: vec![SparseStructuralLayerPlan::Validity { + num_slots: 1, + validity: null_set(SparsePositionSet::empty()), + }], + num_items: 1, + num_visible_items: 1, + }); + let err = extra.ensure_exhausted().unwrap_err(); + assert_invalid_input_contains(err, "1 unconsumed layer"); + } + + fn nested_plan() -> SparseStructuralPlan { + SparseStructuralPlan { + layers: vec![ + SparseStructuralLayerPlan::Validity { + num_slots: 6, + validity: null_set(SparsePositionSet::Explicit(vec![1, 4])), + }, + SparseStructuralLayerPlan::List { + num_slots: 6, + num_child_slots: 5, + non_empty_positions: SparsePositionSet::Explicit(vec![0, 2, 5]), + counts: SparseCountSet::from_counts(vec![2, 1, 2]).unwrap(), + validity: null_set(SparsePositionSet::Explicit(vec![1, 4])), + }, + SparseStructuralLayerPlan::FixedSizeList { + num_slots: 5, + dimension: 2, + validity: valid_set(SparsePositionSet::range(1, 3)), + }, + ], + num_items: 13, + num_visible_items: 10, + } + } + + #[test] + fn discontiguous_projection_preserves_outer_to_inner_order() { + let selection = slice_sparse_plan(&nested_plan(), &[5..6, 0..1], 6).unwrap(); + assert_eq!(selection.leaf_ranges, vec![6..10, 0..4]); + assert_eq!(selection.plan.num_visible_items, 8); + selection.plan.validate(2).unwrap(); + + let SparseStructuralLayerPlan::List { + non_empty_positions, + counts, + .. + } = &selection.plan.layers[1] + else { + panic!("expected projected list layer"); + }; + assert_eq!(*non_empty_positions, SparsePositionSet::all(2)); + assert_eq!(*counts, SparseCountSet::constant(2, 2)); + } + + #[test] + fn no_value_projection_keeps_empty_and_null_list_structure() { + let selection = slice_sparse_plan(&nested_plan(), &[1..2, 3..4], 6).unwrap(); + assert!(selection.leaf_ranges.is_empty()); + assert_eq!(selection.plan.num_visible_items, 0); + selection.plan.validate(2).unwrap(); + + let SparseStructuralLayerPlan::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + } = &selection.plan.layers[1] + else { + panic!("expected projected list layer"); + }; + assert_eq!((*num_slots, *num_child_slots), (2, 0)); + assert_eq!(*non_empty_positions, SparsePositionSet::Empty); + assert_eq!(*counts, SparseCountSet::Empty); + assert_eq!(*validity, null_set(SparsePositionSet::range(0, 1))); + } + + #[test] + fn rejects_missing_value_compression_and_buffer_count_mismatch() { + let decompressors = DefaultDecompressionStrategy::default(); + let mut layout = sparse_layout(); + layout.value_compression = None; + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "missing value compression"); + + let mut layout = sparse_layout(); + layout.num_buffers = 2; + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "declares 2 value buffers"); + } + + #[test] + fn rejects_inconsistent_chunk_count_before_io() { + let decompressors = DefaultDecompressionStrategy::default(); + + let layout = sparse_layout(); + let err = SparseStructuralScheduler::try_new( + &[(0, 16), (16, 8)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "declares 2 chunks for 1 visible items"); + } + + #[cfg(feature = "lz4")] + #[test] + fn accepts_large_general_decompression_headers() { + let encoding = general_lz4(ProtobufUtils21::flat(64, None)); + let Some(pb21::compressive_encoding::Compression::General(general)) = + encoding.compression.as_ref() + else { + panic!("expected General compression"); + }; + let declared_size = 65_u32 * 1024 * 1024; + + SparseStructuralScheduler::validate_general_buffer_header( + general, + &declared_size.to_le_bytes(), + "test", + ) + .unwrap(); + } + + #[cfg(feature = "lz4")] + #[tokio::test] + async fn rejects_malformed_general_structural_buffers_as_invalid_input() { + let mut layout = sparse_layout(); + layout.structural_layers.push(validity_layer( + 1, + null_positions(position_set( + pb21::sparse_position_set::Positions::Explicit(general_lz4(ProtobufUtils21::flat( + 64, None, + ))), + 1, + )), + )); + let decompressors = DefaultDecompressionStrategy::default(); + + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8), (16, 8)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&[0; 8]); + data.extend_from_slice(&8_u32.to_le_bytes()); + data.extend_from_slice(&[0xff; 4]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + let Err(err) = scheduler.initialize(&io).await else { + panic!("expected malformed General buffer to be rejected"); + }; + assert_invalid_input_contains(err, "decompression failed"); + } + + #[cfg(feature = "lz4")] + #[tokio::test] + async fn rejects_malformed_general_value_buffer() { + let mut layout = sparse_layout(); + layout.value_compression = Some(general_lz4(ProtobufUtils21::flat(32, None))); + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&0_u16.to_le_bytes()); + data.extend_from_slice(&0_u16.to_le_bytes()); + data.extend_from_slice(&[0; 4]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + scheduler.initialize(&io).await.unwrap(); + let mut page_tasks = scheduler.schedule_ranges(&[0..1], &io).unwrap(); + let mut decoder = page_tasks.pop().unwrap().decoder_fut.await.unwrap(); + let Err(err) = decoder.drain(1).unwrap().decode() else { + panic!("expected malformed General value buffer to be rejected"); + }; + assert_invalid_input_contains(err, "missing its length prefix"); + } + + #[test] + fn rejects_layer_domain_and_fixed_size_list_mismatches() { + let decompressors = DefaultDecompressionStrategy::default(); + let mut layout = sparse_layout(); + layout.num_items = 2; + layout.num_visible_items = 2; + layout + .structural_layers + .push(validity_layer(1, null_positions(position_empty()))); + layout + .structural_layers + .push(validity_layer(2, null_positions(position_empty()))); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "layer 1 has 2 slots, expected 1"); + + let mut layout = sparse_layout(); + layout.num_items = 4; + layout.num_visible_items = 4; + layout.structural_layers.push(fixed_size_list_layer( + 2, + 3, + null_positions(position_empty()), + )); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 2, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "terminal domain has 6 slots"); + } + + #[test] + fn rejects_invalid_validity_and_list_count_semantics() { + let decompressors = DefaultDecompressionStrategy::default(); + let mut layout = sparse_layout(); + layout.structural_layers.push(validity_layer( + 1, + validity( + pb21::sparse_validity_set::Meaning::SparseValidityUnspecified, + position_empty(), + ), + )); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "meaning is unspecified"); + + let mut layout = sparse_layout(); + layout.num_items = 3; + layout.num_visible_items = 3; + layout.structural_layers.push(list_layer( + 2, + 3, + position_all(2), + count_constant(2), + null_positions(position_empty()), + )); + let err = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 2, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap_err(); + assert_invalid_input_contains(err, "count sum 4 does not match child slots 3"); + } + + #[tokio::test] + async fn rejects_unordered_explicit_positions_after_decompression() { + let mut layout = sparse_layout(); + layout.num_items = 4; + layout.num_visible_items = 4; + layout + .structural_layers + .push(validity_layer(4, null_positions(position_explicit(2)))); + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8), (16, 16)], + 0, + 4, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&4_u32.to_le_bytes()); + data.extend_from_slice(&[0; 8]); + data.extend_from_slice(&3_u64.to_le_bytes()); + data.extend_from_slice(&0_u64.to_le_bytes()); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + + let Err(err) = scheduler.initialize(&io).await else { + panic!("expected unordered sparse positions to be rejected"); + }; + assert_invalid_input_contains(err, "positions must be strictly increasing"); + } + + #[tokio::test] + async fn rejects_chunk_metadata_value_and_byte_sum_mismatches() { + let layout = sparse_layout(); + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let mut data = Vec::new(); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&[0; 8]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + let Err(err) = scheduler.initialize(&io).await else { + panic!("expected chunk byte sum mismatch"); + }; + assert_invalid_input_contains(err, "describes 16 value bytes"); + + let mut layout = sparse_layout(); + layout.num_items = 2; + layout.num_visible_items = 2; + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8)], + 0, + 2, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&[0; 8]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + let Err(err) = scheduler.initialize(&io).await else { + panic!("expected chunk value sum mismatch"); + }; + assert_invalid_input_contains(err, "metadata has 1, layout has 2"); + } + + #[tokio::test] + async fn rejects_malformed_value_chunk_before_decompression() { + let layout = sparse_layout(); + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, 8)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&[0; 8]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + scheduler.initialize(&io).await.unwrap(); + let mut page_tasks = scheduler.schedule_ranges(&[0..1], &io).unwrap(); + let page_task = page_tasks.pop().unwrap(); + let mut decoder = page_task.decoder_fut.await.unwrap(); + let decode_task = decoder.drain(1).unwrap(); + let Err(err) = decode_task.decode() else { + panic!("expected malformed value chunk to be rejected"); + }; + assert_invalid_input_contains(err, "flat values buffer has 0 bytes, expected 4"); + } + + #[tokio::test] + async fn accepts_value_chunks_larger_than_64_mib() { + let chunk_size = 65_u64 * 1024 * 1024; + let layout = sparse_layout(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 8), (8, chunk_size)], + 0, + 1, + DataType::Int32, + &layout, + &DefaultDecompressionStrategy::default(), + ) + .unwrap(); + let words_minus_one = u32::try_from(chunk_size / MINIBLOCK_ALIGNMENT as u64 - 1).unwrap(); + let mut metadata = Vec::new(); + metadata.extend_from_slice(&words_minus_one.to_le_bytes()); + metadata.extend_from_slice(&1_u32.to_le_bytes()); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(metadata))); + + scheduler.initialize(&io).await.unwrap(); + } + + #[tokio::test] + async fn rejects_value_chunks_above_the_miniblock_limit() { + let num_values = miniblock::MAX_CONFIGURABLE_MINIBLOCK_VALUES + 1; + let mut layout = sparse_layout(); + layout.num_items = num_values; + layout.num_visible_items = num_values; + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 16), (16, 16)], + 0, + num_values, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let mut data = Vec::new(); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&(num_values as u32).to_le_bytes()); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&0_u32.to_le_bytes()); + data.extend_from_slice(&[0; 16]); + let io: Arc = Arc::new(SimulatedScheduler::new(Bytes::from(data))); + + let Err(err) = scheduler.initialize(&io).await else { + panic!("expected oversized value chunk to be rejected"); + }; + assert_invalid_input_contains(err, "exceeding the mini-block limit"); + } + + #[derive(Debug, Clone)] + struct RecordingIo { + data: Bytes, + calls: Arc>>>>, + } + + impl RecordingIo { + fn new(data: Bytes) -> Self { + Self { + data, + calls: Arc::new(Mutex::new(Vec::new())), + } + } + } + + impl EncodingsIo for RecordingIo { + fn submit_request( + &self, + ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, Result>> { + self.calls.lock().unwrap().push(ranges.clone()); + let data = self.data.clone(); + async move { + ranges + .into_iter() + .map(|range| { + let start = usize_from_u64(range.start, "test range start")?; + let end = usize_from_u64(range.end, "test range end")?; + if start > end || end > data.len() { + return Err(Error::invalid_input_source( + "Test I/O range is outside fixture data".into(), + )); + } + Ok(data.slice(start..end)) + }) + .collect() + } + .boxed() + } + } + + #[tokio::test] + async fn selective_read_requests_only_intersecting_value_chunk() { + let mut layout = sparse_layout(); + layout.num_items = 4; + layout.num_visible_items = 4; + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 16), (16, 32)], + 0, + 4, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + + let mut data = Vec::new(); + for _ in 0..2 { + data.extend_from_slice(&1_u32.to_le_bytes()); + data.extend_from_slice(&2_u32.to_le_bytes()); + } + for values in [[10_i32, 20], [30, 40]] { + data.extend_from_slice(&0_u16.to_le_bytes()); + data.extend_from_slice(&8_u16.to_le_bytes()); + data.extend_from_slice(&[0; 4]); + for value in values { + data.extend_from_slice(&value.to_le_bytes()); + } + } + + let io = Arc::new(RecordingIo::new(Bytes::from(data))); + let trait_io: Arc = io.clone(); + scheduler.initialize(&trait_io).await.unwrap(); + let mut page_tasks = scheduler.schedule_ranges(&[2..3], &trait_io).unwrap(); + let page_task = page_tasks.pop().unwrap(); + let mut decoder = page_task.decoder_fut.await.unwrap(); + let decoded = decoder.drain(1).unwrap().decode().unwrap(); + assert_eq!(decoded.data.num_values(), 1); + + let calls = io.calls.lock().unwrap(); + assert_eq!(calls.as_slice(), &[vec![0..16], vec![32..48]]); + } + + #[tokio::test] + async fn empty_leaf_selection_rebuilds_offsets_without_value_io() { + let mut layout = sparse_layout(); + layout.num_visible_items = 0; + layout.structural_layers.push(list_layer( + 1, + 0, + position_empty(), + count_empty(), + null_positions(position_empty()), + )); + let decompressors = DefaultDecompressionStrategy::default(); + let mut scheduler = SparseStructuralScheduler::try_new( + &[(0, 0), (0, 0)], + 0, + 1, + DataType::Int32, + &layout, + &decompressors, + ) + .unwrap(); + let io = Arc::new(RecordingIo::new(Bytes::new())); + let trait_io: Arc = io.clone(); + scheduler.initialize(&trait_io).await.unwrap(); + let mut page_tasks = scheduler.schedule_ranges(&[0..1], &trait_io).unwrap(); + let page_task = page_tasks.pop().unwrap(); + let mut decoder = page_task.decoder_fut.await.unwrap(); + let decoded = decoder.drain(1).unwrap().decode().unwrap(); + assert_eq!(decoded.data.num_values(), 0); + + let mut repdef = CompositeRepDefUnraveler::new(vec![decoded.repdef]); + let (offsets, validity) = repdef.unravel_offsets::().unwrap(); + assert_eq!(offsets.as_ref(), &[0, 0]); + assert!(validity.is_none()); + + let calls = io.calls.lock().unwrap(); + assert_eq!(calls.len(), 2); + assert!(calls[1].is_empty(), "value payload must not be requested"); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse/writer.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse/writer.rs new file mode 100644 index 000000000..ad910c6d8 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/primitive/sparse/writer.rs @@ -0,0 +1,2408 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Sparse structural planning and serialization. + +use std::iter; + +use arrow_buffer::BooleanBuffer; +use lance_core::{Error, Result, datatypes::Field, utils::bit::pad_bytes}; + +use crate::{ + buffer::LanceBuffer, + compression::CompressionStrategy, + data::{BlockInfo, DataBlock, FixedWidthDataBlock}, + decoder::PageEncoding, + encoder::EncodedPage, + format::pb21::{self, CompressiveEncoding}, + repdef::{NormalizedStructuralLayer, NormalizedStructuralPlan}, + statistics::ComputeStat, +}; + +use super::super::MiniblockChunkSize; + +use super::{ + SparseCountSet, SparsePositionSet, SparseStructuralLayerPlan, SparseStructuralPlan, + SparseValidityMeaning, SparseValiditySet, +}; +use crate::encodings::logical::primitive::{ + FILL_BYTE, MINIBLOCK_ALIGNMENT, + miniblock::{MiniBlockCompressed, MiniBlockCompressionContext}, +}; + +#[derive(Clone, Copy, Default)] +struct PositionSetStats { + count: u64, + first: u64, + last: u64, + is_contiguous: bool, +} + +impl PositionSetStats { + fn observe(&mut self, position: u64) { + if self.count == 0 { + self.first = position; + self.is_contiguous = true; + } else if self.last.checked_add(1) != Some(position) { + self.is_contiguous = false; + } + self.last = position; + self.count += 1; + } + + fn encoded_cost(&self, domain_len: u64) -> u64 { + if self.count == 0 || self.count == domain_len || self.is_contiguous { + 0 + } else { + self.count + } + } + + fn to_set( + self, + validity: &BooleanBuffer, + want_valid: bool, + domain_len: u64, + label: &str, + ) -> Result { + if self.count == 0 { + return Ok(SparsePositionSet::empty()); + } + if self.count == domain_len { + return Ok(SparsePositionSet::all(domain_len)); + } + if self.is_contiguous { + return Ok(SparsePositionSet::range(self.first, self.count)); + } + + let capacity = usize::try_from(self.count).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} positions exceed usize::MAX").into(), + ) + })?; + let mut positions = Vec::with_capacity(capacity); + for (index, is_valid) in validity.iter().enumerate() { + if is_valid == want_valid { + positions.push(u64::try_from(index).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} position exceeds u64::MAX").into(), + ) + })?); + } + } + SparsePositionSet::from_positions(positions, domain_len, label) + } +} + +fn usize_to_u64(value: usize, label: &str) -> Result { + u64::try_from(value).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural {label} {value} exceeds u64::MAX").into(), + ) + }) +} + +fn validity_set( + validity: Option<&BooleanBuffer>, + num_slots: usize, + label: &str, +) -> Result { + let Some(validity) = validity else { + return Ok(SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions: SparsePositionSet::empty(), + }); + }; + if validity.len() != num_slots { + return Err(Error::invalid_input_source( + format!( + "Sparse structural {label} validity length {} does not match {} slots", + validity.len(), + num_slots + ) + .into(), + )); + } + + let domain_len = usize_to_u64(num_slots, "validity domain")?; + let mut valid_stats = PositionSetStats::default(); + let mut null_stats = PositionSetStats::default(); + for (index, is_valid) in validity.iter().enumerate() { + let index = usize_to_u64(index, "validity position")?; + if is_valid { + valid_stats.observe(index); + } else { + null_stats.observe(index); + } + } + + if null_stats.count == 0 { + return Ok(SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions: SparsePositionSet::empty(), + }); + } + if valid_stats.count == 0 { + return Ok(SparseValiditySet { + meaning: SparseValidityMeaning::ValidPositions, + positions: SparsePositionSet::empty(), + }); + } + + let valid_cost = valid_stats.encoded_cost(domain_len); + let null_cost = null_stats.encoded_cost(domain_len); + if valid_cost < null_cost { + Ok(SparseValiditySet { + meaning: SparseValidityMeaning::ValidPositions, + positions: valid_stats.to_set(validity, true, domain_len, label)?, + }) + } else { + Ok(SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions: null_stats.to_set(validity, false, domain_len, label)?, + }) + } +} + +/// Builds the semantic sparse plan directly from the once-normalized Arrow layers. +pub(in crate::encodings::logical::primitive) fn plan( + normalized: &NormalizedStructuralPlan, + num_visible_items: u64, +) -> Result { + let mut layers = Vec::with_capacity(normalized.layers().len()); + let mut num_items = num_visible_items; + + for layer in normalized.layers() { + match layer { + NormalizedStructuralLayer::Validity { + validity, + num_slots, + } => { + layers.push(SparseStructuralLayerPlan::Validity { + num_slots: usize_to_u64(num_slots, "validity slot count")?, + validity: validity_set(validity, num_slots, "validity")?, + }); + } + NormalizedStructuralLayer::FixedSizeList { + validity, + dimension, + num_slots, + } => { + if dimension == 0 { + return Err(Error::invalid_input_source( + "Sparse structural fixed-size-list dimension is zero".into(), + )); + } + layers.push(SparseStructuralLayerPlan::FixedSizeList { + num_slots: usize_to_u64(num_slots, "fixed-size-list slot count")?, + dimension: usize_to_u64(dimension, "fixed-size-list dimension")?, + validity: validity_set(validity, num_slots, "fixed-size-list validity")?, + }); + } + NormalizedStructuralLayer::List { + offsets, + validity, + num_slots, + } => { + let expected_offsets = num_slots.checked_add(1).ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural list offset count overflows".into(), + ) + })?; + if offsets.len() != expected_offsets { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list has {} offsets for {} slots", + offsets.len(), + num_slots + ) + .into(), + )); + } + if offsets.first().copied() != Some(0) { + return Err(Error::invalid_input_source( + "Sparse structural list offsets must start at zero".into(), + )); + } + + let mut non_empty_positions = Vec::new(); + let mut counts = Vec::new(); + for slot in 0..num_slots { + let start = offsets[slot]; + let end = offsets[slot + 1]; + let count = end.checked_sub(start).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural list offsets decrease at slot {slot}: {start}..{end}" + ) + .into(), + ) + })?; + let is_valid = validity.is_none_or(|validity| validity.value(slot)); + if !is_valid && count != 0 { + return Err(Error::invalid_input_source( + format!( + "Sparse structural null list slot {slot} has {count} child slots" + ) + .into(), + )); + } + if is_valid && count > 0 { + non_empty_positions.push(usize_to_u64(slot, "list position")?); + counts.push(u64::try_from(count).map_err(|_| { + Error::invalid_input_source( + format!("Sparse structural list count {count} exceeds u64::MAX") + .into(), + ) + })?); + } + } + + let num_slots_u64 = usize_to_u64(num_slots, "list slot count")?; + let num_non_empty = usize_to_u64(non_empty_positions.len(), "list position count")?; + num_items = num_items + .checked_add(num_slots_u64 - num_non_empty) + .ok_or_else(|| { + Error::invalid_input_source( + "Sparse structural item count overflows u64".into(), + ) + })?; + let num_child_slots = offsets.last().copied().ok_or_else(|| { + Error::invalid_input_source("Sparse structural list has no offsets".into()) + })?; + let num_child_slots = u64::try_from(num_child_slots).map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse structural list child slot count {num_child_slots} is negative" + ) + .into(), + ) + })?; + layers.push(SparseStructuralLayerPlan::List { + num_slots: num_slots_u64, + num_child_slots, + non_empty_positions: SparsePositionSet::from_positions( + non_empty_positions, + num_slots_u64, + "list non-empty", + )?, + counts: SparseCountSet::from_counts(counts)?, + validity: validity_set(validity, num_slots, "list validity")?, + }); + } + } + } + + let row_domain = match layers.first() { + Some(SparseStructuralLayerPlan::Validity { num_slots, .. }) + | Some(SparseStructuralLayerPlan::List { num_slots, .. }) + | Some(SparseStructuralLayerPlan::FixedSizeList { num_slots, .. }) => *num_slots, + None => { + return Err(Error::invalid_input_source( + "Sparse structural encoding requires at least one Arrow structural layer".into(), + )); + } + }; + let plan = SparseStructuralPlan { + layers, + num_items, + num_visible_items, + }; + plan.validate(row_domain)?; + Ok(plan) +} + +/// ConstantLayout remains the canonical representation when no value payload exists. +pub(in crate::encodings::logical::primitive) fn uses_constant_layout( + plan: &SparseStructuralPlan, + field: &Field, +) -> bool { + if plan.num_visible_items == 0 { + return true; + } + if matches!(field.data_type(), arrow_schema::DataType::Struct(fields) if fields.is_empty()) { + return true; + } + + let Some(layer) = plan.layers.last() else { + return false; + }; + let (num_slots, validity) = match layer { + SparseStructuralLayerPlan::Validity { + num_slots, + validity, + } + | SparseStructuralLayerPlan::List { + num_slots, + validity, + .. + } + | SparseStructuralLayerPlan::FixedSizeList { + num_slots, + validity, + .. + } => (*num_slots, validity), + }; + match validity.meaning { + SparseValidityMeaning::NullPositions => validity.positions.len() == num_slots, + SparseValidityMeaning::ValidPositions => validity.positions.is_empty(), + } +} + +fn supports_fixed_size_list_values(data: &DataBlock) -> bool { + match data { + DataBlock::FixedWidth(_) => true, + DataBlock::FixedSizeList(list) => supports_fixed_size_list_values(list.child.as_ref()), + DataBlock::Nullable(nullable) => supports_fixed_size_list_values(nullable.data.as_ref()), + _ => false, + } +} + +/// Whether the sparse writer can encode this value block without changing its value path. +pub fn supports_value_block(data: &DataBlock) -> bool { + match data { + DataBlock::FixedWidth(_) | DataBlock::VariableWidth(_) => true, + DataBlock::Struct(data) => !data.has_variable_width_child(), + DataBlock::FixedSizeList(data) => supports_fixed_size_list_values(data.child.as_ref()), + DataBlock::Empty() + | DataBlock::Constant(_) + | DataBlock::AllNull(_) + | DataBlock::Nullable(_) + | DataBlock::Opaque(_) + | DataBlock::Dictionary(_) => false, + } +} + +struct SparseMiniBlockChunk { + buffer_sizes: Vec, + num_values: u32, +} + +struct SparseMiniBlockCompressed { + data: Vec, + chunks: Vec, +} + +struct SerializedValuePage { + num_buffers: u64, + data: LanceBuffer, + metadata: LanceBuffer, +} + +pub struct PreparedSparseValues { + num_values: u64, + value_compression: CompressiveEncoding, + values: SerializedValuePage, +} + +pub enum SparseValueInput { + Unprepared(DataBlock), + Prepared(PreparedSparseValues), +} + +struct EncodedStructuralPlan { + layers: Vec, + buffers: Vec, +} + +fn with_explicit_value_counts( + compressed: MiniBlockCompressed, +) -> Result { + let mut values_in_previous_chunks = 0_u64; + let mut chunks = Vec::with_capacity(compressed.chunks.len()); + for chunk in compressed.chunks { + let num_values = chunk.num_values(values_in_previous_chunks, compressed.num_values); + values_in_previous_chunks = values_in_previous_chunks + .checked_add(num_values) + .ok_or_else(|| Error::internal("Sparse value count overflows u64".to_string()))?; + chunks.push(SparseMiniBlockChunk { + buffer_sizes: chunk.buffer_sizes, + num_values: u32::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!( + "Sparse value chunk has {num_values} visible values, which exceeds the u32 metadata limit" + ) + .into(), + ) + })?, + }); + } + if values_in_previous_chunks != compressed.num_values { + return Err(Error::internal(format!( + "Sparse value chunks describe {values_in_previous_chunks} values, expected {}", + compressed.num_values + ))); + } + Ok(SparseMiniBlockCompressed { + data: compressed.data, + chunks, + }) +} + +fn serialize_value_chunks( + compressed: SparseMiniBlockCompressed, + miniblock_chunk_size: MiniblockChunkSize, +) -> Result { + let bytes_data = compressed.data.iter().map(LanceBuffer::len).sum::(); + let num_buffers = compressed.data.len(); + let mut data_buffer = Vec::with_capacity(bytes_data + 9 * num_buffers); + let mut metadata = Vec::with_capacity(compressed.chunks.len() * 8); + let mut buffer_offsets = vec![0_usize; num_buffers]; + + for chunk in compressed.chunks { + if chunk.buffer_sizes.len() != num_buffers { + return Err(Error::internal(format!( + "Sparse chunk has {} value buffer sizes, expected {num_buffers}", + chunk.buffer_sizes.len() + ))); + } + + let chunk_start = data_buffer.len(); + debug_assert_eq!(chunk_start % MINIBLOCK_ALIGNMENT, 0); + data_buffer.extend_from_slice(&0_u16.to_le_bytes()); + if miniblock_chunk_size == MiniblockChunkSize::U32 { + for buffer_size in &chunk.buffer_sizes { + data_buffer.extend_from_slice(&buffer_size.to_le_bytes()); + } + } else { + for buffer_size in &chunk.buffer_sizes { + let buffer_size = u16::try_from(*buffer_size).map_err(|_| { + Error::internal(format!( + "Sparse value buffer size ({buffer_size} bytes) exceeds 16-bit metadata" + )) + })?; + data_buffer.extend_from_slice(&buffer_size.to_le_bytes()); + } + } + let add_padding = |buffer: &mut Vec| { + let padding = pad_bytes::(buffer.len()); + buffer.extend(iter::repeat_n(FILL_BYTE, padding)); + }; + add_padding(&mut data_buffer); + + for (buffer_size, (buffer, buffer_offset)) in chunk + .buffer_sizes + .iter() + .zip(compressed.data.iter().zip(buffer_offsets.iter_mut())) + { + let start = *buffer_offset; + let end = start.checked_add(*buffer_size as usize).ok_or_else(|| { + Error::internal("Sparse value buffer range overflows".to_string()) + })?; + let bytes = buffer.as_ref().get(start..end).ok_or_else(|| { + Error::internal(format!( + "Sparse value chunk requests bytes {start}..{end} from a {}-byte buffer", + buffer.len() + )) + })?; + *buffer_offset = end; + data_buffer.extend_from_slice(bytes); + add_padding(&mut data_buffer); + } + + let chunk_bytes = data_buffer.len() - chunk_start; + if chunk_bytes == 0 || !chunk_bytes.is_multiple_of(MINIBLOCK_ALIGNMENT) { + return Err(Error::internal(format!( + "Sparse value chunk size {chunk_bytes} is not a positive multiple of {MINIBLOCK_ALIGNMENT}" + ))); + } + let words_minus_one = chunk_bytes / MINIBLOCK_ALIGNMENT - 1; + metadata.extend_from_slice( + &u32::try_from(words_minus_one) + .map_err(|_| { + Error::internal(format!( + "Sparse value chunk size {chunk_bytes} exceeds the metadata limit" + )) + })? + .to_le_bytes(), + ); + metadata.extend_from_slice(&chunk.num_values.to_le_bytes()); + } + + for (index, (consumed, buffer)) in buffer_offsets + .iter() + .zip(compressed.data.iter()) + .enumerate() + { + if *consumed != buffer.len() { + return Err(Error::internal(format!( + "Sparse value buffer {index} consumed {consumed} bytes, expected {}", + buffer.len() + ))); + } + } + + Ok(SerializedValuePage { + num_buffers: usize_to_u64(num_buffers, "value buffer count")?, + data: LanceBuffer::from(data_buffer), + metadata: LanceBuffer::from(metadata), + }) +} + +pub fn prepare_values( + field: &Field, + compression_strategy: &dyn CompressionStrategy, + data: DataBlock, + miniblock_chunk_size: MiniblockChunkSize, +) -> Result { + match &data { + DataBlock::AllNull(_) => { + return Err(Error::internal( + "All-null values must use ConstantLayout".to_string(), + )); + } + DataBlock::Dictionary(_) => { + return Err(Error::not_supported_source( + "Sparse layout does not support dictionary data blocks".into(), + )); + } + DataBlock::Struct(data) if data.has_variable_width_child() => { + return Err(Error::not_supported_source( + "Sparse layout does not support variable-width packed struct data blocks".into(), + )); + } + _ => {} + } + + let num_values = data.num_values(); + let compressor = compression_strategy.create_miniblock_compressor(field, &data)?; + let support_large_chunk = miniblock_chunk_size == MiniblockChunkSize::U32; + let compression_context = MiniBlockCompressionContext::new(0, support_large_chunk, false); + let (compressed, value_compression) = compressor.compress(compression_context, data)?; + let values = serialize_value_chunks( + with_explicit_value_counts(compressed)?, + miniblock_chunk_size, + )?; + Ok(PreparedSparseValues { + num_values, + value_compression, + values, + }) +} + +fn encode_u64_values( + values: Vec, + compression_strategy: &dyn CompressionStrategy, +) -> Result<(LanceBuffer, CompressiveEncoding)> { + let num_values = usize_to_u64(values.len(), "u64 value count")?; + let mut block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(values), + bits_per_value: 64, + num_values, + block_info: BlockInfo::new(), + }); + block.compute_stat(); + let field = Field::new_arrow("", arrow_schema::DataType::UInt64, false)?; + let (compressor, encoding) = compression_strategy.create_block_compressor(&field, &block)?; + Ok((compressor.compress(block)?, encoding)) +} + +fn positions_to_deltas(positions: &[u64], label: &str) -> Result> { + let mut previous = 0_u64; + positions + .iter() + .copied() + .enumerate() + .map(|(index, position)| { + if index > 0 && position <= previous { + return Err(Error::invalid_input_source( + format!("Sparse structural {label} positions must be strictly increasing") + .into(), + )); + } + let delta = if index == 0 { + position + } else { + position - previous + }; + previous = position; + Ok(delta) + }) + .collect() +} + +fn encode_position_set( + positions: &SparsePositionSet, + compression_strategy: &dyn CompressionStrategy, + label: &str, +) -> Result<(Option, pb21::SparsePositionSet)> { + let (buffer, positions_pb) = match positions { + SparsePositionSet::Empty => ( + None, + pb21::sparse_position_set::Positions::Empty(pb21::SparsePositionEmpty {}), + ), + SparsePositionSet::All { .. } => ( + None, + pb21::sparse_position_set::Positions::All(pb21::SparsePositionAll {}), + ), + SparsePositionSet::Range { start, len } => ( + None, + pb21::sparse_position_set::Positions::Range(pb21::SparsePositionRange { + start: *start, + length: *len, + }), + ), + SparsePositionSet::Explicit(positions) => { + if positions.is_empty() { + return Err(Error::internal(format!( + "Sparse structural {label} explicit set is empty" + ))); + } + let (buffer, encoding) = + encode_u64_values(positions_to_deltas(positions, label)?, compression_strategy)?; + ( + Some(buffer), + pb21::sparse_position_set::Positions::Explicit(encoding), + ) + } + }; + Ok(( + buffer, + pb21::SparsePositionSet { + positions: Some(positions_pb), + num_positions: positions.len(), + }, + )) +} + +fn encode_count_set( + counts: &SparseCountSet, + compression_strategy: &dyn CompressionStrategy, +) -> Result<(Option, pb21::SparseCountSet)> { + let (buffer, counts_pb) = match counts { + SparseCountSet::Empty => ( + None, + pb21::sparse_count_set::Counts::Empty(pb21::SparseCountEmpty {}), + ), + SparseCountSet::Constant { value, .. } => ( + None, + pb21::sparse_count_set::Counts::Constant(pb21::SparseCountConstant { value: *value }), + ), + SparseCountSet::Explicit { counts, .. } => { + if counts.is_empty() { + return Err(Error::internal( + "Sparse structural explicit count set is empty".to_string(), + )); + } + let (buffer, encoding) = encode_u64_values(counts.to_vec(), compression_strategy)?; + ( + Some(buffer), + pb21::sparse_count_set::Counts::Explicit(encoding), + ) + } + }; + Ok(( + buffer, + pb21::SparseCountSet { + counts: Some(counts_pb), + }, + )) +} + +fn encode_validity_set( + validity: &SparseValiditySet, + compression_strategy: &dyn CompressionStrategy, + label: &str, +) -> Result<(Option, pb21::SparseValiditySet)> { + let (buffer, positions) = + encode_position_set(&validity.positions, compression_strategy, label)?; + let meaning = match validity.meaning { + SparseValidityMeaning::NullPositions => { + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions + } + SparseValidityMeaning::ValidPositions => { + pb21::sparse_validity_set::Meaning::SparseValidityValidPositions + } + }; + Ok(( + buffer, + pb21::SparseValiditySet { + meaning: meaning as i32, + positions: Some(positions), + }, + )) +} + +fn encode_structural_plan( + plan: &SparseStructuralPlan, + compression_strategy: &dyn CompressionStrategy, +) -> Result { + let mut layers = Vec::with_capacity(plan.layers.len()); + let mut buffers = Vec::new(); + + for layer in &plan.layers { + match layer { + SparseStructuralLayerPlan::Validity { + num_slots, + validity, + } => { + let (validity_buffer, validity) = + encode_validity_set(validity, compression_strategy, "validity")?; + buffers.extend(validity_buffer); + layers.push(pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::Validity( + pb21::SparseValidityLayer { + num_slots: *num_slots, + validity: Some(validity), + }, + )), + }); + } + SparseStructuralLayerPlan::List { + num_slots, + num_child_slots, + non_empty_positions, + counts, + validity, + } => { + if non_empty_positions.len() != counts.len() { + return Err(Error::invalid_input_source( + format!( + "Sparse structural list has {} non-empty positions but {} counts", + non_empty_positions.len(), + counts.len() + ) + .into(), + )); + } + let (position_buffer, non_empty_positions) = encode_position_set( + non_empty_positions, + compression_strategy, + "list non-empty", + )?; + buffers.extend(position_buffer); + let (count_buffer, counts) = encode_count_set(counts, compression_strategy)?; + buffers.extend(count_buffer); + let (validity_buffer, validity) = + encode_validity_set(validity, compression_strategy, "list validity")?; + buffers.extend(validity_buffer); + layers.push(pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::List( + pb21::SparseListLayer { + num_slots: *num_slots, + num_child_slots: *num_child_slots, + non_empty_positions: Some(non_empty_positions), + counts: Some(counts), + validity: Some(validity), + }, + )), + }); + } + SparseStructuralLayerPlan::FixedSizeList { + num_slots, + dimension, + validity, + } => { + let (validity_buffer, validity) = encode_validity_set( + validity, + compression_strategy, + "fixed-size-list validity", + )?; + buffers.extend(validity_buffer); + num_slots.checked_mul(*dimension).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Sparse structural fixed-size-list child slot count overflows: slots={num_slots}, dimension={dimension}" + ) + .into(), + ) + })?; + layers.push(pb21::SparseStructuralLayer { + layer: Some(pb21::sparse_structural_layer::Layer::FixedSizeList( + pb21::SparseFixedSizeListLayer { + num_slots: *num_slots, + dimension: *dimension, + validity: Some(validity), + }, + )), + }); + } + } + } + + Ok(EncodedStructuralPlan { layers, buffers }) +} + +#[allow(clippy::too_many_arguments)] +pub(in crate::encodings::logical::primitive) fn encode_page( + column_idx: u32, + field: &Field, + compression_strategy: &dyn CompressionStrategy, + values: SparseValueInput, + plan: SparseStructuralPlan, + row_number: u64, + num_rows: u64, + miniblock_chunk_size: MiniblockChunkSize, +) -> Result { + let PreparedSparseValues { + num_values, + value_compression, + values, + } = match values { + SparseValueInput::Unprepared(data) => { + prepare_values(field, compression_strategy, data, miniblock_chunk_size)? + } + SparseValueInput::Prepared(prepared) => prepared, + }; + if plan.num_visible_items != num_values { + return Err(Error::internal(format!( + "Sparse structural plan has {} visible items but data has {} values", + plan.num_visible_items, num_values + ))); + } + let structural = encode_structural_plan(&plan, compression_strategy)?; + let description = pb21::PageLayout { + layout: Some(pb21::page_layout::Layout::SparseLayout( + pb21::SparseLayout { + value_compression: Some(value_compression), + num_buffers: values.num_buffers, + num_items: plan.num_items, + num_visible_items: plan.num_visible_items, + has_large_chunk: miniblock_chunk_size == MiniblockChunkSize::U32, + structural_layers: structural.layers, + }, + )), + }; + + let mut page_data = Vec::with_capacity(2 + structural.buffers.len()); + page_data.push(values.metadata); + page_data.push(values.data); + page_data.extend(structural.buffers); + Ok(EncodedPage { + data: page_data, + description: PageEncoding::Structural(description), + num_rows, + row_number, + column_idx, + }) +} + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{ + Array, ArrayRef, DictionaryArray, FixedSizeBinaryArray, FixedSizeListArray, Int8Array, + Int32Array, LargeListArray, ListArray, StringArray, StructArray, + builder::{Int32Builder, MapBuilder, StringBuilder}, + types::Int8Type, + }; + use arrow_buffer::{BooleanBuffer, NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field as ArrowField, Fields}; + + use crate::{ + constants::{ + PACKED_STRUCT_META_KEY, STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_META_KEY, + STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_SPARSE, + }, + data::FixedSizeListBlock, + encoder::{ + ColumnIndexSequence, EncodingOptions, FieldEncoder, MIN_PAGE_BUFFER_ALIGNMENT, + OutOfLineBuffers, + }, + testing::{ + TestCases, TestEncoding, check_round_trip_encoding_of_data, test_encoding_strategy, + }, + }; + + use super::*; + + fn sparse_metadata() -> HashMap { + HashMap::from([( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_SPARSE.to_string(), + )]) + } + + fn structural_metadata(value: &str) -> HashMap { + HashMap::from([(STRUCTURAL_ENCODING_META_KEY.to_string(), value.to_string())]) + } + + fn null_buffer(validity: impl IntoIterator) -> NullBuffer { + NullBuffer::new(BooleanBuffer::from_iter(validity)) + } + + fn list_i32(offsets: Vec, validity: Option>) -> ArrayRef { + let num_values = offsets.last().copied().unwrap_or_default(); + let values = Arc::new(Int32Array::from_iter_values(0..num_values)) as ArrayRef; + Arc::new( + ListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Int32, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + values, + validity.map(null_buffer), + ) + .unwrap(), + ) + } + + fn sparse_list_values( + num_rows: usize, + stride: usize, + values: ArrayRef, + item_field: Arc, + ) -> ArrayRef { + let mut offsets = Vec::with_capacity(num_rows + 1); + let mut num_values = 0_i32; + offsets.push(num_values); + for row in 0..num_rows { + if (row + 1).is_multiple_of(stride) || row + 1 == num_rows { + num_values += 1; + } + offsets.push(num_values); + } + assert_eq!(values.len(), num_values as usize); + Arc::new( + ListArray::try_new( + item_field, + OffsetBuffer::new(ScalarBuffer::from(offsets)), + values, + None, + ) + .unwrap(), + ) + } + + fn sparse_i32_list(num_rows: usize, stride: usize) -> ArrayRef { + let num_values = num_rows.div_ceil(stride); + sparse_list_values( + num_rows, + stride, + Arc::new(Int32Array::from_iter_values(0..num_values as i32)), + Arc::new(ArrowField::new("item", DataType::Int32, true)), + ) + } + + fn unsplittable_nested_list(values: ArrayRef, item_field: Arc) -> ArrayRef { + const NUM_INNER_LISTS: usize = 70_000; + + let mut inner_offsets = vec![0_i32; NUM_INNER_LISTS + 1]; + assert!(!values.is_empty()); + assert!(values.len() <= NUM_INNER_LISTS); + for value_index in 1..=values.len() { + inner_offsets[NUM_INNER_LISTS - values.len() + value_index] = value_index as i32; + } + let inner = Arc::new( + ListArray::try_new( + item_field, + OffsetBuffer::new(ScalarBuffer::from(inner_offsets)), + values, + None, + ) + .unwrap(), + ) as ArrayRef; + Arc::new( + ListArray::try_new( + Arc::new(ArrowField::new("item", inner.data_type().clone(), true)), + OffsetBuffer::new(ScalarBuffer::from(vec![0_i32, NUM_INNER_LISTS as i32])), + inner, + None, + ) + .unwrap(), + ) + } + + fn variable_packed_struct_values(num_values: usize) -> (ArrayRef, Arc) { + let fields = Fields::from(vec![ArrowField::new("value", DataType::Utf8, false)]); + let values = Arc::new(StructArray::new( + fields.clone(), + vec![Arc::new(StringArray::from_iter_values( + (0..num_values).map(|index| format!("value-{index}")), + ))], + None, + )) as ArrayRef; + let item_field = Arc::new( + ArrowField::new("item", DataType::Struct(fields), true).with_metadata(HashMap::from([ + (PACKED_STRUCT_META_KEY.to_string(), "true".to_string()), + ])), + ); + (values, item_field) + } + + fn dictionary_values(num_values: usize) -> (ArrayRef, Arc) { + let keys = Int8Array::from_iter_values((0..num_values).map(|index| (index % 2) as i8)); + let values = Arc::new(StringArray::from(vec!["value-0", "value-1"])); + let dictionary = Arc::new(DictionaryArray::::try_new(keys, values).unwrap()); + let item_field = Arc::new(ArrowField::new( + "item", + dictionary.data_type().clone(), + true, + )); + (dictionary, item_field) + } + + fn large_list_i32(offsets: Vec, validity: Option>) -> ArrayRef { + let num_values = offsets.last().copied().unwrap_or_default(); + let values = Arc::new(Int32Array::from_iter_values(0..num_values as i32)) as ArrayRef; + Arc::new( + LargeListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Int32, true)), + OffsetBuffer::new(ScalarBuffer::from(offsets)), + values, + validity.map(null_buffer), + ) + .unwrap(), + ) + } + + fn map_i32() -> ArrayRef { + let mut builder = MapBuilder::new(None, StringBuilder::new(), Int32Builder::new()); + builder.keys().append_value("a"); + builder.values().append_value(1); + builder.append(true).unwrap(); + builder.append(false).unwrap(); + builder.append(true).unwrap(); + builder.keys().append_value("b"); + builder.values().append_null(); + builder.keys().append_value("c"); + builder.values().append_value(3); + builder.append(true).unwrap(); + Arc::new(builder.finish()) + } + + fn fixed_size_list_struct() -> ArrayRef { + let fields = Fields::from(vec![ArrowField::new("value", DataType::Int32, true)]); + let child = Arc::new(StructArray::new( + fields.clone(), + vec![Arc::new(Int32Array::from(vec![ + Some(0), + Some(1), + None, + Some(3), + Some(4), + Some(5), + None, + Some(7), + Some(8), + Some(9), + Some(10), + None, + ]))], + Some(null_buffer([ + true, true, false, true, true, true, true, false, true, true, true, true, + ])), + )) as ArrayRef; + Arc::new( + FixedSizeListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Struct(fields), true)), + 2, + child, + Some(null_buffer([true, false, true, true, false, true])), + ) + .unwrap(), + ) + } + + fn list_fixed_size_list_struct() -> ArrayRef { + let struct_fields = Fields::from(vec![ArrowField::new("value", DataType::Int32, true)]); + let structs = Arc::new(StructArray::new( + struct_fields.clone(), + vec![Arc::new(Int32Array::from(vec![ + Some(0), + None, + Some(2), + Some(3), + None, + Some(5), + ]))], + Some(null_buffer([true, true, false, true, true, true])), + )) as ArrayRef; + let fixed_size_list = Arc::new( + FixedSizeListArray::try_new( + Arc::new(ArrowField::new( + "item", + DataType::Struct(struct_fields), + true, + )), + 2, + structs, + Some(null_buffer([true, false, true])), + ) + .unwrap(), + ) as ArrayRef; + Arc::new( + ListArray::try_new( + Arc::new(ArrowField::new( + "item", + fixed_size_list.data_type().clone(), + true, + )), + OffsetBuffer::new(ScalarBuffer::from(vec![0_i32, 1, 1, 3, 3])), + fixed_size_list, + Some(null_buffer([true, false, true, true])), + ) + .unwrap(), + ) + } + + fn nullable_struct() -> ArrayRef { + let fields = Fields::from(vec![ArrowField::new("value", DataType::Int32, true)]); + Arc::new(StructArray::new( + fields, + vec![Arc::new(Int32Array::from(vec![ + Some(10), + Some(20), + None, + Some(40), + Some(50), + ]))], + Some(null_buffer([true, false, true, true, false])), + )) + } + + fn deeply_nested() -> ArrayRef { + let leaf = Arc::new(Int32Array::from(vec![ + Some(0), + None, + Some(2), + Some(3), + None, + Some(5), + Some(6), + Some(7), + ])) as ArrayRef; + let inner = Arc::new( + LargeListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Int32, true)), + OffsetBuffer::new(ScalarBuffer::from(vec![0_i64, 2, 2, 3, 5, 5, 8])), + leaf, + Some(null_buffer([true, false, true, true, true, true])), + ) + .unwrap(), + ) as ArrayRef; + let struct_fields = Fields::from(vec![ArrowField::new( + "inner", + inner.data_type().clone(), + true, + )]); + let structs = Arc::new(StructArray::new( + struct_fields.clone(), + vec![inner], + Some(null_buffer([true, true, false, true, true, true])), + )) as ArrayRef; + Arc::new( + ListArray::try_new( + Arc::new(ArrowField::new( + "item", + DataType::Struct(struct_fields), + true, + )), + OffsetBuffer::new(ScalarBuffer::from(vec![0_i32, 2, 2, 2, 4, 6])), + structs, + Some(null_buffer([true, false, true, true, true])), + ) + .unwrap(), + ) + } + + fn page_layout(page: &EncodedPage) -> &pb21::page_layout::Layout { + let PageEncoding::Structural(layout) = &page.description else { + panic!("expected structural page encoding"); + }; + layout.layout.as_ref().expect("page layout must be present") + } + + fn create_encoder( + array: &ArrayRef, + version: TestEncoding, + metadata: HashMap, + ) -> Result> { + let arrow_field = + ArrowField::new("values", array.data_type().clone(), true).with_metadata(metadata); + let field = Field::try_from(&arrow_field)?; + let strategy = test_encoding_strategy(version); + let options = EncodingOptions { + cache_bytes_per_column: 1, + ..Default::default() + }; + crate::testing::create_test_field_encoder( + strategy.as_ref(), + &field, + &mut ColumnIndexSequence::default(), + &options, + ) + } + + async fn encode_pages( + array: ArrayRef, + version: TestEncoding, + metadata: HashMap, + ) -> Result> { + encode_chunks(vec![array], version, metadata).await + } + + async fn encode_chunks( + arrays: Vec, + version: TestEncoding, + metadata: HashMap, + ) -> Result> { + let first = arrays + .first() + .ok_or_else(|| Error::internal("test input has no arrays".to_string()))?; + let mut encoder = create_encoder(first, version, metadata)?; + let mut external_buffers = OutOfLineBuffers::new(0, MIN_PAGE_BUFFER_ALIGNMENT); + let mut pages = Vec::new(); + let mut row_number = 0_u64; + for array in arrays { + let num_rows = array.len() as u64; + for task in encoder.maybe_encode( + array, + &mut external_buffers, + crate::repdef::RepDefBuilder::default(), + row_number, + num_rows, + )? { + pages.push(task.await?); + } + row_number += num_rows; + } + for task in encoder.flush(&mut external_buffers)? { + pages.push(task.await?); + } + for column in encoder.finish(&mut external_buffers).await? { + pages.extend(column.final_pages); + } + Ok(pages) + } + + fn sparse_layout(page: &EncodedPage) -> &pb21::SparseLayout { + let pb21::page_layout::Layout::SparseLayout(sparse) = page_layout(page) else { + panic!("expected SparseLayout, got {:?}", page_layout(page)); + }; + sparse + } + + fn list_layer(sparse: &pb21::SparseLayout) -> &pb21::SparseListLayer { + sparse + .structural_layers + .iter() + .find_map(|layer| match layer.layer.as_ref() { + Some(pb21::sparse_structural_layer::Layer::List(layer)) => Some(layer), + _ => None, + }) + .expect("expected sparse list layer") + } + + fn validity_layer(layer: &pb21::SparseStructuralLayer) -> Option<&pb21::SparseValidityLayer> { + match layer.layer.as_ref() { + Some(pb21::sparse_structural_layer::Layer::Validity(layer)) => Some(layer), + _ => None, + } + } + + fn layer_num_slots(layer: &pb21::SparseStructuralLayer) -> u64 { + match layer.layer.as_ref().expect("expected sparse layer variant") { + pb21::sparse_structural_layer::Layer::Validity(layer) => layer.num_slots, + pb21::sparse_structural_layer::Layer::List(layer) => layer.num_slots, + pb21::sparse_structural_layer::Layer::FixedSizeList(layer) => layer.num_slots, + } + } + + fn fixed_size_list_dimension(layer: &pb21::SparseStructuralLayer) -> Option { + match layer.layer.as_ref() { + Some(pb21::sparse_structural_layer::Layer::FixedSizeList(layer)) => { + Some(layer.dimension) + } + _ => None, + } + } + + #[test] + fn test_sparse_value_block_eligibility() { + let fixed = DataBlock::from_array(Int32Array::from_iter_values(0..4)); + assert!(supports_value_block(&fixed)); + + let variable = DataBlock::from_array(StringArray::from(vec!["a", "b"])); + assert!(supports_value_block(&variable)); + + let nullable = DataBlock::from_array(Int32Array::from(vec![Some(1), None])); + assert!(!supports_value_block(&nullable)); + + let all_null = DataBlock::from_array(Int32Array::from(vec![None, None])); + assert!(!supports_value_block(&all_null)); + + let (dictionary, _) = dictionary_values(4); + assert!(!supports_value_block(&DataBlock::from_arrays( + &[dictionary], + 4 + ))); + + let fixed_fields = Fields::from(vec![ArrowField::new("value", DataType::Int32, false)]); + let fixed_struct = StructArray::new( + fixed_fields, + vec![Arc::new(Int32Array::from_iter_values(0..4))], + None, + ); + assert!(supports_value_block(&DataBlock::from_array(fixed_struct))); + + let variable_fields = Fields::from(vec![ArrowField::new("value", DataType::Utf8, false)]); + let variable_struct = StructArray::new( + variable_fields, + vec![Arc::new(StringArray::from(vec!["a", "b"]))], + None, + ); + assert!(!supports_value_block(&DataBlock::from_array( + variable_struct + ))); + + let fixed_size_list = FixedSizeListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Int32, false)), + 2, + Arc::new(Int32Array::from_iter_values(0..4)), + None, + ) + .unwrap(); + assert!(supports_value_block(&DataBlock::from_array( + fixed_size_list + ))); + + let unsupported_fixed_size_list = DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(DataBlock::from_array(StringArray::from(vec![ + "a", "b", "c", "d", + ]))), + dimension: 2, + }); + assert!(!supports_value_block(&unsupported_fixed_size_list)); + } + + fn planned_list( + offsets: Vec, + list_validity: Option>, + leaf_validity: Option>, + ) -> SparseStructuralPlan { + let num_values = u64::try_from(*offsets.last().unwrap()).unwrap(); + let mut builder = crate::repdef::RepDefBuilder::default(); + assert!(!builder.add_offsets( + OffsetBuffer::new(ScalarBuffer::from(offsets)), + list_validity.map(null_buffer), + )); + if let Some(leaf_validity) = leaf_validity { + builder.add_validity_bitmap(null_buffer(leaf_validity)); + } else { + builder.add_no_null(num_values as usize); + } + let normalized = crate::repdef::RepDefBuilder::normalize(vec![builder]); + plan(&normalized, num_values).unwrap() + } + + fn planned_list_layer(plan: &SparseStructuralPlan) -> &SparseStructuralLayerPlan { + plan.layers + .iter() + .find(|layer| matches!(layer, SparseStructuralLayerPlan::List { .. })) + .expect("expected planned list layer") + } + + #[test] + fn test_semantic_position_and_count_forms() { + let empty = planned_list(vec![0, 0, 0, 0], None, None); + assert_eq!(empty.num_items, 3); + assert_eq!(empty.num_visible_items, 0); + assert!(matches!( + planned_list_layer(&empty), + SparseStructuralLayerPlan::List { + non_empty_positions: SparsePositionSet::Empty, + counts: SparseCountSet::Empty, + .. + } + )); + + let all = planned_list(vec![0, 2, 4, 6], None, None); + assert_eq!(all.num_items, 6); + assert!(matches!( + planned_list_layer(&all), + SparseStructuralLayerPlan::List { + non_empty_positions: SparsePositionSet::All { len: 3 }, + counts: SparseCountSet::Constant { value: 2, len: 3 }, + .. + } + )); + + let range = planned_list(vec![0, 2, 4, 4, 4], None, None); + assert_eq!(range.num_items, 6); + assert!(matches!( + planned_list_layer(&range), + SparseStructuralLayerPlan::List { + non_empty_positions: SparsePositionSet::Range { start: 0, len: 2 }, + counts: SparseCountSet::Constant { value: 2, len: 2 }, + .. + } + )); + + let explicit = planned_list(vec![0, 1, 1, 4, 4, 6], None, None); + assert_eq!(explicit.num_items, 8); + assert!(matches!( + planned_list_layer(&explicit), + SparseStructuralLayerPlan::List { + non_empty_positions: SparsePositionSet::Explicit(positions), + counts: SparseCountSet::Explicit { counts, .. }, + .. + } if positions == &vec![0, 2, 4] && counts.as_ref() == [1, 3, 2] + )); + } + + #[test] + fn test_validity_polarity_uses_semantic_encoded_cost() { + let mostly_valid = BooleanBuffer::from_iter([true, false, true, true, false, true]); + let validity = validity_set(Some(&mostly_valid), mostly_valid.len(), "test").unwrap(); + assert_eq!(validity.meaning, SparseValidityMeaning::NullPositions); + assert!(matches!(validity.positions, SparsePositionSet::Explicit(ref p) if p == &[1, 4])); + + let mostly_null = BooleanBuffer::from_iter([false, true, false, false, true, false]); + let validity = validity_set(Some(&mostly_null), mostly_null.len(), "test").unwrap(); + assert_eq!(validity.meaning, SparseValidityMeaning::ValidPositions); + assert!(matches!(validity.positions, SparsePositionSet::Explicit(ref p) if p == &[1, 4])); + + let valid_island = BooleanBuffer::from_iter([false, false, true, true, false]); + let validity = validity_set(Some(&valid_island), valid_island.len(), "test").unwrap(); + assert_eq!(validity.meaning, SparseValidityMeaning::ValidPositions); + assert!(matches!( + validity.positions, + SparsePositionSet::Range { start: 2, len: 2 } + )); + + let all_valid = BooleanBuffer::from_iter([true, true, true]); + let validity = validity_set(Some(&all_valid), all_valid.len(), "test").unwrap(); + assert_eq!(validity.meaning, SparseValidityMeaning::NullPositions); + assert!(matches!(validity.positions, SparsePositionSet::Empty)); + + let all_null = BooleanBuffer::from_iter([false, false, false]); + let validity = validity_set(Some(&all_null), all_null.len(), "test").unwrap(); + assert_eq!(validity.meaning, SparseValidityMeaning::ValidPositions); + assert!(matches!(validity.positions, SparsePositionSet::Empty)); + } + + #[tokio::test] + async fn test_explicit_sparse_nullable_primitive_roundtrip() { + let array = Arc::new(Int32Array::from(vec![ + Some(10), + None, + Some(20), + Some(30), + None, + Some(40), + ])) as ArrayRef; + let pages = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert_eq!(pages.len(), 1); + let sparse = sparse_layout(&pages[0]); + assert_eq!(sparse.num_items, 6); + assert_eq!(sparse.num_visible_items, 6); + assert_eq!(sparse.structural_layers.len(), 1); + let validity = validity_layer(&sparse.structural_layers[0]) + .unwrap() + .validity + .as_ref() + .unwrap(); + assert_eq!( + validity.meaning, + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions as i32 + ); + assert!(matches!( + validity.positions.as_ref().unwrap().positions, + Some(pb21::sparse_position_set::Positions::Explicit(_)) + )); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..5) + .with_indices(vec![0, 2, 5]); + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_nullable_struct_roundtrip() { + let array = nullable_struct(); + let pages = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert_eq!(pages.len(), 1); + let sparse = sparse_layout(&pages[0]); + assert_eq!(sparse.structural_layers.len(), 2); + assert!( + sparse + .structural_layers + .iter() + .all(|layer| validity_layer(layer).is_some()) + ); + let struct_validity = validity_layer(&sparse.structural_layers[0]) + .unwrap() + .validity + .as_ref() + .unwrap(); + assert_eq!( + struct_validity.meaning, + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions as i32 + ); + assert!(matches!( + struct_validity.positions.as_ref().unwrap().positions, + Some(pb21::sparse_position_set::Positions::Explicit(_)) + )); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..5) + .with_indices(vec![0, 2, 4]); + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_struct_with_constant_and_sparse_children() { + let fields = Fields::from(vec![ + ArrowField::new("constant", DataType::Int32, true), + ArrowField::new("sparse", DataType::Int32, true), + ]); + let array = Arc::new(StructArray::new( + fields, + vec![ + Arc::new(Int32Array::from(vec![None::; 5])), + Arc::new(Int32Array::from(vec![ + Some(10), + Some(20), + None, + Some(40), + Some(50), + ])), + ], + Some(null_buffer([true, false, true, true, false])), + )) as ArrayRef; + let pages = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(pages.iter().any(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::ConstantLayout(_) + ))); + assert!(pages.iter().any(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::SparseLayout(_) + ))); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..5) + .with_indices(vec![0, 2, 4]); + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_emits_both_validity_polarities() { + let mostly_valid = Arc::new(Int32Array::from(vec![ + Some(0), + None, + Some(2), + Some(3), + None, + Some(5), + ])) as ArrayRef; + let mostly_null = Arc::new(Int32Array::from(vec![ + None, + Some(1), + None, + None, + Some(4), + None, + ])) as ArrayRef; + + let null_positions = encode_pages( + mostly_valid.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + let validity = validity_layer(&sparse_layout(&null_positions[0]).structural_layers[0]) + .unwrap() + .validity + .as_ref() + .unwrap(); + assert_eq!( + validity.meaning, + pb21::sparse_validity_set::Meaning::SparseValidityNullPositions as i32 + ); + + let valid_positions = encode_pages( + mostly_null.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + let validity = validity_layer(&sparse_layout(&valid_positions[0]).structural_layers[0]) + .unwrap() + .validity + .as_ref() + .unwrap(); + assert_eq!( + validity.meaning, + pb21::sparse_validity_set::Meaning::SparseValidityValidPositions as i32 + ); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..5) + .with_indices(vec![0, 2, 5]); + for array in [mostly_valid, mostly_null] { + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + } + + #[tokio::test] + async fn test_explicit_sparse_nested_page_boundaries_range_and_take() { + let nested = deeply_nested(); + let chunks = vec![nested.slice(0, 2), nested.slice(2, 3)]; + let pages = encode_chunks( + chunks.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(pages.len() >= 2, "expected multiple sparse pages"); + let sparse = pages + .iter() + .map(sparse_layout) + .collect::>(); + assert!(sparse.iter().any(|layout| { + layout + .structural_layers + .iter() + .filter(|layer| { + matches!( + layer.layer.as_ref(), + Some(pb21::sparse_structural_layer::Layer::List(_)) + ) + }) + .count() + >= 2 + })); + assert!(sparse.iter().any(|layout| { + layout + .structural_layers + .iter() + .any(|layer| validity_layer(layer).is_some()) + })); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_batch_size(2) + .with_range(1..5) + .with_range(2..4) + .with_indices(vec![0, 2, 4]) + .with_indices(vec![1, 3]); + check_round_trip_encoding_of_data(chunks, &cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_list_and_large_list_null_empty_roundtrip() { + let list = list_i32( + vec![0, 0, 0, 2, 3, 3], + Some(vec![false, true, true, true, true]), + ); + let large_list = large_list_i32( + vec![0, 0, 0, 2, 3, 3], + Some(vec![false, true, true, true, true]), + ); + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(0..5) + .with_range(1..4) + .with_indices(vec![0, 1, 4]) + .with_indices(vec![2, 3]); + + for array in [list, large_list] { + let pages = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(pages.iter().map(sparse_layout).all(|layout| { + layout.structural_layers.iter().any(|layer| { + matches!( + layer.layer.as_ref(), + Some(pb21::sparse_structural_layer::Layer::List(_)) + ) + }) + })); + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + } + + #[tokio::test] + async fn test_explicit_sparse_map_and_fixed_size_list_roundtrip() { + let map = map_i32(); + let map_pages = encode_pages( + map.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(map_pages.iter().map(sparse_layout).any(|layout| { + layout.structural_layers.iter().any(|layer| { + matches!( + layer.layer.as_ref(), + Some(pb21::sparse_structural_layer::Layer::List(_)) + ) + }) + })); + let map_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..4) + .with_indices(vec![0, 2, 3]); + check_round_trip_encoding_of_data(vec![map], &map_cases, sparse_metadata()).await; + + let fsl = fixed_size_list_struct(); + let fsl_pages = encode_pages( + fsl.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + for page in &fsl_pages { + let layout = sparse_layout(page); + let outer_slots = layer_num_slots(layout.structural_layers.first().unwrap()); + let fixed_size_scale = layout + .structural_layers + .iter() + .filter_map(fixed_size_list_dimension) + .product::(); + assert_eq!(page.num_rows, outer_slots * fixed_size_scale); + } + assert!(fsl_pages.iter().map(sparse_layout).any(|layout| { + layout + .structural_layers + .iter() + .any(|layer| fixed_size_list_dimension(layer) == Some(2)) + })); + let fsl_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..6) + .with_indices(vec![0, 3, 5]); + check_round_trip_encoding_of_data(vec![fsl], &fsl_cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_list_fixed_size_list_struct_roundtrip() { + let array = list_fixed_size_list_struct(); + let pages = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(pages.iter().map(sparse_layout).any(|layout| { + let kinds = layout + .structural_layers + .iter() + .map(|layer| match layer.layer.as_ref().unwrap() { + pb21::sparse_structural_layer::Layer::Validity(_) => "validity", + pb21::sparse_structural_layer::Layer::List(_) => "list", + pb21::sparse_structural_layer::Layer::FixedSizeList(_) => "fixed-size-list", + }) + .collect::>(); + kinds.starts_with(&["list", "fixed-size-list"]) + })); + for page in &pages { + let layout = sparse_layout(page); + let outer_slots = layer_num_slots(layout.structural_layers.first().unwrap()); + assert_eq!(page.num_rows, outer_slots * 2); + } + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..4) + .with_indices(vec![0, 2, 3]) + .with_indices(vec![1, 3]); + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + + #[tokio::test] + async fn test_explicit_sparse_serializes_semantic_list_forms() { + let all_array = list_i32(vec![0, 2, 4, 6], None); + let all = encode_pages( + all_array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + let all_layer = list_layer(sparse_layout(&all[0])); + assert!(matches!( + all_layer.non_empty_positions.as_ref().unwrap().positions, + Some(pb21::sparse_position_set::Positions::All(_)) + )); + assert!(matches!( + all_layer.counts.as_ref().unwrap().counts, + Some(pb21::sparse_count_set::Counts::Constant( + pb21::SparseCountConstant { value: 2 } + )) + )); + assert_eq!(all[0].data.len(), 2); + + let range_array = list_i32(vec![0, 2, 4, 4, 4], None); + let range = encode_pages( + range_array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + let range_layer = list_layer(sparse_layout(&range[0])); + assert!(matches!( + range_layer.non_empty_positions.as_ref().unwrap().positions, + Some(pb21::sparse_position_set::Positions::Range( + pb21::SparsePositionRange { + start: 0, + length: 2 + } + )) + )); + assert!(matches!( + range_layer.counts.as_ref().unwrap().counts, + Some(pb21::sparse_count_set::Counts::Constant( + pb21::SparseCountConstant { value: 2 } + )) + )); + assert_eq!(range[0].data.len(), 2); + + let explicit_array = list_i32(vec![0, 1, 1, 4, 4, 6], None); + let explicit = encode_pages( + explicit_array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + let explicit_layer = list_layer(sparse_layout(&explicit[0])); + assert!(matches!( + explicit_layer + .non_empty_positions + .as_ref() + .unwrap() + .positions, + Some(pb21::sparse_position_set::Positions::Explicit(_)) + )); + assert!(matches!( + explicit_layer.counts.as_ref().unwrap().counts, + Some(pb21::sparse_count_set::Counts::Explicit(_)) + )); + assert_eq!(explicit[0].data.len(), 4); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1]) + .with_range(1..3) + .with_indices(vec![0, 2]); + for array in [all_array, range_array, explicit_array] { + check_round_trip_encoding_of_data(vec![array], &cases, sparse_metadata()).await; + } + } + + #[tokio::test] + async fn test_constant_layout_boundary_is_explicit() { + let structural_only = encode_pages( + list_i32(vec![0, 0, 0, 0], None), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&structural_only[0]), + pb21::page_layout::Layout::ConstantLayout(_) + )); + + let empty_struct = Arc::new(StructArray::new_empty_fields(3, None)) as ArrayRef; + let empty_struct_pages = encode_pages( + empty_struct, + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&empty_struct_pages[0]), + pb21::page_layout::Layout::ConstantLayout(_) + )); + + let all_null = Arc::new(Int32Array::from(vec![None, None, None])) as ArrayRef; + let all_null_pages = + encode_pages(all_null, TestEncoding::StructuralSparse, sparse_metadata()) + .await + .unwrap(); + assert!(matches!( + page_layout(&all_null_pages[0]), + pb21::page_layout::Layout::ConstantLayout(_) + )); + + let constant = Arc::new(Int32Array::from(vec![7, 7, 7])) as ArrayRef; + let constant_pages = + encode_pages(constant, TestEncoding::StructuralSparse, sparse_metadata()) + .await + .unwrap(); + assert!(matches!( + page_layout(&constant_pages[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + } + + #[tokio::test] + async fn test_default_and_explicit_dense_layouts_are_unchanged() { + let array = Arc::new(Int32Array::from_iter_values(0..16)) as ArrayRef; + let v2_2_default = encode_pages(array.clone(), TestEncoding::StructuralU32, HashMap::new()) + .await + .unwrap(); + let v2_2_miniblock = encode_pages( + array.clone(), + TestEncoding::StructuralU32, + structural_metadata(STRUCTURAL_ENCODING_MINIBLOCK), + ) + .await + .unwrap(); + assert_eq!(v2_2_default.len(), v2_2_miniblock.len()); + for (default, explicit) in v2_2_default.iter().zip(v2_2_miniblock.iter()) { + assert!(matches!( + page_layout(default), + pb21::page_layout::Layout::MiniBlockLayout(_) + )); + assert_eq!(page_layout(default), page_layout(explicit)); + assert_eq!(default.data, explicit.data); + } + + let v2_3_default = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&v2_3_default[0]), + pb21::page_layout::Layout::MiniBlockLayout(_) + )); + + let v2_3_miniblock = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_MINIBLOCK), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&v2_3_miniblock[0]), + pb21::page_layout::Layout::MiniBlockLayout(_) + )); + + let v2_3_fullzip = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_FULLZIP), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&v2_3_fullzip[0]), + pb21::page_layout::Layout::FullZipLayout(_) + )); + + let v2_3_sparse = encode_pages(array, TestEncoding::StructuralSparse, sparse_metadata()) + .await + .unwrap(); + assert!(matches!( + page_layout(&v2_3_sparse[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + } + + #[tokio::test] + async fn test_auto_sparse_for_split_required_page() { + const NUM_ROWS: usize = 70_000; + let array = sparse_i32_list(NUM_ROWS, 2_000); + + let within_budget = encode_pages( + sparse_i32_list(4_096, 1_024), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert_eq!(within_budget.len(), 1); + assert!(matches!( + page_layout(&within_budget[0]), + pb21::page_layout::Layout::MiniBlockLayout(_) + )); + + let automatic = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert_eq!(automatic.len(), 1); + assert!(matches!( + page_layout(&automatic[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + + let miniblock = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_MINIBLOCK), + ) + .await + .unwrap(); + assert!(miniblock.len() > 1); + assert!(miniblock.iter().all(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::MiniBlockLayout(_) + ))); + + let fullzip = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_FULLZIP), + ) + .await + .unwrap(); + assert_eq!(fullzip.len(), miniblock.len()); + assert!(fullzip.iter().all(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::FullZipLayout(_) + ))); + + let sparse = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert_eq!(sparse.len(), 1); + assert!(matches!( + page_layout(&sparse[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + + let v2_2_default = encode_pages(array.clone(), TestEncoding::StructuralU32, HashMap::new()) + .await + .unwrap(); + let v2_2_miniblock = encode_pages( + array.clone(), + TestEncoding::StructuralU32, + structural_metadata(STRUCTURAL_ENCODING_MINIBLOCK), + ) + .await + .unwrap(); + assert_eq!(v2_2_default.len(), v2_2_miniblock.len()); + for (default, explicit) in v2_2_default.iter().zip(v2_2_miniblock.iter()) { + assert_eq!(page_layout(default), page_layout(explicit)); + assert_eq!(default.data, explicit.data); + } + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1024 * 1024]) + .with_batch_size(NUM_ROWS as u32) + .with_range(1_999..2_002) + .with_indices(vec![0, 1_999, 2_000, NUM_ROWS as u64 - 1]); + check_round_trip_encoding_of_data(vec![array], &cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_auto_sparse_for_single_row_over_budget() { + let array = unsplittable_nested_list( + Arc::new(Int32Array::from(vec![42, 43])), + Arc::new(ArrowField::new("item", DataType::Int32, true)), + ); + + let automatic = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert_eq!(automatic.len(), 1); + assert!(matches!( + page_layout(&automatic[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + + let v2_2 = encode_pages(array.clone(), TestEncoding::StructuralU32, HashMap::new()) + .await + .unwrap(); + assert_eq!(v2_2.len(), 1); + assert!(matches!( + page_layout(&v2_2[0]), + pb21::page_layout::Layout::FullZipLayout(_) + )); + + let miniblock_error = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_MINIBLOCK), + ) + .await + .unwrap_err(); + assert!( + miniblock_error + .to_string() + .contains("Mini-block cannot encode 70000 rep/def levels") + ); + + let fullzip = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + structural_metadata(STRUCTURAL_ENCODING_FULLZIP), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&fullzip[0]), + pb21::page_layout::Layout::FullZipLayout(_) + )); + + let explicit_sparse = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap(); + assert!(matches!( + page_layout(&explicit_sparse[0]), + pb21::page_layout::Layout::SparseLayout(_) + )); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_range(0..1) + .with_indices(vec![0]); + check_round_trip_encoding_of_data(vec![array], &cases, HashMap::new()).await; + } + + #[tokio::test] + async fn test_auto_sparse_keeps_unsupported_values_dense() { + const NUM_ROWS: usize = 70_000; + let num_values = NUM_ROWS.div_ceil(2_000); + + let (dictionary, dictionary_field) = dictionary_values(num_values); + let dictionary_array = sparse_list_values(NUM_ROWS, 2_000, dictionary, dictionary_field); + let dictionary_pages = encode_pages( + dictionary_array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert!(dictionary_pages.len() > 1); + assert!(dictionary_pages.iter().all(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::MiniBlockLayout(_) + ))); + + let (packed_values, packed_field) = variable_packed_struct_values(num_values); + let packed_array = sparse_list_values(NUM_ROWS, 2_000, packed_values, packed_field); + let packed_pages = encode_pages( + packed_array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert!(packed_pages.len() > 1); + assert!(packed_pages.iter().all(|page| matches!( + page_layout(page), + pb21::page_layout::Layout::FullZipLayout(_) + ))); + + let (packed_values, packed_field) = variable_packed_struct_values(2); + let unsplittable_packed = unsplittable_nested_list(packed_values, packed_field); + let packed_fallback = encode_pages( + unsplittable_packed.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + assert_eq!(packed_fallback.len(), 1); + assert!(matches!( + page_layout(&packed_fallback[0]), + pb21::page_layout::Layout::FullZipLayout(_) + )); + + let (dictionary, dictionary_field) = dictionary_values(2); + let unsplittable_dictionary = unsplittable_nested_list(dictionary, dictionary_field); + let v2_2_error = encode_pages( + unsplittable_dictionary.clone(), + TestEncoding::StructuralU32, + HashMap::new(), + ) + .await + .unwrap_err(); + let v2_3_error = encode_pages( + unsplittable_dictionary, + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap_err(); + assert_eq!(v2_3_error.to_string(), v2_2_error.to_string()); + assert!( + v2_3_error + .to_string() + .contains("Mini-block cannot encode 70000 rep/def levels") + ); + + let cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1024 * 1024]) + .with_batch_size(NUM_ROWS as u32) + .with_range(1_999..2_002) + .with_indices(vec![0, 2_000, NUM_ROWS as u64 - 1]); + check_round_trip_encoding_of_data(vec![dictionary_array], &cases, HashMap::new()).await; + let packed_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1024 * 1024]) + .with_batch_size(NUM_ROWS as u32); + check_round_trip_encoding_of_data(vec![packed_array], &packed_cases, HashMap::new()).await; + + let single_row_cases = TestCases::default() + .with_encoding(TestEncoding::StructuralSparse) + .with_page_sizes(vec![1024 * 1024]) + .with_range(0..1) + .with_indices(vec![0]); + check_round_trip_encoding_of_data( + vec![unsplittable_packed], + &single_row_cases, + HashMap::new(), + ) + .await; + } + + #[tokio::test] + async fn test_auto_sparse_wide_values_keep_dense_fallback() { + let first = vec![0xAB_u8; 5_000]; + let second = vec![0xCD_u8; 5_000]; + let fixed_size_binary = Arc::new( + FixedSizeBinaryArray::try_from_sparse_iter_with_size( + [Some(first.as_slice()), Some(second.as_slice())].into_iter(), + 5_000, + ) + .unwrap(), + ) as ArrayRef; + + const FSL_DIMENSION: i32 = 2_048; + let fixed_size_list = Arc::new( + FixedSizeListArray::try_new( + Arc::new(ArrowField::new("item", DataType::Int32, true)), + FSL_DIMENSION, + Arc::new(Int32Array::from_iter_values(0..(FSL_DIMENSION * 2))), + None, + ) + .unwrap(), + ) as ArrayRef; + + for (label, values) in [ + ("fixed-size binary", fixed_size_binary), + ("fixed-size list", fixed_size_list), + ] { + let item_field = Arc::new(ArrowField::new("item", values.data_type().clone(), true)); + let array = unsplittable_nested_list(values, item_field); + + let v2_2 = encode_pages(array.clone(), TestEncoding::StructuralU32, HashMap::new()) + .await + .unwrap(); + let v2_3 = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + HashMap::new(), + ) + .await + .unwrap(); + for pages in [&v2_2, &v2_3] { + assert_eq!(pages.len(), 1, "unexpected {label} page count"); + assert!( + matches!( + page_layout(&pages[0]), + pb21::page_layout::Layout::FullZipLayout(_) + ), + "{label} should retain the dense full-zip fallback" + ); + } + + let explicit_error = encode_pages( + array.clone(), + TestEncoding::StructuralSparse, + sparse_metadata(), + ) + .await + .unwrap_err(); + assert!( + explicit_error.to_string().contains("too wide"), + "explicit sparse should preserve the {label} value error: {explicit_error}" + ); + + let cases = TestCases::default() + .with_u32_structural_encodings() + .with_range(0..1) + .with_indices(vec![0]); + check_round_trip_encoding_of_data(vec![array], &cases, HashMap::new()).await; + } + } + + #[test] + fn test_explicit_sparse_rejects_lance_2_2() { + let array = Arc::new(Int32Array::from(vec![Some(1), None])) as ArrayRef; + let Err(error) = create_encoder(&array, TestEncoding::StructuralU32, sparse_metadata()) + else { + panic!("expected Lance 2.2 to reject explicit sparse encoding"); + }; + assert!( + error + .to_string() + .contains("not enabled by the selected file format") + ); + + let structural_only = list_i32(vec![0, 0, 0], None); + let Err(error) = create_encoder( + &structural_only, + TestEncoding::StructuralU32, + sparse_metadata(), + ) else { + panic!("expected Lance 2.2 structural-only input to reject explicit sparse encoding"); + }; + assert!( + error + .to_string() + .contains("not enabled by the selected file format") + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/logical/struct.rs b/lance-artifact/rust/lance-encoding/src/encodings/logical/struct.rs new file mode 100644 index 000000000..cc3e88d44 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/logical/struct.rs @@ -0,0 +1,951 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + collections::{BinaryHeap, VecDeque}, + ops::Range, + sync::Arc, +}; + +use super::{ + fixed_size_list::StructuralFixedSizeListDecoder, list::StructuralListDecoder, + map::StructuralMapDecoder, primitive::StructuralPrimitiveFieldDecoder, +}; +use crate::{ + decoder::{ + DecodedArray, FilterExpression, LoadedPageShard, NextDecodeTask, PageEncoding, + ScheduledScanLine, SchedulerContext, StructuralDecodeArrayTask, StructuralFieldDecoder, + StructuralFieldScheduler, StructuralSchedulingJob, + }, + encoder::{EncodeTask, EncodedColumn, EncodedPage, FieldEncoder, OutOfLineBuffers}, + format::pb, + repdef::{CompositeRepDefUnraveler, RepDefBuilder}, +}; +use arrow_array::{Array, ArrayRef, StructArray, cast::AsArray}; +use arrow_schema::{DataType, Fields}; +use futures::{ + FutureExt, StreamExt, TryStreamExt, + future::BoxFuture, + stream::{FuturesOrdered, FuturesUnordered}, +}; +use itertools::Itertools; +use lance_arrow::FieldExt; +use lance_arrow::{deepcopy::deep_copy_nulls, r#struct::StructArrayExt}; +use lance_core::{Error, Result}; +use log::trace; + +#[derive(Debug)] +struct StructuralSchedulingJobWithStatus<'a> { + col_idx: u32, + col_name: &'a str, + job: Box, + rows_scheduled: u64, + rows_remaining: u64, + ready_scan_lines: VecDeque, +} + +impl PartialEq for StructuralSchedulingJobWithStatus<'_> { + fn eq(&self, other: &Self) -> bool { + self.col_idx == other.col_idx + } +} + +impl Eq for StructuralSchedulingJobWithStatus<'_> {} + +impl PartialOrd for StructuralSchedulingJobWithStatus<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for StructuralSchedulingJobWithStatus<'_> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + // Note this is reversed to make it min-heap + other.rows_scheduled.cmp(&self.rows_scheduled) + } +} + +/// Scheduling job for struct data +/// +/// The order in which we schedule the children is important. We want to schedule the child +/// with the least amount of data first. +/// +/// This allows us to decode entire rows as quickly as possible +#[derive(Debug)] +struct RepDefStructSchedulingJob<'a> { + /// A min-heap whose key is the # of rows currently scheduled + children: BinaryHeap>, + rows_scheduled: u64, + num_rows: u64, +} + +impl<'a> RepDefStructSchedulingJob<'a> { + fn new( + scheduler: &'a StructuralStructScheduler, + children: Vec>, + num_rows: u64, + ) -> Self { + let children = children + .into_iter() + .enumerate() + .map(|(idx, job)| StructuralSchedulingJobWithStatus { + col_idx: idx as u32, + col_name: scheduler.child_fields[idx].name(), + job, + rows_scheduled: 0, + rows_remaining: num_rows, + ready_scan_lines: VecDeque::new(), + }) + .collect::>(); + Self { + children, + rows_scheduled: 0, + num_rows, + } + } +} + +impl StructuralSchedulingJob for RepDefStructSchedulingJob<'_> { + fn schedule_next( + &mut self, + mut context: &mut SchedulerContext, + ) -> Result> { + if self.children.is_empty() { + // Special path for empty structs + if self.rows_scheduled == self.num_rows { + return Ok(Vec::new()); + } + self.rows_scheduled = self.num_rows; + return Ok(vec![ScheduledScanLine { + decoders: Vec::new(), + rows_scheduled: self.num_rows, + }]); + } + + let mut decoders = Vec::new(); + let old_rows_scheduled = self.rows_scheduled; + // Schedule as many children as we need to until we have scheduled at least one + // complete row + while old_rows_scheduled == self.rows_scheduled { + if self.children.is_empty() { + // Early exit when schedulers are exhausted prematurely (TODO: does this still happen?) + return Ok(Vec::new()); + } + let mut next_child = self.children.pop().unwrap(); + if next_child.ready_scan_lines.is_empty() { + let scoped = context.push(next_child.col_name, next_child.col_idx); + let child_scans = next_child.job.schedule_next(scoped.context)?; + context = scoped.pop(); + if child_scans.is_empty() { + // Continue without pushing next_child back onto the heap (it is done) + continue; + } + next_child.ready_scan_lines.extend(child_scans); + } + let child_scan = next_child.ready_scan_lines.pop_front().unwrap(); + trace!( + "Scheduled {} rows for child {}", + child_scan.rows_scheduled, next_child.col_idx + ); + next_child.rows_scheduled += child_scan.rows_scheduled; + next_child.rows_remaining -= child_scan.rows_scheduled; + decoders.extend(child_scan.decoders); + self.children.push(next_child); + self.rows_scheduled = self.children.peek().unwrap().rows_scheduled; + } + let struct_rows_scheduled = self.rows_scheduled - old_rows_scheduled; + Ok(vec![ScheduledScanLine { + decoders, + rows_scheduled: struct_rows_scheduled, + }]) + } +} + +/// A scheduler for structs +/// +/// The implementation is actually a bit more tricky than one might initially think. We can't just +/// go through and schedule each column one after the other. This would mean our decode can't start +/// until nearly all the data has arrived (since we need data from each column to yield a batch) +/// +/// Instead, we schedule in row-major fashion +/// +/// Note: this scheduler is the starting point for all decoding. This is because we treat the top-level +/// record batch as a non-nullable struct. +#[derive(Debug)] +pub struct StructuralStructScheduler { + children: Vec>, + child_fields: Fields, +} + +impl StructuralStructScheduler { + pub fn new(children: Vec>, child_fields: Fields) -> Self { + Self { + children, + child_fields, + } + } +} + +impl StructuralFieldScheduler for StructuralStructScheduler { + fn schedule_ranges<'a>( + &'a self, + ranges: &[Range], + filter: &FilterExpression, + ) -> Result> { + let num_rows = ranges.iter().map(|r| r.end - r.start).sum(); + + let child_schedulers = self + .children + .iter() + .map(|child| child.schedule_ranges(ranges, filter)) + .collect::>>()?; + + Ok(Box::new(RepDefStructSchedulingJob::new( + self, + child_schedulers, + num_rows, + ))) + } + + fn initialize<'a>( + &'a mut self, + filter: &'a FilterExpression, + context: &'a SchedulerContext, + ) -> BoxFuture<'a, Result<()>> { + let children_initialization = self + .children + .iter_mut() + .map(|child| child.initialize(filter, context)) + .collect::>(); + async move { + children_initialization + .map(|res| res.map(|_| ())) + .try_collect::>() + .await?; + Ok(()) + } + .boxed() + } +} + +#[derive(Debug)] +pub struct StructuralStructDecoder { + children: Vec>, + data_type: DataType, + child_fields: Fields, + // The root decoder is slightly different because it cannot have nulls + is_root: bool, +} + +impl StructuralStructDecoder { + pub fn new(fields: Fields, should_validate: bool, is_root: bool) -> Result { + let children = fields + .iter() + .map(|field| Self::field_to_decoder(field, should_validate)) + .collect::>>()?; + let data_type = DataType::Struct(fields.clone()); + Ok(Self { + data_type, + children, + child_fields: fields, + is_root, + }) + } + + fn field_to_decoder( + field: &Arc, + should_validate: bool, + ) -> Result> { + match field.data_type() { + DataType::Struct(fields) => { + if field.is_packed_struct() || field.is_blob() { + let decoder = + StructuralPrimitiveFieldDecoder::new(&field.clone(), should_validate); + Ok(Box::new(decoder)) + } else { + Ok(Box::new(Self::new(fields.clone(), should_validate, false)?)) + } + } + DataType::List(child_field) | DataType::LargeList(child_field) => { + let child_decoder = Self::field_to_decoder(child_field, should_validate)?; + Ok(Box::new(StructuralListDecoder::new( + child_decoder, + field.data_type().clone(), + ))) + } + DataType::FixedSizeList(child_field, _) + if matches!(child_field.data_type(), DataType::Struct(_)) => + { + // FixedSizeList containing Struct needs structural decoding + let child_decoder = Self::field_to_decoder(child_field, should_validate)?; + Ok(Box::new(StructuralFixedSizeListDecoder::new( + child_decoder, + field.data_type().clone(), + ))) + } + DataType::Map(entries_field, keys_sorted) => { + if *keys_sorted { + return Err(Error::not_supported_source( + "Map data type with keys_sorted=true is not supported yet" + .to_string() + .into(), + )); + } + let child_decoder = Self::field_to_decoder(entries_field, should_validate)?; + Ok(Box::new(StructuralMapDecoder::new( + child_decoder, + field.data_type().clone(), + ))) + } + DataType::RunEndEncoded(_, _) => todo!(), + DataType::ListView(_) | DataType::LargeListView(_) => todo!(), + DataType::Union(_, _) => todo!(), + _ => Ok(Box::new(StructuralPrimitiveFieldDecoder::new( + field, + should_validate, + ))), + } + } + + pub fn drain_batch_task(&mut self, num_rows: u64) -> Result { + let array_drain = self.drain(num_rows)?; + Ok(NextDecodeTask { + num_rows, + task: Box::new(array_drain), + }) + } +} + +impl StructuralFieldDecoder for StructuralStructDecoder { + fn accept_page(&mut self, mut child: LoadedPageShard) -> Result<()> { + // children with empty path should not be delivered to this method + let child_idx = child.path.pop_front().unwrap(); + // This decoder is intended for one of our children + self.children[child_idx as usize].accept_page(child)?; + Ok(()) + } + + fn drain(&mut self, num_rows: u64) -> Result> { + let child_tasks = self + .children + .iter_mut() + .map(|child| child.drain(num_rows)) + .collect::>>()?; + Ok(Box::new(RepDefStructDecodeTask { + children: child_tasks, + child_fields: self.child_fields.clone(), + is_root: self.is_root, + num_rows, + })) + } + + fn data_type(&self) -> &DataType { + &self.data_type + } +} + +#[derive(Debug)] +struct RepDefStructDecodeTask { + children: Vec>, + child_fields: Fields, + is_root: bool, + num_rows: u64, +} + +impl StructuralDecodeArrayTask for RepDefStructDecodeTask { + fn decode(self: Box) -> Result { + if self.children.is_empty() { + return Ok(DecodedArray { + array: Arc::new(StructArray::new_empty_fields(self.num_rows as usize, None)), + repdef: CompositeRepDefUnraveler::new(vec![]), + data_size: 0, + }); + } + + let arrays = self + .children + .into_iter() + .map(|task| task.decode()) + .collect::>>()?; + let mut children = Vec::with_capacity(arrays.len()); + let mut repdefs = Vec::with_capacity(arrays.len()); + let mut data_size = 0u64; + let mut arrays_iter = arrays.into_iter(); + let first_array = arrays_iter.next().ok_or_else(|| { + Error::internal("Struct decoder unexpectedly has no child arrays".to_string()) + })?; + let length = first_array.array.len(); + + // The repdef should be identical across all children at this point + repdefs.push(first_array.repdef); + data_size += first_array.data_size; + children.push(first_array.array); + + for array in arrays_iter { + if length != array.array.len() { + return Err(Error::invalid_input_source( + format!( + "Struct child array length {} does not match sibling length {}", + array.array.len(), + length + ) + .into(), + )); + } + data_size += array.data_size; + children.push(array.array); + repdefs.push(array.repdef); + } + + // Dense rep/def state can retain child-specific repetition information after a child + // decoder finishes, so comparing dense siblings is not meaningful. If any child carries + // sparse state, keep a sparse child as the canonical structural plan and compare it with + // every other sparse sibling so sparse metadata is never silently discarded. + let primary_repdef = repdefs + .iter() + .position(CompositeRepDefUnraveler::has_sparse) + .unwrap_or(0); + let mut repdef = repdefs.swap_remove(primary_repdef); + if repdef.has_sparse() { + for sibling in repdefs { + if sibling.has_sparse() { + repdef.add_compatibility_check(sibling); + } + } + } + + let validity = if self.is_root { + repdef.ensure_exhausted()?; + None + } else { + repdef.unravel_validity(length)? + }; + + let array = StructArray::try_new(self.child_fields, children, validity) + .map_err(|e| Error::invalid_input_source(e.to_string().into()))?; + Ok(DecodedArray { + array: Arc::new(array), + repdef, + data_size, + }) + } +} + +/// A structural encoder for struct fields +/// +/// The struct's validity is added to the rep/def builder +/// and the builder is cloned to all children. +pub struct StructStructuralEncoder { + keep_original_array: bool, + children: Vec>, +} + +impl StructStructuralEncoder { + pub fn new(keep_original_array: bool, children: Vec>) -> Self { + Self { + keep_original_array, + children, + } + } +} + +impl FieldEncoder for StructStructuralEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + mut repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + let struct_array = array.as_struct(); + let mut struct_array = struct_array.normalize_slicing()?; + if let Some(validity) = struct_array.nulls() { + if self.keep_original_array { + repdef.add_validity_bitmap(validity.clone()) + } else { + repdef.add_validity_bitmap(deep_copy_nulls(Some(validity)).unwrap()) + } + struct_array = struct_array.pushdown_nulls()?; + } else { + repdef.add_no_null(struct_array.len()); + } + let child_tasks = self + .children + .iter_mut() + .zip(struct_array.columns().iter()) + .map(|(encoder, arr)| { + encoder.maybe_encode( + arr.clone(), + external_buffers, + repdef.clone(), + row_number, + num_rows, + ) + }) + .collect::>>()?; + Ok(child_tasks.into_iter().flatten().collect::>()) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + self.children + .iter_mut() + .map(|encoder| encoder.flush(external_buffers)) + .flatten_ok() + .collect::>>() + } + + fn num_columns(&self) -> u32 { + self.children + .iter() + .map(|child| child.num_columns()) + .sum::() + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + let mut child_columns = self + .children + .iter_mut() + .map(|child| child.finish(external_buffers)) + .collect::>(); + async move { + let mut encoded_columns = Vec::with_capacity(child_columns.len()); + while let Some(child_cols) = child_columns.next().await { + encoded_columns.extend(child_cols?); + } + Ok(encoded_columns) + } + .boxed() + } +} + +pub struct StructFieldEncoder { + children: Vec>, + column_index: u32, + num_rows_seen: u64, +} + +impl StructFieldEncoder { + pub fn new(children: Vec>, column_index: u32) -> Self { + Self { + children, + column_index, + num_rows_seen: 0, + } + } +} + +impl FieldEncoder for StructFieldEncoder { + fn maybe_encode( + &mut self, + array: ArrayRef, + external_buffers: &mut OutOfLineBuffers, + repdef: RepDefBuilder, + row_number: u64, + num_rows: u64, + ) -> Result> { + self.num_rows_seen += array.len() as u64; + let struct_array = array.as_struct(); + let child_tasks = self + .children + .iter_mut() + .zip(struct_array.columns().iter()) + .map(|(encoder, arr)| { + encoder.maybe_encode( + arr.clone(), + external_buffers, + repdef.clone(), + row_number, + num_rows, + ) + }) + .collect::>>()?; + Ok(child_tasks.into_iter().flatten().collect::>()) + } + + fn flush(&mut self, external_buffers: &mut OutOfLineBuffers) -> Result> { + let child_tasks = self + .children + .iter_mut() + .map(|encoder| encoder.flush(external_buffers)) + .collect::>>()?; + Ok(child_tasks.into_iter().flatten().collect::>()) + } + + fn num_columns(&self) -> u32 { + self.children + .iter() + .map(|child| child.num_columns()) + .sum::() + + 1 + } + + fn finish( + &mut self, + external_buffers: &mut OutOfLineBuffers, + ) -> BoxFuture<'_, Result>> { + let mut child_columns = self + .children + .iter_mut() + .map(|child| child.finish(external_buffers)) + .collect::>(); + let num_rows_seen = self.num_rows_seen; + let column_index = self.column_index; + async move { + let mut columns = Vec::new(); + // Add a column for the struct header + let mut header = EncodedColumn::default(); + header.final_pages.push(EncodedPage { + data: Vec::new(), + description: PageEncoding::Legacy(pb::ArrayEncoding { + array_encoding: Some(pb::array_encoding::ArrayEncoding::Struct( + pb::SimpleStruct {}, + )), + }), + num_rows: num_rows_seen, + column_idx: column_index, + row_number: 0, // Not used by legacy encoding + }); + columns.push(header); + // Now run finish on the children + while let Some(child_cols) = child_columns.next().await { + columns.extend(child_cols?); + } + Ok(columns) + } + .boxed() + } +} + +#[cfg(test)] +mod tests { + + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{ + Array, ArrayRef, Float64Array, Int32Array, Int64Array, ListArray, StructArray, + builder::{Int32Builder, ListBuilder}, + }; + use arrow_buffer::{BooleanBuffer, NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field, Fields}; + + use crate::testing::{TestCases, check_basic_random, check_round_trip_encoding_of_data}; + + #[test_log::test(tokio::test)] + async fn test_simple_struct() { + let data_type = DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::Int32, false), + Field::new("b", DataType::Int32, false), + ])); + let field = Field::new("", data_type, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_nullable_struct() { + // Test data struct> + // - score: null + // location: + // x: 1 + // y: 6 + // - score: 12 + // location: + // x: 2 + // y: null + // - score: 13 + // location: + // x: 3 + // y: 8 + // - score: 14 + // location: null + // - null + // + let inner_fields = Fields::from(vec![ + Field::new("x", DataType::Int32, false), + Field::new("y", DataType::Int32, true), + ]); + let inner_struct = DataType::Struct(inner_fields.clone()); + let outer_fields = Fields::from(vec![ + Field::new("score", DataType::Int32, true), + Field::new("location", inner_struct, true), + ]); + + let x_vals = Int32Array::from(vec![Some(1), Some(2), Some(3), Some(4), Some(5)]); + let y_vals = Int32Array::from(vec![Some(6), None, Some(8), Some(9), Some(10)]); + let scores = Int32Array::from(vec![None, Some(12), Some(13), Some(14), Some(15)]); + + let location_validity = NullBuffer::from(vec![true, true, true, false, true]); + let locations = StructArray::new( + inner_fields, + vec![Arc::new(x_vals), Arc::new(y_vals)], + Some(location_validity), + ); + + let rows_validity = NullBuffer::from(vec![true, true, true, true, false]); + let rows = StructArray::new( + outer_fields, + vec![Arc::new(scores), Arc::new(locations)], + Some(rows_validity), + ); + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(rows)], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_masked_nonempty_list() { + // [[1, 2], [NULL], [4], [], NULL, NULL-STRUCT] + // + let items = Int32Array::from(vec![Some(1), Some(2), None, Some(4), Some(5), Some(6)]); + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 2, 3, 4, 4, 4, 5])); + let list_validity = BooleanBuffer::from(vec![true, true, true, true, false, true]); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Int32, true)), + offsets, + Arc::new(items), + Some(NullBuffer::new(list_validity)), + ); + let struct_validity = BooleanBuffer::from(vec![true, true, true, true, true, false]); + let struct_array = StructArray::new( + Fields::from(vec![Field::new( + "inner_list", + list_array.data_type().clone(), + true, + )]), + vec![Arc::new(list_array)], + Some(NullBuffer::new(struct_validity)), + ); + check_round_trip_encoding_of_data( + vec![Arc::new(struct_array)], + &TestCases::default().with_structural_encodings(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_struct_list() { + // [[1, 2], [NULL], [4], [], NULL, NULL-STRUCT] + // + let items = Int32Array::from(vec![Some(1), Some(2), None, Some(4)]); + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 2, 3, 4, 4, 4, 4])); + let list_validity = BooleanBuffer::from(vec![true, true, true, true, false, true]); + let list_array = ListArray::new( + Arc::new(Field::new("item", DataType::Int32, true)), + offsets, + Arc::new(items), + Some(NullBuffer::new(list_validity)), + ); + let struct_validity = BooleanBuffer::from(vec![true, true, true, true, true, false]); + let struct_array = StructArray::new( + Fields::from(vec![Field::new( + "inner_list", + list_array.data_type().clone(), + true, + )]), + vec![Arc::new(list_array)], + Some(NullBuffer::new(struct_validity)), + ); + check_round_trip_encoding_of_data( + vec![Arc::new(struct_array)], + &TestCases::default().with_structural_encodings(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_struct_list() { + let data_type = DataType::Struct(Fields::from(vec![ + Field::new( + "inner_list", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + ), + Field::new("outer_int", DataType::Int32, true), + ])); + let field = Field::new("row", data_type, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_empty_struct() { + // It's technically legal for a struct to have 0 children, need to + // make sure we support that + let data_type = DataType::Struct(Fields::from(Vec::::default())); + let field = Field::new("row", data_type, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_complicated_struct() { + let data_type = DataType::Struct(Fields::from(vec![ + Field::new("int", DataType::Int32, true), + Field::new( + "inner", + DataType::Struct(Fields::from(vec![ + Field::new("inner_int", DataType::Int32, true), + Field::new( + "inner_list", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + ), + ])), + true, + ), + Field::new("outer_binary", DataType::Binary, true), + ])); + let field = Field::new("row", data_type, false); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_list_of_struct_with_null_struct_element() { + // Regression: a list containing structs where most struct elements are null + // causes a length mismatch during decoding with V2_2 encoding. + use arrow_array::StringArray; + + let tag_array = StringArray::from(vec![ + Some("valid"), + Some("null_struct"), + Some("valid"), + Some("valid"), + ]); + let struct_fields = Fields::from(vec![Field::new("tag", DataType::Utf8, true)]); + // 3 out of 4 struct elements are null + let struct_validity = NullBuffer::from(vec![false, true, false, false]); + let struct_array = StructArray::new( + struct_fields.clone(), + vec![Arc::new(tag_array)], + Some(struct_validity), + ); + + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 4])); + let list_field = Field::new("item", DataType::Struct(struct_fields), true); + let list_array = + ListArray::new(Arc::new(list_field), offsets, Arc::new(struct_array), None); + + check_round_trip_encoding_of_data( + vec![Arc::new(list_array)], + &TestCases::default().with_u32_structural_encodings(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_list_of_struct_with_all_null_child() { + use arrow_array::StringArray; + + let a_array = StringArray::from(vec![Some("w"), Some("x"), Some("y"), Some("z")]); + let b_array = StringArray::from(vec![None::<&str>, None, None, None]); + let struct_fields = Fields::from(vec![ + Field::new("a", DataType::Utf8, true), + Field::new("b", DataType::Utf8, true), + ]); + let struct_array = StructArray::new( + struct_fields.clone(), + vec![Arc::new(a_array), Arc::new(b_array)], + None, + ); + + let offsets = OffsetBuffer::new(ScalarBuffer::::from(vec![0, 2, 4])); + let list_field = Field::new("item", DataType::Struct(struct_fields), true); + let list_array = + ListArray::new(Arc::new(list_field), offsets, Arc::new(struct_array), None); + + check_round_trip_encoding_of_data( + vec![Arc::new(list_array)], + &TestCases::default() + .with_range(0..1) + .with_range(1..2) + .with_indices(vec![0]) + .with_indices(vec![1]) + .with_structural_encodings(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_list_of_struct_with_constant_child_and_empty_lists() { + let item_fields = Fields::from(vec![ + Field::new("maneuver", DataType::Int64, true), + Field::new("remaining_dist", DataType::Float64, true), + ]); + let item_array = StructArray::new( + item_fields.clone(), + vec![ + Arc::new(Int64Array::from(vec![Some(3), Some(3), Some(3)])), + Arc::new(Float64Array::from(vec![Some(1.0), Some(2.0), Some(3.0)])), + ], + None, + ); + + let list_field = Field::new("item", DataType::Struct(item_fields), true); + let list_array = ListArray::new( + Arc::new(list_field), + OffsetBuffer::new(ScalarBuffer::::from(vec![0, 1, 2, 3, 3, 3, 3])), + Arc::new(item_array), + None, + ); + + let data_fields = Fields::from(vec![Field::new( + "maneuvers", + list_array.data_type().clone(), + true, + )]); + let data_array = StructArray::new(data_fields.clone(), vec![Arc::new(list_array)], None); + let row_validity = NullBuffer::from(vec![true, true, true, true, true, false]); + let row_array = StructArray::new( + Fields::from(vec![Field::new( + "data", + DataType::Struct(data_fields), + true, + )]), + vec![Arc::new(data_array)], + Some(row_validity), + ); + + check_round_trip_encoding_of_data( + vec![Arc::new(row_array)], + &TestCases::default().with_structural_encodings(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_ragged_scheduling() { + // This test covers scheduling when batches straddle page boundaries + + // Create a list with 10k nulls + let items_builder = Int32Builder::new(); + let mut list_builder = ListBuilder::new(items_builder); + for _ in 0..10000 { + list_builder.append_null(); + } + let list_array = Arc::new(list_builder.finish()); + let int_array = Arc::new(Int32Array::from_iter_values(0..10000)); + let fields = vec![ + Field::new("", list_array.data_type().clone(), true), + Field::new("", int_array.data_type().clone(), true), + ]; + let struct_array = Arc::new(StructArray::new( + Fields::from(fields), + vec![list_array, int_array], + None, + )) as ArrayRef; + let struct_arrays = (0..10000) + // Intentionally skip in some randomish amount to create more ragged scheduling + .step_by(437) + .map(|offset| struct_array.slice(offset, 437.min(10000 - offset))) + .collect::>(); + check_round_trip_encoding_of_data(struct_arrays, &TestCases::default(), HashMap::new()) + .await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical.rs new file mode 100644 index 000000000..0439c0216 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical.rs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod binary; +#[cfg(feature = "bitpacking")] +pub mod bitpacking; +pub mod block; +pub mod byte_stream_split; +pub mod constant; +pub mod fsst; +pub mod general; +pub mod packed; +pub mod rle; +pub mod value; diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/binary.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/binary.rs new file mode 100644 index 000000000..06df60434 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/binary.rs @@ -0,0 +1,1325 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Basic encodings for variable width data +//! +//! These are not compression but represent the "leaf" encodings for variable length data +//! where we simply match the data with the rules of the structural encoding. +//! +//! These encodings are transparent since we aren't actually doing any compression. No information +//! is needed in the encoding description. + +use arrow_array::OffsetSizeTrait; +use byteorder::{ByteOrder, LittleEndian}; +use core::panic; + +use crate::compression::{ + BlockCompressor, BlockDecompressor, MiniBlockDecompressor, VariablePerValueDecompressor, +}; + +use crate::buffer::LanceBuffer; +use crate::data::{BlockInfo, DataBlock, VariableWidthBlock}; +use crate::encodings::logical::primitive::fullzip::{PerValueCompressor, PerValueDataBlock}; +use crate::encodings::logical::primitive::miniblock::{ + MAX_MINIBLOCK_VALUES, MiniBlockChunk, MiniBlockCompressed, MiniBlockCompressionContext, + MiniBlockCompressor, +}; +use crate::format::pb21::CompressiveEncoding; +use crate::format::pb21::compressive_encoding::Compression; +use crate::format::{ProtobufUtils21, pb21}; + +use lance_core::utils::bit::pad_bytes_to; +use lance_core::{Error, Result}; + +#[derive(Debug)] +pub struct BinaryMiniBlockEncoder { + minichunk_size: i64, +} + +impl Default for BinaryMiniBlockEncoder { + fn default() -> Self { + Self { + minichunk_size: *AIM_MINICHUNK_SIZE, + } + } +} + +const DEFAULT_AIM_MINICHUNK_SIZE: i64 = 4 * 1024; + +pub static AIM_MINICHUNK_SIZE: std::sync::LazyLock = std::sync::LazyLock::new(|| { + std::env::var("LANCE_BINARY_MINIBLOCK_CHUNK_SIZE") + .unwrap_or_else(|_| DEFAULT_AIM_MINICHUNK_SIZE.to_string()) + .parse::() + .unwrap_or(DEFAULT_AIM_MINICHUNK_SIZE) +}); + +// Make it to support both u32 and u64 +fn chunk_offsets( + offsets: &[N], + data: &[u8], + alignment: usize, + minichunk_size: i64, +) -> (Vec, Vec) { + #[derive(Debug)] + struct ChunkInfo { + chunk_start_offset_in_orig_idx: usize, + chunk_last_offset_in_orig_idx: usize, + // the bytes in every chunk starts at `chunk.bytes_start_offset` + bytes_start_offset: usize, + // every chunk is padded to 8 bytes. + // we need to interpret every chunk as &[u32] so we need it to padded at least to 4 bytes, + // this field can actually be eliminated and I can use `num_bytes` in `MiniBlockChunk` to compute + // the `output_total_bytes`. + padded_chunk_size: usize, + } + + let byte_width: usize = N::get_byte_width(); + let mut chunks_info = vec![]; + let mut chunks = vec![]; + let mut last_offset_in_orig_idx = 0; + loop { + let this_last_offset_in_orig_idx = + search_next_offset_idx(offsets, last_offset_in_orig_idx, minichunk_size); + + let num_values_in_this_chunk = this_last_offset_in_orig_idx - last_offset_in_orig_idx; + let chunk_bytes = offsets[this_last_offset_in_orig_idx] - offsets[last_offset_in_orig_idx]; + let this_chunk_size = + (num_values_in_this_chunk + 1) * byte_width + chunk_bytes.to_usize().unwrap(); + + let padded_chunk_size = this_chunk_size.next_multiple_of(alignment); + debug_assert!(padded_chunk_size > 0); + + let this_chunk_bytes_start_offset = (num_values_in_this_chunk + 1) * byte_width; + chunks_info.push(ChunkInfo { + chunk_start_offset_in_orig_idx: last_offset_in_orig_idx, + chunk_last_offset_in_orig_idx: this_last_offset_in_orig_idx, + bytes_start_offset: this_chunk_bytes_start_offset, + padded_chunk_size, + }); + chunks.push(MiniBlockChunk { + log_num_values: if this_last_offset_in_orig_idx == offsets.len() - 1 { + 0 + } else { + num_values_in_this_chunk.trailing_zeros() as u8 + }, + buffer_sizes: vec![padded_chunk_size as u32], + }); + if this_last_offset_in_orig_idx == offsets.len() - 1 { + break; + } + last_offset_in_orig_idx = this_last_offset_in_orig_idx; + } + + let output_total_bytes = chunks_info + .iter() + .map(|chunk_info| chunk_info.padded_chunk_size) + .sum::(); + + let mut output: Vec = Vec::with_capacity(output_total_bytes); + + for chunk in chunks_info { + let this_chunk_offsets: Vec = offsets + [chunk.chunk_start_offset_in_orig_idx..=chunk.chunk_last_offset_in_orig_idx] + .iter() + .map(|offset| { + *offset - offsets[chunk.chunk_start_offset_in_orig_idx] + + N::from_usize(chunk.bytes_start_offset).unwrap() + }) + .collect(); + + let this_chunk_offsets = LanceBuffer::reinterpret_vec(this_chunk_offsets); + output.extend_from_slice(&this_chunk_offsets); + + let start_in_orig = offsets[chunk.chunk_start_offset_in_orig_idx] + .to_usize() + .unwrap(); + let end_in_orig = offsets[chunk.chunk_last_offset_in_orig_idx] + .to_usize() + .unwrap(); + output.extend_from_slice(&data[start_in_orig..end_in_orig]); + + // pad this chunk to make it align to desired bytes. + const PAD_BYTE: u8 = 72; + let pad_len = pad_bytes_to(output.len(), alignment); + + // Compare with usize literal to avoid type mismatch with N + if pad_len > 0_usize { + output.extend(std::iter::repeat_n(PAD_BYTE, pad_len)); + } + } + (vec![LanceBuffer::reinterpret_vec(output)], chunks) +} + +// search for the next offset index to cut the values into a chunk. +// this function incrementally peek the number of values in a chunk, +// each time multiplies the number of values by 2. +// It returns the offset_idx in `offsets` that belongs to this chunk. +fn search_next_offset_idx( + offsets: &[N], + last_offset_idx: usize, + minichunk_size: i64, +) -> usize { + // MiniBlockChunk uses `log_num_values == 0` as a sentinel for the final chunk. This means we + // must avoid creating 1-value chunks except for the final chunk, even if the configured + // `minichunk_size` is too small to fit more than one value. + let remaining_values = offsets.len().saturating_sub(last_offset_idx + 1); + if remaining_values <= 1 { + return offsets.len() - 1; + } + + let mut num_values = 2; + let mut new_num_values = num_values * 2; + loop { + if last_offset_idx + new_num_values >= offsets.len() { + let existing_bytes = offsets[offsets.len() - 1] - offsets[last_offset_idx]; + // existing bytes plus the new offset size + let new_size = existing_bytes + + N::from_usize((offsets.len() - last_offset_idx) * N::get_byte_width()).unwrap(); + if new_size.to_i64().unwrap() <= minichunk_size { + // case 1: can fit the rest of all data into a miniblock + return offsets.len() - 1; + } else { + // case 2: can only fit the last tried `num_values` into a miniblock + return last_offset_idx + num_values; + } + } + let existing_bytes = offsets[last_offset_idx + new_num_values] - offsets[last_offset_idx]; + let new_size = + existing_bytes + N::from_usize((new_num_values + 1) * N::get_byte_width()).unwrap(); + if new_size.to_i64().unwrap() <= minichunk_size { + if new_num_values * 2 > *MAX_MINIBLOCK_VALUES as usize { + // hit the max number of values limit + break; + } + num_values = new_num_values; + new_num_values *= 2; + } else { + break; + } + } + last_offset_idx + num_values +} + +impl BinaryMiniBlockEncoder { + pub fn new(minichunk_size: Option) -> Self { + Self { + minichunk_size: minichunk_size.unwrap_or(*AIM_MINICHUNK_SIZE), + } + } + + // put binary data into chunks, every chunk is less than or equal to `minichunk_size`. + // In each chunk, offsets are put first then followed by binary bytes data, each chunk is padded to 8 bytes. + // the offsets in the chunk points to the bytes offset in this chunk. + fn chunk_data(&self, data: VariableWidthBlock) -> (MiniBlockCompressed, CompressiveEncoding) { + // TODO: Support compression of offsets + // TODO: Support general compression of data + match data.bits_per_offset { + 32 => { + let offsets = data.offsets.borrow_to_typed_slice::(); + let (buffers, chunks) = + chunk_offsets(offsets.as_ref(), &data.data, 4, self.minichunk_size); + ( + MiniBlockCompressed { + data: buffers, + chunks, + num_values: data.num_values, + }, + ProtobufUtils21::variable(ProtobufUtils21::flat(32, None), None), + ) + } + 64 => { + let offsets = data.offsets.borrow_to_typed_slice::(); + let (buffers, chunks) = + chunk_offsets(offsets.as_ref(), &data.data, 8, self.minichunk_size); + ( + MiniBlockCompressed { + data: buffers, + chunks, + num_values: data.num_values, + }, + ProtobufUtils21::variable(ProtobufUtils21::flat(64, None), None), + ) + } + _ => panic!("Unsupported bits_per_offset={}", data.bits_per_offset), + } + } +} + +impl MiniBlockCompressor for BinaryMiniBlockEncoder { + fn compress( + &self, + _context: MiniBlockCompressionContext, + data: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match data { + DataBlock::VariableWidth(variable_width) => Ok(self.chunk_data(variable_width)), + _ => Err(Error::invalid_input_source( + format!( + "Cannot compress a data block of type {} with BinaryMiniBlockEncoder", + data.name() + ) + .into(), + )), + } + } +} + +#[derive(Debug)] +pub struct BinaryMiniBlockDecompressor { + bits_per_offset: u8, +} + +impl BinaryMiniBlockDecompressor { + pub fn new(bits_per_offset: u8) -> Self { + assert!(bits_per_offset == 32 || bits_per_offset == 64); + Self { bits_per_offset } + } + + pub fn from_variable(variable: &pb21::Variable) -> Self { + if let Compression::Flat(flat) = variable + .offsets + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + { + Self { + bits_per_offset: flat.bits_per_value as u8, + } + } else { + panic!("Unsupported offsets compression: {:?}", variable.offsets); + } + } +} + +/// Cold path: pinpoint why the chunk-relative offsets of a binary mini-block +/// chunk failed validation. +fn chunk_offset_violation_error>(offsets: &[T], chunk_len: usize) -> Error { + let mut previous: u64 = offsets[0].into(); + for (position, &offset) in offsets.iter().enumerate().skip(1) { + let offset: u64 = offset.into(); + if offset < previous { + return Error::corrupt_file_named( + "binary mini-block", + format!( + "value offset at position {position} decreases: {offset} < {previous} \ + (chunk is {chunk_len} bytes)" + ), + ); + } + previous = offset; + } + Error::corrupt_file_named( + "binary mini-block", + format!("value offset {previous} is out of bounds for a chunk of {chunk_len} bytes"), + ) +} + +impl MiniBlockDecompressor for BinaryMiniBlockDecompressor { + // decompress a MiniBlock of binary data, the num_values must be less than or equal + // to the number of values this MiniBlock has, BinaryMiniBlock doesn't store `the number of values` + // it has so assertion can not be done here and the caller of `decompress` must ensure + // `num_values` <= number of values in the chunk. + // + // The chunk-relative value offsets at the front of the chunk come straight + // from the file and are used to slice the chunk buffer, so corrupt values + // must surface as a typed error instead of a panic or an out-of-bounds + // read. The monotonicity check rides along the existing rebase loop (the + // `&=` accumulation keeps it branchless) so validation adds no extra pass. + fn decompress(&self, data: Vec, num_values: u64) -> Result { + assert_eq!(data.len(), 1); + let data = data.into_iter().next().unwrap(); + + let bytes_per_offset = self.bits_per_offset as usize / 8; + if !data.len().is_multiple_of(bytes_per_offset) { + return Err(Error::corrupt_file_named( + "binary mini-block", + format!( + "chunk size {} is not a multiple of the {}-byte offset width", + data.len(), + bytes_per_offset + ), + )); + } + let num_offsets = (num_values as usize).checked_add(1).ok_or_else(|| { + Error::corrupt_file_named( + "binary mini-block", + format!("cannot decode {num_values} values from a single chunk"), + ) + })?; + if data.len() / bytes_per_offset < num_offsets { + return Err(Error::corrupt_file_named( + "binary mini-block", + format!( + "chunk of {} bytes holds {} offsets but decoding {} values requires {}", + data.len(), + data.len() / bytes_per_offset, + num_values, + num_offsets + ), + )); + } + + // The value region must start past the offsets being decoded, otherwise + // the offset table itself aliases into the value bytes. A lower bound + // (not equality) because a prefix read of the chunk legitimately leaves + // unrequested offsets between the requested prefix and the values. + let min_value_region_start = num_offsets * bytes_per_offset; + let value_region_overlap_error = |first: u64| { + Error::corrupt_file_named( + "binary mini-block", + format!( + "value region starts at offset {first} which overlaps the {num_offsets} \ + requested offsets ({min_value_region_start} bytes)" + ), + ) + }; + + if self.bits_per_offset == 64 { + let offsets_buffer = data.borrow_to_typed_slice::(); + let offsets = &offsets_buffer.as_ref()[..num_offsets]; + + let first = offsets[0]; + if first < min_value_region_start as u64 { + return Err(value_region_overlap_error(first)); + } + let mut previous = first; + let mut is_monotonic = true; + let result_offsets = offsets + .iter() + .map(|&offset| { + is_monotonic &= previous <= offset; + previous = offset; + offset.wrapping_sub(first) + }) + .collect::>(); + let last = offsets[num_offsets - 1]; + if !is_monotonic || last as usize > data.len() { + return Err(chunk_offset_violation_error(offsets, data.len())); + } + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(data[first as usize..last as usize].to_vec()), + offsets: LanceBuffer::reinterpret_vec(result_offsets), + bits_per_offset: 64, + num_values, + block_info: BlockInfo::new(), + })) + } else { + let offsets_buffer = data.borrow_to_typed_slice::(); + let offsets = &offsets_buffer.as_ref()[..num_offsets]; + + let first = offsets[0]; + if (first as u64) < min_value_region_start as u64 { + return Err(value_region_overlap_error(first as u64)); + } + let mut previous = first; + let mut is_monotonic = true; + let result_offsets = offsets + .iter() + .map(|&offset| { + is_monotonic &= previous <= offset; + previous = offset; + offset.wrapping_sub(first) + }) + .collect::>(); + let last = offsets[num_offsets - 1]; + if !is_monotonic || last as usize > data.len() { + return Err(chunk_offset_violation_error(offsets, data.len())); + } + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(data[first as usize..last as usize].to_vec()), + offsets: LanceBuffer::reinterpret_vec(result_offsets), + bits_per_offset: 32, + num_values, + block_info: BlockInfo::new(), + })) + } + } +} + +/// Most basic encoding for variable-width data which does no compression at all +/// The DataBlock memory layout looks like below: +/// +/// | bits_per_offset | bytes_start_offset | offsets data | bytes data | +/// | ------------------------- | ------------------------- | ------------ | ---------- | +/// | /8 bytes | /8 bytes | offsets_len | data_len | +/// +/// It's used in VariableEncoder and BinaryBlockDecompressor +/// +#[derive(Debug, Default)] +pub struct VariableEncoder {} + +impl BlockCompressor for VariableEncoder { + fn compress(&self, mut data: DataBlock) -> Result { + match data { + DataBlock::VariableWidth(ref mut variable_width_data) => { + match variable_width_data.bits_per_offset { + 32 => { + let offsets = variable_width_data.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + // The first 4 bytes store the bits per offset, the next 4 bytes store the start + // offset of the bytes data, then offsets data, then bytes data. + let bytes_start_offset = 4 + 4 + std::mem::size_of_val(offsets) as u32; + + let output_total_bytes = + bytes_start_offset as usize + variable_width_data.data.len(); + let mut output: Vec = Vec::with_capacity(output_total_bytes); + + // Store bit_per_offset info + output.extend_from_slice(&(32_u32).to_le_bytes()); + + // store `bytes_start_offset` in the next 4 bytes of output buffer + output.extend_from_slice(&(bytes_start_offset).to_le_bytes()); + + // store offsets + output.extend_from_slice(&variable_width_data.offsets); + + // store bytes + output.extend_from_slice(&variable_width_data.data); + Ok(LanceBuffer::from(output)) + } + 64 => { + let offsets = variable_width_data.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + // The first 8 bytes store the bits per offset, the next 8 bytes store the start + // offset of the bytes data, then offsets data, then bytes data. + let bytes_start_offset = 8 + 8 + std::mem::size_of_val(offsets) as u64; + + let output_total_bytes = + bytes_start_offset as usize + variable_width_data.data.len(); + let mut output: Vec = Vec::with_capacity(output_total_bytes); + + // Store bit_per_offset info + output.extend_from_slice(&(64_u64).to_le_bytes()); + + // store `bytes_start_offset` in the next 8 bytes of output buffer + output.extend_from_slice(&(bytes_start_offset).to_le_bytes()); + + // store offsets + output.extend_from_slice(&variable_width_data.offsets); + + // store bytes + output.extend_from_slice(&variable_width_data.data); + Ok(LanceBuffer::from(output)) + } + _ => { + panic!( + "BinaryBlockEncoder does not work with {} bits per offset VariableWidth DataBlock.", + variable_width_data.bits_per_offset + ); + } + } + } + _ => { + panic!("BinaryBlockEncoder can only work with Variable Width DataBlock."); + } + } + } +} + +impl PerValueCompressor for VariableEncoder { + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)> { + let DataBlock::VariableWidth(variable) = data else { + panic!("BinaryPerValueCompressor can only work with Variable Width DataBlock."); + }; + + let encoding = ProtobufUtils21::variable( + ProtobufUtils21::flat(variable.bits_per_offset as u64, None), + None, + ); + Ok((PerValueDataBlock::Variable(variable), encoding)) + } +} + +#[derive(Debug, Default)] +pub struct VariableDecoder {} + +impl VariablePerValueDecompressor for VariableDecoder { + fn decompress(&self, data: VariableWidthBlock) -> Result { + Ok(DataBlock::VariableWidth(data)) + } +} + +#[derive(Debug, Default)] +pub struct BinaryBlockDecompressor {} + +impl BlockDecompressor for BinaryBlockDecompressor { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + // In older (not quite stable) versions we stored the bits per offset as a single byte and then the num_values + // as four bytes. However, this led to alignment problems and was wasteful since we already store the num_values + // in higher layers. + // + // In the standard scheme we use 4 bytes for the bits per offset and 4 bytes for the bytes_start_offset and we + // rely on the passed in num_values to be correct. + + // This isn't perfect but it's probably good enough and the best I think we can do. The bits per offset will + // never be more than 255 and it's little endian so the last 3 bytes will always be 0. These will be the least + // significant 3 bytes of the number of values in the old scheme. It's pretty unlikely these are all 0 (that would + // mean there are at least 16M values in a single page) so we'll use this to determine if the old scheme is used. + // + // The header fields and the offsets themselves come straight from the file. + // The structural checks below (all O(1)) reject blocks whose regions do not + // line up; the offset *values* are validated later, by the mandatory layout + // validation in `VariableWidthBlock::into_arrow`, so they are not rescanned + // here. + if data.len() < 4 { + return Err(Error::corrupt_file_named( + "variable-width block", + format!( + "block of {} bytes is too small to hold a header", + data.len() + ), + )); + } + let is_old_scheme = data[1] != 0 || data[2] != 0 || data[3] != 0; + + let ensure_header = |header_len: usize| { + if data.len() < header_len { + return Err(Error::corrupt_file_named( + "variable-width block", + format!( + "block of {} bytes is too small for a {} byte header", + data.len(), + header_len + ), + )); + } + Ok(()) + }; + let (bits_per_offset, bytes_start_offset, offset_start) = if is_old_scheme { + // Old scheme + let bits_per_offset = data[0]; + match bits_per_offset { + 32 => { + ensure_header(9)?; + debug_assert_eq!(LittleEndian::read_u32(&data[1..5]), num_values as u32); + let bytes_start_offset = LittleEndian::read_u32(&data[5..9]); + (bits_per_offset, bytes_start_offset as u64, 9_u64) + } + 64 => { + ensure_header(17)?; + debug_assert_eq!(LittleEndian::read_u64(&data[1..9]), num_values); + let bytes_start_offset = LittleEndian::read_u64(&data[9..17]); + (bits_per_offset, bytes_start_offset, 17) + } + _ => { + return Err(Error::invalid_input_source( + format!("Unsupported bits_per_offset={}", bits_per_offset).into(), + )); + } + } + } else { + // Standard scheme + let bits_per_offset = LittleEndian::read_u32(&data[0..4]) as u8; + match bits_per_offset { + 32 => { + ensure_header(8)?; + let bytes_start_offset = LittleEndian::read_u32(&data[4..8]); + (bits_per_offset, bytes_start_offset as u64, 8) + } + 64 => { + ensure_header(16)?; + let bytes_start_offset = LittleEndian::read_u64(&data[8..16]); + (bits_per_offset, bytes_start_offset, 16) + } + _ => { + return Err(Error::invalid_input_source( + format!("Unsupported bits_per_offset={}", bits_per_offset).into(), + )); + } + } + }; + + // The offsets region sits between the header and `bytes_start_offset` + // and must hold exactly `num_values + 1` offsets starting at zero. + let expected_offsets_bytes = num_values + .checked_add(1) + .and_then(|num_offsets| num_offsets.checked_mul(bits_per_offset as u64 / 8)) + .ok_or_else(|| { + Error::corrupt_file_named( + "variable-width block", + format!("offsets region size overflows for {num_values} values"), + ) + })?; + if bytes_start_offset < offset_start || bytes_start_offset > data.len() as u64 { + return Err(Error::corrupt_file_named( + "variable-width block", + format!( + "bytes start offset {} is outside the block (header: {} bytes, block: {} bytes)", + bytes_start_offset, + offset_start, + data.len() + ), + )); + } + if bytes_start_offset - offset_start != expected_offsets_bytes { + return Err(Error::corrupt_file_named( + "variable-width block", + format!( + "expected {} offset bytes for {} values but found {}", + expected_offsets_bytes, + num_values, + bytes_start_offset - offset_start + ), + )); + } + + // the next `bytes_start_offset - offset_start` stores the offsets. + let offsets = data.slice_with_length( + offset_start as usize, + (bytes_start_offset - offset_start) as usize, + ); + let first_offset = match bits_per_offset { + 32 => LittleEndian::read_u32(&offsets[0..4]) as u64, + _ => LittleEndian::read_u64(&offsets[0..8]), + }; + if first_offset != 0 { + return Err(Error::corrupt_file_named( + "variable-width block", + format!("first offset must be 0 but found {first_offset}"), + )); + } + + // the rest are the binary bytes. + let data = data.slice_with_length( + bytes_start_offset as usize, + data.len() - bytes_start_offset as usize, + ); + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data, + offsets, + bits_per_offset, + num_values, + block_info: BlockInfo::new(), + })) + } +} + +#[cfg(test)] +mod tests { + use arrow_array::{ + ArrayRef, StringArray, + builder::{LargeStringBuilder, StringBuilder}, + }; + use arrow_schema::{DataType, Field}; + + use crate::{ + buffer::LanceBuffer, + constants::{ + COMPRESSION_META_KEY, STRUCTURAL_ENCODING_FULLZIP, STRUCTURAL_ENCODING_META_KEY, + STRUCTURAL_ENCODING_MINIBLOCK, + }, + data::{BlockInfo, DataBlock, VariableWidthBlock}, + testing::check_specific_random, + }; + use rstest::rstest; + use std::{collections::HashMap, sync::Arc, vec}; + + use crate::testing::{ + FnArrayGeneratorProvider, TestCases, check_basic_random, check_round_trip_encoding_of_data, + }; + + #[test_log::test(tokio::test)] + async fn test_utf8_binary() { + let field = Field::new("", DataType::Utf8, false); + check_specific_random(field, TestCases::basic().with_structural_encodings()).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_binary( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + #[values(DataType::Utf8, DataType::Binary)] data_type: DataType, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let field = Field::new("", data_type, false).with_metadata(field_metadata); + check_basic_random(field).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_binary_fsst( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + #[values(DataType::Binary, DataType::Utf8)] data_type: DataType, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + field_metadata.insert(COMPRESSION_META_KEY.to_string(), "fsst".into()); + let field = Field::new("", data_type, true).with_metadata(field_metadata); + // TODO (https://github.com/lance-format/lance/issues/4783) + let test_cases = TestCases::default().with_structural_encodings(); + check_specific_random(field, test_cases).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_fsst_large_binary( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + #[values(DataType::LargeBinary, DataType::LargeUtf8)] data_type: DataType, + ) { + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + field_metadata.insert(COMPRESSION_META_KEY.to_string(), "fsst".into()); + let field = Field::new("", data_type, true).with_metadata(field_metadata); + check_specific_random(field, TestCases::basic().with_structural_encodings()).await; + } + + #[test_log::test(tokio::test)] + async fn test_large_binary() { + let field = Field::new("", DataType::LargeBinary, true); + check_basic_random(field).await; + } + + #[test_log::test(tokio::test)] + async fn test_large_utf8() { + let field = Field::new("", DataType::LargeUtf8, true); + check_basic_random(field).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_small_strings( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + ) { + use crate::testing::check_basic_generated; + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + let field = Field::new("", DataType::Utf8, true).with_metadata(field_metadata); + check_basic_generated( + field, + Box::new(FnArrayGeneratorProvider::new(move || { + lance_datagen::array::utf8_prefix_plus_counter("user_", /*is_large=*/ false) + })), + ) + .await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_simple_binary( + #[values(STRUCTURAL_ENCODING_MINIBLOCK, STRUCTURAL_ENCODING_FULLZIP)] + structural_encoding: &str, + #[values(DataType::Utf8, DataType::Binary)] data_type: DataType, + ) { + let string_array = StringArray::from(vec![Some("abc"), None, Some("pqr"), None, Some("m")]); + let string_array = arrow_cast::cast(&string_array, &data_type).unwrap(); + + let mut field_metadata = HashMap::new(); + field_metadata.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + structural_encoding.into(), + ); + + let test_cases = TestCases::default() + .with_range(0..2) + .with_range(0..3) + .with_range(1..3) + .with_indices(vec![0, 1, 3, 4]); + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + field_metadata, + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_sliced_utf8() { + let string_array = StringArray::from(vec![Some("abc"), Some("de"), None, Some("fgh")]); + let string_array = string_array.slice(1, 3); + + let test_cases = TestCases::default() + .with_range(0..1) + .with_range(0..2) + .with_range(1..2); + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_bigger_than_max_page_size() { + // Create an array with one single 32MiB string + let big_string = String::from_iter((0..(32 * 1024 * 1024)).map(|_| '0')); + let string_array = StringArray::from(vec![ + Some(big_string), + Some("abc".to_string()), + None, + None, + Some("xyz".to_string()), + ]); + + // Drop the max page size to 1MiB + let test_cases = TestCases::default().with_max_page_size(1024 * 1024); + + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &test_cases, + HashMap::new(), + ) + .await; + + // This is a regression testing the case where a page with X rows is split into Y parts + // where the number of parts is not evenly divisible by the number of rows. In this + // case we are splitting 90 rows into 4 parts. + let big_string = String::from_iter((0..(1000 * 1000)).map(|_| '0')); + let string_array = StringArray::from_iter_values((0..90).map(|_| big_string.clone())); + + check_round_trip_encoding_of_data( + vec![Arc::new(string_array)], + &TestCases::default(), + HashMap::new(), + ) + .await; + } + + #[test_log::test(tokio::test)] + async fn test_empty_strings() { + // Scenario 1: Some strings are empty + + let values = [Some("abc"), Some(""), None]; + // Test empty list at beginning, middle, and end + for order in [[0, 1, 2], [1, 0, 2], [2, 0, 1]] { + let mut string_builder = StringBuilder::new(); + for idx in order { + string_builder.append_option(values[idx]); + } + let string_array = Arc::new(string_builder.finish()); + let test_cases = TestCases::default() + .with_indices(vec![1]) + .with_indices(vec![0]) + .with_indices(vec![2]) + .with_indices(vec![0, 1]); + check_round_trip_encoding_of_data( + vec![string_array.clone()], + &test_cases, + HashMap::new(), + ) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()) + .await; + } + + // Scenario 2: All strings are empty + + // When encoding an array of empty strings there are no bytes to encode + // which is strange and we want to ensure we handle it + let string_array = Arc::new(StringArray::from(vec![Some(""), None, Some("")])); + + let test_cases = TestCases::default().with_range(0..2).with_indices(vec![1]); + check_round_trip_encoding_of_data(vec![string_array.clone()], &test_cases, HashMap::new()) + .await; + let test_cases = test_cases.with_batch_size(1); + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + #[ignore] // This test is quite slow in debug mode + async fn test_jumbo_string() { + // This is an overflow test. We have a list of lists where each list + // has 1Mi items. We encode 5000 of these lists and so we have over 4Gi in the + // offsets range + let mut string_builder = LargeStringBuilder::new(); + // a 1 MiB string + let giant_string = String::from_iter((0..(1024 * 1024)).map(|_| '0')); + for _ in 0..5000 { + string_builder.append_option(Some(&giant_string)); + } + let giant_array = Arc::new(string_builder.finish()) as ArrayRef; + let arrs = vec![giant_array]; + + // // We can't validate because our validation relies on concatenating all input arrays + let test_cases = TestCases::default().without_validation(); + check_round_trip_encoding_of_data(arrs, &test_cases, HashMap::new()).await; + } + + #[rstest] + #[test_log::test(tokio::test)] + async fn test_binary_dictionary_encoding( + #[values(true, false)] with_nulls: bool, + #[values(100, 500, 35000)] dict_size: u32, + ) { + let test_cases = TestCases::default().with_structural_encodings(); + let strings = (0..dict_size) + .map(|i| i.to_string()) + .collect::>(); + + let repeated_strings: Vec<_> = strings + .iter() + .cycle() + .take(70000) + .enumerate() + .map(|(i, s)| { + if with_nulls && i % 7 == 0 { + None + } else { + Some(s.clone()) + } + }) + .collect(); + let string_array = Arc::new(StringArray::from(repeated_strings)) as ArrayRef; + check_round_trip_encoding_of_data(vec![string_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_binary_encoding_verification() { + use lance_datagen::{ByteCount, RowCount}; + + let test_cases = TestCases::default() + .with_expected_encoding("variable") + .with_structural_encodings(); + + // Test both automatic selection and explicit configuration + // 1. Test automatic binary encoding selection (small strings that won't trigger FSST) + let arr_small = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_utf8(ByteCount::from(10), false)) + .into_batch_rows(RowCount::from(1000)) + .unwrap() + .column(0) + .clone(); + check_round_trip_encoding_of_data(vec![arr_small], &test_cases, HashMap::new()).await; + + // 2. Test explicit "none" compression to force binary encoding + let metadata_explicit = + HashMap::from([("lance-encoding:compression".to_string(), "none".to_string())]); + let arr_large = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_utf8(ByteCount::from(50), false)) + .into_batch_rows(RowCount::from(2000)) + .unwrap() + .column(0) + .clone(); + check_round_trip_encoding_of_data(vec![arr_large], &test_cases, metadata_explicit).await; + } + + #[test] + fn test_binary_miniblock_with_misaligned_buffer() { + use super::BinaryMiniBlockDecompressor; + use crate::buffer::LanceBuffer; + use crate::compression::MiniBlockDecompressor; + use crate::data::DataBlock; + + // Test case 1: u32 offsets + { + let decompressor = BinaryMiniBlockDecompressor { + bits_per_offset: 32, + }; + + // Create test data with u32 offsets + // BinaryMiniBlock format: all offsets followed by all string data + // Need to ensure total size is divisible by 4 for u32 + let mut test_data = Vec::new(); + + // Offsets section (3 offsets for 2 values + 1 end offset) + test_data.extend_from_slice(&12u32.to_le_bytes()); // offset to start of strings (after offsets) + test_data.extend_from_slice(&15u32.to_le_bytes()); // offset to second string + test_data.extend_from_slice(&20u32.to_le_bytes()); // offset to end + + // String data section + test_data.extend_from_slice(b"ABCXYZ"); // 6 bytes of string data + test_data.extend_from_slice(&[0, 0]); // 2 bytes padding to make total 20 bytes (divisible by 4) + + // Create a misaligned buffer by adding padding and slicing + let mut padded = Vec::with_capacity(test_data.len() + 1); + padded.push(0xFF); // Padding byte to misalign + padded.extend_from_slice(&test_data); + + let bytes = bytes::Bytes::from(padded); + let misaligned = bytes.slice(1..); // Skip first byte to create misalignment + + // Create LanceBuffer with bytes_per_value=1 to bypass alignment check + let buffer = LanceBuffer::from_bytes(misaligned, 1); + + // Verify the buffer is actually misaligned + let ptr = buffer.as_ref().as_ptr(); + assert_ne!( + ptr.align_offset(4), + 0, + "Test setup: buffer should be misaligned for u32" + ); + + // Decompress with misaligned buffer - should work with borrow_to_typed_slice + let result = decompressor.decompress(vec![buffer], 2); + assert!( + result.is_ok(), + "Decompression should succeed with misaligned buffer" + ); + + // Verify the data is correct + if let Ok(DataBlock::VariableWidth(block)) = result { + assert_eq!(block.num_values, 2); + // Data should be the strings (including padding from the original buffer) + assert_eq!(&block.data.as_ref()[..6], b"ABCXYZ"); + } else { + panic!("Expected VariableWidth block"); + } + } + + // Test case 2: u64 offsets + { + let decompressor = BinaryMiniBlockDecompressor { + bits_per_offset: 64, + }; + + // Create test data with u64 offsets + let mut test_data = Vec::new(); + + // Offsets section (3 offsets for 2 values + 1 end offset) + test_data.extend_from_slice(&24u64.to_le_bytes()); // offset to start of strings (after offsets) + test_data.extend_from_slice(&29u64.to_le_bytes()); // offset to second string + test_data.extend_from_slice(&40u64.to_le_bytes()); // offset to end (divisible by 8) + + // String data section + test_data.extend_from_slice(b"HelloWorld"); // 10 bytes of string data + test_data.extend_from_slice(&[0, 0, 0, 0, 0, 0]); // 6 bytes padding to make total 40 bytes (divisible by 8) + + // Create misaligned buffer + let mut padded = Vec::with_capacity(test_data.len() + 3); + padded.extend_from_slice(&[0xFF, 0xFF, 0xFF]); // 3 bytes padding for misalignment + padded.extend_from_slice(&test_data); + + let bytes = bytes::Bytes::from(padded); + let misaligned = bytes.slice(3..); // Skip 3 bytes + + let buffer = LanceBuffer::from_bytes(misaligned, 1); + + // Verify misalignment for u64 + let ptr = buffer.as_ref().as_ptr(); + assert_ne!( + ptr.align_offset(8), + 0, + "Test setup: buffer should be misaligned for u64" + ); + + // Decompress should succeed + let result = decompressor.decompress(vec![buffer], 2); + assert!( + result.is_ok(), + "Decompression should succeed with misaligned u64 buffer" + ); + + if let Ok(DataBlock::VariableWidth(block)) = result { + assert_eq!(block.num_values, 2); + // Data should be the strings (including padding from the original buffer) + assert_eq!(&block.data.as_ref()[..10], b"HelloWorld"); + } else { + panic!("Expected VariableWidth block"); + } + } + } + + #[test] + fn test_binary_miniblock_rejects_corrupt_offsets() { + use super::BinaryMiniBlockDecompressor; + use crate::compression::MiniBlockDecompressor; + use lance_core::Error; + + // Chunk layout mirrors the on-disk format for ["alpha", "beta", "gamma"]: + // LE u32 offsets [16, 21, 25, 30] followed by the value bytes, padded to + // a multiple of 8 bytes. + fn chunk_u32(offsets: &[u32], values: &[u8]) -> LanceBuffer { + let mut chunk = offsets + .iter() + .flat_map(|offset| offset.to_le_bytes()) + .collect::>(); + chunk.extend_from_slice(values); + chunk.resize(chunk.len().next_multiple_of(8), 0); + LanceBuffer::from(chunk) + } + + let decompressor = BinaryMiniBlockDecompressor::new(32); + + // The tail offset points past the end of the 32-byte chunk. + let err = decompressor + .decompress( + vec![chunk_u32(&[16, 21, 25, 100_000], b"alphabetagamma")], + 3, + ) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("out of bounds"), "{err}"); + + // Offsets go backwards, which would underflow the rebase subtraction. + let err = decompressor + .decompress(vec![chunk_u32(&[16, 25, 21, 30], b"alphabetagamma")], 3) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("decreases"), "{err}"); + + // The first offset points inside the offset table, which would alias + // the serialized offsets into the value bytes. + let err = decompressor + .decompress(vec![chunk_u32(&[0, 21, 25, 30], b"alphabetagamma")], 3) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("overlaps"), "{err}"); + + // The chunk stores fewer offsets than the requested value count needs. + let err = decompressor + .decompress(vec![chunk_u32(&[8, 8], &[])], 3) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("requires 4"), "{err}"); + + // The chunk size is not a multiple of the offset width. + let err = decompressor + .decompress(vec![LanceBuffer::from(vec![0u8; 10])], 1) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("multiple"), "{err}"); + + // 64-bit offsets take the same validation path. + fn chunk_u64(offsets: &[u64], values: &[u8]) -> LanceBuffer { + let mut chunk = offsets + .iter() + .flat_map(|offset| offset.to_le_bytes()) + .collect::>(); + chunk.extend_from_slice(values); + chunk.resize(chunk.len().next_multiple_of(8), 0); + LanceBuffer::from(chunk) + } + let decompressor = BinaryMiniBlockDecompressor::new(64); + let err = decompressor + .decompress( + vec![chunk_u64(&[32, 37, 41, 100_000], b"alphabetagamma")], + 3, + ) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("out of bounds"), "{err}"); + let err = decompressor + .decompress(vec![chunk_u64(&[0, 37, 41, 46], b"alphabetagamma")], 3) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("overlaps"), "{err}"); + + // A valid chunk still decodes: offsets rebase to [0, 5, 9, 14]. + let decompressor = BinaryMiniBlockDecompressor::new(32); + let block = decompressor + .decompress(vec![chunk_u32(&[16, 21, 25, 30], b"alphabetagamma")], 3) + .unwrap(); + let DataBlock::VariableWidth(block) = block else { + panic!("expected a variable-width block"); + }; + assert_eq!(block.data.as_ref(), b"alphabetagamma"); + assert_eq!( + block.offsets, + LanceBuffer::reinterpret_vec(vec![0_u32, 5, 9, 14]) + ); + } + + fn encoded_binary_block(bits_per_offset: u8) -> Vec { + use crate::compression::BlockCompressor; + + let offsets = match bits_per_offset { + 32 => LanceBuffer::reinterpret_vec(vec![0_i32, 5, 9, 14]), + 64 => LanceBuffer::reinterpret_vec(vec![0_i64, 5, 9, 14]), + _ => unreachable!(), + }; + let block = DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::copy_slice(b"alphabetagamma"), + offsets, + bits_per_offset, + num_values: 3, + block_info: BlockInfo::new(), + }); + BlockCompressor::compress(&super::VariableEncoder::default(), block) + .unwrap() + .as_ref() + .to_vec() + } + + /// The block decompressor only checks the block structure (all O(1)); bad + /// offset values inside a structurally-sound block are rejected by the + /// mandatory layout validation when the block is converted to Arrow. + #[rstest] + #[case::i32_tail_out_of_bounds(32, 3, 100_000, "out of bounds")] + #[case::i64_tail_out_of_bounds(64, 3, 15, "out of bounds")] + #[case::i32_non_monotonic(32, 2, 4, "non-monotonic")] + #[case::i64_non_monotonic(64, 2, 4, "non-monotonic")] + fn test_binary_block_bad_offsets_rejected_at_arrow_conversion( + #[case] bits_per_offset: u8, + #[case] mutated_offset_index: usize, + #[case] mutated_offset_value: u64, + #[case] expected_message: &str, + ) { + use crate::compression::BlockDecompressor; + use lance_core::Error; + + let mut encoded = encoded_binary_block(bits_per_offset); + let bytes_per_offset = (bits_per_offset / 8) as usize; + // The standard scheme header is two offset-width fields. + let mutated_offset_start = bytes_per_offset * (2 + mutated_offset_index); + encoded[mutated_offset_start..mutated_offset_start + bytes_per_offset] + .copy_from_slice(&mutated_offset_value.to_le_bytes()[..bytes_per_offset]); + + let block = super::BinaryBlockDecompressor::default() + .decompress(LanceBuffer::from(encoded), 3) + .unwrap(); + let data_type = match bits_per_offset { + 32 => DataType::Binary, + _ => DataType::LargeBinary, + }; + let err = block.into_arrow(data_type, false).unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains(expected_message), "{err}"); + } + + #[test] + fn test_binary_block_rejects_corrupt_structure() { + use crate::compression::BlockDecompressor; + use lance_core::Error; + + let decompressor = super::BinaryBlockDecompressor::default(); + + // The first offset must be zero. + let mut encoded = encoded_binary_block(32); + encoded[8..12].copy_from_slice(&5_u32.to_le_bytes()); + let err = decompressor + .decompress(LanceBuffer::from(encoded), 3) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("first offset"), "{err}"); + + // The offsets region must hold exactly num_values + 1 offsets. + let encoded = encoded_binary_block(32); + let err = decompressor + .decompress(LanceBuffer::from(encoded), 4) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("offset bytes"), "{err}"); + + // A block too small to hold its header is rejected, not a panic. + let err = decompressor + .decompress(LanceBuffer::from(vec![0_u8; 2]), 1) + .unwrap_err(); + assert!(matches!(err, Error::CorruptFile { .. }), "{err:?}"); + assert!(err.to_string().contains("too small"), "{err}"); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/bitpacking.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/bitpacking.rs new file mode 100644 index 000000000..9b7759110 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/bitpacking.rs @@ -0,0 +1,859 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Bitpacking encodings +//! +//! These encodings look for unused higher order bits and discard them. For example, if we +//! have a u32 array and all values are between 0 and 5000 then we only need 12 bits to store +//! each value. The encoding will discard the upper 20 bits and only store 12 bits. +//! +//! This is a simple encoding that works well for data that has a small range. +//! +//! In order to decode the values we need to know the bit width of the values. This can be stored +//! inline with the data (miniblock) or in the encoding description, out of line (full zip). +//! +//! The encoding is transparent because the output has a fixed width (just like the input) and +//! we can easily jump to the correct value. + +use arrow_array::types::UInt64Type; +use arrow_array::{Array, PrimitiveArray}; +use arrow_buffer::ArrowNativeType; +use lance_bitpacking::BitPacking; + +use lance_core::{Error, Result}; + +use crate::buffer::LanceBuffer; +use crate::compression::{BlockCompressor, BlockDecompressor, MiniBlockDecompressor}; +use crate::data::BlockInfo; +use crate::data::{DataBlock, FixedWidthDataBlock}; +use crate::encodings::logical::primitive::miniblock::{ + MiniBlockChunk, MiniBlockCompressed, MiniBlockCompressionContext, MiniBlockCompressor, +}; +use crate::format::pb21::CompressiveEncoding; +use crate::format::{ProtobufUtils21, pb21}; +use crate::statistics::{GetStat, Stat}; +use bytemuck::Pod; + +pub(crate) const LOG_ELEMS_PER_CHUNK: u8 = 10; +/// Number of values encoded in each inline bitpacking chunk. +pub const ELEMS_PER_CHUNK: u64 = 1 << LOG_ELEMS_PER_CHUNK; + +#[derive(Debug, Default)] +pub struct InlineBitpacking { + uncompressed_bit_width: u64, +} + +impl InlineBitpacking { + pub fn new(uncompressed_bit_width: u64) -> Self { + Self { + uncompressed_bit_width, + } + } + + pub fn from_description(description: &pb21::InlineBitpacking) -> Self { + Self { + uncompressed_bit_width: description.uncompressed_bits_per_value, + } + } + + /// The minimum number of bytes required to actually get compression + /// + /// We have to compress in blocks of 1024 values. For example, we can compress 500 2-byte (1000 bytes) + /// values into 1024 2-bit values (256 bytes) for a win but we don't want to compress 10 2-byte values + /// into 1024 2-bit values because that's not a win. + pub fn min_size_bytes(compressed_bit_width: u64) -> u64 { + (ELEMS_PER_CHUNK * compressed_bit_width).div_ceil(8) + } + + /// Bitpacks a FixedWidthDataBlock into compressed chunks of 1024 values + /// + /// Each chunk can have a different bit width + /// + /// Each chunk has the compressed bit width stored inline in the chunk itself. + fn bitpack_chunked( + data: FixedWidthDataBlock, + ) -> MiniBlockCompressed { + debug_assert!(data.num_values > 0); + let data_buffer = data.data.borrow_to_typed_slice::(); + let data_buffer = data_buffer.as_ref(); + + let bit_widths = data.expect_stat(Stat::BitWidth); + let bit_widths_array = bit_widths + .as_any() + .downcast_ref::>() + .unwrap(); + + let (packed_chunk_sizes, total_size) = bit_widths_array + .values() + .iter() + .map(|&bit_width| { + let chunk_size = ((1024 * bit_width) / data.bits_per_value) as usize; + (chunk_size, chunk_size + 1) + }) + .fold( + (Vec::with_capacity(bit_widths_array.len()), 0), + |(mut sizes, total), (size, inc)| { + sizes.push(size); + (sizes, total + inc) + }, + ); + + let mut output: Vec = Vec::with_capacity(total_size); + let mut chunks = Vec::with_capacity(bit_widths_array.len()); + + for (i, packed_chunk_size) in packed_chunk_sizes + .iter() + .enumerate() + .take(bit_widths_array.len() - 1) + { + let start_elem = i * ELEMS_PER_CHUNK as usize; + let bit_width = bit_widths_array.value(i) as usize; + output.push(T::from_usize(bit_width).unwrap()); + let output_len = output.len(); + unsafe { + output.set_len(output_len + *packed_chunk_size); + BitPacking::unchecked_pack( + bit_width, + &data_buffer[start_elem..][..ELEMS_PER_CHUNK as usize], + &mut output[output_len..][..*packed_chunk_size], + ); + } + chunks.push(MiniBlockChunk { + buffer_sizes: vec![((1 + *packed_chunk_size) * std::mem::size_of::()) as u32], + log_num_values: LOG_ELEMS_PER_CHUNK, + }); + } + + // Handle the last chunk + let last_chunk_elem_num = if data.num_values.is_multiple_of(ELEMS_PER_CHUNK) { + ELEMS_PER_CHUNK + } else { + data.num_values % ELEMS_PER_CHUNK + }; + let mut last_chunk: Vec = vec![T::from_usize(0).unwrap(); ELEMS_PER_CHUNK as usize]; + last_chunk[..last_chunk_elem_num as usize].clone_from_slice( + &data_buffer[data.num_values as usize - last_chunk_elem_num as usize..], + ); + let bit_width = bit_widths_array.value(bit_widths_array.len() - 1) as usize; + output.push(T::from_usize(bit_width).unwrap()); + let output_len = output.len(); + unsafe { + output.set_len(output_len + packed_chunk_sizes[bit_widths_array.len() - 1]); + BitPacking::unchecked_pack( + bit_width, + &last_chunk, + &mut output[output_len..][..packed_chunk_sizes[bit_widths_array.len() - 1]], + ); + } + chunks.push(MiniBlockChunk { + buffer_sizes: vec![ + ((1 + packed_chunk_sizes[bit_widths_array.len() - 1]) * std::mem::size_of::()) + as u32, + ], + log_num_values: 0, + }); + + MiniBlockCompressed { + data: vec![LanceBuffer::reinterpret_vec(output)], + chunks, + num_values: data.num_values, + } + } + + fn chunk_data(&self, data: FixedWidthDataBlock) -> (MiniBlockCompressed, CompressiveEncoding) { + assert!(data.bits_per_value.is_multiple_of(8)); + assert_eq!(data.bits_per_value, self.uncompressed_bit_width); + let bits_per_value = data.bits_per_value; + let compressed = match bits_per_value { + 8 => Self::bitpack_chunked::(data), + 16 => Self::bitpack_chunked::(data), + 32 => Self::bitpack_chunked::(data), + 64 => Self::bitpack_chunked::(data), + _ => unreachable!(), + }; + ( + compressed, + ProtobufUtils21::inline_bitpacking( + bits_per_value, + // TODO: Could potentially compress the data here + None, + ), + ) + } + + fn unchunk( + data: LanceBuffer, + num_values: u64, + ) -> Result { + // This macro decompresses a chunk(1024 values) of bitpacked values. + let uncompressed_bit_width = std::mem::size_of::() * 8; + let word_size = std::mem::size_of::(); + + if data.len() < word_size { + return Err(Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking chunk is too small for {}-byte header: {} bytes", + word_size, + data.len() + ), + )); + } + if !data.len().is_multiple_of(word_size) { + return Err(Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking chunk size must be a multiple of {} bytes, got {} bytes", + word_size, + data.len() + ), + )); + } + if num_values > ELEMS_PER_CHUNK { + return Err(Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking chunk has {} values, expected at most {}", + num_values, ELEMS_PER_CHUNK + ), + )); + } + + let chunk_words = data.borrow_to_typed_view::(); + let bit_width_value = chunk_words[0].as_usize(); + if bit_width_value > uncompressed_bit_width { + return Err(Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking width {} exceeds {}-bit values", + bit_width_value, uncompressed_bit_width + ), + )); + } + let chunk = &chunk_words[1..]; + // bit_width_value has already been verified to be <= uncompressed_bit_width + // (8/16/32/64), so bit_width_value * ELEMS_PER_CHUNK (1024) can never + // overflow usize on supported targets. Keep checked_mul as defense in depth. + let expected_num_bits = bit_width_value + .checked_mul(ELEMS_PER_CHUNK as usize) + .ok_or_else(|| { + Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking width {} overflows chunk bit count", + bit_width_value + ), + ) + })?; + let expected_num_bytes = expected_num_bits / 8; + let actual_num_bytes = std::mem::size_of_val(chunk); + if actual_num_bytes != expected_num_bytes { + return Err(Error::corrupt_file_named( + "inline_bitpacking", + format!( + "Inline bitpacking payload has {} bytes, expected {} bytes for bit width {}", + actual_num_bytes, expected_num_bytes, bit_width_value + ), + )); + } + + let mut decompressed = vec![T::default(); ELEMS_PER_CHUNK as usize]; + unsafe { + BitPacking::unchecked_unpack(bit_width_value, chunk, &mut decompressed); + } + + decompressed.truncate(num_values as usize); + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(decompressed), + bits_per_value: uncompressed_bit_width as u64, + num_values, + block_info: BlockInfo::new(), + })) + } + + /// An empty fixed-width block, used for the `num_values == 0` short-circuit in + /// both decompressor entry points so empty blocks skip chunk validation entirely. + fn empty_block(&self) -> DataBlock { + DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::empty(), + bits_per_value: self.uncompressed_bit_width, + num_values: 0, + block_info: BlockInfo::new(), + }) + } +} + +impl MiniBlockCompressor for InlineBitpacking { + fn compress( + &self, + _context: MiniBlockCompressionContext, + chunk: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match chunk { + DataBlock::FixedWidth(fixed_width) => Ok(self.chunk_data(fixed_width)), + _ => Err(Error::invalid_input_source( + format!( + "Cannot compress a data block of type {} with BitpackMiniBlockEncoder", + chunk.name() + ) + .into(), + )), + } + } +} + +impl BlockCompressor for InlineBitpacking { + fn compress(&self, data: DataBlock) -> Result { + let fixed_width = data.as_fixed_width().unwrap(); + let (chunked, _) = self.chunk_data(fixed_width); + Ok(chunked.data.into_iter().next().unwrap()) + } +} + +impl MiniBlockDecompressor for InlineBitpacking { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + assert_eq!(data.len(), 1); + let data = data.into_iter().next().unwrap(); + if num_values == 0 { + // Empty mini-blocks have no inline bit-width header to decode. + return Ok(self.empty_block()); + } + match self.uncompressed_bit_width { + 8 => Self::unchunk::(data, num_values), + 16 => Self::unchunk::(data, num_values), + 32 => Self::unchunk::(data, num_values), + 64 => Self::unchunk::(data, num_values), + _ => unimplemented!("Bitpacking word size must be 8, 16, 32, or 64"), + } + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + num_values + .checked_mul(self.uncompressed_bit_width) + .map(|bits| bits.div_ceil(8)) + } +} + +impl BlockDecompressor for InlineBitpacking { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + if num_values == 0 { + // Empty blocks carry no inline bit-width header to decode; avoid + // spurious "too small for header" corrupt-file errors and mirror + // the MiniBlockDecompressor path. See #7794. + return Ok(self.empty_block()); + } + match self.uncompressed_bit_width { + 8 => Self::unchunk::(data, num_values), + 16 => Self::unchunk::(data, num_values), + 32 => Self::unchunk::(data, num_values), + 64 => Self::unchunk::(data, num_values), + _ => unimplemented!("Bitpacking word size must be 8, 16, 32, or 64"), + } + } +} + +/// Bitpacks a FixedWidthDataBlock with a given bit width. +/// +/// Each chunk of 1024 values is packed with a constant bit width. For the tail we compare the +/// cost of padding and packing against storing the raw values: if padding yields a smaller +/// representation we pack; otherwise we append the raw tail. +fn bitpack_out_of_line( + data: FixedWidthDataBlock, + compressed_bits_per_value: usize, +) -> LanceBuffer { + let data_buffer = data.data.borrow_to_typed_slice::(); + let data_buffer = data_buffer.as_ref(); + + let num_chunks = data_buffer.len().div_ceil(ELEMS_PER_CHUNK as usize); + let last_chunk_is_runt = data_buffer.len() % ELEMS_PER_CHUNK as usize != 0; + let words_per_chunk = (ELEMS_PER_CHUNK as usize * compressed_bits_per_value) + .div_ceil(data.bits_per_value as usize); + #[allow(clippy::uninit_vec)] + let mut output: Vec = Vec::with_capacity(num_chunks * words_per_chunk); + #[allow(clippy::uninit_vec)] + unsafe { + output.set_len(num_chunks * words_per_chunk); + } + + let num_whole_chunks = if last_chunk_is_runt { + num_chunks - 1 + } else { + num_chunks + }; + + // Simple case for complete chunks + for i in 0..num_whole_chunks { + let input_start = i * ELEMS_PER_CHUNK as usize; + let input_end = input_start + ELEMS_PER_CHUNK as usize; + let output_start = i * words_per_chunk; + let output_end = output_start + words_per_chunk; + unsafe { + BitPacking::unchecked_pack( + compressed_bits_per_value, + &data_buffer[input_start..input_end], + &mut output[output_start..output_end], + ); + } + } + + if !last_chunk_is_runt { + return LanceBuffer::reinterpret_vec(output); + } + + let last_chunk_start = num_whole_chunks * ELEMS_PER_CHUNK as usize; + // Safety: output ensures to have those values. + unsafe { + output.set_len(num_whole_chunks * words_per_chunk); + } + let remaining_items = data_buffer.len() - last_chunk_start; + + let uncompressed_bits = data.bits_per_value as usize; + let tail_bit_savings = uncompressed_bits.saturating_sub(compressed_bits_per_value); + let padding_cost = compressed_bits_per_value * (ELEMS_PER_CHUNK as usize - remaining_items); + let tail_pack_savings = tail_bit_savings.saturating_mul(remaining_items); + debug_assert!(remaining_items > 0, "remaining_items must be non-zero"); + debug_assert!(tail_bit_savings > 0, "tail_bit_savings must be non-zero"); + + if padding_cost < tail_pack_savings { + // Padding buys us more than it costs: pad to 1024 values and pack them as a normal chunk. + let mut last_chunk: Vec = vec![T::from_usize(0).unwrap(); ELEMS_PER_CHUNK as usize]; + last_chunk[..remaining_items].copy_from_slice(&data_buffer[last_chunk_start..]); + let start = output.len(); + unsafe { + // Capacity reserves a full chunk for each block; extend the visible length and fill it immediately. + output.set_len(start + words_per_chunk); + BitPacking::unchecked_pack( + compressed_bits_per_value, + &last_chunk, + &mut output[start..start + words_per_chunk], + ); + } + } else { + // Padding would waste space; append tail values as-is. + output.extend_from_slice(&data_buffer[last_chunk_start..]); + } + + LanceBuffer::reinterpret_vec(output) +} + +/// Unpacks a FixedWidthDataBlock that has been bitpacked with a constant bit width. +/// +/// The compressed bit width is provided while the uncompressed width comes from `T`. +/// Depending on the encoding decision the final chunk may be fully packed (with padding) +/// or stored as raw tail values. We infer the layout from the buffer length. +fn unpack_out_of_line( + data: FixedWidthDataBlock, + num_values: usize, + compressed_bits_per_value: usize, +) -> FixedWidthDataBlock { + let words_per_chunk = (ELEMS_PER_CHUNK as usize * compressed_bits_per_value) + .div_ceil(data.bits_per_value as usize); + let compressed_words = data.data.borrow_to_typed_slice::(); + + let num_whole_chunks = num_values / ELEMS_PER_CHUNK as usize; + let tail_values = num_values % ELEMS_PER_CHUNK as usize; + let expected_full_words = num_whole_chunks * words_per_chunk; + let expected_new_len = expected_full_words + tail_values; + let tail_is_raw = tail_values > 0 && compressed_words.len() == expected_new_len; + + let extra_tail_capacity = ELEMS_PER_CHUNK as usize; + #[allow(clippy::uninit_vec)] + let mut decompressed: Vec = + Vec::with_capacity(num_values.saturating_add(extra_tail_capacity)); + let chunk_value_len = num_whole_chunks * ELEMS_PER_CHUNK as usize; + unsafe { + decompressed.set_len(chunk_value_len); + } + + for chunk_idx in 0..num_whole_chunks { + let input_start = chunk_idx * words_per_chunk; + let input_end = input_start + words_per_chunk; + let output_start = chunk_idx * ELEMS_PER_CHUNK as usize; + let output_end = output_start + ELEMS_PER_CHUNK as usize; + unsafe { + BitPacking::unchecked_unpack( + compressed_bits_per_value, + &compressed_words[input_start..input_end], + &mut decompressed[output_start..output_end], + ); + } + } + + if tail_values > 0 { + // The tail might be padded and bit packed or it might be appended raw. We infer the + // layout from the buffer length to decode appropriately. + if tail_is_raw { + let tail_start = expected_full_words; + decompressed.extend_from_slice(&compressed_words[tail_start..tail_start + tail_values]); + } else { + let tail_start = expected_full_words; + let output_start = decompressed.len(); + unsafe { + decompressed.set_len(output_start + ELEMS_PER_CHUNK as usize); + } + unsafe { + BitPacking::unchecked_unpack( + compressed_bits_per_value, + &compressed_words[tail_start..tail_start + words_per_chunk], + &mut decompressed[output_start..output_start + ELEMS_PER_CHUNK as usize], + ); + } + decompressed.truncate(output_start + tail_values); + } + } + + debug_assert_eq!(decompressed.len(), num_values); + + FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(decompressed), + bits_per_value: data.bits_per_value, + num_values: num_values as u64, + block_info: BlockInfo::new(), + } +} + +/// A transparent compressor that bit packs data +/// +/// In order for the encoding to be transparent we must have a fixed bit width +/// across the entire array. Chunking within the buffer is not supported. This +/// means that we will be slightly less efficient than something like the mini-block +/// approach. +/// +/// This was an interesting experiment but it can't be used as a per-value compressor +/// at the moment. The resulting data IS transparent but it's not quite so simple. We +/// compress in blocks of 1024 and each block has a fixed size but also has some padding. +/// +/// We do use this as a block compressor currently. +/// +/// In other words, if we try the simple math to access the item at index `i` we will be +/// out of luck because `bits_per_value * i` is not the location. What we need is something +/// like: +/// +/// ```ignore +/// let chunk_idx = i / 1024; +/// let chunk_offset = i % 1024; +/// bits_per_chunk * chunk_idx + bits_per_value * chunk_offset +/// ``` +/// +/// However, this logic isn't expressible with the per-value traits we have today. We can +/// enhance these traits should we need to support it at some point in the future. +#[derive(Debug)] +pub struct OutOfLineBitpacking { + compressed_bit_width: u64, + uncompressed_bit_width: u64, +} + +impl OutOfLineBitpacking { + pub fn new(compressed_bit_width: u64, uncompressed_bit_width: u64) -> Self { + Self { + compressed_bit_width, + uncompressed_bit_width, + } + } +} + +impl BlockCompressor for OutOfLineBitpacking { + fn compress(&self, data: DataBlock) -> Result { + let fixed_width = data.as_fixed_width().unwrap(); + let compressed = match fixed_width.bits_per_value { + 8 => bitpack_out_of_line::(fixed_width, self.compressed_bit_width as usize), + 16 => bitpack_out_of_line::(fixed_width, self.compressed_bit_width as usize), + 32 => bitpack_out_of_line::(fixed_width, self.compressed_bit_width as usize), + 64 => bitpack_out_of_line::(fixed_width, self.compressed_bit_width as usize), + _ => panic!("Bitpacking word size must be 8,16,32,64"), + }; + Ok(compressed) + } +} + +impl BlockDecompressor for OutOfLineBitpacking { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + let word_size = match self.uncompressed_bit_width { + 8 => std::mem::size_of::(), + 16 => std::mem::size_of::(), + 32 => std::mem::size_of::(), + 64 => std::mem::size_of::(), + _ => panic!("Bitpacking word size must be 8,16,32,64"), + }; + debug_assert_eq!(data.len() % word_size, 0); + let total_words = (data.len() / word_size) as u64; + let block = FixedWidthDataBlock { + data, + bits_per_value: self.uncompressed_bit_width, + num_values: total_words, + block_info: BlockInfo::new(), + }; + + let unpacked = match self.uncompressed_bit_width { + 8 => unpack_out_of_line::( + block, + num_values as usize, + self.compressed_bit_width as usize, + ), + 16 => unpack_out_of_line::( + block, + num_values as usize, + self.compressed_bit_width as usize, + ), + 32 => unpack_out_of_line::( + block, + num_values as usize, + self.compressed_bit_width as usize, + ), + 64 => unpack_out_of_line::( + block, + num_values as usize, + self.compressed_bit_width as usize, + ), + _ => unreachable!(), + }; + Ok(DataBlock::FixedWidth(unpacked)) + } +} + +#[cfg(test)] +mod test { + use std::{collections::HashMap, sync::Arc}; + + use arrow_array::{Array, Int8Array, Int64Array}; + use arrow_buffer::ArrowNativeType; + use arrow_schema::DataType; + use bytemuck::Pod; + use lance_bitpacking::BitPacking; + use rstest::rstest; + + use super::{ELEMS_PER_CHUNK, InlineBitpacking, bitpack_out_of_line, unpack_out_of_line}; + use crate::{ + buffer::LanceBuffer, + compression::{BlockDecompressor, MiniBlockDecompressor}, + data::{BlockInfo, DataBlock, FixedWidthDataBlock}, + testing::{TestCases, check_round_trip_encoding_of_data}, + }; + + #[rstest] + #[case::u8(8)] + #[case::u16(16)] + #[case::u32(32)] + #[case::u64(64)] + fn test_inline_bitpacking_decompress_empty_miniblock(#[case] bit_width: u64) { + let decompressor = InlineBitpacking::new(bit_width); + let decompressed = + MiniBlockDecompressor::decompress(&decompressor, vec![LanceBuffer::empty()], 0) + .unwrap(); + + let DataBlock::FixedWidth(block) = decompressed else { + panic!("Expected FixedWidth block"); + }; + assert_eq!(block.bits_per_value, bit_width); + assert_eq!(block.num_values, 0); + assert_eq!(block.data.len(), 0); + } + + // Regression test for #7794: the block-level decompressor must short-circuit + // on num_values == 0 the same way the mini-block decompressor does, instead + // of reporting a spurious "too small for header" corrupt-file error. + #[rstest] + #[case::u8(8)] + #[case::u16(16)] + #[case::u32(32)] + #[case::u64(64)] + fn test_inline_bitpacking_decompress_empty_block(#[case] bit_width: u64) { + let decompressor = InlineBitpacking::new(bit_width); + let decompressed = + BlockDecompressor::decompress(&decompressor, LanceBuffer::empty(), 0).unwrap(); + + let DataBlock::FixedWidth(block) = decompressed else { + panic!("Expected FixedWidth block"); + }; + assert_eq!(block.bits_per_value, bit_width); + assert_eq!(block.num_values, 0); + assert_eq!(block.data.len(), 0); + } + + fn roundtrip_unchunk(values: &[T], bit_width: usize) + where + T: ArrowNativeType + BitPacking + Pod, + { + assert!(values.len() <= ELEMS_PER_CHUNK as usize); + let num_values = values.len() as u64; + + let mut padded = vec![T::from_usize(0).unwrap(); ELEMS_PER_CHUNK as usize]; + padded[..values.len()].copy_from_slice(values); + + let packed_words = ELEMS_PER_CHUNK as usize * bit_width / (std::mem::size_of::() * 8); + let mut chunk: Vec = Vec::with_capacity(1 + packed_words); + chunk.push(T::from_usize(bit_width).unwrap()); + let out_len = chunk.len(); + chunk.resize(out_len + packed_words, T::from_usize(0).unwrap()); + unsafe { + BitPacking::unchecked_pack(bit_width, &padded, &mut chunk[out_len..]); + } + + let data = LanceBuffer::reinterpret_vec(chunk); + let decoded = InlineBitpacking::unchunk::(data, num_values).unwrap(); + let DataBlock::FixedWidth(fixed) = decoded else { + panic!("expected FixedWidth DataBlock"); + }; + let decoded_values = fixed.data.borrow_to_typed_view::(); + assert_eq!(decoded_values.as_ref(), values); + } + + fn assert_corrupt_unchunk(data: LanceBuffer, num_values: u64, expected_message: &str) + where + T: ArrowNativeType + BitPacking + Pod, + { + let err = InlineBitpacking::unchunk::(data, num_values).unwrap_err(); + assert!(matches!(err, lance_core::Error::CorruptFile { .. })); + let err = err.to_string(); + assert!( + err.contains(expected_message), + "expected error containing {expected_message:?}, got {err:?}" + ); + } + + #[test] + fn unchunk_u32_bw12_tail() { + let values: Vec = (0..500).map(|i| ((i * 7) % (1 << 12)) as u32).collect(); + roundtrip_unchunk(&values, 12); + } + + #[test] + fn unchunk_u64_bw23_full() { + let values: Vec = (0..1024).map(|i| ((i * 3) % (1 << 23)) as u64).collect(); + roundtrip_unchunk(&values, 23); + } + + #[rstest] + #[case::too_small_header(LanceBuffer::from(vec![1, 2, 3]), 1, "too small")] + #[case::misaligned_chunk_size(LanceBuffer::from(vec![0, 0, 0, 0, 0]), 1, "multiple")] + #[case::too_many_values( + LanceBuffer::reinterpret_vec(vec![0_u32]), + ELEMS_PER_CHUNK + 1, + "expected at most" + )] + #[case::payload_size_mismatch(LanceBuffer::reinterpret_vec(vec![12_u32]), 1, "payload")] + #[case::invalid_bit_width(LanceBuffer::reinterpret_vec(vec![33_u32]), 1, "exceeds")] + fn unchunk_rejects( + #[case] data: LanceBuffer, + #[case] num_values: u64, + #[case] expected_message: &str, + ) { + assert_corrupt_unchunk::(data, num_values, expected_message); + } + + #[test_log::test(tokio::test)] + async fn test_miniblock_bitpack() { + let test_cases = TestCases::default().with_structural_encodings(); + + let arrays = vec![ + Arc::new(Int8Array::from(vec![100; 1024])) as Arc, + Arc::new(Int8Array::from(vec![1; 1024])) as Arc, + Arc::new(Int8Array::from(vec![16; 1024])) as Arc, + Arc::new(Int8Array::from(vec![-1; 1024])) as Arc, + Arc::new(Int8Array::from(vec![5; 1])) as Arc, + ]; + check_round_trip_encoding_of_data(arrays, &test_cases, HashMap::new()).await; + + for data_type in [DataType::Int16, DataType::Int32, DataType::Int64] { + let int64_arrays = vec![ + Int64Array::from(vec![3; 1024]), + Int64Array::from(vec![8; 1024]), + Int64Array::from(vec![16; 1024]), + Int64Array::from(vec![100; 1024]), + Int64Array::from(vec![512; 1024]), + Int64Array::from(vec![1000; 1024]), + Int64Array::from(vec![2000; 1024]), + Int64Array::from(vec![-1; 10]), + ]; + + let mut arrays = vec![]; + for int64_array in int64_arrays { + arrays.push(arrow_cast::cast(&int64_array, &data_type).unwrap()); + } + + check_round_trip_encoding_of_data(arrays, &test_cases, HashMap::new()).await; + } + } + + #[test_log::test(tokio::test)] + async fn test_bitpack_encoding_verification() { + use arrow_array::Int32Array; + + // Test bitpacking encoding verification with varied small values that should trigger bitpacking + let test_cases = TestCases::default() + .with_expected_encoding("inline_bitpacking") + .with_structural_encodings(); + + // Generate data with varied small values to avoid RLE + // Mix different values but keep them small to trigger bitpacking + let mut values = Vec::new(); + for i in 0..2048 { + values.push(i % 16); // Values 0-15, varied enough to avoid RLE + } + + let arrays = vec![Arc::new(Int32Array::from(values)) as Arc]; + + // Explicitly disable BSS to ensure bitpacking is tested + let mut metadata = HashMap::new(); + metadata.insert("lance-encoding:bss".to_string(), "off".to_string()); + + check_round_trip_encoding_of_data(arrays, &test_cases, metadata.clone()).await; + } + + #[test_log::test(tokio::test)] + async fn test_miniblock_bitpack_zero_chunk_selection() { + use arrow_array::Int32Array; + + let test_cases = TestCases::default() + .with_expected_encoding("inline_bitpacking") + .with_structural_encodings(); + + // Build 2048 values: first 1024 all zeros (bit_width=0), + // next 1024 small varied values to avoid RLE and trigger bitpacking. + let mut vals = vec![0i32; 1024]; + for i in 0..1024 { + vals.push(i % 16); + } + + let arrays = vec![Arc::new(Int32Array::from(vals)) as Arc]; + + // Disable BSS and RLE to prefer bitpacking in selection + let mut metadata = HashMap::new(); + metadata.insert("lance-encoding:bss".to_string(), "off".to_string()); + metadata.insert("lance-encoding:rle-threshold".to_string(), "0".to_string()); + + check_round_trip_encoding_of_data(arrays, &test_cases, metadata).await; + } + + #[test] + fn test_out_of_line_bitpack_raw_tail_roundtrip() { + let bit_width = 8usize; + let word_bits = std::mem::size_of::() as u64 * 8; + let values: Vec = (0..1025).map(|i| (i % 200) as u32).collect(); + let input = FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(values.clone()), + bits_per_value: word_bits, + num_values: values.len() as u64, + block_info: BlockInfo::new(), + }; + + let compressed = bitpack_out_of_line::(input, bit_width); + let compressed_words = compressed.borrow_to_typed_slice::().to_vec(); + let words_per_chunk = (ELEMS_PER_CHUNK as usize * bit_width).div_ceil(word_bits as usize); + assert_eq!( + compressed_words.len(), + words_per_chunk + (values.len() - ELEMS_PER_CHUNK as usize), + ); + + let compressed_block = FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(compressed_words.clone()), + bits_per_value: word_bits, + num_values: compressed_words.len() as u64, + block_info: BlockInfo::new(), + }; + + let decoded = unpack_out_of_line::(compressed_block, values.len(), bit_width); + let decoded_values = decoded.data.borrow_to_typed_slice::(); + assert_eq!(decoded_values.as_ref(), values.as_slice()); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/block.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/block.rs new file mode 100644 index 000000000..188af50c8 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/block.rs @@ -0,0 +1,842 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Encodings based on traditional block compression schemes +//! +//! Traditional compressors take in a buffer and return a smaller buffer. All encoding +//! description is shoved into the compressed buffer and the entire buffer is needed to +//! decompress any of the data. +//! +//! These encodings are not transparent, which limits our ability to use them. In addition +//! they are often quite expensive in CPU terms. +//! +//! However, they are effective and useful for some cases. For example, when working with large +//! variable length values (e.g. source code files) they can be very effective. +//! +//! The module introduces the `[BufferCompressor]` trait which describes the interface for a +//! traditional block compressor. It is implemented for the most common compression schemes +//! (zstd, lz4, etc). +//! +//! There is not yet a mini-block variant of this compressor (but could easily be one) and the +//! full zip variant works by applying compression on a per-value basis (which allows it to be +//! transparent). + +use arrow_buffer::ArrowNativeType; +use lance_core::{Error, Result}; + +use std::str::FromStr; + +use crate::compression::{BlockCompressor, BlockDecompressor}; +use crate::encodings::physical::binary::{BinaryBlockDecompressor, VariableEncoder}; +use crate::format::{ + ProtobufUtils21, + pb21::{self, CompressiveEncoding}, +}; +use crate::{ + buffer::LanceBuffer, + compression::VariablePerValueDecompressor, + data::{BlockInfo, DataBlock, VariableWidthBlock}, + encodings::logical::primitive::fullzip::{PerValueCompressor, PerValueDataBlock}, +}; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct CompressionConfig { + pub(crate) scheme: CompressionScheme, + pub(crate) level: Option, +} + +impl CompressionConfig { + /// Create a compression configuration for an encoding mechanism. + pub fn new(scheme: CompressionScheme, level: Option) -> Self { + Self { scheme, level } + } + + /// Return the selected compression scheme. + pub fn scheme(&self) -> CompressionScheme { + self.scheme + } + + /// Return the optional compression level. + pub fn level(&self) -> Option { + self.level + } +} + +impl Default for CompressionConfig { + fn default() -> Self { + Self { + scheme: CompressionScheme::Lz4, + level: Some(0), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum CompressionScheme { + None, + Fsst, + Zstd, + Lz4, +} + +impl TryFrom for pb21::CompressionScheme { + type Error = Error; + + fn try_from(scheme: CompressionScheme) -> Result { + match scheme { + CompressionScheme::Lz4 => Ok(Self::CompressionAlgorithmLz4), + CompressionScheme::Zstd => Ok(Self::CompressionAlgorithmZstd), + _ => Err(Error::invalid_input(format!( + "Unsupported compression scheme: {:?}", + scheme + ))), + } + } +} + +impl TryFrom for CompressionScheme { + type Error = Error; + + fn try_from(scheme: pb21::CompressionScheme) -> Result { + match scheme { + pb21::CompressionScheme::CompressionAlgorithmLz4 => Ok(Self::Lz4), + pb21::CompressionScheme::CompressionAlgorithmZstd => Ok(Self::Zstd), + _ => Err(Error::invalid_input(format!( + "Unsupported compression scheme: {:?}", + scheme + ))), + } + } +} + +impl std::fmt::Display for CompressionScheme { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let scheme_str = match self { + Self::Fsst => "fsst", + Self::Zstd => "zstd", + Self::None => "none", + Self::Lz4 => "lz4", + }; + write!(f, "{}", scheme_str) + } +} + +impl FromStr for CompressionScheme { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "none" => Ok(Self::None), + "fsst" => Ok(Self::Fsst), + "zstd" => Ok(Self::Zstd), + "lz4" => Ok(Self::Lz4), + _ => Err(Error::invalid_input(format!( + "Unknown compression scheme: {}", + s + ))), + } + } +} + +pub trait BufferCompressor: std::fmt::Debug + Send + Sync { + fn compress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()>; + fn decompress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()>; + fn config(&self) -> CompressionConfig; +} + +#[cfg(feature = "zstd")] +mod zstd { + use std::io::{Cursor, Write}; + use std::sync::{Mutex, OnceLock}; + + use super::*; + + use ::zstd::bulk::{Compressor, decompress_to_buffer}; + use ::zstd::stream::copy_decode; + + /// A zstd buffer compressor that lazily creates and reuses compression contexts. + /// + /// The compression context is cached to enable reuse across chunks within a + /// page. It is lazily initialized to prevent it from getting initialized on + /// decode-only codepaths. + /// + /// Reuse is not implemented for decompression, only for compression: + /// * The single-threaded benefit of reuse was negligible when measured. + /// * Decompressors can get shared across threads, leading to mutex + /// contention if the same strategy is used as for compression here. This + /// should be mitigable with pooling but we can skip the complexity until a + /// need is demonstrated. The multithreaded decode benchmark effectively + /// demonstrates this scenario. + pub struct ZstdBufferCompressor { + compression_level: i32, + compressor: OnceLock>, String>>, + } + + impl std::fmt::Debug for ZstdBufferCompressor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ZstdBufferCompressor") + .field("compression_level", &self.compression_level) + .finish() + } + } + + impl ZstdBufferCompressor { + pub fn new(compression_level: i32) -> Self { + Self { + compression_level, + compressor: OnceLock::new(), + } + } + + fn get_compressor(&self) -> Result<&Mutex>> { + self.compressor + .get_or_init(|| { + Compressor::new(self.compression_level) + .map(Mutex::new) + .map_err(|e| e.to_string()) + }) + .as_ref() + .map_err(|e| Error::internal(format!("Failed to create zstd compressor: {}", e))) + } + + // https://datatracker.ietf.org/doc/html/rfc8878 + fn is_raw_stream_format(&self, input_buf: &[u8]) -> bool { + if input_buf.len() < 8 { + return true; // can't be length prefixed format if less than 8 bytes + } + // read the first 4 bytes as the magic number + let mut magic_buf = [0u8; 4]; + magic_buf.copy_from_slice(&input_buf[..4]); + let magic = u32::from_le_bytes(magic_buf); + + // see RFC 8878, section 3.1.1. Zstandard Frames, which defines the magic number + const ZSTD_MAGIC_NUMBER: u32 = 0xFD2FB528; + if magic == ZSTD_MAGIC_NUMBER { + // the compressed buffer starts like a Zstd frame. + // Per RFC 8878, the reserved bit (with Bit Number 3, the 4th bit) in the FHD (frame header descriptor) MUST be 0 + // see section 3.1.1.1.1. 'Frame_Header_Descriptor' and section 3.1.1.1.1.4. 'Reserved Bit' for details + const FHD_BYTE_INDEX: usize = 4; + let fhd_byte = input_buf[FHD_BYTE_INDEX]; + const FHD_RESERVED_BIT_MASK: u8 = 0b0001_0000; + let reserved_bit = fhd_byte & FHD_RESERVED_BIT_MASK; + + if reserved_bit != 0 { + // this bit is 1. This is NOT a valid zstd frame. + // therefore, it must be length prefixed format where the length coincidentally + // started with the magic number + false + } else { + // the reserved bit is 0. This is consistent with a valid Zstd frame. + // treat it as raw stream format + true + } + } else { + // doesn't start with the magic number, so it can't be the raw stream format + false + } + } + + fn decompress_length_prefixed_zstd( + &self, + input_buf: &[u8], + output_buf: &mut Vec, + ) -> Result<()> { + const LENGTH_PREFIX_SIZE: usize = 8; + let mut len_buf = [0u8; LENGTH_PREFIX_SIZE]; + len_buf.copy_from_slice(&input_buf[..LENGTH_PREFIX_SIZE]); + + let uncompressed_len = u64::from_le_bytes(len_buf) as usize; + + let start = output_buf.len(); + output_buf.resize(start + uncompressed_len, 0); + + let compressed_data = &input_buf[LENGTH_PREFIX_SIZE..]; + decompress_to_buffer(compressed_data, &mut output_buf[start..])?; + Ok(()) + } + } + + impl BufferCompressor for ZstdBufferCompressor { + fn compress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + output_buf.write_all(&(input_buf.len() as u64).to_le_bytes())?; + + let max_compressed_size = ::zstd::zstd_safe::compress_bound(input_buf.len()); + let start_pos = output_buf.len(); + output_buf.resize(start_pos + max_compressed_size, 0); + + let compressed_size = self + .get_compressor()? + .lock() + .unwrap() + .compress_to_buffer(input_buf, &mut output_buf[start_pos..]) + .map_err(|e| Error::internal(format!("Zstd compression error: {}", e)))?; + + output_buf.truncate(start_pos + compressed_size); + Ok(()) + } + + fn decompress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + if input_buf.is_empty() { + return Ok(()); + } + + let is_raw_stream_format = self.is_raw_stream_format(input_buf); + if is_raw_stream_format { + copy_decode(Cursor::new(input_buf), output_buf)?; + } else { + self.decompress_length_prefixed_zstd(input_buf, output_buf)?; + } + + Ok(()) + } + + fn config(&self) -> CompressionConfig { + CompressionConfig { + scheme: CompressionScheme::Zstd, + level: Some(self.compression_level), + } + } + } +} + +#[cfg(feature = "lz4")] +mod lz4 { + use super::*; + + #[derive(Debug, Default)] + pub struct Lz4BufferCompressor {} + + impl BufferCompressor for Lz4BufferCompressor { + fn compress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + // Remember the starting position + let start_pos = output_buf.len(); + + // LZ4 needs space for the compressed data + let max_size = ::lz4::block::compress_bound(input_buf.len())?; + // Resize to ensure we have enough space (including 4 bytes for size header) + output_buf.resize(start_pos + max_size + 4, 0); + + let compressed_size = ::lz4::block::compress_to_buffer( + input_buf, + None, + true, + &mut output_buf[start_pos..], + ) + .map_err(|err| Error::internal(format!("LZ4 compression error: {}", err)))?; + + // Truncate to actual size + output_buf.truncate(start_pos + compressed_size); + Ok(()) + } + + fn decompress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + // When prepend_size is true, LZ4 stores the uncompressed size in the first 4 bytes + // We can read this to know exactly how much space we need + if input_buf.len() < 4 { + return Err(Error::internal("LZ4 compressed data too short".to_string())); + } + + // Read the uncompressed size from the first 4 bytes (little-endian) + let uncompressed_size = + u32::from_le_bytes([input_buf[0], input_buf[1], input_buf[2], input_buf[3]]) + as usize; + + // Remember the starting position + let start_pos = output_buf.len(); + + // Resize to ensure we have the exact space needed + output_buf.resize(start_pos + uncompressed_size, 0); + + // Now decompress directly into the buffer slice + let decompressed_size = + ::lz4::block::decompress_to_buffer(input_buf, None, &mut output_buf[start_pos..]) + .map_err(|err| Error::internal(format!("LZ4 decompression error: {}", err)))?; + + // Truncate to actual decompressed size (should be same as uncompressed_size) + output_buf.truncate(start_pos + decompressed_size); + + Ok(()) + } + + fn config(&self) -> CompressionConfig { + CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + } + } + } +} + +#[derive(Debug, Default)] +pub struct NoopBufferCompressor {} + +impl BufferCompressor for NoopBufferCompressor { + fn compress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + output_buf.extend_from_slice(input_buf); + Ok(()) + } + + fn decompress(&self, input_buf: &[u8], output_buf: &mut Vec) -> Result<()> { + output_buf.extend_from_slice(input_buf); + Ok(()) + } + + fn config(&self) -> CompressionConfig { + CompressionConfig { + scheme: CompressionScheme::None, + level: None, + } + } +} + +pub struct GeneralBufferCompressor {} + +impl GeneralBufferCompressor { + pub fn get_compressor( + compression_config: CompressionConfig, + ) -> Result> { + match compression_config.scheme { + // FSST has its own compression path and isn't implemented as a generic buffer compressor + CompressionScheme::Fsst => Err(Error::invalid_input_source( + "fsst is not usable as a general buffer compressor".into(), + )), + CompressionScheme::Zstd => { + #[cfg(feature = "zstd")] + { + Ok(Box::new(zstd::ZstdBufferCompressor::new( + compression_config.level.unwrap_or(0), + ))) + } + #[cfg(not(feature = "zstd"))] + { + Err(Error::invalid_input_source( + "package was not built with zstd support".into(), + )) + } + } + CompressionScheme::Lz4 => { + #[cfg(feature = "lz4")] + { + Ok(Box::new(lz4::Lz4BufferCompressor::default())) + } + #[cfg(not(feature = "lz4"))] + { + Err(Error::invalid_input_source( + "package was not built with lz4 support".into(), + )) + } + } + CompressionScheme::None => Ok(Box::new(NoopBufferCompressor {})), + } + } +} + +/// A block decompressor that first applies general-purpose compression (LZ4/Zstd) +/// before delegating to an inner block decompressor. +#[derive(Debug)] +pub struct GeneralBlockDecompressor { + inner: Box, + compressor: Box, +} + +impl GeneralBlockDecompressor { + pub fn try_new( + inner: Box, + compression: CompressionConfig, + ) -> Result { + let compressor = GeneralBufferCompressor::get_compressor(compression)?; + Ok(Self { inner, compressor }) + } +} + +impl BlockDecompressor for GeneralBlockDecompressor { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + let mut decompressed = Vec::new(); + self.compressor.decompress(&data, &mut decompressed)?; + self.inner + .decompress(LanceBuffer::from(decompressed), num_values) + } +} + +// An encoder which uses generic compression, such as zstd/lz4 to encode buffers +#[derive(Debug)] +pub struct CompressedBufferEncoder { + pub(crate) compressor: Box, +} + +impl Default for CompressedBufferEncoder { + fn default() -> Self { + // Pick zstd if available, otherwise lz4, otherwise none + #[cfg(feature = "zstd")] + let (scheme, level) = (CompressionScheme::Zstd, Some(0)); + #[cfg(all(feature = "lz4", not(feature = "zstd")))] + let (scheme, level) = (CompressionScheme::Lz4, None); + #[cfg(not(any(feature = "zstd", feature = "lz4")))] + let (scheme, level) = (CompressionScheme::None, None); + + let compressor = + GeneralBufferCompressor::get_compressor(CompressionConfig { scheme, level }).unwrap(); + Self { compressor } + } +} + +impl CompressedBufferEncoder { + pub fn try_new(compression_config: CompressionConfig) -> Result { + let compressor = GeneralBufferCompressor::get_compressor(compression_config)?; + Ok(Self { compressor }) + } + + pub fn from_scheme(scheme: pb21::CompressionScheme) -> Result { + let scheme = CompressionScheme::try_from(scheme)?; + Ok(Self { + compressor: GeneralBufferCompressor::get_compressor(CompressionConfig { + scheme, + level: Some(0), + })?, + }) + } +} + +impl CompressedBufferEncoder { + pub fn per_value_compress( + &self, + data: &[u8], + offsets: &[T], + compressed: &mut Vec, + ) -> Result { + let mut new_offsets: Vec = Vec::with_capacity(offsets.len()); + new_offsets.push(T::from_usize(0).unwrap()); + + for off in offsets.windows(2) { + let start = off[0].as_usize(); + let end = off[1].as_usize(); + self.compressor.compress(&data[start..end], compressed)?; + new_offsets.push(T::from_usize(compressed.len()).unwrap()); + } + + Ok(LanceBuffer::reinterpret_vec(new_offsets)) + } + + pub fn per_value_decompress( + &self, + data: &[u8], + offsets: &[T], + decompressed: &mut Vec, + ) -> Result { + let mut new_offsets: Vec = Vec::with_capacity(offsets.len()); + new_offsets.push(T::from_usize(0).unwrap()); + + for off in offsets.windows(2) { + let start = off[0].as_usize(); + let end = off[1].as_usize(); + self.compressor + .decompress(&data[start..end], decompressed)?; + new_offsets.push(T::from_usize(decompressed.len()).unwrap()); + } + + Ok(LanceBuffer::reinterpret_vec(new_offsets)) + } +} + +impl PerValueCompressor for CompressedBufferEncoder { + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)> { + let data_type = data.name(); + let data = data.as_variable_width().ok_or(Error::internal(format!( + "Attempt to use CompressedBufferEncoder on data of type {}", + data_type + )))?; + + let data_bytes = &data.data; + let mut compressed = Vec::with_capacity(data_bytes.len()); + + let new_offsets = match data.bits_per_offset { + 32 => self.per_value_compress::( + data_bytes, + &data.offsets.borrow_to_typed_slice::(), + &mut compressed, + )?, + 64 => self.per_value_compress::( + data_bytes, + &data.offsets.borrow_to_typed_slice::(), + &mut compressed, + )?, + _ => unreachable!(), + }; + + let compressed = PerValueDataBlock::Variable(VariableWidthBlock { + bits_per_offset: data.bits_per_offset, + data: LanceBuffer::from(compressed), + offsets: new_offsets, + num_values: data.num_values, + block_info: BlockInfo::new(), + }); + + // TODO: Support setting the level + // TODO: Support underlying compression of data (e.g. defer to binary encoding for offset bitpacking) + let encoding = ProtobufUtils21::wrapped( + self.compressor.config(), + ProtobufUtils21::variable( + ProtobufUtils21::flat(data.bits_per_offset as u64, None), + None, + ), + )?; + + Ok((compressed, encoding)) + } +} + +impl VariablePerValueDecompressor for CompressedBufferEncoder { + fn decompress(&self, data: VariableWidthBlock) -> Result { + let data_bytes = &data.data; + let mut decompressed = Vec::with_capacity(data_bytes.len() * 2); + + let new_offsets = match data.bits_per_offset { + 32 => self.per_value_decompress( + data_bytes, + &data.offsets.borrow_to_typed_slice::(), + &mut decompressed, + )?, + 64 => self.per_value_decompress( + data_bytes, + &data.offsets.borrow_to_typed_slice::(), + &mut decompressed, + )?, + _ => unreachable!(), + }; + Ok(DataBlock::VariableWidth(VariableWidthBlock { + bits_per_offset: data.bits_per_offset, + data: LanceBuffer::from(decompressed), + offsets: new_offsets, + num_values: data.num_values, + block_info: BlockInfo::new(), + })) + } +} + +impl BlockCompressor for CompressedBufferEncoder { + fn compress(&self, data: DataBlock) -> Result { + let encoded = match data { + DataBlock::FixedWidth(fixed_width) => fixed_width.data, + DataBlock::VariableWidth(variable_width) => { + // Wrap VariableEncoder to handle the encoding + let encoder = VariableEncoder::default(); + BlockCompressor::compress(&encoder, DataBlock::VariableWidth(variable_width))? + } + _ => { + return Err(Error::invalid_input_source( + "Unsupported data block type".into(), + )); + } + }; + + let mut compressed = Vec::new(); + self.compressor.compress(&encoded, &mut compressed)?; + Ok(LanceBuffer::from(compressed)) + } +} + +impl BlockDecompressor for CompressedBufferEncoder { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + let mut decompressed = Vec::new(); + self.compressor.decompress(&data, &mut decompressed)?; + + // Delegate to BinaryBlockDecompressor which handles the inline metadata + let inner_decoder = BinaryBlockDecompressor::default(); + inner_decoder.decompress(LanceBuffer::from(decompressed), num_values) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::str::FromStr; + + use crate::encodings::physical::block::zstd::ZstdBufferCompressor; + + #[test] + fn test_compression_scheme_from_str() { + assert_eq!( + CompressionScheme::from_str("none").unwrap(), + CompressionScheme::None + ); + assert_eq!( + CompressionScheme::from_str("zstd").unwrap(), + CompressionScheme::Zstd + ); + } + + #[test] + fn test_compression_scheme_from_str_invalid() { + assert!(CompressionScheme::from_str("invalid").is_err()); + } + + #[cfg(feature = "zstd")] + mod zstd { + use std::io::Write; + + use super::*; + + #[test] + fn test_compress_zstd_with_length_prefixed() { + let compressor = ZstdBufferCompressor::new(0); + let input_data = b"Hello, world!"; + let mut compressed_data = Vec::new(); + + compressor + .compress(input_data, &mut compressed_data) + .unwrap(); + let mut decompressed_data = Vec::new(); + compressor + .decompress(&compressed_data, &mut decompressed_data) + .unwrap(); + assert_eq!(input_data, decompressed_data.as_slice()); + } + + #[test] + fn test_zstd_compress_decompress_multiple_times() { + let compressor = ZstdBufferCompressor::new(0); + let (input_data_1, input_data_2) = (b"Hello ", b"World"); + let mut compressed_data = Vec::new(); + + compressor + .compress(input_data_1, &mut compressed_data) + .unwrap(); + let compressed_length_1 = compressed_data.len(); + + compressor + .compress(input_data_2, &mut compressed_data) + .unwrap(); + + let mut decompressed_data = Vec::new(); + compressor + .decompress( + &compressed_data[..compressed_length_1], + &mut decompressed_data, + ) + .unwrap(); + + compressor + .decompress( + &compressed_data[compressed_length_1..], + &mut decompressed_data, + ) + .unwrap(); + + // the output should contain both input_data_1 and input_data_2 + assert_eq!( + decompressed_data.len(), + input_data_1.len() + input_data_2.len() + ); + assert_eq!( + &decompressed_data[..input_data_1.len()], + input_data_1, + "First part of decompressed data should match input_1" + ); + assert_eq!( + &decompressed_data[input_data_1.len()..], + input_data_2, + "Second part of decompressed data should match input_2" + ); + } + + #[test] + fn test_compress_zstd_raw_stream_format_and_decompress_with_length_prefixed() { + let compressor = ZstdBufferCompressor::new(0); + let input_data = b"Hello, world!"; + let mut compressed_data = Vec::new(); + + // compress using raw stream format + let mut encoder = ::zstd::Encoder::new(&mut compressed_data, 0).unwrap(); + encoder.write_all(input_data).unwrap(); + encoder.finish().expect("failed to encode data with zstd"); + + // decompress using length prefixed format + let mut decompressed_data = Vec::new(); + compressor + .decompress(&compressed_data, &mut decompressed_data) + .unwrap(); + assert_eq!(input_data, decompressed_data.as_slice()); + } + } + + #[cfg(feature = "lz4")] + mod lz4 { + use std::{collections::HashMap, sync::Arc}; + + use arrow_schema::{DataType, Field}; + use lance_datagen::array::{binary_prefix_plus_counter, utf8_prefix_plus_counter}; + + use super::*; + + use crate::constants::DICT_SIZE_RATIO_META_KEY; + use crate::{ + constants::{ + COMPRESSION_META_KEY, DICT_DIVISOR_META_KEY, STRUCTURAL_ENCODING_FULLZIP, + STRUCTURAL_ENCODING_META_KEY, + }, + encodings::physical::block::lz4::Lz4BufferCompressor, + testing::{FnArrayGeneratorProvider, TestCases, check_round_trip_encoding_generated}, + }; + + #[test] + fn test_lz4_compress_decompress() { + let compressor = Lz4BufferCompressor::default(); + let input_data = b"Hello, world!"; + let mut compressed_data = Vec::new(); + + compressor + .compress(input_data, &mut compressed_data) + .unwrap(); + let mut decompressed_data = Vec::new(); + compressor + .decompress(&compressed_data, &mut decompressed_data) + .unwrap(); + assert_eq!(input_data, decompressed_data.as_slice()); + } + + #[test_log::test(tokio::test)] + async fn test_lz4_compress_round_trip() { + for data_type in &[ + DataType::Utf8, + DataType::LargeUtf8, + DataType::Binary, + DataType::LargeBinary, + ] { + let field = Field::new("", data_type.clone(), false); + let mut field_meta = HashMap::new(); + field_meta.insert(COMPRESSION_META_KEY.to_string(), "lz4".to_string()); + // Some bad cardinality estimatation causes us to use dictionary encoding currently + // which causes the expected encoding check to fail. + field_meta.insert(DICT_DIVISOR_META_KEY.to_string(), "100000".to_string()); + field_meta.insert(DICT_SIZE_RATIO_META_KEY.to_string(), "0.0001".to_string()); + // Also disable size-based dictionary encoding + field_meta.insert( + STRUCTURAL_ENCODING_META_KEY.to_string(), + STRUCTURAL_ENCODING_FULLZIP.to_string(), + ); + let field = field.with_metadata(field_meta); + let test_cases = TestCases::basic() + // Need to use large pages as small pages might be too small to compress + .with_page_sizes(vec![1024 * 1024]) + .with_expected_encoding("zstd") + .with_structural_encodings(); + + // Can't use the default random provider because random data isn't compressible + // and we will fallback to uncompressed encoding + let datagen = Box::new(FnArrayGeneratorProvider::new(move || match data_type { + DataType::Utf8 => utf8_prefix_plus_counter("compressme", false), + DataType::Binary => { + binary_prefix_plus_counter(Arc::from(b"compressme".to_owned()), false) + } + DataType::LargeUtf8 => utf8_prefix_plus_counter("compressme", true), + DataType::LargeBinary => { + binary_prefix_plus_counter(Arc::from(b"compressme".to_owned()), true) + } + _ => panic!("Unsupported data type: {:?}", data_type), + })); + + check_round_trip_encoding_generated(field, datagen, test_cases).await; + } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/byte_stream_split.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/byte_stream_split.rs new file mode 100644 index 000000000..d4dbd1045 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/byte_stream_split.rs @@ -0,0 +1,452 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! # Byte Stream Split (BSS) Miniblock Format +//! +//! Byte Stream Split is a data transformation technique that improves compression +//! by reorganizing multi-byte values to group bytes from the same position together. +//! This is particularly effective for data where some byte positions have low entropy. +//! +//! ## How It Works +//! +//! BSS splits multi-byte values by byte position, creating separate streams +//! for each byte position across all values. This transformation is most beneficial +//! when certain byte positions have low entropy (e.g., high-order bytes that are +//! mostly zeros, sign-extended bytes, or floating-point sign/exponent bytes that +//! cluster around common values). +//! +//! ### Example +//! +//! Input data (f32): `[1.0, 2.0, 3.0, 4.0]` +//! +//! In little-endian bytes: +//! - 1.0 = `[00, 00, 80, 3F]` +//! - 2.0 = `[00, 00, 00, 40]` +//! - 3.0 = `[00, 00, 40, 40]` +//! - 4.0 = `[00, 00, 80, 40]` +//! +//! After BSS transformation: +//! - Byte stream 0: `[00, 00, 00, 00]` (all first bytes) +//! - Byte stream 1: `[00, 00, 00, 00]` (all second bytes) +//! - Byte stream 2: `[80, 00, 40, 80]` (all third bytes) +//! - Byte stream 3: `[3F, 40, 40, 40]` (all fourth bytes) +//! +//! Output: `[00, 00, 00, 00, 00, 00, 00, 00, 80, 00, 40, 80, 3F, 40, 40, 40]` +//! +//! ## Compression Benefits +//! +//! BSS itself doesn't compress data - it reorders it. The compression benefit +//! comes when BSS is combined with general-purpose compression (e.g., LZ4): +//! +//! 1. **Timestamps**: Sequential timestamps have similar high-order bytes +//! 2. **Sensor data**: Readings often vary in a small range, sharing exponent bits +//! 3. **Financial data**: Prices may cluster around certain values +//! +//! ## Supported Types +//! +//! - 32-bit floating point (f32) +//! - 64-bit floating point (f64) +//! +//! ## Chunk Handling +//! +//! - Maximum chunk size depends on data type: +//! - f32: 1024 values (4KB per chunk) +//! - f64: 512 values (4KB per chunk) +//! - All chunks share a single global buffer +//! - Non-last chunks always contain power-of-2 values + +use std::fmt::Debug; + +use crate::buffer::LanceBuffer; +use crate::compression::MiniBlockDecompressor; +use crate::compression_config::BssMode; +use crate::data::{BlockInfo, DataBlock, FixedWidthDataBlock}; +use crate::encodings::logical::primitive::miniblock::{ + MiniBlockChunk, MiniBlockCompressed, MiniBlockCompressionContext, MiniBlockCompressor, +}; +use crate::format::ProtobufUtils21; +use crate::format::pb21::CompressiveEncoding; +use crate::statistics::{GetStat, Stat}; +use arrow_array::{cast::AsArray, types::UInt64Type}; +use lance_core::Result; + +/// Byte Stream Split encoder for floating point values +/// +/// This encoding splits floating point values by byte position and stores +/// each byte stream separately. This improves compression ratios for +/// floating point data with similar patterns. +#[derive(Debug, Clone)] +pub struct ByteStreamSplitEncoder { + bits_per_value: usize, +} + +impl ByteStreamSplitEncoder { + pub fn new(bits_per_value: usize) -> Self { + assert!( + bits_per_value == 32 || bits_per_value == 64, + "ByteStreamSplit only supports 32-bit (f32) or 64-bit (f64) values" + ); + Self { bits_per_value } + } + + fn bytes_per_value(&self) -> usize { + self.bits_per_value / 8 + } + + fn max_chunk_size(&self) -> usize { + // For ByteStreamSplit, total bytes = bytes_per_value * chunk_size + // MAX_MINIBLOCK_BYTES = 8186 + // For f32 (4 bytes): 8186 / 4 = 2046, so max chunk = 1024 (power of 2) + // For f64 (8 bytes): 8186 / 8 = 1023, so max chunk = 512 (power of 2) + match self.bits_per_value { + 32 => 1024, + 64 => 512, + _ => unreachable!("ByteStreamSplit only supports 32 or 64 bit values"), + } + } +} + +impl MiniBlockCompressor for ByteStreamSplitEncoder { + fn compress( + &self, + _context: MiniBlockCompressionContext, + page: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match page { + DataBlock::FixedWidth(data) => { + let num_values = data.num_values; + let bytes_per_value = self.bytes_per_value(); + + if num_values == 0 { + return Ok(( + MiniBlockCompressed { + data: vec![], + chunks: vec![], + num_values: 0, + }, + ProtobufUtils21::byte_stream_split(ProtobufUtils21::flat( + self.bits_per_value as u64, + None, + )), + )); + } + + let total_size = num_values as usize * bytes_per_value; + let mut global_buffer = vec![0u8; total_size]; + + let mut chunks = Vec::new(); + let data_slice = data.data.as_ref(); + let mut processed_values = 0usize; + let max_chunk_size = self.max_chunk_size(); + + while processed_values < num_values as usize { + let chunk_size = (num_values as usize - processed_values).min(max_chunk_size); + let chunk_offset = processed_values * bytes_per_value; + + // Create chunk-local byte streams + for i in 0..chunk_size { + let src_offset = (processed_values + i) * bytes_per_value; + for j in 0..bytes_per_value { + // Store in chunk-local byte stream format + let dst_offset = chunk_offset + j * chunk_size + i; + global_buffer[dst_offset] = data_slice[src_offset + j]; + } + } + + let chunk_bytes = chunk_size * bytes_per_value; + let log_num_values = if processed_values + chunk_size == num_values as usize { + 0 // Last chunk + } else { + chunk_size.ilog2() as u8 + }; + + debug_assert!(chunk_bytes > 0); + chunks.push(MiniBlockChunk { + buffer_sizes: vec![chunk_bytes as u32], + log_num_values, + }); + + processed_values += chunk_size; + } + + let data_buffers = vec![LanceBuffer::from(global_buffer)]; + + // TODO: Should support underlying compression + let encoding = ProtobufUtils21::byte_stream_split(ProtobufUtils21::flat( + self.bits_per_value as u64, + None, + )); + + Ok(( + MiniBlockCompressed { + data: data_buffers, + chunks, + num_values, + }, + encoding, + )) + } + _ => Err(lance_core::Error::invalid_input_source( + "ByteStreamSplit encoding only supports FixedWidth data blocks".into(), + )), + } + } +} + +/// Byte Stream Split decompressor +#[derive(Debug)] +pub struct ByteStreamSplitDecompressor { + bits_per_value: usize, +} + +impl ByteStreamSplitDecompressor { + pub fn new(bits_per_value: usize) -> Self { + assert!( + bits_per_value == 32 || bits_per_value == 64, + "ByteStreamSplit only supports 32-bit (f32) or 64-bit (f64) values" + ); + Self { bits_per_value } + } + + fn bytes_per_value(&self) -> usize { + self.bits_per_value / 8 + } +} + +impl MiniBlockDecompressor for ByteStreamSplitDecompressor { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + if num_values == 0 { + return Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::empty(), + bits_per_value: self.bits_per_value as u64, + num_values: 0, + block_info: BlockInfo::new(), + })); + } + + let bytes_per_value = self.bytes_per_value(); + let total_bytes = num_values as usize * bytes_per_value; + + if data.len() != 1 { + return Err(lance_core::Error::invalid_input_source( + format!( + "ByteStreamSplit decompression expects 1 buffer, but got {}", + data.len() + ) + .into(), + )); + } + + let input_buffer = &data[0]; + + if input_buffer.len() != total_bytes { + return Err(lance_core::Error::invalid_input_source( + format!( + "Expected {} bytes for decompression, but got {}", + total_bytes, + input_buffer.len() + ) + .into(), + )); + } + + let mut output = vec![0u8; total_bytes]; + + // Input buffer contains chunk-local byte streams + for i in 0..num_values as usize { + for j in 0..bytes_per_value { + let src_offset = j * num_values as usize + i; + output[i * bytes_per_value + j] = input_buffer[src_offset]; + } + } + + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(output), + bits_per_value: self.bits_per_value as u64, + num_values, + block_info: BlockInfo::new(), + })) + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + num_values.checked_mul(self.bytes_per_value() as u64) + } +} + +/// Determine if BSS should be used based on mode and data characteristics +pub fn should_use_bss(data: &FixedWidthDataBlock, mode: BssMode) -> bool { + // Only support 32-bit and 64-bit values + // BSS is most effective for these common types (floats, timestamps, etc.) + // 16-bit values have limited benefit with only 2 streams + if data.bits_per_value != 32 && data.bits_per_value != 64 { + return false; + } + + let sensitivity = mode.to_sensitivity(); + + // Fast paths + if sensitivity <= 0.0 { + return false; + } + if sensitivity >= 1.0 { + return true; + } + + // Auto mode: check byte position entropy + evaluate_entropy_for_bss(data, sensitivity) +} + +/// Evaluate if BSS should be used based on byte position entropy +fn evaluate_entropy_for_bss(data: &FixedWidthDataBlock, sensitivity: f32) -> bool { + // Get the precomputed entropy statistics + let Some(entropy_stat) = data.get_stat(Stat::BytePositionEntropy) else { + return false; // No entropy data available + }; + + let entropies = entropy_stat.as_primitive::(); + if entropies.is_empty() { + return false; + } + + // Calculate average entropy across all byte positions + let sum: u64 = entropies.values().iter().sum(); + let avg_entropy = sum as f64 / entropies.len() as f64 / 1000.0; // Scale back from integer + + // Entropy threshold based on sensitivity + // sensitivity = 0.5 (default auto) -> threshold = 4.0 bits + // sensitivity = 0.0 (off) -> threshold = 0.0 (never use) + // sensitivity = 1.0 (on) -> threshold = 8.0 (always use) + let entropy_threshold = sensitivity as f64 * 8.0; + + // Use BSS if average entropy is below threshold + // Lower entropy means more repetitive byte patterns + avg_entropy < entropy_threshold +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_round_trip_f32() { + let encoder = ByteStreamSplitEncoder::new(32); + let decompressor = ByteStreamSplitDecompressor::new(32); + + // Test data + let values: Vec = vec![ + 1.0, + 2.5, + -3.7, + 4.2, + 0.0, + -0.0, + f32::INFINITY, + f32::NEG_INFINITY, + ]; + let bytes: Vec = values.iter().flat_map(|v| v.to_le_bytes()).collect(); + + let data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(bytes), + bits_per_value: 32, + num_values: values.len() as u64, + block_info: BlockInfo::new(), + }); + + // Compress + let (compressed, _encoding) = encoder + .compress(MiniBlockCompressionContext::new(0, true, true), data_block) + .unwrap(); + + // Decompress + let decompressed = decompressor + .decompress(compressed.data, values.len() as u64) + .unwrap(); + let DataBlock::FixedWidth(decompressed_fixed) = &decompressed else { + panic!("Expected FixedWidth DataBlock") + }; + + // Verify + let result_bytes = decompressed_fixed.data.as_ref(); + let result_values: Vec = result_bytes + .chunks_exact(4) + .map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap())) + .collect(); + + assert_eq!(values, result_values); + } + + #[test] + fn test_round_trip_f64() { + let encoder = ByteStreamSplitEncoder::new(64); + let decompressor = ByteStreamSplitDecompressor::new(64); + + // Test data + let values: Vec = vec![ + 1.0, + 2.5, + -3.7, + 4.2, + 0.0, + -0.0, + f64::INFINITY, + f64::NEG_INFINITY, + ]; + let bytes: Vec = values.iter().flat_map(|v| v.to_le_bytes()).collect(); + + let data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(bytes), + bits_per_value: 64, + num_values: values.len() as u64, + block_info: BlockInfo::new(), + }); + + // Compress + let (compressed, _encoding) = encoder + .compress(MiniBlockCompressionContext::new(0, true, true), data_block) + .unwrap(); + + // Decompress + let decompressed = decompressor + .decompress(compressed.data, values.len() as u64) + .unwrap(); + let DataBlock::FixedWidth(decompressed_fixed) = &decompressed else { + panic!("Expected FixedWidth DataBlock") + }; + + // Verify + let result_bytes = decompressed_fixed.data.as_ref(); + let result_values: Vec = result_bytes + .chunks_exact(8) + .map(|chunk| f64::from_le_bytes(chunk.try_into().unwrap())) + .collect(); + + assert_eq!(values, result_values); + } + + #[test] + fn test_empty_data() { + let encoder = ByteStreamSplitEncoder::new(32); + let decompressor = ByteStreamSplitDecompressor::new(32); + + let data_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::empty(), + bits_per_value: 32, + num_values: 0, + block_info: BlockInfo::new(), + }); + + // Compress empty data + let (compressed, _encoding) = encoder + .compress(MiniBlockCompressionContext::new(0, true, true), data_block) + .unwrap(); + + // Decompress empty data + let decompressed = decompressor.decompress(compressed.data, 0).unwrap(); + let DataBlock::FixedWidth(decompressed_fixed) = &decompressed else { + panic!("Expected FixedWidth DataBlock") + }; + + assert_eq!(decompressed_fixed.num_values, 0); + assert_eq!(decompressed_fixed.data.len(), 0); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/constant.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/constant.rs new file mode 100644 index 000000000..c3fa16863 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/constant.rs @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Routines for compressing and decompressing constant-encoded data + +use crate::{ + buffer::LanceBuffer, + compression::{BlockDecompressor, FixedPerValueDecompressor}, + data::{AllNullDataBlock, ConstantDataBlock, DataBlock, FixedWidthDataBlock}, +}; + +use lance_core::Result; + +/// A decompressor for constant-encoded data +#[derive(Debug)] +pub struct ConstantDecompressor { + scalar: Option, +} + +impl ConstantDecompressor { + pub fn new(scalar: Option) -> Self { + Self { scalar } + } +} + +impl BlockDecompressor for ConstantDecompressor { + fn decompress(&self, _data: LanceBuffer, num_values: u64) -> Result { + if let Some(scalar) = self.scalar.clone() { + Ok(DataBlock::Constant(ConstantDataBlock { + data: scalar, + num_values, + })) + } else { + Ok(DataBlock::AllNull(AllNullDataBlock { num_values })) + } + } +} + +impl FixedPerValueDecompressor for ConstantDecompressor { + fn decompress(&self, _data: FixedWidthDataBlock, num_values: u64) -> Result { + if let Some(scalar) = self.scalar.clone() { + Ok(DataBlock::Constant(ConstantDataBlock { + data: scalar, + num_values, + })) + } else { + Ok(DataBlock::AllNull(AllNullDataBlock { num_values })) + } + } + + fn bits_per_value(&self) -> u64 { + self.scalar + .as_ref() + .map(|s| s.len() as u64 * 8) + .unwrap_or(0) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/fsst.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/fsst.rs new file mode 100644 index 000000000..eaa8c9112 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/fsst.rs @@ -0,0 +1,410 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! FSST encoding +//! +//! FSST is a lightweight encoding for variable width data. This module includes +//! adapters for both miniblock and per-value encoding. +//! +//! FSST encoding creates a small symbol table that is needed for decoding. Currently +//! we create one symbol table per disk page and store it in the description. +//! +//! TODO: This seems to be potentially limiting. Perhaps we should create one symbol +//! table per mini-block chunk? In the per-value compression it may even make sense to +//! create multiple symbol tables for a single value! +//! +//! FSST encoding is transparent. + +use lance_core::{Error, Result}; + +use crate::{ + buffer::LanceBuffer, + compression::{MiniBlockDecompressor, VariablePerValueDecompressor}, + data::{BlockInfo, DataBlock, VariableWidthBlock}, + encodings::logical::primitive::{ + fullzip::{PerValueCompressor, PerValueDataBlock}, + miniblock::{MiniBlockCompressed, MiniBlockCompressionContext, MiniBlockCompressor}, + }, + format::{ + ProtobufUtils21, + pb21::{self, CompressiveEncoding}, + }, +}; + +use super::binary::BinaryMiniBlockEncoder; + +struct FsstCompressed { + data: VariableWidthBlock, + symbol_table: Vec, +} + +impl FsstCompressed { + fn fsst_compress(data: DataBlock) -> Result { + match data { + DataBlock::VariableWidth(variable_width) => { + match variable_width.bits_per_offset { + 32 => { + let offsets = variable_width.offsets.borrow_to_typed_slice::(); + let offsets_slice = offsets.as_ref(); + let bytes_data = variable_width.data.into_buffer(); + + // prepare compression output buffer + let mut dest_offsets = vec![0_i32; offsets_slice.len() * 2]; + let mut dest_values = vec![0_u8; bytes_data.len() * 2]; + let mut symbol_table = vec![0_u8; fsst::fsst::FSST_SYMBOL_TABLE_SIZE]; + + // fsst compression + fsst::fsst::compress( + &mut symbol_table, + bytes_data.as_slice(), + offsets_slice, + &mut dest_values, + &mut dest_offsets, + )?; + + // construct `DataBlock` for BinaryMiniBlockEncoder, we may want some `DataBlock` construct methods later + let compressed = VariableWidthBlock { + data: LanceBuffer::reinterpret_vec(dest_values), + bits_per_offset: 32, + offsets: LanceBuffer::reinterpret_vec(dest_offsets), + num_values: variable_width.num_values, + block_info: BlockInfo::new(), + }; + + Ok(Self { + data: compressed, + symbol_table, + }) + } + 64 => { + let offsets = variable_width.offsets.borrow_to_typed_slice::(); + let offsets_slice = offsets.as_ref(); + let bytes_data = variable_width.data.into_buffer(); + + // prepare compression output buffer + let mut dest_offsets = vec![0_i64; offsets_slice.len() * 2]; + let mut dest_values = vec![0_u8; bytes_data.len() * 2]; + let mut symbol_table = vec![0_u8; fsst::fsst::FSST_SYMBOL_TABLE_SIZE]; + + // fsst compression + fsst::fsst::compress( + &mut symbol_table, + bytes_data.as_slice(), + offsets_slice, + &mut dest_values, + &mut dest_offsets, + )?; + + // construct `DataBlock` for BinaryMiniBlockEncoder, we may want some `DataBlock` construct methods later + let compressed = VariableWidthBlock { + data: LanceBuffer::reinterpret_vec(dest_values), + bits_per_offset: 64, + offsets: LanceBuffer::reinterpret_vec(dest_offsets), + num_values: variable_width.num_values, + block_info: BlockInfo::new(), + }; + + Ok(Self { + data: compressed, + symbol_table, + }) + } + _ => panic!( + "Unsupported offsets type {}", + variable_width.bits_per_offset + ), + } + } + _ => Err(Error::invalid_input_source( + format!( + "Cannot compress a data block of type {} with FsstEncoder", + data.name() + ) + .into(), + )), + } + } +} + +#[derive(Debug, Default)] +pub struct FsstMiniBlockEncoder { + minichunk_size: Option, +} + +impl FsstMiniBlockEncoder { + pub fn new(minichunk_size: Option) -> Self { + Self { minichunk_size } + } +} + +impl MiniBlockCompressor for FsstMiniBlockEncoder { + fn compress( + &self, + context: MiniBlockCompressionContext, + data: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + let compressed = FsstCompressed::fsst_compress(data)?; + + let data_block = DataBlock::VariableWidth(compressed.data); + + // compress the fsst compressed data using `BinaryMiniBlockEncoder` + let binary_compressor = Box::new(BinaryMiniBlockEncoder::new(self.minichunk_size)) + as Box; + + let (binary_miniblock_compressed, binary_array_encoding) = + binary_compressor.compress(context, data_block)?; + + Ok(( + binary_miniblock_compressed, + ProtobufUtils21::fsst(binary_array_encoding, compressed.symbol_table), + )) + } +} + +#[derive(Debug)] +pub struct FsstPerValueEncoder { + inner: Box, +} + +impl FsstPerValueEncoder { + pub fn new(inner: Box) -> Self { + Self { inner } + } +} + +impl PerValueCompressor for FsstPerValueEncoder { + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)> { + let compressed = FsstCompressed::fsst_compress(data)?; + + let data_block = DataBlock::VariableWidth(compressed.data); + + let (binary_compressed, binary_array_encoding) = self.inner.compress(data_block)?; + + Ok(( + binary_compressed, + ProtobufUtils21::fsst(binary_array_encoding, compressed.symbol_table), + )) + } +} + +#[derive(Debug)] +pub struct FsstPerValueDecompressor { + symbol_table: LanceBuffer, + inner_decompressor: Box, +} + +impl FsstPerValueDecompressor { + pub fn new( + symbol_table: LanceBuffer, + inner_decompressor: Box, + ) -> Self { + Self { + symbol_table, + inner_decompressor, + } + } +} + +impl VariablePerValueDecompressor for FsstPerValueDecompressor { + fn decompress(&self, data: VariableWidthBlock) -> Result { + // Step 1. Run inner decompressor + let compressed_variable_data = self + .inner_decompressor + .decompress(data)? + .as_variable_width() + .unwrap(); + + // Step 2. FSST decompress + let bytes = compressed_variable_data.data.borrow_to_typed_slice::(); + let bytes = bytes.as_ref(); + + match compressed_variable_data.bits_per_offset { + 32 => { + let offsets = compressed_variable_data + .offsets + .borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + let num_values = compressed_variable_data.num_values; + + // The data will expand at most 8 times + // The offsets will be the same size because we have the same # of strings + let mut decompress_bytes_buf = vec![0u8; bytes.len() * 8]; + let mut decompress_offset_buf = vec![0i32; offsets.len()]; + fsst::fsst::decompress( + &self.symbol_table, + bytes, + offsets, + &mut decompress_bytes_buf, + &mut decompress_offset_buf, + )?; + + // Ensure the offsets array is trimmed to exactly num_values + 1 elements + decompress_offset_buf.truncate((num_values + 1) as usize); + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(decompress_bytes_buf), + offsets: LanceBuffer::reinterpret_vec(decompress_offset_buf), + bits_per_offset: 32, + num_values, + block_info: BlockInfo::new(), + })) + } + 64 => { + let offsets = compressed_variable_data + .offsets + .borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + let num_values = compressed_variable_data.num_values; + + // The data will expand at most 8 times + // The offsets will be the same size because we have the same # of strings + let mut decompress_bytes_buf = vec![0u8; bytes.len() * 8]; + let mut decompress_offset_buf = vec![0i64; offsets.len()]; + fsst::fsst::decompress( + &self.symbol_table, + bytes, + offsets, + &mut decompress_bytes_buf, + &mut decompress_offset_buf, + )?; + + // Ensure the offsets array is trimmed to exactly num_values + 1 elements + decompress_offset_buf.truncate((num_values + 1) as usize); + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(decompress_bytes_buf), + offsets: LanceBuffer::reinterpret_vec(decompress_offset_buf), + bits_per_offset: 64, + num_values, + block_info: BlockInfo::new(), + })) + } + _ => panic!( + "Unsupported offset type {}", + compressed_variable_data.bits_per_offset, + ), + } + } +} + +#[derive(Debug)] +pub struct FsstMiniBlockDecompressor { + symbol_table: LanceBuffer, + inner_decompressor: Box, +} + +impl FsstMiniBlockDecompressor { + pub fn new( + description: &pb21::Fsst, + inner_decompressor: Box, + ) -> Self { + Self { + symbol_table: LanceBuffer::from_bytes(description.symbol_table.clone(), 1), + inner_decompressor, + } + } +} + +impl MiniBlockDecompressor for FsstMiniBlockDecompressor { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + // Step 1. decompress data use `BinaryMiniBlockDecompressor` + // Extract the bits_per_offset from the binary encoding + let compressed_data_block = self.inner_decompressor.decompress(data, num_values)?; + let DataBlock::VariableWidth(compressed_data_block) = compressed_data_block else { + panic!("BinaryMiniBlockDecompressor should output VariableWidth DataBlock") + }; + + // Step 2. FSST decompress + let bytes = &compressed_data_block.data; + let (decompress_bytes_buf, decompress_offset_buf) = + if compressed_data_block.bits_per_offset == 64 { + let offsets = compressed_data_block.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + + // The data will expand at most 8 times + // The offsets will be the same size because we have the same # of strings + let mut decompress_bytes_buf = vec![0u8; bytes.len() * 8]; + let mut decompress_offset_buf = vec![0i64; offsets.len()]; + fsst::fsst::decompress( + &self.symbol_table, + bytes.as_ref(), + offsets, + &mut decompress_bytes_buf, + &mut decompress_offset_buf, + )?; + + // Ensure the offsets array is trimmed to exactly num_values + 1 elements + decompress_offset_buf.truncate((num_values + 1) as usize); + + ( + decompress_bytes_buf, + LanceBuffer::reinterpret_vec(decompress_offset_buf), + ) + } else { + let offsets = compressed_data_block.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + + // The data will expand at most 8 times + // The offsets will be the same size because we have the same # of strings + let mut decompress_bytes_buf = vec![0u8; bytes.len() * 8]; + let mut decompress_offset_buf = vec![0i32; offsets.len()]; + fsst::fsst::decompress( + &self.symbol_table, + bytes.as_ref(), + offsets, + &mut decompress_bytes_buf, + &mut decompress_offset_buf, + )?; + + // Ensure the offsets array is trimmed to exactly num_values + 1 elements + decompress_offset_buf.truncate((num_values + 1) as usize); + + ( + decompress_bytes_buf, + LanceBuffer::reinterpret_vec(decompress_offset_buf), + ) + }; + + Ok(DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(decompress_bytes_buf), + offsets: decompress_offset_buf, + bits_per_offset: compressed_data_block.bits_per_offset, + num_values, + block_info: BlockInfo::new(), + })) + } +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use lance_datagen::{ByteCount, RowCount}; + + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + + #[test_log::test(tokio::test)] + async fn test_fsst() { + let test_cases = TestCases::default() + .with_expected_encoding("fsst") + .with_structural_encodings(); + + // Generate data suitable for FSST (large strings, total size > 32KB) + let arr = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_utf8(ByteCount::from(100), false)) + .into_batch_rows(RowCount::from(5000)) + .unwrap() + .column(0) + .clone(); + + // Test both explicit metadata and automatic selection + // 1. Test with explicit FSST metadata + let metadata_explicit = + HashMap::from([("lance-encoding:compression".to_string(), "fsst".to_string())]); + check_round_trip_encoding_of_data(vec![arr.clone()], &test_cases, metadata_explicit).await; + + // 2. Test automatic FSST selection based on data characteristics + // FSST should be chosen automatically: max_len >= 5 and total_size >= 32KB + check_round_trip_encoding_of_data(vec![arr], &test_cases, HashMap::new()).await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/general.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/general.rs new file mode 100644 index 000000000..bea6a85cb --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/general.rs @@ -0,0 +1,683 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use log::trace; + +use crate::{ + Result, + buffer::LanceBuffer, + compression::MiniBlockDecompressor, + data::DataBlock, + encodings::{ + logical::primitive::miniblock::{ + MiniBlockCompressed, MiniBlockCompressionContext, MiniBlockCompressor, + }, + physical::block::{CompressionConfig, GeneralBufferCompressor}, + }, + format::{ProtobufUtils21, pb21::CompressiveEncoding}, +}; + +/// A miniblock compressor that wraps another miniblock compressor and applies +/// general-purpose compression (LZ4, Zstd) to the resulting buffers. +#[derive(Debug)] +pub struct GeneralMiniBlockCompressor { + inner: Box, + compression: CompressionConfig, +} + +impl GeneralMiniBlockCompressor { + pub fn new(inner: Box, compression: CompressionConfig) -> Self { + Self { inner, compression } + } +} + +/// Minimum buffer size to consider for compression +const MIN_BUFFER_SIZE_FOR_COMPRESSION: usize = 4 * 1024; + +use super::super::logical::primitive::miniblock::MiniBlockChunk; + +impl MiniBlockCompressor for GeneralMiniBlockCompressor { + fn compress( + &self, + context: MiniBlockCompressionContext, + page: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + // First, compress with the inner compressor + let (inner_compressed, inner_encoding) = self.inner.compress(context, page)?; + + // Return the original encoding without compression if there's no data or + // the first buffer is not large enough + if inner_compressed.data.is_empty() + || inner_compressed.data[0].len() < MIN_BUFFER_SIZE_FOR_COMPRESSION + { + return Ok((inner_compressed, inner_encoding)); + } + + // We'll compress each chunk's portion of the first buffer independently + let first_buffer = &inner_compressed.data[0]; + let mut compressed_first_buffer = Vec::new(); + let mut new_chunks = Vec::with_capacity(inner_compressed.chunks.iter().len()); + let mut offset = 0usize; + let mut total_original_size = 0usize; + + let compressor = GeneralBufferCompressor::get_compressor(self.compression)?; + + for chunk in &inner_compressed.chunks { + let chunk_first_buffer_size = chunk.buffer_sizes[0] as usize; + + let chunk_data = &first_buffer.as_ref()[offset..offset + chunk_first_buffer_size]; + total_original_size += chunk_first_buffer_size; + + let compressed_start = compressed_first_buffer.len(); + compressor.compress(chunk_data, &mut compressed_first_buffer)?; + let compressed_size = compressed_first_buffer.len() - compressed_start; + + // Create new chunk with updated first buffer size + let mut new_buffer_sizes = chunk.buffer_sizes.clone(); + new_buffer_sizes[0] = compressed_size as u32; + + new_chunks.push(MiniBlockChunk { + buffer_sizes: new_buffer_sizes, + log_num_values: chunk.log_num_values, + }); + + offset += chunk_first_buffer_size; + } + + // Check if compression was effective + let compressed_total_size = compressed_first_buffer.len(); + if compressed_total_size >= total_original_size { + // Compression didn't help, return original + return Ok((inner_compressed, inner_encoding)); + } + + trace!( + "First buffer compressed from {} to {} bytes (ratio: {:.2})", + total_original_size, + compressed_total_size, + compressed_total_size as f32 / total_original_size as f32 + ); + + // Build final buffers: compressed first buffer + remaining original buffers + let mut final_buffers = vec![LanceBuffer::from(compressed_first_buffer)]; + final_buffers.extend(inner_compressed.data.into_iter().skip(1)); + + let compressed_result = MiniBlockCompressed { + data: final_buffers, + chunks: new_chunks, + num_values: inner_compressed.num_values, + }; + + // Return compressed encoding + let encoding = ProtobufUtils21::wrapped(self.compression, inner_encoding)?; + Ok((compressed_result, encoding)) + } +} + +/// A miniblock decompressor that first decompresses buffers using general-purpose +/// compression (LZ4, Zstd) and then delegates to an inner miniblock decompressor. +#[derive(Debug)] +pub struct GeneralMiniBlockDecompressor { + inner: Box, + compression: CompressionConfig, +} + +impl GeneralMiniBlockDecompressor { + pub fn new(inner: Box, compression: CompressionConfig) -> Self { + Self { inner, compression } + } +} + +impl MiniBlockDecompressor for GeneralMiniBlockDecompressor { + fn decompress(&self, mut data: Vec, num_values: u64) -> Result { + let mut decompressed_buffer = Vec::new(); + + let decompressor = GeneralBufferCompressor::get_compressor(self.compression)?; + decompressor.decompress(&data[0], &mut decompressed_buffer)?; + data[0] = LanceBuffer::from(decompressed_buffer); + + self.inner.decompress(data, num_values) + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + self.inner.decoded_size_bytes(num_values) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::compression::{DecompressionStrategy, DefaultDecompressionStrategy}; + use crate::data::{BlockInfo, FixedWidthDataBlock}; + use crate::encodings::physical::block::CompressionScheme; + use crate::encodings::physical::rle::RleEncoder; + use crate::encodings::physical::value::ValueEncoder; + use crate::format::pb21; + use crate::format::pb21::compressive_encoding::Compression; + use arrow_array::{Float64Array, Int32Array}; + + fn miniblock_context() -> MiniBlockCompressionContext { + MiniBlockCompressionContext::new(0, true, true) + } + + #[derive(Debug)] + struct TestCase { + name: &'static str, + inner_encoder: Box, + compression: CompressionConfig, + data: DataBlock, + expected_compressed: bool, // Whether we expect compression to be applied + min_compression_ratio: f32, // Minimum compression ratio if compressed + } + + fn create_test_cases() -> Vec { + vec![ + // Small data with RLE - should not compress due to size threshold + TestCase { + name: "small_rle_data", + inner_encoder: Box::new(RleEncoder::new()), + compression: CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + data: create_repeated_i32_block(vec![1, 1, 1, 1, 2, 2, 2, 2]), + expected_compressed: false, + min_compression_ratio: 1.0, + }, + // Large repeated data with RLE + LZ4 + TestCase { + name: "large_rle_lz4", + inner_encoder: Box::new(RleEncoder::new()), + compression: CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + data: create_pattern_i32_block(2048, |i| (i / 8) as i32), + expected_compressed: false, // RLE already compresses well, additional LZ4 may not help + min_compression_ratio: 1.0, + }, + // Large repeated data with RLE + Zstd + TestCase { + name: "large_rle_zstd", + inner_encoder: Box::new(RleEncoder::new()), + compression: CompressionConfig { + scheme: CompressionScheme::Zstd, + level: Some(3), + }, + data: create_pattern_i32_block(8192, |i| (i / 16) as i32), + expected_compressed: true, // Zstd might provide additional compression + min_compression_ratio: 0.9, // But not as much since RLE already compressed + }, + // Sequential data with ValueEncoder + LZ4 + TestCase { + name: "sequential_value_lz4", + inner_encoder: Box::new(ValueEncoder {}), + compression: CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + data: create_pattern_i32_block(1024, |i| i as i32), + expected_compressed: false, // Sequential data doesn't compress well + min_compression_ratio: 1.0, + }, + // Float data with ValueEncoder + Zstd + TestCase { + name: "float_value_zstd", + inner_encoder: Box::new(ValueEncoder {}), + compression: CompressionConfig { + scheme: CompressionScheme::Zstd, + level: Some(3), + }, + data: create_pattern_f64_block(1024, |i| i as f64 * 0.1), + expected_compressed: true, + min_compression_ratio: 0.9, + }, + ] + } + + fn create_repeated_i32_block(values: Vec) -> DataBlock { + let array = Int32Array::from(values); + DataBlock::from_array(array) + } + + fn create_pattern_i32_block(size: usize, pattern: F) -> DataBlock + where + F: Fn(usize) -> i32, + { + let values: Vec = (0..size).map(pattern).collect(); + let array = Int32Array::from(values); + DataBlock::from_array(array) + } + + fn create_pattern_f64_block(size: usize, pattern: F) -> DataBlock + where + F: Fn(usize) -> f64, + { + let values: Vec = (0..size).map(pattern).collect(); + let array = Float64Array::from(values); + DataBlock::from_array(array) + } + + fn run_round_trip_test(test_case: TestCase) { + let compressor = + GeneralMiniBlockCompressor::new(test_case.inner_encoder, test_case.compression); + + // Compress the data + let (compressed, encoding) = compressor + .compress(miniblock_context(), test_case.data) + .unwrap(); + + // Check if compression was applied as expected + match &encoding.compression { + Some(Compression::General(cm)) => { + assert!( + test_case.expected_compressed, + "{}: Expected compression to be applied", + test_case.name + ); + assert_eq!( + CompressionScheme::try_from(cm.compression.as_ref().unwrap().scheme()).unwrap(), + test_case.compression.scheme + ); + } + _ => { + // Could be RLE or other encoding if compression didn't help + if test_case.expected_compressed { + // Check if it's RLE encoding (which means compression didn't help) + match &encoding.compression { + Some(Compression::Rle(_)) => { + // RLE encoding returned - compression didn't help + } + Some(Compression::Flat(_)) => { + // Flat encoding returned - compression didn't help + } + _ => { + panic!( + "{}: Expected GeneralMiniBlock but got {:?}", + test_case.name, encoding.compression + ); + } + } + } + } + } + + // Verify chunks are created correctly + assert!( + !compressed.chunks.is_empty(), + "{}: No chunks created", + test_case.name + ); + + // Test decompression by simulating the actual miniblock decoding process + let decompressed_data = decompress_miniblock_chunks(&compressed, &encoding); + + // Verify round trip by checking data size + // We expect the decompressed data to match the original number of values + // The bytes per value depends on the test case + let bytes_per_value = if test_case.name.contains("float") { + 8 // f64 + } else { + 4 // i32 + }; + let expected_bytes = compressed.num_values as usize * bytes_per_value; + assert_eq!( + expected_bytes, + decompressed_data.len(), + "{}: Data size mismatch", + test_case.name + ); + + // Check compression ratio if applicable + if test_case.expected_compressed { + let compression_ratio = compressed.data[0].len() as f32 / expected_bytes as f32; + assert!( + compression_ratio <= test_case.min_compression_ratio, + "{}: Compression ratio {} > expected {}", + test_case.name, + compression_ratio, + test_case.min_compression_ratio + ); + } + } + + fn decompress_miniblock_chunks( + compressed: &MiniBlockCompressed, + encoding: &CompressiveEncoding, + ) -> Vec { + let mut decompressed_data = Vec::new(); + let mut offsets = vec![0usize; compressed.data.len()]; // Track offset for each buffer + let decompression_strategy = DefaultDecompressionStrategy::default(); + + for chunk in &compressed.chunks { + let chunk_values = if chunk.log_num_values > 0 { + 1u64 << chunk.log_num_values + } else { + // Last chunk - calculate remaining values + let decompressed_values = + decompressed_data.len() as u64 / get_bytes_per_value(compressed) as u64; + compressed.num_values.saturating_sub(decompressed_values) + }; + + // Extract buffers for this chunk + let mut chunk_buffers = Vec::new(); + for (i, &size) in chunk.buffer_sizes.iter().enumerate() { + if i < compressed.data.len() { + let buffer_data = + compressed.data[i].slice_with_length(offsets[i], size as usize); + chunk_buffers.push(buffer_data); + offsets[i] += size as usize; + } + } + + // Create a decompressor for this chunk + let decompressor = decompression_strategy + .create_miniblock_decompressor(encoding, &decompression_strategy) + .unwrap(); + + // Decompress the chunk + let chunk_decompressed = decompressor + .decompress(chunk_buffers, chunk_values) + .unwrap(); + + match chunk_decompressed { + DataBlock::FixedWidth(ref block) => { + decompressed_data.extend_from_slice(block.data.as_ref()); + } + _ => panic!("Expected FixedWidth block"), + } + } + + decompressed_data + } + + fn get_bytes_per_value(compressed: &MiniBlockCompressed) -> usize { + // This is a simplification - in reality we'd need to know the data type + // For our tests, we mostly use i32 (4 bytes) or f64 (8 bytes) + // We can try to guess based on the data size + if compressed.num_values == 0 { + return 4; // Default to i32 + } + + // For float tests, the number is usually 1024 and we use f64 + if compressed.num_values == 1024 { + return 8; // Likely f64 + } + + 4 // Default to i32 + } + + #[test] + fn test_compressed_mini_block_table_driven() { + for test_case in create_test_cases() { + run_round_trip_test(test_case); + } + } + + #[test] + fn test_compressed_mini_block_threshold() { + // Test that small buffers don't get compressed + let small_test = TestCase { + name: "small_buffer_no_compression", + inner_encoder: Box::new(RleEncoder::new()), + compression: CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + data: create_repeated_i32_block(vec![1, 1, 2, 2]), + expected_compressed: false, + min_compression_ratio: 1.0, + }; + run_round_trip_test(small_test); + } + + #[test] + fn test_compressed_mini_block_with_doubles() { + // Test with large sequential doubles that should compress well with Zstd + // The test focuses on verifying that GeneralMiniBlock works correctly + // when wrapping a simple ValueEncoder + let test_case = TestCase { + name: "float_values_with_zstd", + inner_encoder: Box::new(ValueEncoder {}), + compression: CompressionConfig { + scheme: CompressionScheme::Zstd, + level: Some(3), + }, + // Create enough data to ensure compression is applied + data: create_pattern_f64_block(1024, |i| (i / 10) as f64), + expected_compressed: true, + min_compression_ratio: 0.5, // Zstd should achieve good compression on repetitive data + }; + + run_round_trip_test(test_case); + } + + #[test] + fn test_compressed_mini_block_large_buffers() { + // Use value encoding which doesn't compress data, ensuring large buffers + // Create 1024 i32 values (4KB of data) + let values: Vec = (0..1024).collect(); + let data = LanceBuffer::from_bytes( + bytemuck::cast_slice(&values).to_vec().into(), + std::mem::align_of::() as u64, + ); + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 32, + data, + num_values: 1024, + block_info: BlockInfo::new(), + }); + + // Create compressor with ValueEncoder (no compression) and Zstd wrapper + let inner = Box::new(ValueEncoder {}); + let compression = CompressionConfig { + scheme: CompressionScheme::Zstd, + level: Some(3), + }; + let compressor = GeneralMiniBlockCompressor::new(inner, compression); + + // Compress the data + let (compressed, encoding) = compressor.compress(miniblock_context(), block).unwrap(); + + // Should get GeneralMiniBlock encoding since buffer is 4KB + match &encoding.compression { + Some(Compression::General(cm)) => { + assert!(cm.values.is_some()); + assert_eq!( + cm.compression.as_ref().unwrap().scheme(), + pb21::CompressionScheme::CompressionAlgorithmZstd + ); + assert_eq!(cm.compression.as_ref().unwrap().level, Some(3)); + + // Verify inner encoding is Flat (from ValueEncoder) + match &cm.values.as_ref().unwrap().compression { + Some(Compression::Flat(flat)) => { + assert_eq!(flat.bits_per_value, 32); + } + _ => panic!("Expected Flat inner encoding"), + } + } + _ => panic!("Expected GeneralMiniBlock encoding"), + } + + assert_eq!(compressed.num_values, 1024); + // ValueEncoder produces 1 buffer, so compressed result also has 1 buffer + assert_eq!(compressed.data.len(), 1); + } + + // Special test cases that don't fit the table-driven pattern + + #[test] + fn test_compressed_mini_block_rle_multiple_buffers() { + // RLE produces 2 buffers (values and lengths), test that both are handled correctly + let data = create_repeated_i32_block(vec![1; 100]); + let compressor = GeneralMiniBlockCompressor::new( + Box::new(RleEncoder::new()), + CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + ); + + let (compressed, _) = compressor.compress(miniblock_context(), data).unwrap(); + // RLE produces 2 buffers, but only the first one is compressed + assert_eq!(compressed.data.len(), 2); + } + + #[test] + fn test_rle_with_general_miniblock_wrapper() { + // Test that RLE encoding with bits_per_value >= 32 is automatically wrapped + // in GeneralMiniBlock with LZ4 compression + + // This test directly tests the RLE encoder behavior + // When bits_per_value >= 32, RLE should be wrapped in GeneralMiniBlock with LZ4 + + // Test case 1: 32-bit RLE data + let test_32 = TestCase { + name: "rle_32bit_with_general_wrapper", + inner_encoder: Box::new(RleEncoder::new()), + compression: CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + data: create_repeated_i32_block(vec![1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]), + expected_compressed: false, // RLE already compresses well, LZ4 might not help much + min_compression_ratio: 1.0, + }; + + // For 32-bit RLE, the compression strategy should automatically wrap it + // Let's directly test the compressor + let compressor = GeneralMiniBlockCompressor::new( + Box::new(RleEncoder::new()), + CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + ); + + let (_compressed, encoding) = compressor + .compress(miniblock_context(), test_32.data) + .unwrap(); + + // Verify the encoding structure + match &encoding.compression { + Some(Compression::General(cm)) => { + // Check inner encoding is RLE + match &cm.values.as_ref().unwrap().compression { + Some(Compression::Rle(rle)) => { + let Compression::Flat(values) = + rle.values.as_ref().unwrap().compression.as_ref().unwrap() + else { + panic!("Expected flat for RLE values") + }; + let Compression::Flat(run_lengths) = rle + .run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + else { + panic!("Expected flat for RLE run lengths") + }; + assert_eq!(values.bits_per_value, 32); + assert_eq!(run_lengths.bits_per_value, 8); + } + _ => panic!("Expected RLE as inner encoding"), + } + // Check compression is LZ4 + assert_eq!( + cm.compression.as_ref().unwrap().scheme(), + pb21::CompressionScheme::CompressionAlgorithmLz4 + ); + } + Some(Compression::Rle(_)) => { + // Also acceptable if compression didn't help + } + _ => panic!("Expected GeneralMiniBlock or Rle encoding"), + } + + // Test case 2: 64-bit RLE data + let values_64: Vec = vec![100i64; 50] + .into_iter() + .chain(vec![200i64; 50]) + .chain(vec![300i64; 50]) + .collect(); + let array_64 = arrow_array::Int64Array::from(values_64); + let block_64 = DataBlock::from_array(array_64); + + let compressor_64 = GeneralMiniBlockCompressor::new( + Box::new(RleEncoder::new()), + CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + ); + + let (_compressed_64, encoding_64) = compressor_64 + .compress(miniblock_context(), block_64) + .unwrap(); + + // Verify the encoding structure for 64-bit + match &encoding_64.compression { + Some(Compression::General(cm)) => { + // Check inner encoding is RLE + match &cm.values.as_ref().unwrap().compression { + Some(Compression::Rle(rle)) => { + let Compression::Flat(values) = + rle.values.as_ref().unwrap().compression.as_ref().unwrap() + else { + panic!("Expected flat for RLE values") + }; + let Compression::Flat(run_lengths) = rle + .run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + else { + panic!("Expected flat for RLE run lengths") + }; + assert_eq!(values.bits_per_value, 64); + assert_eq!(run_lengths.bits_per_value, 8); + } + _ => panic!("Expected RLE as inner encoding for 64-bit"), + } + // Check compression is LZ4 + assert_eq!( + cm.compression.as_ref().unwrap().scheme(), + pb21::CompressionScheme::CompressionAlgorithmLz4 + ); + } + Some(Compression::Rle(_)) => { + // Also acceptable if compression didn't help + } + _ => panic!("Expected GeneralMiniBlock or Rle encoding for 64-bit"), + } + } + + #[test] + fn test_compressed_mini_block_empty_data() { + let empty_array = Int32Array::from(vec![] as Vec); + let empty_block = DataBlock::from_array(empty_array); + + let compressor = GeneralMiniBlockCompressor::new( + Box::new(ValueEncoder {}), + CompressionConfig { + scheme: CompressionScheme::Lz4, + level: None, + }, + ); + + let result = compressor.compress(miniblock_context(), empty_block); + match result { + Ok((compressed, _)) => { + assert_eq!(compressed.num_values, 0); + } + Err(_) => { + // Empty data might not be supported by ValueEncoder + } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/packed.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/packed.rs new file mode 100644 index 000000000..81879f9c4 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/packed.rs @@ -0,0 +1,1214 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Packed encoding +//! +//! These encodings take struct data and compress it in a way that all fields are collected +//! together. +//! +//! This encoding can be transparent or opaque. In order to be transparent we must use transparent +//! compression on all children. Then we can zip together the compressed children. + +use std::{convert::TryInto, sync::Arc}; + +use arrow_array::types::UInt64Type; + +use lance_core::{Error, Result, datatypes::Field}; + +use crate::{ + buffer::LanceBuffer, + compression::{ + CompressionStrategy, FixedPerValueDecompressor, MiniBlockDecompressor, + VariablePerValueDecompressor, + }, + data::{ + BlockInfo, DataBlock, DataBlockBuilder, FixedWidthDataBlock, StructDataBlock, + VariableWidthBlock, + }, + encodings::logical::primitive::{ + fullzip::{PerValueCompressor, PerValueDataBlock}, + miniblock::{MiniBlockCompressed, MiniBlockCompressionContext, MiniBlockCompressor}, + }, + format::{ + ProtobufUtils21, + pb21::{CompressiveEncoding, PackedStruct, compressive_encoding::Compression}, + }, + statistics::{GetStat, Stat}, +}; + +use super::value::{ValueDecompressor, ValueEncoder}; + +// Transforms a `StructDataBlock` into a row major `FixedWidthDataBlock`. +// Only fields with fixed-width fields are supported for now, and the +// assumption that all fields has `bits_per_value % 8 == 0` is made. +fn struct_data_block_to_fixed_width_data_block( + struct_data_block: StructDataBlock, + bits_per_values: &[u64], +) -> DataBlock { + let data_size = struct_data_block.expect_single_stat::(Stat::DataSize); + let mut output = Vec::with_capacity(data_size as usize); + let num_values = struct_data_block.children[0].num_values(); + + for i in 0..num_values as usize { + for (j, child) in struct_data_block.children.iter().enumerate() { + let bytes_per_value = (bits_per_values[j] / 8) as usize; + let this_data = child + .as_fixed_width_ref() + .unwrap() + .data + .slice_with_length(bytes_per_value * i, bytes_per_value); + output.extend_from_slice(&this_data); + } + } + + DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: bits_per_values.iter().copied().sum(), + data: LanceBuffer::from(output), + num_values, + block_info: BlockInfo::default(), + }) +} + +#[derive(Debug, Default)] +pub struct PackedStructFixedWidthMiniBlockEncoder {} + +impl MiniBlockCompressor for PackedStructFixedWidthMiniBlockEncoder { + fn compress( + &self, + context: MiniBlockCompressionContext, + data: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match data { + DataBlock::Struct(struct_data_block) => { + let bits_per_values = struct_data_block.children.iter().map(|data_block| data_block.as_fixed_width_ref().unwrap().bits_per_value).collect::>(); + + // transform struct datablock to fixed-width data block. + let data_block = struct_data_block_to_fixed_width_data_block(struct_data_block, &bits_per_values); + + // store and transformed fixed-width data block. + let value_miniblock_compressor = Box::new(ValueEncoder::default()) as Box; + let (value_miniblock_compressed, value_array_encoding) = + value_miniblock_compressor.compress(context, data_block)?; + + Ok(( + value_miniblock_compressed, + ProtobufUtils21::packed_struct(value_array_encoding, bits_per_values), + )) + } + _ => Err(Error::invalid_input_source(format!( + "Cannot compress a data block of type {} with PackedStructFixedWidthBlockEncoder", + data.name() + ) + .into())), + } + } +} + +#[derive(Debug)] +pub struct PackedStructFixedWidthMiniBlockDecompressor { + bits_per_values: Vec, + array_encoding: Box, +} + +impl PackedStructFixedWidthMiniBlockDecompressor { + pub fn new(description: &PackedStruct) -> Self { + let array_encoding: Box = match description + .values + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + { + Compression::Flat(flat) => Box::new(ValueDecompressor::from_flat(flat)), + _ => panic!( + "Currently only `ArrayEncoding::Flat` is supported in packed struct encoding in Lance 2.1." + ), + }; + Self { + bits_per_values: description.bits_per_value.clone(), + array_encoding, + } + } +} + +impl MiniBlockDecompressor for PackedStructFixedWidthMiniBlockDecompressor { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + assert_eq!(data.len(), 1); + let encoded_data_block = self.array_encoding.decompress(data, num_values)?; + let DataBlock::FixedWidth(encoded_data_block) = encoded_data_block else { + panic!("ValueDecompressor should output FixedWidth DataBlock") + }; + + let bytes_per_values = self + .bits_per_values + .iter() + .map(|bits_per_value| *bits_per_value as usize / 8) + .collect::>(); + + assert!(encoded_data_block.bits_per_value % 8 == 0); + let encoded_bytes_per_row = (encoded_data_block.bits_per_value / 8) as usize; + + // use a prefix_sum vector as a helper to reconstruct to `StructDataBlock`. + let mut prefix_sum = vec![0; self.bits_per_values.len()]; + for i in 0..(self.bits_per_values.len() - 1) { + prefix_sum[i + 1] = prefix_sum[i] + bytes_per_values[i]; + } + + let mut children_data_block = vec![]; + for i in 0..self.bits_per_values.len() { + let child_buf_size = bytes_per_values[i] * num_values as usize; + let mut child_buf: Vec = Vec::with_capacity(child_buf_size); + + for j in 0..num_values as usize { + // the start of the data at this row is `j * encoded_bytes_per_row`, and the offset for this field is `prefix_sum[i]`, this field has length `bytes_per_values[i]`. + let this_value = encoded_data_block.data.slice_with_length( + prefix_sum[i] + (j * encoded_bytes_per_row), + bytes_per_values[i], + ); + + child_buf.extend_from_slice(&this_value); + } + + let child = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(child_buf), + bits_per_value: self.bits_per_values[i], + num_values, + block_info: BlockInfo::default(), + }); + children_data_block.push(child); + } + Ok(DataBlock::Struct(StructDataBlock { + children: children_data_block, + block_info: BlockInfo::default(), + validity: None, + })) + } +} + +#[derive(Debug)] +enum VariablePackedFieldData { + Fixed { + block: FixedWidthDataBlock, + }, + Variable { + block: VariableWidthBlock, + bits_per_length: u64, + }, +} + +impl VariablePackedFieldData { + fn append_row_bytes(&self, row_idx: usize, output: &mut Vec) -> Result<()> { + match self { + Self::Fixed { block } => { + let bits_per_value = block.bits_per_value; + if bits_per_value % 8 != 0 { + return Err(Error::invalid_input( + "Packed struct variable encoding requires byte-aligned fixed-width children", + )); + } + let bytes_per_value = (bits_per_value / 8) as usize; + let start = row_idx + .checked_mul(bytes_per_value) + .ok_or_else(|| Error::invalid_input("Packed struct row size overflow"))?; + let end = start + bytes_per_value; + let data = block.data.as_ref(); + if end > data.len() { + return Err(Error::invalid_input( + "Packed struct fixed child out of bounds", + )); + } + output.extend_from_slice(&data[start..end]); + Ok(()) + } + Self::Variable { + block, + bits_per_length, + } => { + if bits_per_length % 8 != 0 { + return Err(Error::invalid_input( + "Packed struct variable children must have byte-aligned length prefixes", + )); + } + let prefix_bytes = (*bits_per_length / 8) as usize; + if !(prefix_bytes == 4 || prefix_bytes == 8) { + return Err(Error::invalid_input( + "Packed struct variable children must use 32 or 64-bit length prefixes", + )); + } + match block.bits_per_offset { + 32 => { + let offsets = block.offsets.borrow_to_typed_slice::(); + let start = offsets[row_idx] as usize; + let end = offsets[row_idx + 1] as usize; + if end > block.data.len() { + return Err(Error::invalid_input( + "Packed struct variable child offsets out of bounds", + )); + } + let len = (end - start) as u32; + if prefix_bytes != std::mem::size_of::() { + return Err(Error::invalid_input( + "Packed struct variable child length prefix mismatch", + )); + } + output.extend_from_slice(&len.to_le_bytes()); + output.extend_from_slice(&block.data[start..end]); + Ok(()) + } + 64 => { + let offsets = block.offsets.borrow_to_typed_slice::(); + let start = offsets[row_idx] as usize; + let end = offsets[row_idx + 1] as usize; + if end > block.data.len() { + return Err(Error::invalid_input( + "Packed struct variable child offsets out of bounds", + )); + } + let len = (end - start) as u64; + if prefix_bytes != std::mem::size_of::() { + return Err(Error::invalid_input( + "Packed struct variable child length prefix mismatch", + )); + } + output.extend_from_slice(&len.to_le_bytes()); + output.extend_from_slice(&block.data[start..end]); + Ok(()) + } + _ => Err(Error::invalid_input( + "Packed struct variable child must use 32 or 64-bit offsets", + )), + } + } + } + } +} + +#[derive(Debug)] +pub struct PackedStructVariablePerValueEncoder { + strategy: Arc, + fields: Vec, +} + +impl PackedStructVariablePerValueEncoder { + pub fn new(strategy: Arc, fields: Vec) -> Self { + Self { strategy, fields } + } +} + +impl PerValueCompressor for PackedStructVariablePerValueEncoder { + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)> { + let DataBlock::Struct(struct_block) = data else { + return Err(Error::invalid_input( + "Packed struct encoder requires Struct data block", + )); + }; + + if struct_block.children.is_empty() { + return Err(Error::invalid_input( + "Packed struct encoder requires at least one child field", + )); + } + if struct_block.children.len() != self.fields.len() { + return Err(Error::invalid_input( + "Struct field metadata does not match number of children", + )); + } + + let num_values = struct_block.children[0].num_values(); + for child in struct_block.children.iter() { + if child.num_values() != num_values { + return Err(Error::invalid_input( + "Packed struct children must have matching value counts", + )); + } + } + + let mut field_data = Vec::with_capacity(self.fields.len()); + let mut field_metadata = Vec::with_capacity(self.fields.len()); + + for (field, child_block) in self.fields.iter().zip(struct_block.children) { + let compressor = self.strategy.create_per_value(field, &child_block)?; + let (compressed, encoding) = compressor.compress(child_block)?; + match compressed { + PerValueDataBlock::Fixed(block) => { + field_metadata.push(ProtobufUtils21::packed_struct_field_fixed( + encoding, + block.bits_per_value, + )); + field_data.push(VariablePackedFieldData::Fixed { block }); + } + PerValueDataBlock::Variable(block) => { + let bits_per_length = block.bits_per_offset as u64; + field_metadata.push(ProtobufUtils21::packed_struct_field_variable( + encoding, + bits_per_length, + )); + field_data.push(VariablePackedFieldData::Variable { + block, + bits_per_length, + }); + } + } + } + + let mut row_data: Vec = Vec::new(); + let mut row_offsets: Vec = Vec::with_capacity(num_values as usize + 1); + row_offsets.push(0); + let mut total_bytes: usize = 0; + let mut max_row_len: usize = 0; + for row in 0..num_values as usize { + let start = row_data.len(); + for field in &field_data { + field.append_row_bytes(row, &mut row_data)?; + } + let end = row_data.len(); + let row_len = end - start; + max_row_len = max_row_len.max(row_len); + total_bytes = total_bytes + .checked_add(row_len) + .ok_or_else(|| Error::invalid_input("Packed struct row data size overflow"))?; + row_offsets.push(end as u64); + } + debug_assert_eq!(total_bytes, row_data.len()); + + let use_u32_offsets = total_bytes <= u32::MAX as usize && max_row_len <= u32::MAX as usize; + let bits_per_offset = if use_u32_offsets { 32 } else { 64 }; + let offsets_buffer = if use_u32_offsets { + let offsets_u32 = row_offsets + .iter() + .map(|&offset| offset as u32) + .collect::>(); + LanceBuffer::reinterpret_vec(offsets_u32) + } else { + LanceBuffer::reinterpret_vec(row_offsets) + }; + + let data_block = VariableWidthBlock { + data: LanceBuffer::from(row_data), + bits_per_offset, + offsets: offsets_buffer, + num_values, + block_info: BlockInfo::new(), + }; + + Ok(( + PerValueDataBlock::Variable(data_block), + ProtobufUtils21::packed_struct_variable(field_metadata), + )) + } +} + +#[derive(Debug)] +pub(crate) enum VariablePackedStructFieldKind { + Fixed { + bits_per_value: u64, + decompressor: Arc, + }, + Variable { + bits_per_length: u64, + decompressor: Arc, + }, +} + +#[derive(Debug)] +pub(crate) struct VariablePackedStructFieldDecoder { + pub(crate) kind: VariablePackedStructFieldKind, +} + +#[derive(Debug)] +pub struct PackedStructVariablePerValueDecompressor { + fields: Vec, +} + +impl PackedStructVariablePerValueDecompressor { + pub(crate) fn new(fields: Vec) -> Self { + Self { fields } + } +} + +enum FieldAccumulator { + Fixed { + builder: DataBlockBuilder, + bits_per_value: u64, + empty_value: DataBlock, + }, + Variable32 { + builder: DataBlockBuilder, + empty_value: DataBlock, + }, + Variable64 { + builder: DataBlockBuilder, + empty_value: DataBlock, + }, +} + +impl FieldAccumulator { + // In full-zip variable packed decoding, rep/def may produce a visible row + // with an empty payload (e.g. null/invalid item). We still need to append + // one placeholder per child so child row counts remain aligned. + fn append_empty(&mut self) -> Result<()> { + match self { + Self::Fixed { + builder, + empty_value, + .. + } => builder.append(empty_value, 0..1), + Self::Variable32 { + builder, + empty_value, + } => builder.append(empty_value, 0..1), + Self::Variable64 { + builder, + empty_value, + } => builder.append(empty_value, 0..1), + } + } +} + +impl VariablePerValueDecompressor for PackedStructVariablePerValueDecompressor { + fn decompress(&self, data: VariableWidthBlock) -> Result { + let num_values = data.num_values; + let offsets_u64 = match data.bits_per_offset { + 32 => data + .offsets + .borrow_to_typed_slice::() + .iter() + .map(|v| *v as u64) + .collect::>(), + 64 => data + .offsets + .borrow_to_typed_slice::() + .as_ref() + .to_vec(), + _ => { + return Err(Error::invalid_input( + "Packed struct row offsets must be 32 or 64 bits", + )); + } + }; + + if offsets_u64.len() != num_values as usize + 1 { + return Err(Error::invalid_input( + "Packed struct row offsets length mismatch", + )); + } + + let mut accumulators = Vec::with_capacity(self.fields.len()); + for field in &self.fields { + match &field.kind { + VariablePackedStructFieldKind::Fixed { bits_per_value, .. } => { + if bits_per_value % 8 != 0 { + return Err(Error::invalid_input( + "Packed struct fixed child must be byte-aligned", + )); + } + let bytes_per_value = bits_per_value.checked_div(8).ok_or_else(|| { + Error::invalid_input("Invalid bits per value for packed struct field") + })?; + let estimate = bytes_per_value.checked_mul(num_values).ok_or_else(|| { + Error::invalid_input("Packed struct fixed child allocation overflow") + })?; + let empty_value = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(vec![0_u8; bytes_per_value as usize]), + bits_per_value: *bits_per_value, + num_values: 1, + block_info: BlockInfo::new(), + }); + accumulators.push(FieldAccumulator::Fixed { + builder: DataBlockBuilder::with_capacity_estimate(estimate), + bits_per_value: *bits_per_value, + empty_value, + }); + } + VariablePackedStructFieldKind::Variable { + bits_per_length, .. + } => match bits_per_length { + 32 => accumulators.push(FieldAccumulator::Variable32 { + builder: DataBlockBuilder::with_capacity_estimate(data.data.len() as u64), + empty_value: DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::empty(), + bits_per_offset: 32, + offsets: LanceBuffer::reinterpret_vec(vec![0_u32, 0_u32]), + num_values: 1, + block_info: BlockInfo::new(), + }), + }), + 64 => accumulators.push(FieldAccumulator::Variable64 { + builder: DataBlockBuilder::with_capacity_estimate(data.data.len() as u64), + empty_value: DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::empty(), + bits_per_offset: 64, + offsets: LanceBuffer::reinterpret_vec(vec![0_u64, 0_u64]), + num_values: 1, + block_info: BlockInfo::new(), + }), + }), + _ => { + return Err(Error::invalid_input( + "Packed struct variable child must use 32 or 64-bit length prefixes", + )); + } + }, + } + } + + for row_idx in 0..num_values as usize { + let row_start = offsets_u64[row_idx] as usize; + let row_end = offsets_u64[row_idx + 1] as usize; + if row_end > data.data.len() || row_start > row_end { + return Err(Error::invalid_input( + "Packed struct row bounds exceed buffer", + )); + } + if row_start == row_end { + for accumulator in accumulators.iter_mut() { + accumulator.append_empty()?; + } + continue; + } + let mut cursor = row_start; + for (field, accumulator) in self.fields.iter().zip(accumulators.iter_mut()) { + match (&field.kind, accumulator) { + ( + VariablePackedStructFieldKind::Fixed { bits_per_value, .. }, + FieldAccumulator::Fixed { + builder, + bits_per_value: acc_bits, + .. + }, + ) => { + debug_assert_eq!(bits_per_value, acc_bits); + let bytes_per_value = (bits_per_value / 8) as usize; + let end = cursor + bytes_per_value; + if end > row_end { + return Err(Error::invalid_input( + "Packed struct fixed child exceeds row bounds", + )); + } + let value_block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::from(data.data[cursor..end].to_vec()), + bits_per_value: *bits_per_value, + num_values: 1, + block_info: BlockInfo::new(), + }); + builder.append(&value_block, 0..1)?; + cursor = end; + } + ( + VariablePackedStructFieldKind::Variable { + bits_per_length, .. + }, + FieldAccumulator::Variable32 { builder, .. }, + ) => { + if *bits_per_length != 32 { + return Err(Error::invalid_input( + "Packed struct length prefix size mismatch", + )); + } + let end = cursor + std::mem::size_of::(); + if end > row_end { + return Err(Error::invalid_input( + "Packed struct variable child length prefix out of bounds", + )); + } + let len = u32::from_le_bytes( + data.data[cursor..end] + .try_into() + .expect("slice has exact length"), + ) as usize; + cursor = end; + let value_end = cursor + len; + if value_end > row_end { + return Err(Error::invalid_input( + "Packed struct variable child exceeds row bounds", + )); + } + let value_block = DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(data.data[cursor..value_end].to_vec()), + bits_per_offset: 32, + offsets: LanceBuffer::reinterpret_vec(vec![0_u32, len as u32]), + num_values: 1, + block_info: BlockInfo::new(), + }); + builder.append(&value_block, 0..1)?; + cursor = value_end; + } + ( + VariablePackedStructFieldKind::Variable { + bits_per_length, .. + }, + FieldAccumulator::Variable64 { builder, .. }, + ) => { + if *bits_per_length != 64 { + return Err(Error::invalid_input( + "Packed struct length prefix size mismatch", + )); + } + let end = cursor + std::mem::size_of::(); + if end > row_end { + return Err(Error::invalid_input( + "Packed struct variable child length prefix out of bounds", + )); + } + let len = u64::from_le_bytes( + data.data[cursor..end] + .try_into() + .expect("slice has exact length"), + ) as usize; + cursor = end; + let value_end = cursor + len; + if value_end > row_end { + return Err(Error::invalid_input( + "Packed struct variable child exceeds row bounds", + )); + } + let value_block = DataBlock::VariableWidth(VariableWidthBlock { + data: LanceBuffer::from(data.data[cursor..value_end].to_vec()), + bits_per_offset: 64, + offsets: LanceBuffer::reinterpret_vec(vec![0_u64, len as u64]), + num_values: 1, + block_info: BlockInfo::new(), + }); + builder.append(&value_block, 0..1)?; + cursor = value_end; + } + _ => { + return Err(Error::invalid_input( + "Packed struct accumulator kind mismatch", + )); + } + } + } + if cursor != row_end { + return Err(Error::invalid_input( + "Packed struct row parsing did not consume full row", + )); + } + } + + let mut children = Vec::with_capacity(self.fields.len()); + for (field, accumulator) in self.fields.iter().zip(accumulators) { + match (field, accumulator) { + ( + VariablePackedStructFieldDecoder { + kind: VariablePackedStructFieldKind::Fixed { decompressor, .. }, + }, + FieldAccumulator::Fixed { builder, .. }, + ) => { + let DataBlock::FixedWidth(block) = builder.finish() else { + panic!("Expected fixed-width datablock from builder"); + }; + let decoded = decompressor.decompress(block, num_values)?; + children.push(decoded); + } + ( + VariablePackedStructFieldDecoder { + kind: + VariablePackedStructFieldKind::Variable { + bits_per_length, + decompressor, + }, + }, + FieldAccumulator::Variable32 { builder, .. }, + ) => { + let DataBlock::VariableWidth(mut block) = builder.finish() else { + panic!("Expected variable-width datablock from builder"); + }; + debug_assert_eq!(block.bits_per_offset, 32); + block.bits_per_offset = (*bits_per_length) as u8; + let decoded = decompressor.decompress(block)?; + children.push(decoded); + } + ( + VariablePackedStructFieldDecoder { + kind: + VariablePackedStructFieldKind::Variable { + bits_per_length, + decompressor, + }, + }, + FieldAccumulator::Variable64 { builder, .. }, + ) => { + let DataBlock::VariableWidth(mut block) = builder.finish() else { + panic!("Expected variable-width datablock from builder"); + }; + debug_assert_eq!(block.bits_per_offset, 64); + block.bits_per_offset = (*bits_per_length) as u8; + let decoded = decompressor.decompress(block)?; + children.push(decoded); + } + _ => { + return Err(Error::invalid_input( + "Packed struct accumulator mismatch during finalize", + )); + } + } + } + + Ok(DataBlock::Struct(StructDataBlock { + children, + block_info: BlockInfo::new(), + validity: None, + })) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + compression::DefaultDecompressionStrategy, + compression_config::CompressionParams, + constants::PACKED_STRUCT_META_KEY, + statistics::ComputeStat, + testing::{ + TestCases, TestEncoding, check_round_trip_encoding_of_data, test_compression_strategy, + }, + }; + use arrow_array::{ + Array, ArrayRef, BinaryArray, Int32Array, Int64Array, LargeStringArray, StringArray, + StructArray, UInt32Array, + }; + use arrow_schema::{DataType, Field as ArrowField, Fields}; + use std::collections::HashMap; + use std::sync::Arc; + + fn fixed_block_from_array(array: Int64Array) -> FixedWidthDataBlock { + let num_values = array.len() as u64; + let block = DataBlock::from_arrays(&[Arc::new(array) as ArrayRef], num_values); + match block { + DataBlock::FixedWidth(block) => block, + _ => panic!("Expected fixed-width data block"), + } + } + + fn fixed_i32_block_from_array(array: Int32Array) -> FixedWidthDataBlock { + let num_values = array.len() as u64; + let block = DataBlock::from_arrays(&[Arc::new(array) as ArrayRef], num_values); + match block { + DataBlock::FixedWidth(block) => block, + _ => panic!("Expected fixed-width data block"), + } + } + + fn variable_block_from_string_array(array: StringArray) -> VariableWidthBlock { + let num_values = array.len() as u64; + let block = DataBlock::from_arrays(&[Arc::new(array) as ArrayRef], num_values); + match block { + DataBlock::VariableWidth(block) => block, + _ => panic!("Expected variable-width block"), + } + } + + fn variable_block_from_large_string_array(array: LargeStringArray) -> VariableWidthBlock { + let num_values = array.len() as u64; + let block = DataBlock::from_arrays(&[Arc::new(array) as ArrayRef], num_values); + match block { + DataBlock::VariableWidth(block) => block, + _ => panic!("Expected variable-width block"), + } + } + + fn variable_block_from_binary_array(array: BinaryArray) -> VariableWidthBlock { + let num_values = array.len() as u64; + let block = DataBlock::from_arrays(&[Arc::new(array) as ArrayRef], num_values); + match block { + DataBlock::VariableWidth(block) => block, + _ => panic!("Expected variable-width block"), + } + } + + #[test] + fn variable_packed_struct_round_trip() -> Result<()> { + let arrow_fields: Fields = vec![ + ArrowField::new("id", DataType::UInt32, false), + ArrowField::new("name", DataType::Utf8, true), + ] + .into(); + let arrow_struct = ArrowField::new("item", DataType::Struct(arrow_fields), false); + let struct_field = Field::try_from(&arrow_struct)?; + + let ids = vec![1_u32, 2, 42]; + let id_bytes = ids + .iter() + .flat_map(|value| value.to_le_bytes()) + .collect::>(); + let mut id_block = FixedWidthDataBlock { + data: LanceBuffer::reinterpret_vec(ids), + bits_per_value: 32, + num_values: 3, + block_info: BlockInfo::new(), + }; + id_block.compute_stat(); + let id_block = DataBlock::FixedWidth(id_block); + + let name_offsets = vec![0_i32, 1, 4, 4]; + let name_bytes = b"abcz".to_vec(); + let mut name_block = VariableWidthBlock { + data: LanceBuffer::from(name_bytes.clone()), + bits_per_offset: 32, + offsets: LanceBuffer::reinterpret_vec(name_offsets.clone()), + num_values: 3, + block_info: BlockInfo::new(), + }; + name_block.compute_stat(); + let name_block = DataBlock::VariableWidth(name_block); + + let struct_block = StructDataBlock { + children: vec![id_block, name_block], + block_info: BlockInfo::new(), + validity: None, + }; + + let data_block = DataBlock::Struct(struct_block); + + let compression_strategy = + test_compression_strategy(TestEncoding::StructuralU32, CompressionParams::default()); + let compressor = crate::compression::CompressionStrategy::create_per_value( + compression_strategy.as_ref(), + &struct_field, + &data_block, + )?; + let (compressed, encoding) = compressor.compress(data_block)?; + + let PerValueDataBlock::Variable(zipped) = compressed else { + panic!("expected variable-width packed struct output"); + }; + + let decompression_strategy = DefaultDecompressionStrategy::default(); + let decompressor = + crate::compression::DecompressionStrategy::create_variable_per_value_decompressor( + &decompression_strategy, + &encoding, + )?; + let decoded = decompressor.decompress(zipped)?; + + let DataBlock::Struct(decoded_struct) = decoded else { + panic!("expected struct datablock after decode"); + }; + + let decoded_id = decoded_struct.children[0].as_fixed_width_ref().unwrap(); + assert_eq!(decoded_id.bits_per_value, 32); + assert_eq!(decoded_id.data.as_ref(), id_bytes.as_slice()); + + let decoded_name = decoded_struct.children[1].as_variable_width_ref().unwrap(); + assert_eq!(decoded_name.bits_per_offset, 32); + let decoded_offsets = decoded_name.offsets.borrow_to_typed_slice::(); + assert_eq!(decoded_offsets.as_ref(), name_offsets.as_slice()); + assert_eq!(decoded_name.data.as_ref(), name_bytes.as_slice()); + + Ok(()) + } + + #[test] + fn variable_packed_struct_large_utf8_round_trip() -> Result<()> { + let arrow_fields: Fields = vec![ + ArrowField::new("value", DataType::Int64, false), + ArrowField::new("text", DataType::LargeUtf8, false), + ] + .into(); + let arrow_struct = ArrowField::new("item", DataType::Struct(arrow_fields), false); + let struct_field = Field::try_from(&arrow_struct)?; + + let id_block = fixed_block_from_array(Int64Array::from(vec![10, 20, 30, 40])); + let payload_array = LargeStringArray::from(vec![ + "alpha", + "a considerably longer payload for testing", + "mid", + "z", + ]); + let payload_block = variable_block_from_large_string_array(payload_array); + + let struct_block = StructDataBlock { + children: vec![ + DataBlock::FixedWidth(id_block.clone()), + DataBlock::VariableWidth(payload_block.clone()), + ], + block_info: BlockInfo::new(), + validity: None, + }; + + let data_block = DataBlock::Struct(struct_block); + + let compression_strategy = + test_compression_strategy(TestEncoding::StructuralU32, CompressionParams::default()); + let compressor = crate::compression::CompressionStrategy::create_per_value( + compression_strategy.as_ref(), + &struct_field, + &data_block, + )?; + let (compressed, encoding) = compressor.compress(data_block)?; + + let PerValueDataBlock::Variable(zipped) = compressed else { + panic!("expected variable-width packed struct output"); + }; + + let decompression_strategy = DefaultDecompressionStrategy::default(); + let decompressor = + crate::compression::DecompressionStrategy::create_variable_per_value_decompressor( + &decompression_strategy, + &encoding, + )?; + let decoded = decompressor.decompress(zipped)?; + + let DataBlock::Struct(decoded_struct) = decoded else { + panic!("expected struct datablock after decode"); + }; + + let decoded_id = decoded_struct.children[0].as_fixed_width_ref().unwrap(); + assert_eq!(decoded_id.bits_per_value, 64); + assert_eq!(decoded_id.data.as_ref(), id_block.data.as_ref()); + + let decoded_payload = decoded_struct.children[1].as_variable_width_ref().unwrap(); + assert_eq!(decoded_payload.bits_per_offset, 64); + assert_eq!( + decoded_payload + .offsets + .borrow_to_typed_slice::() + .as_ref(), + payload_block + .offsets + .borrow_to_typed_slice::() + .as_ref() + ); + assert_eq!(decoded_payload.data.as_ref(), payload_block.data.as_ref()); + + Ok(()) + } + + #[tokio::test] + async fn variable_packed_struct_utf8_round_trip() { + // schema: Struct + let fields = Fields::from(vec![ + Arc::new(ArrowField::new("id", DataType::UInt32, false)), + Arc::new(ArrowField::new("uri", DataType::Utf8, false)), + Arc::new(ArrowField::new("long_text", DataType::LargeUtf8, false)), + ]); + + // mark struct as packed + let mut meta = HashMap::new(); + meta.insert(PACKED_STRUCT_META_KEY.to_string(), "true".to_string()); + + let array = Arc::new(StructArray::from(vec![ + ( + fields[0].clone(), + Arc::new(UInt32Array::from(vec![1, 2, 3])) as ArrayRef, + ), + ( + fields[1].clone(), + Arc::new(StringArray::from(vec![ + Some("a"), + Some("b"), + Some("/tmp/x"), + ])) as ArrayRef, + ), + ( + fields[2].clone(), + Arc::new(LargeStringArray::from(vec![ + Some("alpha"), + Some("a considerably longer payload for testing"), + Some("mid"), + ])) as ArrayRef, + ), + ])); + + let test_cases = TestCases::default() + .with_u32_structural_encodings() + .with_expected_encoding("variable_packed_struct"); + + check_round_trip_encoding_of_data(vec![array], &test_cases, meta).await; + } + + #[test] + fn variable_packed_struct_multi_variable_round_trip() -> Result<()> { + let arrow_fields: Fields = vec![ + ArrowField::new("category", DataType::Utf8, false), + ArrowField::new("payload", DataType::Binary, false), + ArrowField::new("count", DataType::Int32, false), + ] + .into(); + let arrow_struct = ArrowField::new("item", DataType::Struct(arrow_fields), false); + let struct_field = Field::try_from(&arrow_struct)?; + + let category_array = StringArray::from(vec!["red", "blue", "green", "red"]); + let category_block = variable_block_from_string_array(category_array); + let payload_values: Vec> = + vec![vec![0x01, 0x02], vec![], vec![0x05, 0x06, 0x07], vec![0xff]]; + let payload_array = + BinaryArray::from_iter_values(payload_values.iter().map(|v| v.as_slice())); + let payload_block = variable_block_from_binary_array(payload_array); + let count_block = fixed_i32_block_from_array(Int32Array::from(vec![1, 2, 3, 4])); + + let struct_block = StructDataBlock { + children: vec![ + DataBlock::VariableWidth(category_block.clone()), + DataBlock::VariableWidth(payload_block.clone()), + DataBlock::FixedWidth(count_block.clone()), + ], + block_info: BlockInfo::new(), + validity: None, + }; + + let data_block = DataBlock::Struct(struct_block); + + let compression_strategy = + test_compression_strategy(TestEncoding::StructuralU32, CompressionParams::default()); + let compressor = crate::compression::CompressionStrategy::create_per_value( + compression_strategy.as_ref(), + &struct_field, + &data_block, + )?; + let (compressed, encoding) = compressor.compress(data_block)?; + + let PerValueDataBlock::Variable(zipped) = compressed else { + panic!("expected variable-width packed struct output"); + }; + + let decompression_strategy = DefaultDecompressionStrategy::default(); + let decompressor = + crate::compression::DecompressionStrategy::create_variable_per_value_decompressor( + &decompression_strategy, + &encoding, + )?; + let decoded = decompressor.decompress(zipped)?; + + let DataBlock::Struct(decoded_struct) = decoded else { + panic!("expected struct datablock after decode"); + }; + + let decoded_category = decoded_struct.children[0].as_variable_width_ref().unwrap(); + assert_eq!(decoded_category.bits_per_offset, 32); + assert_eq!( + decoded_category + .offsets + .borrow_to_typed_slice::() + .as_ref(), + category_block + .offsets + .borrow_to_typed_slice::() + .as_ref() + ); + assert_eq!(decoded_category.data.as_ref(), category_block.data.as_ref()); + + let decoded_payload = decoded_struct.children[1].as_variable_width_ref().unwrap(); + assert_eq!(decoded_payload.bits_per_offset, 32); + assert_eq!( + decoded_payload + .offsets + .borrow_to_typed_slice::() + .as_ref(), + payload_block + .offsets + .borrow_to_typed_slice::() + .as_ref() + ); + assert_eq!(decoded_payload.data.as_ref(), payload_block.data.as_ref()); + + let decoded_count = decoded_struct.children[2].as_fixed_width_ref().unwrap(); + assert_eq!(decoded_count.bits_per_value, 32); + assert_eq!(decoded_count.data.as_ref(), count_block.data.as_ref()); + + Ok(()) + } + + #[test] + fn variable_packed_struct_requires_v22() { + let arrow_fields: Fields = vec![ + ArrowField::new("value", DataType::Int64, false), + ArrowField::new("text", DataType::Utf8, false), + ] + .into(); + let arrow_struct = ArrowField::new("item", DataType::Struct(arrow_fields), false); + let struct_field = Field::try_from(&arrow_struct).unwrap(); + + let value_block = fixed_block_from_array(Int64Array::from(vec![1, 2, 3])); + let text_block = + variable_block_from_string_array(StringArray::from(vec!["a", "bb", "ccc"])); + + let struct_block = StructDataBlock { + children: vec![ + DataBlock::FixedWidth(value_block), + DataBlock::VariableWidth(text_block), + ], + block_info: BlockInfo::new(), + validity: None, + }; + + let compression_strategy = + test_compression_strategy(TestEncoding::StructuralU16, CompressionParams::default()); + let result = + compression_strategy.create_per_value(&struct_field, &DataBlock::Struct(struct_block)); + + assert!(matches!(result, Err(Error::NotSupported { .. }))); + } + + #[test] + fn variable_packed_struct_decompress_empty_row() -> Result<()> { + let strategy = DefaultDecompressionStrategy::default(); + let fixed_decompressor = Arc::from( + crate::compression::DecompressionStrategy::create_fixed_per_value_decompressor( + &strategy, + &ProtobufUtils21::flat(32, None), + )?, + ); + let variable_decompressor = Arc::from( + crate::compression::DecompressionStrategy::create_variable_per_value_decompressor( + &strategy, + &ProtobufUtils21::variable(ProtobufUtils21::flat(32, None), None), + )?, + ); + + let decompressor = PackedStructVariablePerValueDecompressor::new(vec![ + VariablePackedStructFieldDecoder { + kind: VariablePackedStructFieldKind::Fixed { + bits_per_value: 32, + decompressor: fixed_decompressor, + }, + }, + VariablePackedStructFieldDecoder { + kind: VariablePackedStructFieldKind::Variable { + bits_per_length: 32, + decompressor: variable_decompressor, + }, + }, + ]); + + let mut row_data = Vec::new(); + row_data.extend_from_slice(&1_u32.to_le_bytes()); + row_data.extend_from_slice(&1_u32.to_le_bytes()); + row_data.extend_from_slice(b"a"); + row_data.extend_from_slice(&2_u32.to_le_bytes()); + row_data.extend_from_slice(&0_u32.to_le_bytes()); + + let input = VariableWidthBlock { + data: LanceBuffer::from(row_data), + bits_per_offset: 32, + offsets: LanceBuffer::reinterpret_vec(vec![0_u32, 9_u32, 9_u32, 17_u32]), + num_values: 3, + block_info: BlockInfo::new(), + }; + + let decoded = decompressor.decompress(input)?; + let DataBlock::Struct(decoded_struct) = decoded else { + panic!("expected struct output"); + }; + + let fixed = decoded_struct.children[0].as_fixed_width_ref().unwrap(); + assert_eq!(fixed.bits_per_value, 32); + assert_eq!( + fixed.data.borrow_to_typed_slice::().as_ref(), + &[1, 0, 2] + ); + + let variable = decoded_struct.children[1].as_variable_width_ref().unwrap(); + assert_eq!(variable.bits_per_offset, 32); + assert_eq!( + variable.offsets.borrow_to_typed_slice::().as_ref(), + &[0_u32, 1_u32, 1_u32, 1_u32] + ); + assert_eq!(variable.data.as_ref(), b"a"); + + Ok(()) + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/rle.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/rle.rs new file mode 100644 index 000000000..0f3e330a3 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/rle.rs @@ -0,0 +1,3250 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! # RLE (Run-Length Encoding) +//! +//! RLE compression for Lance, optimized for data with repeated values. +//! +//! ## Encoding Format +//! +//! RLE uses a dual-buffer format to store compressed data: +//! +//! - **Values Buffer**: Stores unique values in their original data type +//! - **Lengths Buffer**: Stores the repeat count for each value as u8, u16, or u32 +//! +//! ### Example +//! +//! Input data: `[1, 1, 1, 2, 2, 3, 3, 3, 3]` +//! +//! Encoded as: +//! - Values buffer: `[1, 2, 3]` (3 × 4 bytes for i32) +//! - Lengths buffer: `[3, 2, 4]` (3 × 1 byte for u8 in compatibility mode) +//! +//! ### Long Run Handling +//! +//! In compatibility mode, when a run exceeds 255 values, it is split into multiple +//! runs of 255 followed by a final run with the remainder. RLE v2 can use u16 or +//! u32 run lengths to reduce this splitting. +//! +//! ## Supported Types +//! +//! RLE supports all fixed-width primitive types: +//! - 8-bit: u8, i8 +//! - 16-bit: u16, i16 +//! - 32-bit: u32, i32, f32 +//! - 64-bit: u64, i64, f64 +//! +//! ## Compression Strategy +//! +//! RLE is automatically selected when: +//! - The run count (number of value transitions) < 50% of total values +//! - This indicates sufficient repetition for RLE to be effective +//! +//! ## MiniBlock Chunk Handling +//! +//! When used in the miniblock path, all chunks share two global buffers (values and lengths). +//! Each chunk's `buffer_sizes` identifies its slice within those global buffers. Non-last chunks +//! contain a power-of-2 number of values. +//! +//! NOTE: The current encoder uses a 2048-value cap per chunk as a workaround for +//! . +//! +//! ## Block Format +//! +//! When used in the block compression path, the encoded output is a single buffer: +//! `[8-byte header: values buffer size][values buffer][run_lengths buffer]`. + +use arrow_buffer::{ArrowNativeType, ScalarBuffer}; +use log::trace; + +use crate::buffer::LanceBuffer; +use crate::compression::{BlockCompressor, BlockDecompressor, MiniBlockDecompressor}; +use crate::data::DataBlock; +use crate::data::{BlockInfo, FixedWidthDataBlock}; +use crate::encodings::logical::primitive::miniblock::{ + MAX_MINIBLOCK_BYTES, MAX_MINIBLOCK_VALUES, MiniBlockChunk, MiniBlockCompressed, + MiniBlockCompressionContext, MiniBlockCompressor, +}; +use crate::encodings::physical::block::{CompressionConfig, GeneralBufferCompressor}; +use crate::format::ProtobufUtils21; +use crate::format::pb21::CompressiveEncoding; + +use lance_core::{Error, Result}; + +/// Width used to encode RLE run lengths. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum RunLengthWidth { + /// Compatibility mode. Runs longer than 255 values are split. + U8, + /// RLE v2 mode for runs up to 65,535 values per entry. + U16, + /// RLE v2 mode for runs up to 4,294,967,295 values per entry. + U32, +} + +impl RunLengthWidth { + pub(crate) fn from_bits(bits_per_value: u64) -> Option { + match bits_per_value { + 8 => Some(Self::U8), + 16 => Some(Self::U16), + 32 => Some(Self::U32), + _ => None, + } + } + + pub(crate) fn bits_per_value(self) -> u64 { + match self { + Self::U8 => 8, + Self::U16 => 16, + Self::U32 => 32, + } + } + + fn bytes_per_value(self) -> usize { + match self { + Self::U8 => 1, + Self::U16 => 2, + Self::U32 => 4, + } + } + + fn max_run_length(self) -> u64 { + match self { + Self::U8 => u8::MAX as u64, + Self::U16 => u16::MAX as u64, + Self::U32 => u32::MAX as u64, + } + } + + fn write_length(self, length: u64, dst: &mut Vec) { + match self { + Self::U8 => dst.push(length as u8), + Self::U16 => dst.extend_from_slice(&(length as u16).to_le_bytes()), + Self::U32 => dst.extend_from_slice(&(length as u32).to_le_bytes()), + } + } + + fn read_length(self, bytes: &[u8]) -> u64 { + match self { + Self::U8 => bytes[0] as u64, + Self::U16 => u16::from_le_bytes([bytes[0], bytes[1]]) as u64, + Self::U32 => u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as u64, + } + } +} + +const RUN_LENGTH_WIDTHS: [RunLengthWidth; 3] = + [RunLengthWidth::U8, RunLengthWidth::U16, RunLengthWidth::U32]; + +/// Select the lowest-cost run length width from precomputed entry counts. +pub(crate) fn select_run_length_width_from_entries( + entries: &[u64], + bits_per_value: u64, +) -> Result<(RunLengthWidth, u128)> { + if entries.len() != RUN_LENGTH_WIDTHS.len() { + return Err(Error::invalid_input_source( + format!( + "RLE run length entry statistics must have {} values, got {}", + RUN_LENGTH_WIDTHS.len(), + entries.len() + ) + .into(), + )); + } + + if !matches!(bits_per_value, 8 | 16 | 32 | 64) { + return Err(Error::invalid_input_source( + format!("RLE encoding bits_per_value must be 8, 16, 32, or 64, got {bits_per_value}") + .into(), + )); + } + + let mut best_width = RUN_LENGTH_WIDTHS[0]; + let mut best_cost = rle_encoded_size_from_entries(entries[0], bits_per_value, best_width); + for (&width, &entry_count) in RUN_LENGTH_WIDTHS.iter().zip(entries.iter()).skip(1) { + let cost = rle_encoded_size_from_entries(entry_count, bits_per_value, width); + if cost < best_cost { + best_width = width; + best_cost = cost; + } + } + + Ok((best_width, best_cost)) +} + +pub(crate) fn rle_encoded_size_from_entries( + entry_count: u64, + bits_per_value: u64, + run_length_width: RunLengthWidth, +) -> u128 { + let bytes_per_value = (bits_per_value / 8) as u128; + let bytes_per_length = run_length_width.bytes_per_value() as u128; + (entry_count as u128) * (bytes_per_value + bytes_per_length) +} + +pub(crate) fn run_length_width_index(run_length_width: RunLengthWidth) -> usize { + match run_length_width { + RunLengthWidth::U8 => 0, + RunLengthWidth::U16 => 1, + RunLengthWidth::U32 => 2, + } +} + +pub(crate) fn select_run_length_width( + data: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + max_segment_values: Option, +) -> Result<(RunLengthWidth, u128)> { + let entries = collect_run_length_entries(data, num_values, bits_per_value, max_segment_values)?; + select_run_length_width_from_entries(&entries, bits_per_value) +} + +pub(crate) fn rle_encoded_size( + data: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + max_segment_values: Option, + run_length_width: RunLengthWidth, +) -> Result { + let entries = collect_run_length_entries(data, num_values, bits_per_value, max_segment_values)?; + let width_idx = run_length_width_index(run_length_width); + Ok(rle_encoded_size_from_entries( + entries[width_idx], + bits_per_value, + run_length_width, + )) +} + +fn collect_run_length_entries( + data: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + max_segment_values: Option, +) -> Result<[u64; 3]> { + let num_values = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("RLE num_values does not fit in usize: {num_values}").into(), + ) + })?; + + macro_rules! collect_entries { + ($ty:ty) => {{ + let type_size = std::mem::size_of::<$ty>(); + let expected_bytes = num_values.checked_mul(type_size).ok_or_else(|| { + Error::invalid_input_source( + format!( + "RLE input byte length overflow: {num_values} values of {type_size} bytes" + ) + .into(), + ) + })?; + if data.len() != expected_bytes { + return Err(Error::invalid_input_source( + format!( + "RLE input data size mismatch: {} bytes for {} values of {} bytes", + data.len(), + num_values, + type_size + ) + .into(), + )); + } + let values = data.borrow_to_typed_slice::<$ty>(); + let values = values.get(..num_values).ok_or_else(|| { + Error::invalid_input_source( + format!( + "RLE data has {} values but {} were expected", + values.len(), + num_values + ) + .into(), + ) + })?; + Ok(collect_run_length_entries_from_slice( + values, + max_segment_values, + )) + }}; + } + + match bits_per_value { + 8 => collect_entries!(u8), + 16 => collect_entries!(u16), + 32 => collect_entries!(u32), + 64 => collect_entries!(u64), + _ => Err(Error::invalid_input_source( + format!("RLE encoding bits_per_value must be 8, 16, 32, or 64, got {bits_per_value}") + .into(), + )), + } +} + +fn collect_run_length_entries_from_slice( + values: &[T], + max_segment_values: Option, +) -> [u64; 3] { + if values.is_empty() { + return [0; 3]; + } + + let mut entries = [0u64; 3]; + let mut prev = values[0]; + let mut current_length = 1u64; + + for &value in &values[1..] { + if value != prev { + accumulate_run_length_entries(current_length, max_segment_values, &mut entries); + prev = value; + current_length = 1; + } else { + current_length += 1; + } + } + accumulate_run_length_entries(current_length, max_segment_values, &mut entries); + + entries +} + +pub(crate) fn accumulate_run_length_entries( + run_length: u64, + max_segment_values: Option, + entries: &mut [u64; 3], +) { + let max_segment_values = max_segment_values.unwrap_or(run_length).max(1); + let mut remaining = run_length; + while remaining > 0 { + let segment = remaining.min(max_segment_values); + for (idx, width) in RUN_LENGTH_WIDTHS.iter().enumerate() { + let entry_count = segment.div_ceil(width.max_run_length()); + entries[idx] = entries[idx].saturating_add(entry_count); + } + remaining -= segment; + } +} + +/// RLE encoder for miniblock format +#[derive(Debug)] +pub struct RleEncoder { + run_length_width: RunLengthWidth, + values_compression: Option, + run_lengths_compression: Option, + use_child_bitpacking: bool, +} + +#[derive(Clone)] +struct RleChildCandidate { + encoding: CompressiveEncoding, + data: LanceBuffer, + chunk_sizes: Vec, + size: usize, + requires_num_values: bool, +} + +impl Default for RleEncoder { + fn default() -> Self { + Self::new() + } +} + +impl RleEncoder { + pub fn new() -> Self { + Self { + run_length_width: RunLengthWidth::U8, + values_compression: None, + run_lengths_compression: None, + use_child_bitpacking: false, + } + } + + pub(crate) fn with_run_length_width(run_length_width: RunLengthWidth) -> Self { + Self { + run_length_width, + values_compression: None, + run_lengths_compression: None, + use_child_bitpacking: false, + } + } + + pub(crate) fn with_child_encoding( + run_length_width: RunLengthWidth, + values_compression: Option, + run_lengths_compression: Option, + use_child_bitpacking: bool, + ) -> Self { + Self { + run_length_width, + values_compression, + run_lengths_compression, + use_child_bitpacking, + } + } + + fn encode_data( + &self, + data: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + ) -> Result<(Vec, Vec)> { + if num_values == 0 { + return Ok((Vec::new(), Vec::new())); + } + + let num_values = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("RLE num_values does not fit in usize: {num_values}").into(), + ) + })?; + let bytes_per_value = (bits_per_value / 8) as usize; + let bytes_per_length = self.run_length_width.bytes_per_value(); + + // Pre-allocate global buffers with estimated capacity + // Assume average compression ratio of ~10:1 (10 values per run) + let estimated_runs = num_values / 10; + let mut all_values = Vec::with_capacity(estimated_runs * bytes_per_value); + let mut all_lengths = Vec::with_capacity(estimated_runs * bytes_per_length); + let mut chunks = Vec::new(); + + let mut offset = 0usize; + let mut values_remaining = num_values; + + while values_remaining > 0 { + let values_start = all_values.len(); + let lengths_start = all_lengths.len(); + + let (_num_runs, values_processed, is_last_chunk) = match bits_per_value { + 8 => self.encode_chunk_rolling::( + data, + offset, + values_remaining, + &mut all_values, + &mut all_lengths, + ), + 16 => self.encode_chunk_rolling::( + data, + offset, + values_remaining, + &mut all_values, + &mut all_lengths, + ), + 32 => self.encode_chunk_rolling::( + data, + offset, + values_remaining, + &mut all_values, + &mut all_lengths, + ), + 64 => self.encode_chunk_rolling::( + data, + offset, + values_remaining, + &mut all_values, + &mut all_lengths, + ), + _ => { + return Err(Error::invalid_input_source( + format!( + "RLE encoding bits_per_value must be 8, 16, 32, or 64, got {bits_per_value}" + ) + .into(), + )); + } + }; + + if values_processed == 0 { + // A non-final chunk needs at least two values because log_num_values == 0 + // identifies the final chunk. Report an error instead of returning partial data. + return Err(Error::internal(format!( + "RLE encoder made no progress: values_remaining={values_remaining}, \ + offset={offset}, data_len={}, bits_per_value={bits_per_value}, \ + max_miniblock_values={}", + data.len(), + *MAX_MINIBLOCK_VALUES + ))); + } + + let log_num_values = if is_last_chunk { + 0 + } else { + assert!( + values_processed.is_power_of_two(), + "Non-last chunk must have power-of-2 values" + ); + values_processed.ilog2() as u8 + }; + + let values_size = all_values.len() - values_start; + let lengths_size = all_lengths.len() - lengths_start; + + let chunk = MiniBlockChunk { + buffer_sizes: vec![values_size as u32, lengths_size as u32], + log_num_values, + }; + + chunks.push(chunk); + + offset += values_processed; + values_remaining -= values_processed; + } + + // Return exactly two buffers: values and lengths + Ok(( + vec![ + LanceBuffer::from(all_values), + LanceBuffer::from(all_lengths), + ], + chunks, + )) + } + + fn encode_block_data( + &self, + data: &LanceBuffer, + num_values: u64, + bits_per_value: u64, + ) -> Result> { + match bits_per_value { + 8 => self.encode_block_data_generic::(data, num_values), + 16 => self.encode_block_data_generic::(data, num_values), + 32 => self.encode_block_data_generic::(data, num_values), + 64 => self.encode_block_data_generic::(data, num_values), + _ => Err(Error::invalid_input_source( + format!( + "RLE encoding bits_per_value must be 8, 16, 32, or 64, got {bits_per_value}" + ) + .into(), + )), + } + } + + fn encode_block_data_generic( + &self, + data: &LanceBuffer, + num_values: u64, + ) -> Result> + where + T: bytemuck::Pod + PartialEq + Copy + ArrowNativeType, + { + let num_values = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("RLE num_values does not fit in usize: {num_values}").into(), + ) + })?; + let type_size = std::mem::size_of::(); + let expected_bytes = num_values.checked_mul(type_size).ok_or_else(|| { + Error::invalid_input_source( + format!("RLE input byte length overflow: {num_values} values of {type_size} bytes") + .into(), + ) + })?; + if data.len() != expected_bytes { + return Err(Error::invalid_input_source( + format!( + "RLE input data size mismatch: {} bytes for {} values of {} bytes", + data.len(), + num_values, + type_size + ) + .into(), + )); + } + if num_values == 0 { + return Ok(vec![LanceBuffer::empty(), LanceBuffer::empty()]); + } + + let values_ref = data.borrow_to_typed_slice::(); + let values = values_ref.as_ref(); + let estimated_runs = num_values / 10; + let mut all_values = Vec::with_capacity(estimated_runs * type_size); + let mut all_lengths = + Vec::with_capacity(estimated_runs * self.run_length_width.bytes_per_value()); + self.encode_values(values, &mut all_values, &mut all_lengths); + Ok(vec![ + LanceBuffer::from(all_values), + LanceBuffer::from(all_lengths), + ]) + } + + /// Encodes the largest valid mini-block prefix from `offset`. + fn encode_chunk_rolling( + &self, + data: &LanceBuffer, + offset: usize, + values_remaining: usize, + all_values: &mut Vec, + all_lengths: &mut Vec, + ) -> (usize, usize, bool) + where + T: bytemuck::Pod + PartialEq + Copy + std::fmt::Debug + ArrowNativeType, + { + let type_size = std::mem::size_of::(); + let chunk_start = offset * type_size; + let max_by_count = *MAX_MINIBLOCK_VALUES as usize; + let max_values = values_remaining.min(max_by_count); + let chunk_end = chunk_start + max_values * type_size; + + if chunk_start >= data.len() { + return (0, 0, false); + } + + let chunk_len = chunk_end.min(data.len()) - chunk_start; + let chunk_buffer = data.slice_with_length(chunk_start, chunk_len); + let typed_data_ref = chunk_buffer.borrow_to_typed_slice::(); + let typed_data: &[T] = typed_data_ref.as_ref(); + let max_values = max_values.min(typed_data.len()); + + if typed_data.is_empty() { + return (0, 0, false); + } + + let values_start = all_values.len(); + let all_remaining_values_fit = values_remaining <= max_by_count; + let encoded_size = self.encoded_size(&typed_data[..max_values]); + let (values_to_encode, is_last_chunk) = if all_remaining_values_fit + && encoded_size <= MAX_MINIBLOCK_BYTES as usize + { + (max_values, true) + } else if let Some(values_to_encode) = self.largest_power_of_two_prefix::(typed_data) { + (values_to_encode, false) + } else { + return (0, 0, false); + }; + + self.encode_values(&typed_data[..values_to_encode], all_values, all_lengths); + + let num_runs = (all_values.len() - values_start) / type_size; + (num_runs, values_to_encode, is_last_chunk) + } + + fn largest_power_of_two_prefix(&self, values: &[T]) -> Option + where + T: bytemuck::Pod + PartialEq + Copy, + { + let max_prefix = values.len().min(*MAX_MINIBLOCK_VALUES as usize); + let mut prefix = 1usize << max_prefix.ilog2(); + while prefix > 1 { + if self.encoded_size(&values[..prefix]) <= MAX_MINIBLOCK_BYTES as usize { + return Some(prefix); + } + prefix >>= 1; + } + None + } + + fn encoded_size(&self, values: &[T]) -> usize + where + T: bytemuck::Pod + PartialEq + Copy, + { + if values.is_empty() { + return 0; + } + + let mut current_value = values[0]; + let mut current_length = 1u64; + let mut encoded_size = 0usize; + + for &value in values.iter().skip(1) { + if value == current_value { + current_length += 1; + } else { + encoded_size += self.run_size::(current_length); + current_value = value; + current_length = 1; + } + } + encoded_size += self.run_size::(current_length); + encoded_size + } + + fn run_size(&self, length: u64) -> usize + where + T: bytemuck::Pod, + { + let type_size = std::mem::size_of::(); + let run_chunks = length.div_ceil(self.run_length_width.max_run_length()) as usize; + run_chunks * (type_size + self.run_length_width.bytes_per_value()) + } + + fn encode_values(&self, values: &[T], all_values: &mut Vec, all_lengths: &mut Vec) + where + T: bytemuck::Pod + PartialEq + Copy, + { + if values.is_empty() { + return; + } + + let mut current_value = values[0]; + let mut current_length = 1u64; + + for &value in values.iter().skip(1) { + if value == current_value { + current_length += 1; + } else { + self.add_run(¤t_value, current_length, all_values, all_lengths); + current_value = value; + current_length = 1; + } + } + self.add_run(¤t_value, current_length, all_values, all_lengths); + } + + fn add_run( + &self, + value: &T, + length: u64, + all_values: &mut Vec, + all_lengths: &mut Vec, + ) -> usize + where + T: bytemuck::Pod, + { + let value_bytes = bytemuck::bytes_of(value); + let type_size = std::mem::size_of::(); + let max_run_length = self.run_length_width.max_run_length(); + let num_full_chunks = (length / max_run_length) as usize; + let remainder = length % max_run_length; + + let total_chunks = num_full_chunks + if remainder > 0 { 1 } else { 0 }; + all_values.reserve(total_chunks * type_size); + all_lengths.reserve(total_chunks * self.run_length_width.bytes_per_value()); + + for _ in 0..num_full_chunks { + all_values.extend_from_slice(value_bytes); + self.run_length_width + .write_length(max_run_length, all_lengths); + } + + if remainder > 0 { + all_values.extend_from_slice(value_bytes); + self.run_length_width.write_length(remainder, all_lengths); + } + + total_chunks * (type_size + self.run_length_width.bytes_per_value()) + } + + fn flat_child_candidate( + buffers: &[LanceBuffer], + chunks: &[MiniBlockChunk], + buffer_index: usize, + bits_per_value: u64, + ) -> RleChildCandidate { + RleChildCandidate { + encoding: ProtobufUtils21::flat(bits_per_value, None), + data: buffers[buffer_index].clone(), + chunk_sizes: chunks + .iter() + .map(|chunk| chunk.buffer_sizes[buffer_index]) + .collect(), + size: buffers[buffer_index].len(), + requires_num_values: false, + } + } + + fn general_child_candidate( + buffers: &[LanceBuffer], + chunks: &[MiniBlockChunk], + buffer_index: usize, + bits_per_value: u64, + compression: CompressionConfig, + ) -> Result> { + if buffers.is_empty() || buffers[buffer_index].is_empty() { + return Ok(None); + }; + + let compressor = GeneralBufferCompressor::get_compressor(compression)?; + let original = &buffers[buffer_index]; + let mut compressed = Vec::new(); + let mut offset = 0usize; + let mut total_original_size = 0usize; + let mut compressed_sizes = Vec::with_capacity(chunks.len()); + + for chunk in chunks.iter() { + let chunk_size = chunk.buffer_sizes[buffer_index] as usize; + let end = offset.checked_add(chunk_size).ok_or_else(|| { + Error::invalid_input_source("RLE child buffer offset overflow".into()) + })?; + if end > original.len() { + return Err(Error::invalid_input_source( + format!( + "RLE child buffer {} chunk size exceeds buffer length: end {}, len {}", + buffer_index, + end, + original.len() + ) + .into(), + )); + } + + let start = compressed.len(); + compressor.compress(&original.as_ref()[offset..end], &mut compressed)?; + let compressed_size = compressed.len() - start; + let compressed_size = u32::try_from(compressed_size).map_err(|_| { + Error::invalid_input_source( + format!( + "RLE child buffer {} compressed chunk is too large: {} bytes", + buffer_index, compressed_size + ) + .into(), + ) + })?; + compressed_sizes.push(compressed_size); + total_original_size += chunk_size; + offset = end; + } + + if compressed.len() >= total_original_size { + return Ok(None); + } + + let encoding = + ProtobufUtils21::wrapped(compression, ProtobufUtils21::flat(bits_per_value, None))?; + Ok(Some( + RleChildCandidate { + encoding, + data: LanceBuffer::from(compressed), + chunk_sizes: compressed_sizes, + size: 0, + requires_num_values: false, + } + .with_size_from_data(), + )) + } + + #[cfg(feature = "bitpacking")] + fn bitpacked_child_candidate( + buffers: &[LanceBuffer], + chunks: &[MiniBlockChunk], + buffer_index: usize, + bits_per_value: u64, + ) -> Result> { + let original = &buffers[buffer_index]; + if original.is_empty() { + return Ok(None); + } + let packed_bits = Self::required_bits(original, bits_per_value)?; + if packed_bits >= bits_per_value { + return Ok(None); + } + + let compressor = crate::encodings::physical::bitpacking::OutOfLineBitpacking::new( + packed_bits, + bits_per_value, + ); + let mut packed = Vec::new(); + let mut offset = 0usize; + let mut packed_sizes = Vec::with_capacity(chunks.len()); + let bytes_per_value = usize::try_from(bits_per_value / 8).map_err(|_| { + Error::invalid_input_source( + format!("RLE child bit width is too large: {bits_per_value}").into(), + ) + })?; + + for chunk in chunks { + let chunk_size = chunk.buffer_sizes[buffer_index] as usize; + let end = offset.checked_add(chunk_size).ok_or_else(|| { + Error::invalid_input_source("RLE child buffer offset overflow".into()) + })?; + if end > original.len() { + return Err(Error::invalid_input_source( + format!( + "RLE child buffer {} chunk size exceeds buffer length: end {}, len {}", + buffer_index, + end, + original.len() + ) + .into(), + )); + } + if bytes_per_value == 0 || !chunk_size.is_multiple_of(bytes_per_value) { + return Err(Error::invalid_input_source( + format!( + "RLE child buffer {} chunk has invalid size {} for {} bits per value", + buffer_index, chunk_size, bits_per_value + ) + .into(), + )); + } + + let child_values = (chunk_size / bytes_per_value) as u64; + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value, + data: original.slice_with_length(offset, chunk_size), + num_values: child_values, + block_info: BlockInfo::default(), + }); + let chunk_packed = BlockCompressor::compress(&compressor, block)?; + let packed_size = u32::try_from(chunk_packed.len()).map_err(|_| { + Error::invalid_input_source( + format!( + "RLE child buffer {} bitpacked chunk is too large: {} bytes", + buffer_index, + chunk_packed.len() + ) + .into(), + ) + })?; + packed_sizes.push(packed_size); + packed.extend_from_slice(chunk_packed.as_ref()); + offset = end; + } + + if packed.len() >= original.len() { + return Ok(None); + } + + Ok(Some( + RleChildCandidate { + encoding: ProtobufUtils21::out_of_line_bitpacking( + bits_per_value, + ProtobufUtils21::flat(packed_bits, None), + ), + data: LanceBuffer::from(packed), + chunk_sizes: packed_sizes, + size: 0, + requires_num_values: true, + } + .with_size_from_data(), + )) + } + + #[cfg(feature = "bitpacking")] + fn required_bits(buffer: &LanceBuffer, bits_per_value: u64) -> Result { + let max_value = match bits_per_value { + 8 => buffer.as_ref().iter().map(|value| *value as u64).max(), + 16 => buffer + .as_ref() + .chunks_exact(2) + .map(|value| u16::from_le_bytes(value.try_into().unwrap()) as u64) + .max(), + 32 => buffer + .as_ref() + .chunks_exact(4) + .map(|value| u32::from_le_bytes(value.try_into().unwrap()) as u64) + .max(), + 64 => buffer + .as_ref() + .chunks_exact(8) + .map(|value| u64::from_le_bytes(value.try_into().unwrap())) + .max(), + _ => { + return Err(Error::invalid_input_source( + format!( + "RLE child bitpacking only supports 8, 16, 32, or 64-bit values, got {bits_per_value}" + ) + .into(), + )); + } + } + .unwrap_or(0); + Ok((u64::BITS - max_value.leading_zeros()).max(1) as u64) + } + + fn child_candidates( + buffers: &[LanceBuffer], + chunks: &[MiniBlockChunk], + buffer_index: usize, + bits_per_value: u64, + compression: Option, + use_child_bitpacking: bool, + ) -> Result> { + #[cfg(not(feature = "bitpacking"))] + let _ = use_child_bitpacking; + let mut candidates = vec![Self::flat_child_candidate( + buffers, + chunks, + buffer_index, + bits_per_value, + )]; + if let Some(compression) = compression + && let Some(candidate) = Self::general_child_candidate( + buffers, + chunks, + buffer_index, + bits_per_value, + compression, + )? + { + candidates.push(candidate); + } + #[cfg(feature = "bitpacking")] + { + if use_child_bitpacking + && let Some(candidate) = + Self::bitpacked_child_candidate(buffers, chunks, buffer_index, bits_per_value)? + { + candidates.push(candidate); + } + } + Ok(candidates) + } + + fn select_child_candidates( + values: Vec, + run_lengths: Vec, + ) -> (RleChildCandidate, RleChildCandidate) { + let mut best: Option<(usize, usize, usize)> = None; + for (value_idx, value) in values.iter().enumerate() { + for (length_idx, length) in run_lengths.iter().enumerate() { + if value.requires_num_values && length.requires_num_values { + continue; + } + let size = value.size + length.size; + if best.is_none_or(|(_, _, best_size)| size < best_size) { + best = Some((value_idx, length_idx, size)); + } + } + } + let (value_idx, length_idx, _) = + best.expect("flat RLE child candidates should always be selectable"); + (values[value_idx].clone(), run_lengths[length_idx].clone()) + } + + pub(crate) fn selected_payload_size(&self, data: &FixedWidthDataBlock) -> Result { + let (all_buffers, chunks) = + self.encode_data(&data.data, data.num_values, data.bits_per_value)?; + if all_buffers.is_empty() { + return Ok(0); + } + + let values_candidates = Self::child_candidates( + &all_buffers, + &chunks, + 0, + data.bits_per_value, + self.values_compression, + self.use_child_bitpacking, + )?; + let run_lengths_candidates = Self::child_candidates( + &all_buffers, + &chunks, + 1, + self.run_length_width.bits_per_value(), + self.run_lengths_compression, + self.use_child_bitpacking, + )?; + let (values, run_lengths) = + Self::select_child_candidates(values_candidates, run_lengths_candidates); + Ok((values.size as u128).saturating_add(run_lengths.size as u128)) + } +} + +impl RleChildCandidate { + fn with_size_from_data(mut self) -> Self { + self.size = self.data.len(); + self + } +} + +impl MiniBlockCompressor for RleEncoder { + fn compress( + &self, + _context: MiniBlockCompressionContext, + data: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match data { + DataBlock::FixedWidth(fixed_width) => { + let num_values = fixed_width.num_values; + let bits_per_value = fixed_width.bits_per_value; + + let (all_buffers, chunks) = + self.encode_data(&fixed_width.data, num_values, bits_per_value)?; + if all_buffers.is_empty() { + let compressed = MiniBlockCompressed { + data: all_buffers, + chunks, + num_values, + }; + let encoding = ProtobufUtils21::rle( + ProtobufUtils21::flat(bits_per_value, None), + ProtobufUtils21::flat(self.run_length_width.bits_per_value(), None), + ); + return Ok((compressed, encoding)); + } + + let values_candidates = Self::child_candidates( + &all_buffers, + &chunks, + 0, + bits_per_value, + self.values_compression, + self.use_child_bitpacking, + )?; + let run_lengths_candidates = Self::child_candidates( + &all_buffers, + &chunks, + 1, + self.run_length_width.bits_per_value(), + self.run_lengths_compression, + self.use_child_bitpacking, + )?; + let (values, run_lengths) = + Self::select_child_candidates(values_candidates, run_lengths_candidates); + let chunks = chunks + .into_iter() + .enumerate() + .map(|(idx, chunk)| MiniBlockChunk { + buffer_sizes: vec![values.chunk_sizes[idx], run_lengths.chunk_sizes[idx]], + log_num_values: chunk.log_num_values, + }) + .collect(); + + let compressed = MiniBlockCompressed { + data: vec![values.data, run_lengths.data], + chunks, + num_values, + }; + + let encoding = ProtobufUtils21::rle(values.encoding, run_lengths.encoding); + + Ok((compressed, encoding)) + } + _ => Err(Error::invalid_input_source( + "RLE encoding only supports FixedWidth data blocks".into(), + )), + } + } +} + +impl BlockCompressor for RleEncoder { + // Block format: [8-byte header: values buffer size][values buffer][run_lengths buffer] + fn compress(&self, data: DataBlock) -> Result { + match data { + DataBlock::FixedWidth(fixed_width) => { + let num_values = fixed_width.num_values; + let bits_per_value = fixed_width.bits_per_value; + + let all_buffers = + self.encode_block_data(&fixed_width.data, num_values, bits_per_value)?; + + let values_size = all_buffers[0].len() as u64; + + let mut combined = Vec::new(); + combined.extend_from_slice(&values_size.to_le_bytes()); + combined.extend_from_slice(&all_buffers[0]); + combined.extend_from_slice(&all_buffers[1]); + Ok(LanceBuffer::from(combined)) + } + _ => Err(Error::invalid_input_source( + "RLE encoding only supports FixedWidth data blocks".into(), + )), + } + } +} + +/// RLE decompressor for miniblock format +#[derive(Debug)] +pub struct RleDecompressor { + bits_per_value: u64, + run_length_width: RunLengthWidth, + values: RleChildDecompressor, + run_lengths: RleChildDecompressor, +} + +#[derive(Debug)] +pub(crate) struct RleChildDecompressor { + bits_per_value: u64, + inner: RleChildDecompressorInner, +} + +#[derive(Debug)] +enum RleChildDecompressorInner { + Flat, + Block { + decompressor: Box, + requires_num_values: bool, + }, +} + +impl RleChildDecompressor { + pub(crate) fn flat(bits_per_value: u64) -> Self { + Self { + bits_per_value, + inner: RleChildDecompressorInner::Flat, + } + } + + pub(crate) fn block( + bits_per_value: u64, + decompressor: Box, + requires_num_values: bool, + ) -> Self { + Self { + bits_per_value, + inner: RleChildDecompressorInner::Block { + decompressor, + requires_num_values, + }, + } + } + + pub(crate) fn bits_per_value(&self) -> u64 { + self.bits_per_value + } + + pub(crate) fn requires_num_values(&self) -> bool { + match &self.inner { + RleChildDecompressorInner::Flat => false, + RleChildDecompressorInner::Block { + requires_num_values, + .. + } => *requires_num_values, + } + } + + pub(crate) fn is_identity(&self) -> bool { + matches!(self.inner, RleChildDecompressorInner::Flat) + } + + fn decode( + &self, + data: LanceBuffer, + num_values: Option, + label: &str, + ) -> Result { + match &self.inner { + RleChildDecompressorInner::Flat => Ok(data), + RleChildDecompressorInner::Block { + decompressor, + requires_num_values, + } => { + let num_values = if *requires_num_values { + num_values.ok_or_else(|| { + Error::invalid_input_source( + format!("RLE {label} child compression requires the run count").into(), + ) + })? + } else { + num_values.unwrap_or(0) + }; + let decoded = decompressor.decompress(data, num_values)?; + self.extract_fixed_width(decoded, num_values, label) + } + } + } + + fn extract_fixed_width( + &self, + data: DataBlock, + expected_num_values: u64, + label: &str, + ) -> Result { + match data { + DataBlock::FixedWidth(block) => { + if block.bits_per_value != self.bits_per_value { + return Err(Error::invalid_input_source( + format!( + "RLE {label} child decoded {}-bit values, expected {}", + block.bits_per_value, self.bits_per_value + ) + .into(), + )); + } + if expected_num_values != 0 && block.num_values != expected_num_values { + return Err(Error::invalid_input_source( + format!( + "RLE {label} child decoded {} values, expected {}", + block.num_values, expected_num_values + ) + .into(), + )); + } + Ok(block.data) + } + _ => Err(Error::invalid_input_source( + format!("RLE {label} child decoded to a non fixed-width block").into(), + )), + } + } +} + +impl RleDecompressor { + pub fn new(bits_per_value: u64) -> Self { + Self { + bits_per_value, + run_length_width: RunLengthWidth::U8, + values: RleChildDecompressor::flat(bits_per_value), + run_lengths: RleChildDecompressor::flat(RunLengthWidth::U8.bits_per_value()), + } + } + + pub(crate) fn with_run_length_width( + bits_per_value: u64, + run_length_width: RunLengthWidth, + ) -> Self { + Self { + bits_per_value, + run_length_width, + values: RleChildDecompressor::flat(bits_per_value), + run_lengths: RleChildDecompressor::flat(run_length_width.bits_per_value()), + } + } + + pub(crate) fn with_child_decompressors( + bits_per_value: u64, + run_length_width: RunLengthWidth, + values: RleChildDecompressor, + run_lengths: RleChildDecompressor, + ) -> Self { + Self { + bits_per_value, + run_length_width, + values, + run_lengths, + } + } + + fn decode_data( + &self, + data: Vec, + num_values: u64, + clamp_overflow: bool, + ) -> Result { + if num_values == 0 { + return Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bits_per_value, + data: LanceBuffer::from(vec![]), + num_values: 0, + block_info: BlockInfo::default(), + })); + } + + if data.len() != 2 { + return Err(Error::invalid_input_source( + format!( + "RLE decompressor expects exactly 2 buffers, got {}", + data.len() + ) + .into(), + )); + } + + let mut data_iter = data.into_iter(); + let values_buffer = data_iter.next().unwrap(); + let lengths_buffer = data_iter.next().unwrap(); + let (values_buffer, lengths_buffer) = + self.decode_child_buffers(values_buffer, lengths_buffer)?; + + let decoded_data = match self.bits_per_value { + 8 => self.decode_generic::( + &values_buffer, + &lengths_buffer, + num_values, + clamp_overflow, + )?, + 16 => self.decode_generic::( + &values_buffer, + &lengths_buffer, + num_values, + clamp_overflow, + )?, + 32 => self.decode_generic::( + &values_buffer, + &lengths_buffer, + num_values, + clamp_overflow, + )?, + 64 => self.decode_generic::( + &values_buffer, + &lengths_buffer, + num_values, + clamp_overflow, + )?, + _ => { + return Err(Error::invalid_input_source( + format!( + "RLE decoding bits_per_value must be 8, 16, 32, or 64, got {}", + self.bits_per_value + ) + .into(), + )); + } + }; + + Ok(DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bits_per_value, + data: decoded_data, + num_values, + block_info: BlockInfo::default(), + })) + } + + fn decode_child_buffers( + &self, + values_buffer: LanceBuffer, + lengths_buffer: LanceBuffer, + ) -> Result<(LanceBuffer, LanceBuffer)> { + let values_requires_num_runs = self.values.requires_num_values(); + let lengths_requires_num_runs = self.run_lengths.requires_num_values(); + if values_requires_num_runs && lengths_requires_num_runs { + return Err(Error::invalid_input_source( + "RLE values and run lengths child compression both require the run count".into(), + )); + } + + if values_requires_num_runs { + let lengths_buffer = self + .run_lengths + .decode(lengths_buffer, None, "run lengths")?; + let num_runs = Self::num_child_values( + &lengths_buffer, + self.run_lengths.bits_per_value(), + "run lengths", + )?; + let values_buffer = self + .values + .decode(values_buffer, Some(num_runs), "values")?; + Ok((values_buffer, lengths_buffer)) + } else if lengths_requires_num_runs { + let values_buffer = self.values.decode(values_buffer, None, "values")?; + let num_runs = + Self::num_child_values(&values_buffer, self.values.bits_per_value(), "values")?; + let lengths_buffer = + self.run_lengths + .decode(lengths_buffer, Some(num_runs), "run lengths")?; + Ok((values_buffer, lengths_buffer)) + } else { + let values_buffer = self.values.decode(values_buffer, None, "values")?; + let lengths_buffer = self + .run_lengths + .decode(lengths_buffer, None, "run lengths")?; + Ok((values_buffer, lengths_buffer)) + } + } + + fn num_child_values(buffer: &LanceBuffer, bits_per_value: u64, label: &str) -> Result { + let bytes_per_value = usize::try_from(bits_per_value / 8).map_err(|_| { + Error::invalid_input_source( + format!("RLE {label} child bit width is too large: {bits_per_value}").into(), + ) + })?; + if bytes_per_value == 0 || !buffer.len().is_multiple_of(bytes_per_value) { + return Err(Error::invalid_input_source( + format!( + "RLE {label} child decoded to {} bytes, not divisible by {}", + buffer.len(), + bytes_per_value + ) + .into(), + )); + } + Ok((buffer.len() / bytes_per_value) as u64) + } + + fn decode_generic( + &self, + values_buffer: &LanceBuffer, + lengths_buffer: &LanceBuffer, + num_values: u64, + clamp_overflow: bool, + ) -> Result + where + T: bytemuck::Pod + Copy + std::fmt::Debug + ArrowNativeType, + { + let type_size = std::mem::size_of::(); + let length_size = self.run_length_width.bytes_per_value(); + + if values_buffer.is_empty() || lengths_buffer.is_empty() { + if num_values == 0 { + return Ok(LanceBuffer::empty()); + } else { + return Err(Error::invalid_input_source( + format!("Empty buffers but expected {} values", num_values).into(), + )); + } + } + + if !values_buffer.len().is_multiple_of(type_size) + || !lengths_buffer.len().is_multiple_of(length_size) + { + return Err(Error::invalid_input_source(format!( + "Invalid buffer sizes for RLE {} decoding: values {} bytes (not divisible by {}), lengths {} bytes (not divisible by {})", + std::any::type_name::(), + values_buffer.len(), + type_size, + lengths_buffer.len(), + length_size + ) + .into())); + } + + let num_runs = values_buffer.len() / type_size; + let num_length_entries = lengths_buffer.len() / length_size; + if num_runs != num_length_entries { + return Err(Error::invalid_input_source( + format!( + "Inconsistent RLE buffers: {} runs but {} length entries", + num_runs, num_length_entries + ) + .into(), + )); + } + + let values_ref = values_buffer.borrow_to_typed_slice::(); + let values: &[T] = values_ref.as_ref(); + let lengths = lengths_buffer.as_ref(); + + let expected_value_count = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("RLE num_values does not fit in usize: {num_values}").into(), + ) + })?; + // Legacy miniblock encoders rolled back to a power-of-2 checkpoint after a run + // had already crossed it, so a chunk's run lengths can sum past its declared + // value count (the excess values are re-encoded at the start of the next chunk). + // The pre-run-length-width decoder truncated the excess, so miniblock decoding + // clamps rather than rejects to keep those files readable. Block payloads never + // legitimately overflow, so they decode strictly. + let mut decoded: Vec = Vec::new(); + decoded + .try_reserve_exact(expected_value_count) + .map_err(|_| { + Error::invalid_input_source( + format!("RLE decoding cannot allocate {expected_value_count} values").into(), + ) + })?; + for (value, length_bytes) in values.iter().zip(lengths.chunks_exact(length_size)) { + let length = self.run_length_width.read_length(length_bytes); + if length == 0 { + return Err(Error::invalid_input_source( + "RLE decoding encountered a zero run length".into(), + )); + } + let length = usize::try_from(length).map_err(|_| { + Error::invalid_input_source( + format!("RLE run length does not fit in usize: {length}").into(), + ) + })?; + let remaining = expected_value_count - decoded.len(); + if length > remaining { + if !clamp_overflow { + return Err(Error::invalid_input_source( + format!( + "RLE decoding overflowed expected value count: produced at least {}, expected {}", + decoded.len() + length, + expected_value_count + ) + .into(), + )); + } + decoded.resize(expected_value_count, *value); + break; + } + decoded.resize(decoded.len() + length, *value); + } + + if decoded.len() != expected_value_count { + return Err(Error::invalid_input_source( + format!( + "RLE decoding produced {} values, expected {}", + decoded.len(), + expected_value_count + ) + .into(), + )); + } + + trace!( + "RLE decoded {} {} values", + num_values, + std::any::type_name::() + ); + Ok(LanceBuffer::reinterpret_vec(decoded)) + } +} + +impl MiniBlockDecompressor for RleDecompressor { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + self.decode_data(data, num_values, true) + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + num_values + .checked_mul(self.bits_per_value) + .map(|bits| bits.div_ceil(8)) + } +} + +impl BlockDecompressor for RleDecompressor { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + let (values_buffer, lengths_buffer) = parse_rle_block_frame(&data)?; + self.decode_data(vec![values_buffer, lengths_buffer], num_values, false) + } +} + +/// Split an RLE block-format buffer into its `(values, lengths)` sub-buffers. +/// Frame: `[values_size: u64-le][values bytes][run-length bytes]`. +fn parse_rle_block_frame(data: &LanceBuffer) -> Result<(LanceBuffer, LanceBuffer)> { + // fetch the values_size + if data.len() < 8 { + return Err(Error::invalid_input_source( + format!("Insufficient data size: {}", data.len()).into(), + )); + } + + let values_size_bytes: [u8; 8] = data[..8].try_into().expect("slice length already checked"); + let values_size: usize = u64::from_le_bytes(values_size_bytes) + .try_into() + .map_err(|_| { + Error::invalid_input_source( + format!( + "Invalid values buffer size: {}", + u64::from_le_bytes(values_size_bytes) + ) + .into(), + ) + })?; + + // parse values + let values_start: usize = 8; + let lengths_start = values_start + .checked_add(values_size) + .ok_or_else(|| Error::invalid_input_source("Invalid RLE values buffer size".into()))?; + + if data.len() < lengths_start { + return Err(Error::invalid_input_source( + format!("Insufficient data size: {}", data.len()).into(), + )); + } + + let values_buffer = data.slice_with_length(values_start, values_size); + let lengths_buffer = data.slice_with_length(lengths_start, data.len() - lengths_start); + Ok((values_buffer, lengths_buffer)) +} + +#[derive(Clone, Debug)] +enum RleRunLengths { + U8(ScalarBuffer), + U16(ScalarBuffer), + U32(ScalarBuffer), +} + +impl RleRunLengths { + fn try_new(buffer: LanceBuffer, width: RunLengthWidth) -> Result { + let width_bytes = width.bytes_per_value(); + if !buffer.len().is_multiple_of(width_bytes) { + return Err(Error::invalid_input_source( + format!( + "Invalid RLE run lengths buffer: {} bytes (not divisible by {})", + buffer.len(), + width_bytes + ) + .into(), + )); + } + Ok(match width { + RunLengthWidth::U8 => Self::U8(buffer.borrow_to_typed_slice()), + RunLengthWidth::U16 => Self::U16(buffer.borrow_to_typed_slice()), + RunLengthWidth::U32 => Self::U32(buffer.borrow_to_typed_slice()), + }) + } + + fn len(&self) -> usize { + match self { + Self::U8(lengths) => lengths.len(), + Self::U16(lengths) => lengths.len(), + Self::U32(lengths) => lengths.len(), + } + } + + fn get(&self, index: usize) -> usize { + match self { + Self::U8(lengths) => lengths[index] as usize, + Self::U16(lengths) => lengths[index] as usize, + Self::U32(lengths) => lengths[index] as usize, + } + } + + fn owned_size(&self) -> usize { + match self { + Self::U8(lengths) => std::mem::size_of_val(lengths.as_ref()), + Self::U16(lengths) => std::mem::size_of_val(lengths.as_ref()), + Self::U32(lengths) => std::mem::size_of_val(lengths.as_ref()), + } + } + + fn into_owned(self) -> Self { + match self { + Self::U8(lengths) => Self::U8(ScalarBuffer::from(lengths.as_ref().to_vec())), + Self::U16(lengths) => Self::U16(ScalarBuffer::from(lengths.as_ref().to_vec())), + Self::U32(lengths) => Self::U32(ScalarBuffer::from(lengths.as_ref().to_vec())), + } + } + + fn deep_size(&self) -> usize { + match self { + Self::U8(lengths) => lengths.inner().capacity(), + Self::U16(lengths) => lengths.inner().capacity(), + Self::U32(lengths) => lengths.inner().capacity(), + } + } +} + +/// Validated physical RLE runs for `u16` values. +/// +/// The values and original-width lengths remain unexpanded. The constructor +/// verifies that every length is non-zero and that the runs cover exactly +/// `num_values` logical values. +#[derive(Clone, Debug)] +pub(crate) struct RleRuns { + values: ScalarBuffer, + lengths: RleRunLengths, + num_values: usize, + coalesced_runs: usize, +} + +impl RleRuns { + fn try_new( + values_buffer: LanceBuffer, + lengths_buffer: LanceBuffer, + run_length_width: RunLengthWidth, + num_values: u64, + ) -> Result { + let num_values = usize::try_from(num_values).map_err(|_| { + Error::invalid_input_source( + format!("RLE num_values does not fit in usize: {num_values}").into(), + ) + })?; + let type_size = std::mem::size_of::(); + if !values_buffer.len().is_multiple_of(type_size) { + return Err(Error::invalid_input_source( + format!( + "Invalid RLE u16 values buffer: {} bytes (not divisible by {})", + values_buffer.len(), + type_size + ) + .into(), + )); + } + + let values = values_buffer.borrow_to_typed_slice::(); + let lengths = RleRunLengths::try_new(lengths_buffer, run_length_width)?; + if values.len() != lengths.len() { + return Err(Error::invalid_input_source( + format!( + "Inconsistent RLE buffers: {} runs but {} length entries", + values.len(), + lengths.len() + ) + .into(), + )); + } + if values.is_empty() && num_values != 0 { + return Err(Error::invalid_input_source( + format!("Empty RLE buffers but expected {num_values} values").into(), + )); + } + + let mut decoded_values = 0usize; + let mut coalesced_runs = 0usize; + let mut previous_value = None; + for run in 0..values.len() { + let length = lengths.get(run); + if length == 0 { + return Err(Error::invalid_input_source( + "RLE decoding encountered a zero run length".into(), + )); + } + decoded_values = decoded_values.checked_add(length).ok_or_else(|| { + Error::invalid_input_source("RLE run length sum overflowed usize".into()) + })?; + if decoded_values > num_values { + return Err(Error::invalid_input_source( + format!( + "RLE decoding overflowed expected value count: produced at least {}, expected {}", + decoded_values, num_values + ) + .into(), + )); + } + if previous_value != Some(values[run]) { + coalesced_runs += 1; + previous_value = Some(values[run]); + } + } + if decoded_values != num_values { + return Err(Error::invalid_input_source( + format!( + "RLE decoding produced {} values, expected {}", + decoded_values, num_values + ) + .into(), + )); + } + + Ok(Self { + values, + lengths, + num_values, + coalesced_runs, + }) + } + + pub(crate) fn num_values(&self) -> usize { + self.num_values + } + + pub(crate) fn num_runs(&self) -> usize { + self.values.len() + } + + pub(crate) fn coalesced_runs(&self) -> usize { + self.coalesced_runs + } + + pub(crate) fn owned_size(&self) -> usize { + std::mem::size_of_val(self.values.as_ref()) + self.lengths.owned_size() + } + + pub(crate) fn into_owned(self) -> Self { + Self { + values: ScalarBuffer::from(self.values.as_ref().to_vec()), + lengths: self.lengths.into_owned(), + num_values: self.num_values, + coalesced_runs: self.coalesced_runs, + } + } + + pub(crate) fn deep_size(&self) -> usize { + self.values.inner().capacity() + self.lengths.deep_size() + } + + pub(crate) fn value(&self, run: usize) -> u16 { + self.values[run] + } + + pub(crate) fn length(&self, run: usize) -> usize { + self.lengths.get(run) + } + + pub(crate) fn iter(&self) -> impl ExactSizeIterator + '_ { + (0..self.num_runs()).map(|run| (self.value(run), self.length(run))) + } +} + +impl RleDecompressor { + /// Decode and validate a block frame while preserving its physical runs. + pub(crate) fn decode_u16_runs(&self, data: LanceBuffer, num_values: u64) -> Result { + if self.bits_per_value != 16 { + return Err(Error::invalid_input_source( + format!( + "RLE level values must be 16 bits, got {}", + self.bits_per_value + ) + .into(), + )); + } + let (values_buffer, lengths_buffer) = parse_rle_block_frame(&data)?; + let (values_buffer, lengths_buffer) = + self.decode_child_buffers(values_buffer, lengths_buffer)?; + RleRuns::try_new( + values_buffer, + lengths_buffer, + self.run_length_width, + num_values, + ) + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use super::*; + use crate::compression::{ + DecompressionStrategy, DefaultDecompressionStrategy, create_rle_decompressor, + }; + use crate::data::DataBlock; + use crate::encodings::logical::primitive::miniblock::MAX_MINIBLOCK_VALUES; + use crate::encodings::physical::block::{CompressionConfig, CompressionScheme}; + use crate::{ + buffer::LanceBuffer, + compression::{BlockCompressor, BlockDecompressor}, + }; + use arrow_array::Int32Array; + use rstest::rstest; + + fn compress_miniblock( + compressor: &dyn MiniBlockCompressor, + data: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + compressor.compress(MiniBlockCompressionContext::new(0, true, true), data) + } + + fn expand_u16_runs(runs: &RleRuns) -> Vec { + let mut expanded = Vec::with_capacity(runs.num_values()); + for (value, length) in runs.iter() { + expanded.extend(std::iter::repeat_n(value, length)); + } + expanded + } + + #[test] + fn decode_u16_runs_matches_eager() { + // Near-constant u16 levels (the all-null shape): a few long runs. + let mut levels: Vec = vec![1u16; 1000]; + levels.extend(std::iter::repeat_n(0u16, 500)); + levels.extend(std::iter::repeat_n(2u16, 300)); + let num_values = levels.len() as u64; + + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_slice(Arc::from(levels.clone())), + bits_per_value: 16, + num_values, + block_info: BlockInfo::new(), + }); + let frame = BlockCompressor::compress(&RleEncoder::new(), block).unwrap(); + + let eager = + BlockDecompressor::decompress(&RleDecompressor::new(16), frame.clone(), num_values) + .unwrap(); + let DataBlock::FixedWidth(eager) = eager else { + panic!("expected fixed-width block"); + }; + assert_eq!( + eager.data.borrow_to_typed_slice::().as_ref(), + levels.as_slice() + ); + + // Lazy run form preserves boundaries and expands identically. + let runs = RleDecompressor::new(16) + .decode_u16_runs(frame, num_values) + .unwrap(); + assert_eq!(runs.num_values(), num_values as usize); + // The encoder splits each value into <=255-length runs (4 + 2 + 2 = 8 + // on-disk runs here); the scan identifies 3 coalesced logical runs. + assert_eq!(runs.num_runs(), 8); + assert_eq!(runs.coalesced_runs(), 3); + assert_eq!(expand_u16_runs(&runs), levels); + } + + #[test] + fn decode_u16_runs_empty() { + let mut empty_frame = Vec::new(); + empty_frame.extend_from_slice(&0u64.to_le_bytes()); + let runs = RleDecompressor::new(16) + .decode_u16_runs(LanceBuffer::from(empty_frame), 0) + .unwrap(); + assert_eq!(runs.num_values(), 0); + assert_eq!(runs.num_runs(), 0); + assert_eq!(runs.coalesced_runs(), 0); + + let error = RleDecompressor::new(16) + .decode_u16_runs(LanceBuffer::empty(), 0) + .unwrap_err(); + assert!(error.to_string().contains("Insufficient data size: 0")); + } + + #[rstest] + #[case::zero(0, 1, "zero run length")] + #[case::underflow(1, 2, "produced 1 values, expected 2")] + #[case::overflow(2, 1, "overflowed expected value count")] + #[case::nonempty_for_empty(1, 0, "overflowed expected value count")] + fn decode_u16_runs_rejects_invalid_coverage( + #[case] run_length: u8, + #[case] num_values: u64, + #[case] expected_message: &str, + ) { + let mut frame = Vec::new(); + frame.extend_from_slice(&2u64.to_le_bytes()); + frame.extend_from_slice(&7u16.to_le_bytes()); + frame.push(run_length); + + let error = RleDecompressor::new(16) + .decode_u16_runs(LanceBuffer::from(frame), num_values) + .unwrap_err(); + assert!(matches!(error, lance_core::Error::InvalidInput { .. })); + assert!(error.to_string().contains(expected_message)); + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn decode_u16_runs_supports_compressed_values_child() { + let levels: Vec = (0..1024) + .flat_map(|run| std::iter::repeat_n((run % 8) as u16, 4)) + .collect(); + let num_values = levels.len() as u64; + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_slice(Arc::from(levels.clone())), + bits_per_value: 16, + num_values, + block_info: BlockInfo::new(), + }); + let frame = BlockCompressor::compress(&RleEncoder::new(), block).unwrap(); + let (values, lengths) = parse_rle_block_frame(&frame).unwrap(); + + let compression = test_general_compression(); + let compressor = GeneralBufferCompressor::get_compressor(compression).unwrap(); + let mut compressed_values = Vec::new(); + compressor + .compress(values.as_ref(), &mut compressed_values) + .unwrap(); + let mut compressed_frame = Vec::new(); + compressed_frame.extend_from_slice(&(compressed_values.len() as u64).to_le_bytes()); + compressed_frame.extend_from_slice(&compressed_values); + compressed_frame.extend_from_slice(lengths.as_ref()); + + let encoding = ProtobufUtils21::rle( + ProtobufUtils21::wrapped(compression, ProtobufUtils21::flat(16, None)).unwrap(), + ProtobufUtils21::flat(8, None), + ); + let decompressor = create_rle_decompressor( + expect_rle(&encoding), + &DefaultDecompressionStrategy::default(), + ) + .unwrap(); + let runs = decompressor + .decode_u16_runs(LanceBuffer::from(compressed_frame), num_values) + .unwrap(); + assert_eq!(expand_u16_runs(&runs), levels); + } + + #[test] + fn decode_u16_runs_counts_coalesced_runs() { + // A logically constant page is emitted as ceil(N / 255) equal-valued + // runs (the encoder caps run lengths at 255); the validated view records + // that they can collapse to a single logical run. + let num_values = 5000u64; + let constant: Vec = vec![7u16; num_values as usize]; + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_slice(Arc::from(constant)), + bits_per_value: 16, + num_values, + block_info: BlockInfo::new(), + }); + let frame = BlockCompressor::compress(&RleEncoder::new(), block).unwrap(); + let runs = RleDecompressor::new(16) + .decode_u16_runs(frame, num_values) + .unwrap(); + assert_eq!( + runs.num_runs(), + num_values.div_ceil(u8::MAX as u64) as usize + ); + assert_eq!(runs.coalesced_runs(), 1); + assert_eq!(expand_u16_runs(&runs), vec![7u16; num_values as usize]); + + // Distinct adjacent values must not be merged: alternating single-value + // runs stay separate and still expand to the original. + let alternating: Vec = (0..200u16).map(|i| i % 2).collect(); + let n = alternating.len() as u64; + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + data: LanceBuffer::reinterpret_slice(Arc::from(alternating.clone())), + bits_per_value: 16, + num_values: n, + block_info: BlockInfo::new(), + }); + let frame = BlockCompressor::compress(&RleEncoder::new(), block).unwrap(); + let runs = RleDecompressor::new(16).decode_u16_runs(frame, n).unwrap(); + assert_eq!( + runs.coalesced_runs() as u64, + n, + "no two adjacent values are equal" + ); + assert_eq!(expand_u16_runs(&runs), alternating); + } + + // ========== Core Functionality Tests ========== + + #[test] + fn test_basic_miniblock_rle_encoding() { + let encoder = RleEncoder::new(); + + // Test basic RLE pattern: [1, 1, 1, 2, 2, 3, 3, 3, 3] + let array = Int32Array::from(vec![1, 1, 1, 2, 2, 3, 3, 3, 3]); + let data_block = DataBlock::from_array(array); + + let (compressed, _) = compress_miniblock(&encoder, data_block).unwrap(); + + assert_eq!(compressed.num_values, 9); + assert_eq!(compressed.chunks.len(), 1); + + // Verify compression happened (3 runs instead of 9 values) + let values_buffer = &compressed.data[0]; + let lengths_buffer = &compressed.data[1]; + assert_eq!(values_buffer.len(), 12); // 3 i32 values + assert_eq!(lengths_buffer.len(), 3); // 3 u8 lengths + } + + #[test] + fn test_long_run_splitting() { + let encoder = RleEncoder::new(); + + // Create a run longer than 255 to test splitting + let mut data = vec![42i32; 1000]; // Will be split into 255+255+255+235 + data.extend(&[100i32; 300]); // Will be split into 255+45 + + let array = Int32Array::from(data); + let (compressed, _) = compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + // Should have 6 runs total (4 for first value, 2 for second) + let lengths_buffer = &compressed.data[1]; + assert_eq!(lengths_buffer.len(), 6); + } + + #[test] + fn test_rle_v2_u16_miniblock_encoding() { + let encoder = RleEncoder::with_run_length_width(RunLengthWidth::U16); + + let data = vec![42i32; 1000]; + let array = Int32Array::from(data); + let (compressed, encoding) = + compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + assert_eq!(compressed.data[0].len(), 4); + assert_eq!(compressed.data[1].len(), 2); + assert_eq!(compressed.data[1].as_ref(), &1000u16.to_le_bytes()); + + let rle = match encoding.compression.as_ref().unwrap() { + crate::format::pb21::compressive_encoding::Compression::Rle(rle) => rle, + other => panic!("expected RLE encoding, got {other:?}"), + }; + let run_lengths = rle.run_lengths.as_ref().unwrap(); + let flat = match run_lengths.compression.as_ref().unwrap() { + crate::format::pb21::compressive_encoding::Compression::Flat(flat) => flat, + other => panic!("expected flat run lengths, got {other:?}"), + }; + assert_eq!(flat.bits_per_value, 16); + + let decompressor = RleDecompressor::with_run_length_width(32, RunLengthWidth::U16); + let decompressed = MiniBlockDecompressor::decompress( + &decompressor, + compressed.data, + compressed.num_values, + ) + .unwrap(); + match decompressed { + DataBlock::FixedWidth(block) => { + let values = block.data.borrow_to_typed_slice::(); + assert_eq!(values.as_ref(), vec![42i32; 1000]); + } + _ => panic!("Expected FixedWidth block"), + } + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_rle_miniblock_compressed_values_child() { + let compression = test_general_compression(); + let encoder = + RleEncoder::with_child_encoding(RunLengthWidth::U8, Some(compression), None, false); + let array = Int32Array::from(repeating_runs(1024, 4)); + let (compressed, encoding) = + compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + let rle = expect_rle(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + crate::format::pb21::compressive_encoding::Compression::General(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + crate::format::pb21::compressive_encoding::Compression::Flat(_) + )); + + let decompressor = DefaultDecompressionStrategy::default() + .create_miniblock_decompressor(&encoding, &DefaultDecompressionStrategy::default()) + .unwrap(); + let decoded = + MiniBlockDecompressor::decompress(decompressor.as_ref(), compressed.data, 1024 * 4) + .unwrap(); + assert_decoded_i32_eq(decoded, &repeating_runs(1024, 4)); + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_rle_miniblock_compressed_run_lengths_child() { + let compression = test_general_compression(); + let encoder = + RleEncoder::with_child_encoding(RunLengthWidth::U8, None, Some(compression), false); + let expected = repeating_runs(1024, 4); + let (compressed, encoding) = compress_miniblock( + &encoder, + DataBlock::from_array(Int32Array::from(expected.clone())), + ) + .unwrap(); + + let rle = expect_rle(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + crate::format::pb21::compressive_encoding::Compression::Flat(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + crate::format::pb21::compressive_encoding::Compression::General(_) + )); + + let decompressor = DefaultDecompressionStrategy::default() + .create_miniblock_decompressor(&encoding, &DefaultDecompressionStrategy::default()) + .unwrap(); + let decoded = + MiniBlockDecompressor::decompress(decompressor.as_ref(), compressed.data, 1024 * 4) + .unwrap(); + assert_decoded_i32_eq(decoded, &expected); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_miniblock_bitpacked_run_lengths_child() { + use crate::encodings::physical::bitpacking::OutOfLineBitpacking; + + let expected = repeating_runs(1024, 4); + let (compressed, _) = compress_miniblock( + &RleEncoder::new(), + DataBlock::from_array(Int32Array::from(expected.clone())), + ) + .unwrap(); + let run_lengths = compressed.data[1].clone(); + let num_runs = run_lengths.len() as u64; + let run_lengths_block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 8, + data: run_lengths, + num_values: num_runs, + block_info: BlockInfo::default(), + }); + let bitpacked_run_lengths = + BlockCompressor::compress(&OutOfLineBitpacking::new(3, 8), run_lengths_block).unwrap(); + let encoding = ProtobufUtils21::rle( + ProtobufUtils21::flat(32, None), + ProtobufUtils21::out_of_line_bitpacking(8, ProtobufUtils21::flat(3, None)), + ); + + let decompressor = DefaultDecompressionStrategy::default() + .create_miniblock_decompressor(&encoding, &DefaultDecompressionStrategy::default()) + .unwrap(); + let decoded = MiniBlockDecompressor::decompress( + decompressor.as_ref(), + vec![compressed.data[0].clone(), bitpacked_run_lengths], + expected.len() as u64, + ) + .unwrap(); + assert_decoded_i32_eq(decoded, &expected); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_rejects_two_count_dependent_child_encodings() { + let encoding = ProtobufUtils21::rle( + ProtobufUtils21::out_of_line_bitpacking(32, ProtobufUtils21::flat(3, None)), + ProtobufUtils21::out_of_line_bitpacking(8, ProtobufUtils21::flat(3, None)), + ); + + let err = DefaultDecompressionStrategy::default() + .create_miniblock_decompressor(&encoding, &DefaultDecompressionStrategy::default()) + .unwrap_err(); + assert!( + err.to_string() + .contains("cannot both require the run count") + ); + } + + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_general_compression() -> CompressionConfig { + if cfg!(feature = "zstd") { + CompressionConfig::new(CompressionScheme::Zstd, Some(3)) + } else { + CompressionConfig::new(CompressionScheme::Lz4, None) + } + } + + fn repeating_runs(num_runs: usize, run_length: usize) -> Vec { + let mut values = Vec::with_capacity(num_runs * run_length); + for run in 0..num_runs { + values.extend(std::iter::repeat_n((run % 8) as i32, run_length)); + } + values + } + + fn expect_rle(encoding: &CompressiveEncoding) -> &crate::format::pb21::Rle { + match encoding.compression.as_ref().unwrap() { + crate::format::pb21::compressive_encoding::Compression::Rle(rle) => rle, + other => panic!("expected RLE encoding, got {other:?}"), + } + } + + fn assert_decoded_i32_eq(decoded: DataBlock, expected: &[i32]) { + match decoded { + DataBlock::FixedWidth(block) => { + let values = block.data.borrow_to_typed_slice::(); + assert_eq!(values.as_ref(), expected); + } + _ => panic!("Expected FixedWidth block"), + } + } + + #[test] + #[cfg(any(feature = "lz4", feature = "zstd"))] + fn test_rle_miniblock_compressed_children_multiple_chunks() { + let compression = test_general_compression(); + let encoder = RleEncoder::with_child_encoding( + RunLengthWidth::U8, + Some(compression), + Some(compression), + false, + ); + let expected = repeating_runs(8192, 4); + let (compressed, encoding) = compress_miniblock( + &encoder, + DataBlock::from_array(Int32Array::from(expected.clone())), + ) + .unwrap(); + + assert!(compressed.chunks.len() > 1); + let rle = expect_rle(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + crate::format::pb21::compressive_encoding::Compression::General(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + crate::format::pb21::compressive_encoding::Compression::General(_) + )); + + let decoded = decompress_i32_chunks(&compressed, &encoding); + assert_eq!(decoded, expected); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_miniblock_bitpacks_values_child_when_smaller() { + let encoder = RleEncoder::with_child_encoding(RunLengthWidth::U8, None, None, true); + let expected = monotonic_runs(2048, 4); + let (compressed, encoding) = compress_miniblock( + &encoder, + DataBlock::from_array(Int32Array::from(expected.clone())), + ) + .unwrap(); + + let rle = expect_rle(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + crate::format::pb21::compressive_encoding::Compression::OutOfLineBitpacking(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + crate::format::pb21::compressive_encoding::Compression::Flat(_) + )); + + let decoded = decompress_i32_chunks(&compressed, &encoding); + assert_eq!(decoded, expected); + } + + #[test] + #[cfg(feature = "bitpacking")] + fn test_rle_miniblock_bitpacks_run_lengths_when_values_do_not_shrink() { + let encoder = RleEncoder::with_child_encoding(RunLengthWidth::U8, None, None, true); + let expected = high_entropy_runs(2048, 4); + let (compressed, encoding) = compress_miniblock( + &encoder, + DataBlock::from_array(Int32Array::from(expected.clone())), + ) + .unwrap(); + + let rle = expect_rle(&encoding); + assert!(matches!( + rle.values.as_ref().unwrap().compression.as_ref().unwrap(), + crate::format::pb21::compressive_encoding::Compression::Flat(_) + )); + assert!(matches!( + rle.run_lengths + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap(), + crate::format::pb21::compressive_encoding::Compression::OutOfLineBitpacking(_) + )); + + let decoded = decompress_i32_chunks(&compressed, &encoding); + assert_eq!(decoded, expected); + } + + fn decompress_i32_chunks( + compressed: &MiniBlockCompressed, + encoding: &CompressiveEncoding, + ) -> Vec { + let strategy = DefaultDecompressionStrategy::default(); + let decompressor = strategy + .create_miniblock_decompressor(encoding, &strategy) + .unwrap(); + let mut offsets = vec![0usize; compressed.data.len()]; + let mut values_processed = 0u64; + let mut decoded_values = Vec::new(); + + for chunk in &compressed.chunks { + let chunk_values = chunk.num_values(values_processed, compressed.num_values); + let mut chunk_buffers = Vec::with_capacity(chunk.buffer_sizes.len()); + for (idx, size) in chunk.buffer_sizes.iter().enumerate() { + let size = *size as usize; + chunk_buffers.push(compressed.data[idx].slice_with_length(offsets[idx], size)); + offsets[idx] += size; + } + + let decoded = decompressor + .decompress(chunk_buffers, chunk_values) + .unwrap(); + match decoded { + DataBlock::FixedWidth(block) => { + let values = block.data.borrow_to_typed_slice::(); + decoded_values.extend_from_slice(values.as_ref()); + } + _ => panic!("Expected FixedWidth block"), + } + values_processed += chunk_values; + } + + assert_eq!(values_processed, compressed.num_values); + decoded_values + } + + #[cfg(feature = "bitpacking")] + fn monotonic_runs(num_runs: usize, run_length: usize) -> Vec { + let mut values = Vec::with_capacity(num_runs * run_length); + for run in 0..num_runs { + values.extend(std::iter::repeat_n(run as i32, run_length)); + } + values + } + + #[cfg(feature = "bitpacking")] + fn high_entropy_runs(num_runs: usize, run_length: usize) -> Vec { + let mut values = Vec::with_capacity(num_runs * run_length); + let mut state = 7u64; + for _ in 0..num_runs { + state = state + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + values.extend(std::iter::repeat_n((state >> 32) as i32, run_length)); + } + values + } + + #[test] + fn test_select_run_length_width_prefers_u16_for_long_runs() { + let mut entries = [0u64; 3]; + accumulate_run_length_entries(300, Some(*MAX_MINIBLOCK_VALUES), &mut entries); + let (width, _) = select_run_length_width_from_entries(&entries, 32).unwrap(); + assert_eq!(width, RunLengthWidth::U16); + } + + // ========== Round-trip Tests for Different Types ========== + + #[test] + fn test_round_trip_all_types() { + // Test u8 + test_round_trip_helper(vec![42u8, 42, 42, 100, 100, 255, 255, 255, 255], 8); + + // Test u16 + test_round_trip_helper(vec![1000u16, 1000, 2000, 2000, 2000, 3000], 16); + + // Test i32 + test_round_trip_helper(vec![100i32, 100, 100, -200, -200, 300, 300, 300, 300], 32); + + // Test u64 + test_round_trip_helper(vec![1_000_000_000u64; 5], 64); + } + + fn test_round_trip_helper(data: Vec, bits_per_value: u64) + where + T: bytemuck::Pod + PartialEq + std::fmt::Debug, + { + let encoder = RleEncoder::new(); + let bytes: Vec = data + .iter() + .flat_map(|v| bytemuck::bytes_of(v)) + .copied() + .collect(); + + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value, + data: LanceBuffer::from(bytes), + num_values: data.len() as u64, + block_info: BlockInfo::default(), + }); + + let (compressed, _) = compress_miniblock(&encoder, block).unwrap(); + let decompressor = RleDecompressor::new(bits_per_value); + let decompressed = MiniBlockDecompressor::decompress( + &decompressor, + compressed.data, + compressed.num_values, + ) + .unwrap(); + + match decompressed { + DataBlock::FixedWidth(ref block) => { + // Verify the decompressed data length matches expected + assert_eq!(block.data.len(), data.len() * std::mem::size_of::()); + } + _ => panic!("Expected FixedWidth block"), + } + } + + // ========== Chunk Boundary Tests ========== + + #[test] + fn test_power_of_two_chunking() { + let encoder = RleEncoder::new(); + + // Create data that will require multiple chunks + let test_sizes = vec![1000, 2500, 5000, 10000]; + + for size in test_sizes { + let data: Vec = (0..size) + .map(|i| i / 50) // Create runs of 50 + .collect(); + + let array = Int32Array::from(data); + let (compressed, _) = + compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + // Verify all non-last chunks have power-of-2 values + for (i, chunk) in compressed.chunks.iter().enumerate() { + if i < compressed.chunks.len() - 1 { + assert!(chunk.log_num_values > 0); + let chunk_values = 1u64 << chunk.log_num_values; + assert!(chunk_values.is_power_of_two()); + assert!(chunk_values <= *MAX_MINIBLOCK_VALUES); + } else { + assert_eq!(chunk.log_num_values, 0); + } + } + } + } + + #[rstest] + #[case::u8_lengths(RunLengthWidth::U8)] + #[case::u16_lengths(RunLengthWidth::U16)] + #[case::u32_lengths(RunLengthWidth::U32)] + fn test_miniblock_chunk_counts_match_encoded_runs(#[case] run_length_width: RunLengthWidth) { + // This pattern crosses the 2,048-value boundary in the middle of a two-value run. + let levels = (0..4098) + .map(|index| if index % 3 == 0 { 1u16 } else { 0u16 }) + .collect::>(); + let num_values = levels.len() as u64; + let encoder = RleEncoder::with_run_length_width(run_length_width); + let (buffers, chunks) = encoder + .encode_data( + &LanceBuffer::reinterpret_vec(levels), + num_values, + u16::BITS as u64, + ) + .unwrap(); + + assert_eq!(buffers.len(), 2); + let bytes_per_length = run_length_width.bytes_per_value(); + let mut values_offset = 0usize; + let mut lengths_offset = 0usize; + let mut values_processed = 0u64; + + for chunk in &chunks { + let values_size = chunk.buffer_sizes[0] as usize; + let lengths_size = chunk.buffer_sizes[1] as usize; + let lengths_end = lengths_offset + lengths_size; + let chunk_lengths = &buffers[1].as_ref()[lengths_offset..lengths_end]; + let length_chunks = chunk_lengths.chunks_exact(bytes_per_length); + assert!(length_chunks.remainder().is_empty()); + let num_runs = length_chunks.len(); + let encoded_values = length_chunks + .map(|bytes| run_length_width.read_length(bytes)) + .sum::(); + let declared_values = chunk.num_values(values_processed, num_values); + + assert_eq!(values_size, num_runs * size_of::()); + assert_eq!(encoded_values, declared_values); + + values_offset += values_size; + lengths_offset = lengths_end; + values_processed += declared_values; + } + + assert_eq!(values_processed, num_values); + assert_eq!(values_offset, buffers[0].len()); + assert_eq!(lengths_offset, buffers[1].len()); + } + + // ========== Error Handling Tests ========== + + #[test] + fn test_encoder_rejects_zero_progress() { + let error = RleEncoder::new() + .encode_data(&LanceBuffer::empty(), 1, u16::BITS as u64) + .unwrap_err(); + + assert!( + matches!(&error, Error::Internal { .. }), + "expected internal error, got: {error:?}" + ); + assert!(error.to_string().contains("made no progress")); + assert!(error.to_string().contains("values_remaining=1")); + } + + #[test] + fn test_invalid_buffer_count() { + let decompressor = RleDecompressor::new(32); + let result = MiniBlockDecompressor::decompress( + &decompressor, + vec![LanceBuffer::from(vec![1, 2, 3, 4])], + 10, + ); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("expects exactly 2 buffers") + ); + } + + #[test] + fn test_buffer_consistency() { + let decompressor = RleDecompressor::new(32); + let values = LanceBuffer::from(vec![1, 0, 0, 0]); // 1 i32 value + let lengths = LanceBuffer::from(vec![5, 10]); // 2 lengths - mismatch! + let result = MiniBlockDecompressor::decompress(&decompressor, vec![values, lengths], 15); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("Inconsistent RLE buffers") + ); + } + + #[test] + fn test_u16_length_buffer_must_be_aligned() { + let decompressor = RleDecompressor::with_run_length_width(32, RunLengthWidth::U16); + let values = LanceBuffer::from(vec![1, 0, 0, 0]); + let lengths = LanceBuffer::from(vec![5]); + let result = MiniBlockDecompressor::decompress(&decompressor, vec![values, lengths], 5); + assert!(matches!(&result, Err(Error::InvalidInput { .. }))); + assert!( + result + .unwrap_err() + .to_string() + .contains("not divisible by 2") + ); + } + + #[test] + fn test_rle_rejects_underflow_and_zero_lengths_and_clamps_overflow() { + let decompressor = RleDecompressor::with_run_length_width(32, RunLengthWidth::U16); + let value = LanceBuffer::from(1i32.to_le_bytes().to_vec()); + + let underflow = MiniBlockDecompressor::decompress( + &decompressor, + vec![ + value.clone(), + LanceBuffer::from(4u16.to_le_bytes().to_vec()), + ], + 5, + ) + .unwrap_err(); + assert!(underflow.to_string().contains("produced 4 values")); + + let overflow = MiniBlockDecompressor::decompress( + &decompressor, + vec![ + value.clone(), + LanceBuffer::from(6u16.to_le_bytes().to_vec()), + ], + 5, + ) + .unwrap(); + match overflow { + DataBlock::FixedWidth(block) => { + assert_eq!(block.num_values, 5); + let decoded = block.data.borrow_to_typed_slice::(); + assert_eq!(decoded.as_ref(), &[1i32; 5]); + } + _ => panic!("Expected FixedWidth block"), + } + + let zero = MiniBlockDecompressor::decompress( + &decompressor, + vec![value, LanceBuffer::from(0u16.to_le_bytes().to_vec())], + 5, + ) + .unwrap_err(); + assert!(zero.to_string().contains("zero run length")); + } + + #[test] + fn test_block_rle_rejects_overflow() { + // Block payloads have no chunk boundaries, so run lengths summing past + // num_values can only be corruption and must stay a hard error. + let decompressor = RleDecompressor::with_run_length_width(32, RunLengthWidth::U16); + let values = 1i32.to_le_bytes(); + let lengths = 6u16.to_le_bytes(); + let mut payload = Vec::new(); + payload.extend_from_slice(&(values.len() as u64).to_le_bytes()); + payload.extend_from_slice(&values); + payload.extend_from_slice(&lengths); + + let error = BlockDecompressor::decompress(&decompressor, LanceBuffer::from(payload), 5) + .unwrap_err(); + assert!(matches!(&error, Error::InvalidInput { .. })); + assert!( + error + .to_string() + .contains("overflowed expected value count") + ); + } + + #[test] + fn test_rle_truncates_legacy_chunk_boundary_overflow() { + // Legacy encoders emitted chunks declaring 2048 values whose final run crossed + // the checkpoint boundary (e.g. run lengths summing to 2080); the excess values + // are duplicated at the start of the next chunk and must be ignored here. + let decompressor = RleDecompressor::with_run_length_width(32, RunLengthWidth::U16); + let mut values = Vec::new(); + values.extend_from_slice(&7i32.to_le_bytes()); + values.extend_from_slice(&8i32.to_le_bytes()); + let mut lengths = Vec::new(); + lengths.extend_from_slice(&2000u16.to_le_bytes()); + lengths.extend_from_slice(&80u16.to_le_bytes()); + + let decoded = MiniBlockDecompressor::decompress( + &decompressor, + vec![LanceBuffer::from(values), LanceBuffer::from(lengths)], + 2048, + ) + .unwrap(); + match decoded { + DataBlock::FixedWidth(block) => { + assert_eq!(block.num_values, 2048); + let decoded = block.data.borrow_to_typed_slice::(); + let decoded = decoded.as_ref(); + assert_eq!(decoded.len(), 2048); + assert!(decoded[..2000].iter().all(|&v| v == 7)); + assert!(decoded[2000..].iter().all(|&v| v == 8)); + } + _ => panic!("Expected FixedWidth block"), + } + } + + #[test] + fn test_empty_data_handling() { + let encoder = RleEncoder::new(); + + // Test empty block + let empty_block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::from(vec![]), + num_values: 0, + block_info: BlockInfo::default(), + }); + + let (compressed, _) = compress_miniblock(&encoder, empty_block).unwrap(); + assert_eq!(compressed.num_values, 0); + assert!(compressed.data.is_empty()); + + // Test decompression of empty data + let decompressor = RleDecompressor::new(32); + let decompressed = MiniBlockDecompressor::decompress(&decompressor, vec![], 0).unwrap(); + + match decompressed { + DataBlock::FixedWidth(ref block) => { + assert_eq!(block.num_values, 0); + assert_eq!(block.data.len(), 0); + } + _ => panic!("Expected FixedWidth block"), + } + } + + // ========== Integration Test ========== + + #[test] + fn test_multi_chunk_round_trip() { + let encoder = RleEncoder::new(); + + // Create data that spans multiple chunks with mixed patterns + let mut data = Vec::new(); + + // High compression section + data.extend(vec![999i32; 2000]); + // Low compression section + data.extend(0..1000); + // Another high compression section + data.extend(vec![777i32; 2000]); + + let array = Int32Array::from(data.clone()); + let (compressed, _) = compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + // Manually decompress all chunks + let mut reconstructed = Vec::new(); + let mut values_offset = 0usize; + let mut lengths_offset = 0usize; + let mut values_processed = 0u64; + + // We now have exactly 2 global buffers + assert_eq!(compressed.data.len(), 2); + let global_values = &compressed.data[0]; + let global_lengths = &compressed.data[1]; + + for chunk in &compressed.chunks { + let chunk_values = if chunk.log_num_values > 0 { + 1u64 << chunk.log_num_values + } else { + compressed.num_values - values_processed + }; + + // Extract chunk buffers from global buffers using buffer_sizes + let values_size = chunk.buffer_sizes[0] as usize; + let lengths_size = chunk.buffer_sizes[1] as usize; + + let chunk_values_buffer = global_values.slice_with_length(values_offset, values_size); + let chunk_lengths_buffer = + global_lengths.slice_with_length(lengths_offset, lengths_size); + + let decompressor = RleDecompressor::new(32); + let chunk_data = MiniBlockDecompressor::decompress( + &decompressor, + vec![chunk_values_buffer, chunk_lengths_buffer], + chunk_values, + ) + .unwrap(); + + values_offset += values_size; + lengths_offset += lengths_size; + values_processed += chunk_values; + + match chunk_data { + DataBlock::FixedWidth(ref block) => { + let values: &[i32] = bytemuck::cast_slice(block.data.as_ref()); + reconstructed.extend_from_slice(values); + } + _ => panic!("Expected FixedWidth block"), + } + } + + assert_eq!(reconstructed, data); + } + + #[test] + fn test_1024_boundary_conditions() { + // Comprehensive test for various boundary conditions at 1024 values + // This consolidates multiple bug tests that were previously separate + let encoder = RleEncoder::new(); + let decompressor = RleDecompressor::new(32); + + let test_cases = [ + ("runs_of_2", { + let mut data = Vec::new(); + for i in 0..512 { + data.push(i); + data.push(i); + } + data + }), + ("single_run_1024", vec![42i32; 1024]), + ("alternating_values", { + let mut data = Vec::new(); + for i in 0..1024 { + data.push(i % 2); + } + data + }), + ("run_boundary_255s", { + let mut data = Vec::new(); + data.extend(vec![1i32; 255]); + data.extend(vec![2i32; 255]); + data.extend(vec![3i32; 255]); + data.extend(vec![4i32; 255]); + data.extend(vec![5i32; 4]); + data + }), + ("unique_values_1024", (0..1024).collect::>()), + ("unique_plus_duplicate", { + // 1023 unique values followed by one duplicate (regression test) + let mut data = Vec::new(); + for i in 0..1023 { + data.push(i); + } + data.push(1022i32); // Last value same as second-to-last + data + }), + ("bug_4092_pattern", { + // Test exact scenario that produces 4092 bytes instead of 4096 + let mut data = Vec::new(); + for i in 0..1022 { + data.push(i); + } + data.push(999999i32); + data.push(999999i32); + data + }), + ]; + + for (test_name, data) in test_cases.iter() { + assert_eq!(data.len(), 1024, "Test case {} has wrong length", test_name); + + // Compress the data + let array = Int32Array::from(data.clone()); + let (compressed, _) = + compress_miniblock(&encoder, DataBlock::from_array(array)).unwrap(); + + // Decompress and verify + match MiniBlockDecompressor::decompress( + &decompressor, + compressed.data, + compressed.num_values, + ) { + Ok(decompressed) => match decompressed { + DataBlock::FixedWidth(ref block) => { + let values: &[i32] = bytemuck::cast_slice(block.data.as_ref()); + assert_eq!( + values.len(), + 1024, + "Test case {} got {} values, expected 1024", + test_name, + values.len() + ); + assert_eq!( + block.data.len(), + 4096, + "Test case {} got {} bytes, expected 4096", + test_name, + block.data.len() + ); + assert_eq!(values, &data[..], "Test case {} data mismatch", test_name); + } + _ => panic!("Test case {} expected FixedWidth block", test_name), + }, + Err(e) => { + if e.to_string().contains("4092") { + panic!("Test case {} found bug 4092: {}", test_name, e); + } + panic!("Test case {} failed with error: {}", test_name, e); + } + } + } + } + + #[test] + fn test_low_repetition_50pct_bug() { + // Test case that reproduces the 4092 bytes bug with low repetition (50%) + // This simulates the 1M benchmark case + let encoder = RleEncoder::new(); + + // Create 1M values with low repetition (50% chance of change) + let num_values = 1_048_576; // 1M values + let mut data = Vec::with_capacity(num_values); + let mut value = 0i32; + let mut rng = 12345u64; // Simple deterministic RNG + + for _ in 0..num_values { + data.push(value); + // Simple LCG for deterministic randomness + rng = rng.wrapping_mul(1664525).wrapping_add(1013904223); + // 50% chance to increment value + if (rng >> 16) & 1 == 1 { + value += 1; + } + } + + let bytes: Vec = data.iter().flat_map(|v| v.to_le_bytes()).collect(); + + let block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: 32, + data: LanceBuffer::from(bytes), + num_values: num_values as u64, + block_info: BlockInfo::default(), + }); + + let (compressed, _) = compress_miniblock(&encoder, block).unwrap(); + + // Debug first few chunks + for (i, chunk) in compressed.chunks.iter().take(5).enumerate() { + let _chunk_values = if chunk.log_num_values > 0 { + 1 << chunk.log_num_values + } else { + // Last chunk - calculate remaining + let prev_total: usize = compressed.chunks[..i] + .iter() + .map(|c| 1usize << c.log_num_values) + .sum(); + num_values - prev_total + }; + } + + // Try to decompress + let decompressor = RleDecompressor::new(32); + match MiniBlockDecompressor::decompress( + &decompressor, + compressed.data, + compressed.num_values, + ) { + Ok(decompressed) => match decompressed { + DataBlock::FixedWidth(ref block) => { + assert_eq!( + block.data.len(), + num_values * 4, + "Expected {} bytes but got {}", + num_values * 4, + block.data.len() + ); + } + _ => panic!("Expected FixedWidth block"), + }, + Err(e) => { + if e.to_string().contains("4092") { + panic!("Bug reproduced! {}", e); + } else { + panic!("Unexpected error: {}", e); + } + } + } + } + + // ========== Encoding Verification Tests ========== + + #[test_log::test(tokio::test)] + async fn test_rle_encoding_verification() { + use crate::testing::{TestCases, check_round_trip_encoding_of_data}; + use arrow_array::{Array, Int32Array}; + use lance_datagen::{ArrayGenerator, RowCount}; + use std::collections::HashMap; + use std::sync::Arc; + + let test_cases = TestCases::default() + .with_expected_encoding("rle") + .with_structural_encodings(); + + // Test both explicit metadata and automatic selection + // 1. Test with explicit RLE threshold metadata (also disable BSS) + let mut metadata_explicit = HashMap::new(); + metadata_explicit.insert( + "lance-encoding:rle-threshold".to_string(), + "0.8".to_string(), + ); + metadata_explicit.insert("lance-encoding:bss".to_string(), "off".to_string()); + + let mut generator = RleDataGenerator::new(vec![ + i32::MIN, + i32::MIN, + i32::MIN, + i32::MIN, + i32::MIN + 1, + i32::MIN + 1, + i32::MIN + 1, + i32::MIN + 1, + i32::MIN + 2, + i32::MIN + 2, + i32::MIN + 2, + i32::MIN + 2, + ]); + let data_explicit = generator.generate_default(RowCount::from(10000)).unwrap(); + check_round_trip_encoding_of_data(vec![data_explicit], &test_cases, metadata_explicit) + .await; + + // 2. Test automatic RLE selection based on data characteristics + // 80% repetition should trigger RLE (> default 50% threshold). + // + // Use values with the high bit set so bitpacking can't shrink the values. + // Explicitly disable BSS to ensure RLE is tested + let mut metadata = HashMap::new(); + metadata.insert("lance-encoding:bss".to_string(), "off".to_string()); + + let mut values = vec![i32::MIN; 8000]; // 80% repetition + values.extend( + [ + i32::MIN + 1, + i32::MIN + 2, + i32::MIN + 3, + i32::MIN + 4, + i32::MIN + 5, + ] + .repeat(400), + ); // 20% variety + let arr = Arc::new(Int32Array::from(values)) as Arc; + check_round_trip_encoding_of_data(vec![arr], &test_cases, metadata).await; + + #[cfg(any(feature = "lz4", feature = "zstd"))] + { + let mut metadata = HashMap::new(); + metadata.insert( + "lance-encoding:rle-threshold".to_string(), + "0.8".to_string(), + ); + metadata.insert("lance-encoding:bss".to_string(), "off".to_string()); + metadata.insert( + "lance-encoding:compression".to_string(), + if cfg!(feature = "zstd") { + "zstd".to_string() + } else { + "lz4".to_string() + }, + ); + let mut values = Vec::with_capacity(2048 * 4); + for run in 0..2048 { + values.extend(std::iter::repeat_n(i32::MIN + (run % 8), 4)); + } + let arr = Arc::new(Int32Array::from(values)) as Arc; + check_round_trip_encoding_of_data(vec![arr], &test_cases, metadata).await; + } + } + + /// Generator that produces repetitive patterns suitable for RLE + #[derive(Debug)] + struct RleDataGenerator { + pattern: Vec, + idx: usize, + } + + impl RleDataGenerator { + fn new(pattern: Vec) -> Self { + Self { pattern, idx: 0 } + } + } + + impl lance_datagen::ArrayGenerator for RleDataGenerator { + fn generate( + &mut self, + _length: lance_datagen::RowCount, + _rng: &mut rand_xoshiro::Xoshiro256PlusPlus, + ) -> std::result::Result, arrow_schema::ArrowError> + { + use arrow_array::Int32Array; + use std::sync::Arc; + + // Generate enough repetitive data to trigger RLE + let mut values = Vec::new(); + for _ in 0..10000 { + values.push(self.pattern[self.idx]); + self.idx = (self.idx + 1) % self.pattern.len(); + } + Ok(Arc::new(Int32Array::from(values))) + } + + fn data_type(&self) -> &arrow_schema::DataType { + &arrow_schema::DataType::Int32 + } + + fn element_size_bytes(&self) -> Option { + Some(lance_datagen::ByteCount::from(4)) + } + } + + // ========== Block Related tests ========== + #[test] + fn test_block_decompressor_rejects_overflowing_values_size() { + let decompressor = RleDecompressor::new(32); + + let mut data = Vec::new(); + data.extend_from_slice(&u64::MAX.to_le_bytes()); + let result = BlockDecompressor::decompress(&decompressor, LanceBuffer::from(data), 1); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("Invalid RLE values buffer size") + ); + } + + #[test] + fn test_block_decompressor_too_small() { + let decompressor = RleDecompressor::new(32); + let result = + BlockDecompressor::decompress(&decompressor, LanceBuffer::from(vec![1, 2, 3]), 10); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("Insufficient data size: 3") + ); + } + + #[test] + fn test_block_compressor_header_format() { + let encoder = RleEncoder::new(); + + let data = vec![1i32, 1, 1]; + let array = Int32Array::from(data); + let compressed = BlockCompressor::compress(&encoder, DataBlock::from_array(array)).unwrap(); + + // Verify header format: first 8 bytes should be values_size as u64 + assert!(compressed.len() >= 8); + let values_size_bytes: [u8; 8] = compressed.as_ref()[..8].try_into().unwrap(); + let values_size = u64::from_le_bytes(values_size_bytes); + + // Values buffer should contain 1 i32 value (4 bytes) + assert_eq!(values_size, 4); + + // Total size should be: 8 (header) + 4 (values) + 1 (lengths) + assert_eq!(compressed.len(), 13); + } + + #[test] + fn test_block_compressor_round_trip() { + let encoder = RleEncoder::new(); + let decompressor = RleDecompressor::new(32); + + // Test basic pattern + let data = vec![1i32, 1, 1, 2, 2, 3, 3, 3, 3]; + let array = Int32Array::from(data.clone()); + let data_block = DataBlock::from_array(array); + + let compressed = BlockCompressor::compress(&encoder, data_block).unwrap(); + let decompressed = + BlockDecompressor::decompress(&decompressor, compressed, data.len() as u64).unwrap(); + + match decompressed { + DataBlock::FixedWidth(block) => { + let values: &[i32] = bytemuck::cast_slice(block.data.as_ref()); + assert_eq!(values, &data[..]); + } + _ => panic!("Expected FixedWidth block"), + } + } + + #[test] + fn test_block_compressor_large_data() { + let encoder = RleEncoder::new(); + let decompressor = RleDecompressor::new(32); + + // Create data that will span multiple chunks + // Each chunks can handle ~2048 values, so use 10K values + let mut data = Vec::new(); + data.extend(vec![999i32; 3000]); // First ~2 chunks + data.extend(vec![777i32; 3000]); // Next ~2 chunks + data.extend(vec![555i32; 4000]); // Final ~2 chunks + + let total_values = data.len(); + assert_eq!(total_values, 10000); + + let array = Int32Array::from(data.clone()); + let compressed = BlockCompressor::compress(&encoder, DataBlock::from_array(array)).unwrap(); + let decompressed = + BlockDecompressor::decompress(&decompressor, compressed, total_values as u64).unwrap(); + + match decompressed { + DataBlock::FixedWidth(block) => { + let values: &[i32] = bytemuck::cast_slice(block.data.as_ref()); + assert_eq!(values.len(), total_values); + assert_eq!(values, &data[..]); + } + _ => panic!("Expected FixedWidth block"), + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/encodings/physical/value.rs b/lance-artifact/rust/lance-encoding/src/encodings/physical/value.rs new file mode 100644 index 000000000..0284e9784 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/encodings/physical/value.rs @@ -0,0 +1,1286 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use arrow_buffer::{BooleanBufferBuilder, bit_util}; + +use crate::buffer::LanceBuffer; +use crate::compression::{ + BlockCompressor, BlockDecompressor, FixedPerValueDecompressor, MiniBlockDecompressor, +}; +use crate::data::{ + BlockInfo, DataBlock, FixedSizeListBlock, FixedWidthDataBlock, NullableDataBlock, +}; +use crate::encodings::logical::primitive::fullzip::{PerValueCompressor, PerValueDataBlock}; +use crate::encodings::logical::primitive::miniblock::{ + MAX_MINIBLOCK_BYTES, MAX_MINIBLOCK_VALUES, MiniBlockChunk, MiniBlockCompressed, + MiniBlockCompressionContext, MiniBlockCompressor, +}; +use crate::format::ProtobufUtils21; +use crate::format::pb21::compressive_encoding::Compression; +use crate::format::pb21::{self, CompressiveEncoding}; + +use lance_core::{Error, Result}; + +/// A compression strategy that writes fixed-width data as-is (no compression) +#[derive(Debug, Default)] +pub struct ValueEncoder {} + +impl ValueEncoder { + /// Use the largest chunk we can smaller than 4KiB + fn find_log_vals_per_chunk(bytes_per_word: u64, values_per_word: u64) -> Result<(u64, u64)> { + let mut size_bytes = 2 * bytes_per_word; + let (mut log_num_vals, mut num_vals) = match values_per_word { + 1 => (1, 2), + 8 => (3, 8), + _ => unreachable!(), + }; + + if size_bytes >= MAX_MINIBLOCK_BYTES { + let num_values = 2 * values_per_word; + return Err(Error::invalid_input(format!( + "Value is too wide for miniblock encoding: {} values require {} bytes but a \ + miniblock chunk is limited to {} bytes.", + num_values, size_bytes, MAX_MINIBLOCK_BYTES + ))); + } + + while 2 * size_bytes < MAX_MINIBLOCK_BYTES && 2 * num_vals <= *MAX_MINIBLOCK_VALUES { + log_num_vals += 1; + size_bytes *= 2; + num_vals *= 2; + } + + Ok((log_num_vals, num_vals)) + } + + fn chunk_data(data: FixedWidthDataBlock) -> Result { + // Usually there are X bytes per value. However, when working with boolean + // or FSL we might have some number of bits per value that isn't + // divisible by 8. In this case, to avoid chunking in the middle of a byte + // we calculate how many 8-value words we can fit in a chunk. + let (bytes_per_word, values_per_word) = if data.bits_per_value.is_multiple_of(8) { + (data.bits_per_value / 8, 1) + } else { + (data.bits_per_value, 8) + }; + + // Aim for 4KiB chunks + let (log_vals_per_chunk, vals_per_chunk) = + Self::find_log_vals_per_chunk(bytes_per_word, values_per_word)?; + let num_chunks = bit_util::ceil(data.num_values as usize, vals_per_chunk as usize); + debug_assert_eq!(vals_per_chunk % values_per_word, 0); + let bytes_per_chunk = bytes_per_word * (vals_per_chunk / values_per_word); + let bytes_per_chunk = u32::try_from(bytes_per_chunk).unwrap(); + debug_assert!(bytes_per_chunk > 0); + + let data_buffer = data.data; + + let mut row_offset = 0; + let mut chunks = Vec::with_capacity(num_chunks); + + let mut bytes_counter = 0; + loop { + if row_offset + vals_per_chunk <= data.num_values { + // We can make a full chunk + chunks.push(MiniBlockChunk { + log_num_values: log_vals_per_chunk as u8, + buffer_sizes: vec![bytes_per_chunk], + }); + row_offset += vals_per_chunk; + bytes_counter += bytes_per_chunk as u64; + } else if row_offset < data.num_values { + // Final chunk, special values + let num_bytes = data_buffer.len() as u64 - bytes_counter; + let num_bytes = u32::try_from(num_bytes).unwrap(); + chunks.push(MiniBlockChunk { + log_num_values: 0, + buffer_sizes: vec![num_bytes], + }); + break; + } else { + // If we get here then all chunks were full chunks and we have no remainder chunk + break; + } + } + + debug_assert_eq!(chunks.len(), num_chunks); + + Ok(MiniBlockCompressed { + chunks, + data: vec![data_buffer], + num_values: data.num_values, + }) + } +} + +#[derive(Debug)] +struct MiniblockFslLayer { + validity: Option, + dimension: u64, +} + +/// This impl deals with encoding FSL>>> data as a mini-block compressor. +/// The tricky part of FSL data is that we want to include inner validity buffers (we don't want these +/// to be part of the rep-def because that usually ends up being more expensive). +/// +/// The resulting mini-block will, instead of having a single buffer, have X + 1 buffers where X is +/// the number of FSL layers that contain validity. +/// +/// In the simple case where there is no validity inside the FSL layers, all we are doing here is flattening +/// the FSL layers into a single buffer. +/// +/// Also: We don't allow a row to be broken across chunks. This typically isn't too big of a deal since we +/// are usually dealing with relatively small vectors if we are using mini-block. +/// +/// Note: when we do have validity we have to make copies of the validity buffers because they are bit buffers +/// and we need to bit slice them which requires copies or offsets. Paying the price at write time to make +/// the copies is better than paying the price at read time to do the bit slicing. +impl ValueEncoder { + fn make_fsl_encoding(layers: &[MiniblockFslLayer], bits_per_value: u64) -> CompressiveEncoding { + let mut encoding = ProtobufUtils21::flat(bits_per_value, None); + for layer in layers.iter().rev() { + let has_validity = layer.validity.is_some(); + let dimension = layer.dimension; + encoding = ProtobufUtils21::fsl(dimension, has_validity, encoding); + } + encoding + } + + fn extract_fsl_chunk( + data: &FixedWidthDataBlock, + layers: &[MiniblockFslLayer], + row_offset: usize, + num_rows: usize, + validity_buffers: &mut [Vec], + ) -> Vec { + let mut row_offset = row_offset; + let mut num_values = num_rows; + let mut buffer_counter = 0; + let mut buffer_sizes = Vec::with_capacity(validity_buffers.len() + 1); + for layer in layers { + row_offset *= layer.dimension as usize; + num_values *= layer.dimension as usize; + if let Some(validity) = &layer.validity { + let validity_slice = validity + .clone() + .bit_slice_le_with_length(row_offset, num_values); + validity_buffers[buffer_counter].extend_from_slice(&validity_slice); + buffer_sizes.push(validity_slice.len() as u32); + buffer_counter += 1; + } + } + + let bits_in_chunk = data.bits_per_value * num_values as u64; + let bytes_in_chunk = bits_in_chunk.div_ceil(8); + let bytes_in_chunk = u32::try_from(bytes_in_chunk).unwrap(); + debug_assert!(bytes_in_chunk > 0); + buffer_sizes.push(bytes_in_chunk); + + buffer_sizes + } + + fn chunk_fsl( + data: FixedWidthDataBlock, + layers: Vec, + num_rows: u64, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + // Count size to calculate rows per chunk + let mut ceil_bytes_validity = 0; + let mut cum_dim = 1; + let mut num_validity_buffers = 0; + for layer in &layers { + cum_dim *= layer.dimension; + if layer.validity.is_some() { + ceil_bytes_validity += cum_dim.div_ceil(8); + num_validity_buffers += 1; + } + } + // It's an estimate because validity buffers may have some padding bits + let cum_bits_per_value = data.bits_per_value * cum_dim; + let (cum_bytes_per_word, vals_per_word) = if cum_bits_per_value.is_multiple_of(8) { + (cum_bits_per_value / 8, 1) + } else { + (cum_bits_per_value, 8) + }; + let est_bytes_per_word = (ceil_bytes_validity * vals_per_word) + cum_bytes_per_word; + let (log_rows_per_chunk, rows_per_chunk) = + Self::find_log_vals_per_chunk(est_bytes_per_word, vals_per_word)?; + + let num_chunks = num_rows.div_ceil(rows_per_chunk) as usize; + + // Allocate buffers for validity, these will be slightly bigger than the input validity buffers + let mut chunks = Vec::with_capacity(num_chunks); + let mut validity_buffers: Vec> = Vec::with_capacity(num_validity_buffers); + cum_dim = 1; + for layer in &layers { + cum_dim *= layer.dimension; + if let Some(validity) = &layer.validity { + let layer_bytes_validity = cum_dim.div_ceil(8); + let validity_with_padding = + layer_bytes_validity as usize * num_chunks * rows_per_chunk as usize; + debug_assert!(validity_with_padding >= validity.len()); + validity_buffers.push(Vec::with_capacity( + layer_bytes_validity as usize * num_chunks, + )); + } + } + + // Now go through and extract validity buffers + let mut row_offset = 0; + while row_offset + rows_per_chunk <= num_rows { + let buffer_sizes = Self::extract_fsl_chunk( + &data, + &layers, + row_offset as usize, + rows_per_chunk as usize, + &mut validity_buffers, + ); + row_offset += rows_per_chunk; + chunks.push(MiniBlockChunk { + log_num_values: log_rows_per_chunk as u8, + buffer_sizes, + }) + } + let rows_in_chunk = num_rows - row_offset; + if rows_in_chunk > 0 { + let buffer_sizes = Self::extract_fsl_chunk( + &data, + &layers, + row_offset as usize, + rows_in_chunk as usize, + &mut validity_buffers, + ); + chunks.push(MiniBlockChunk { + log_num_values: 0, + buffer_sizes, + }); + } + + let encoding = Self::make_fsl_encoding(&layers, data.bits_per_value); + // Finally, add the data buffer + let buffers = validity_buffers + .into_iter() + .map(LanceBuffer::from) + .chain(std::iter::once(data.data)) + .collect::>(); + + Ok(( + MiniBlockCompressed { + chunks, + data: buffers, + num_values: num_rows, + }, + encoding, + )) + } + + fn miniblock_fsl(data: DataBlock) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + let num_rows = data.num_values(); + let fsl = data.as_fixed_size_list().unwrap(); + let mut layers = Vec::new(); + let mut child = *fsl.child; + let mut cur_layer = MiniblockFslLayer { + validity: None, + dimension: fsl.dimension, + }; + loop { + if let DataBlock::Nullable(nullable) = child { + cur_layer.validity = Some(nullable.nulls); + child = *nullable.data; + } + match child { + DataBlock::FixedSizeList(inner) => { + layers.push(cur_layer); + cur_layer = MiniblockFslLayer { + validity: None, + dimension: inner.dimension, + }; + child = *inner.child; + } + DataBlock::FixedWidth(inner) => { + layers.push(cur_layer); + return Self::chunk_fsl(inner, layers, num_rows); + } + _ => unreachable!("Unexpected data block type in value encoder's miniblock_fsl"), + } + } + } +} + +struct PerValueFslValidityIter { + buffer: LanceBuffer, + bits_per_row: usize, + offset: usize, +} + +/// In this section we deal with per-value encoding of FSL>>> data. +/// +/// It's easier than mini-block. All we need to do is flatten the FSL layers into a single buffer. +/// This includes any validity buffers we encounter on the way. +impl ValueEncoder { + fn fsl_to_encoding(fsl: &FixedSizeListBlock) -> CompressiveEncoding { + let mut inner = fsl.child.as_ref(); + let mut has_validity = false; + inner = match inner { + DataBlock::Nullable(nullable) => { + has_validity = true; + nullable.data.as_ref() + } + DataBlock::AllNull(_) => { + return ProtobufUtils21::constant(None); + } + _ => inner, + }; + let inner_encoding = match inner { + DataBlock::FixedWidth(fixed_width) => { + ProtobufUtils21::flat(fixed_width.bits_per_value, None) + } + DataBlock::FixedSizeList(inner) => Self::fsl_to_encoding(inner), + _ => unreachable!( + "Unexpected data block type in value encoder's fsl_to_encoding: {}", + inner.name() + ), + }; + ProtobufUtils21::fsl(fsl.dimension, has_validity, inner_encoding) + } + + fn simple_per_value_fsl(fsl: FixedSizeListBlock) -> (PerValueDataBlock, CompressiveEncoding) { + // The simple case is zero-copy, we just return the flattened inner buffer + let encoding = Self::fsl_to_encoding(&fsl); + let num_values = fsl.num_values(); + let mut child = *fsl.child; + let mut cum_dim = 1; + loop { + cum_dim *= fsl.dimension; + match child { + DataBlock::Nullable(nullable) => { + child = *nullable.data; + } + DataBlock::FixedSizeList(inner) => { + child = *inner.child; + } + DataBlock::FixedWidth(inner) => { + let data = FixedWidthDataBlock { + bits_per_value: inner.bits_per_value * cum_dim, + num_values, + data: inner.data, + block_info: BlockInfo::new(), + }; + return (PerValueDataBlock::Fixed(data), encoding); + } + _ => unreachable!( + "Unexpected data block type in value encoder's simple_per_value_fsl" + ), + } + } + } + + fn nullable_per_value_fsl(fsl: FixedSizeListBlock) -> (PerValueDataBlock, CompressiveEncoding) { + // If there are nullable inner values then we need to zip the validity with the values + let encoding = Self::fsl_to_encoding(&fsl); + let num_values = fsl.num_values(); + let mut bytes_per_row = 0; + let mut cum_dim = 1; + let mut current = fsl; + let mut validity_iters: Vec = Vec::new(); + let data_bytes_per_row: usize; + let data_buffer: LanceBuffer; + loop { + cum_dim *= current.dimension; + let mut child = *current.child; + if let DataBlock::Nullable(nullable) = child { + // Each item will need this many bytes of validity prepended to it + bytes_per_row += cum_dim.div_ceil(8) as usize; + validity_iters.push(PerValueFslValidityIter { + buffer: nullable.nulls, + bits_per_row: cum_dim as usize, + offset: 0, + }); + child = *nullable.data; + }; + match child { + DataBlock::FixedSizeList(inner) => { + current = inner; + } + DataBlock::FixedWidth(fixed_width) => { + data_bytes_per_row = + (fixed_width.bits_per_value.div_ceil(8) * cum_dim) as usize; + bytes_per_row += data_bytes_per_row; + data_buffer = fixed_width.data; + break; + } + DataBlock::AllNull(_) => { + data_bytes_per_row = 0; + data_buffer = LanceBuffer::empty(); + break; + } + _ => unreachable!( + "Unexpected data block type in value encoder's nullable_per_value_fsl: {:?}", + child + ), + } + } + + let bytes_needed = bytes_per_row * num_values as usize; + let mut zipped = Vec::with_capacity(bytes_needed); + let data_slice = &data_buffer; + // Hopefully values are pretty large so we don't iterate this loop _too_ many times + for i in 0..num_values as usize { + for validity in validity_iters.iter_mut() { + let validity_slice = validity + .buffer + .bit_slice_le_with_length(validity.offset, validity.bits_per_row); + zipped.extend_from_slice(&validity_slice); + validity.offset += validity.bits_per_row; + } + let start = i * data_bytes_per_row; + let end = start + data_bytes_per_row; + zipped.extend_from_slice(&data_slice[start..end]); + } + + let zipped = LanceBuffer::from(zipped); + let data = PerValueDataBlock::Fixed(FixedWidthDataBlock { + bits_per_value: bytes_per_row as u64 * 8, + num_values, + data: zipped, + block_info: BlockInfo::new(), + }); + (data, encoding) + } + + fn per_value_fsl(fsl: FixedSizeListBlock) -> (PerValueDataBlock, CompressiveEncoding) { + if !fsl.child.is_nullable() { + Self::simple_per_value_fsl(fsl) + } else { + Self::nullable_per_value_fsl(fsl) + } + } +} + +impl BlockCompressor for ValueEncoder { + fn compress(&self, data: DataBlock) -> Result { + let data = match data { + DataBlock::FixedWidth(fixed_width) => fixed_width.data, + _ => unimplemented!( + "Cannot compress block of type {} with ValueEncoder", + data.name() + ), + }; + Ok(data) + } +} + +impl MiniBlockCompressor for ValueEncoder { + fn compress( + &self, + _context: MiniBlockCompressionContext, + chunk: DataBlock, + ) -> Result<(MiniBlockCompressed, CompressiveEncoding)> { + match chunk { + DataBlock::FixedWidth(fixed_width) => { + let encoding = ProtobufUtils21::flat(fixed_width.bits_per_value, None); + Ok((Self::chunk_data(fixed_width)?, encoding)) + } + DataBlock::FixedSizeList(_) => Self::miniblock_fsl(chunk), + _ => Err(Error::invalid_input_source( + format!( + "Cannot compress a data block of type {} with ValueEncoder", + chunk.name() + ) + .into(), + )), + } + } +} + +#[derive(Debug)] +struct ValueFslDesc { + dimension: u64, + has_validity: bool, +} + +/// A decompressor for fixed-width data that has +/// been written, as-is, to disk in single contiguous array +#[derive(Debug)] +pub struct ValueDecompressor { + /// How many bits are in each inner-most item (e.g. FSL would be 32) + bits_per_item: u64, + /// How many bits are in each value (e.g. FSL would be 3200) + /// + /// This number is a little trickier to compute because we also have to include bytes + /// of any inner validity + bits_per_value: u64, + /// How many items are in each value (e.g. FSL would be 100) + items_per_value: u64, + layers: Vec, +} + +impl ValueDecompressor { + pub fn from_flat(description: &pb21::Flat) -> Self { + Self { + bits_per_item: description.bits_per_value, + bits_per_value: description.bits_per_value, + items_per_value: 1, + layers: Vec::default(), + } + } + + pub fn from_fsl(mut description: &pb21::FixedSizeList) -> Self { + let mut layers = Vec::new(); + let mut cum_dim = 1; + let mut bytes_per_value = 0; + loop { + layers.push(ValueFslDesc { + has_validity: description.has_validity, + dimension: description.items_per_value, + }); + cum_dim *= description.items_per_value; + if description.has_validity { + bytes_per_value += cum_dim.div_ceil(8); + } + match description + .values + .as_ref() + .unwrap() + .compression + .as_ref() + .unwrap() + { + Compression::FixedSizeList(inner) => { + description = inner; + } + Compression::Flat(flat) => { + let mut bits_per_value = bytes_per_value * 8; + bits_per_value += flat.bits_per_value * cum_dim; + return Self { + bits_per_item: flat.bits_per_value, + bits_per_value, + items_per_value: cum_dim, + layers, + }; + } + _ => unreachable!(), + } + } + } + + fn buffer_to_block(&self, data: LanceBuffer, num_values: u64) -> DataBlock { + DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bits_per_item, + num_values, + data, + block_info: BlockInfo::new(), + }) + } +} + +impl BlockDecompressor for ValueDecompressor { + fn decompress(&self, data: LanceBuffer, num_values: u64) -> Result { + let block = self.buffer_to_block(data, num_values); + assert_eq!(block.num_values(), num_values); + Ok(block) + } +} + +impl MiniBlockDecompressor for ValueDecompressor { + fn decompress(&self, data: Vec, num_values: u64) -> Result { + let num_items = num_values * self.items_per_value; + let mut buffer_iter = data.into_iter().rev(); + + // Always at least 1 buffer + let data_buf = buffer_iter.next().unwrap(); + let items = self.buffer_to_block(data_buf, num_items); + let mut lists = items; + + for layer in self.layers.iter().rev() { + if layer.has_validity { + let validity_buf = buffer_iter.next().unwrap(); + lists = DataBlock::Nullable(NullableDataBlock { + data: Box::new(lists), + nulls: validity_buf, + block_info: BlockInfo::default(), + }); + } + lists = DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(lists), + dimension: layer.dimension, + }) + } + + assert_eq!(lists.num_values(), num_values); + Ok(lists) + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + if self.has_validity() { + return None; + } + num_values + .checked_mul(self.bits_per_value) + .map(|bits| bits.div_ceil(8)) + } +} + +struct FslDecompressorValidityBuilder { + buffer: BooleanBufferBuilder, + bits_per_row: usize, + bytes_per_row: usize, +} + +// Helper methods for per-value decompression +impl ValueDecompressor { + fn has_validity(&self) -> bool { + self.layers.iter().any(|layer| layer.has_validity) + } + + // If there is no validity then decompression is zero-copy, we just need to restore any FSL layers + fn simple_decompress(&self, data: FixedWidthDataBlock, num_rows: u64) -> DataBlock { + let mut cum_dim = 1; + for layer in &self.layers { + cum_dim *= layer.dimension; + } + debug_assert_eq!(self.bits_per_item, data.bits_per_value / cum_dim); + let mut block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bits_per_item, + num_values: num_rows * cum_dim, + data: data.data, + block_info: BlockInfo::new(), + }); + for layer in self.layers.iter().rev() { + block = DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(block), + dimension: layer.dimension, + }); + } + debug_assert_eq!(num_rows, block.num_values()); + block + } + + // If there is validity then it has been zipped in with the values and we must unzip it + fn unzip_decompress(&self, data: FixedWidthDataBlock, num_rows: usize) -> DataBlock { + // No support for full-zip on per-value encodings + assert_eq!(self.bits_per_item % 8, 0); + let bytes_per_item = self.bits_per_item / 8; + let mut buffer_builders = Vec::with_capacity(self.layers.len()); + let mut cum_dim = 1; + let mut total_size_bytes = 0; + // First, go through the layers, setup our builders, allocate space + for layer in &self.layers { + cum_dim *= layer.dimension as usize; + if layer.has_validity { + let validity_size_bits = cum_dim; + let validity_size_bytes = validity_size_bits.div_ceil(8); + total_size_bytes += num_rows * validity_size_bytes; + buffer_builders.push(FslDecompressorValidityBuilder { + buffer: BooleanBufferBuilder::new(validity_size_bits * num_rows), + bits_per_row: cum_dim, + bytes_per_row: validity_size_bytes, + }) + } + } + let num_items = num_rows * cum_dim; + let data_size = num_items * bytes_per_item as usize; + total_size_bytes += data_size; + let mut data_buffer = Vec::with_capacity(data_size); + + assert_eq!(data.data.len(), total_size_bytes); + + let bytes_per_value = bytes_per_item as usize; + let data_bytes_per_row = bytes_per_value * cum_dim; + + // Next, unzip + let mut data_offset = 0; + while data_offset < total_size_bytes { + for builder in buffer_builders.iter_mut() { + let start = data_offset * 8; + let end = start + builder.bits_per_row; + builder.buffer.append_packed_range(start..end, &data.data); + data_offset += builder.bytes_per_row; + } + let end = data_offset + data_bytes_per_row; + data_buffer.extend_from_slice(&data.data[data_offset..end]); + data_offset += data_bytes_per_row; + } + + // Finally, restore the structure + let mut block = DataBlock::FixedWidth(FixedWidthDataBlock { + bits_per_value: self.bits_per_item, + num_values: num_items as u64, + data: LanceBuffer::from(data_buffer), + block_info: BlockInfo::new(), + }); + + let mut validity_bufs = buffer_builders + .into_iter() + .rev() + .map(|mut b| LanceBuffer::from(b.buffer.finish().into_inner())); + for layer in self.layers.iter().rev() { + if layer.has_validity { + let nullable = NullableDataBlock { + data: Box::new(block), + nulls: validity_bufs.next().unwrap(), + block_info: BlockInfo::new(), + }; + block = DataBlock::Nullable(nullable); + } + block = DataBlock::FixedSizeList(FixedSizeListBlock { + child: Box::new(block), + dimension: layer.dimension, + }); + } + + assert_eq!(num_rows, block.num_values() as usize); + + block + } +} + +impl FixedPerValueDecompressor for ValueDecompressor { + fn decompress(&self, data: FixedWidthDataBlock, num_rows: u64) -> Result { + if self.has_validity() { + Ok(self.unzip_decompress(data, num_rows as usize)) + } else { + Ok(self.simple_decompress(data, num_rows)) + } + } + + fn bits_per_value(&self) -> u64 { + self.bits_per_value + } + + fn decoded_size_bytes(&self, num_values: u64) -> Option { + if self.has_validity() { + return None; + } + num_values + .checked_mul(self.bits_per_value) + .map(|bits| bits.div_ceil(8)) + } +} + +impl PerValueCompressor for ValueEncoder { + fn compress(&self, data: DataBlock) -> Result<(PerValueDataBlock, CompressiveEncoding)> { + let (data, encoding) = match data { + DataBlock::FixedWidth(fixed_width) => { + let encoding = ProtobufUtils21::flat(fixed_width.bits_per_value, None); + (PerValueDataBlock::Fixed(fixed_width), encoding) + } + DataBlock::FixedSizeList(fixed_size_list) => Self::per_value_fsl(fixed_size_list), + _ => unimplemented!( + "Cannot compress block of type {} with ValueEncoder", + data.name() + ), + }; + Ok((data, encoding)) + } +} + +// public tests module because we share the PRIMITIVE_TYPES constant with fixed_size_list +#[cfg(test)] +mod tests { + use std::{ + collections::HashMap, + sync::{Arc, LazyLock}, + }; + + use arrow_array::{ + Array, ArrayRef, Decimal128Array, FixedSizeListArray, Int32Array, ListArray, UInt8Array, + make_array, new_null_array, types::UInt32Type, + }; + use arrow_buffer::{BooleanBuffer, NullBuffer, OffsetBuffer, ScalarBuffer}; + use arrow_schema::{DataType, Field, TimeUnit}; + use lance_datagen::{ArrayGeneratorExt, Dimension, RowCount, array, gen_batch}; + + use crate::{ + compression::{FixedPerValueDecompressor, MiniBlockDecompressor}, + data::DataBlock, + encodings::{ + logical::primitive::{ + fullzip::{PerValueCompressor, PerValueDataBlock}, + miniblock::{MiniBlockCompressionContext, MiniBlockCompressor}, + }, + physical::value::ValueDecompressor, + }, + format::pb21::compressive_encoding::Compression, + testing::{ + FnArrayGeneratorProvider, TestCases, check_basic_random, + check_round_trip_encoding_generated, check_round_trip_encoding_of_data, + }, + }; + + use super::ValueEncoder; + + fn miniblock_context() -> MiniBlockCompressionContext { + MiniBlockCompressionContext::new(0, true, true) + } + + const PRIMITIVE_TYPES: &[DataType] = &[ + DataType::Null, + DataType::FixedSizeBinary(2), + DataType::Date32, + DataType::Date64, + DataType::Int8, + DataType::Int16, + DataType::Int32, + DataType::Int64, + DataType::UInt8, + DataType::UInt16, + DataType::UInt32, + DataType::UInt64, + DataType::Float16, + DataType::Float32, + DataType::Float64, + DataType::Decimal128(10, 10), + DataType::Decimal256(10, 10), + DataType::Timestamp(TimeUnit::Nanosecond, None), + DataType::Time32(TimeUnit::Second), + DataType::Time64(TimeUnit::Nanosecond), + DataType::Duration(TimeUnit::Second), + // The Interval type is supported by the reader but the writer works with Lance schema + // at the moment and Lance schema can't parse interval + // DataType::Interval(IntervalUnit::DayTime), + ]; + + #[test_log::test(tokio::test)] + async fn test_simple_value() { + let items = Arc::new(Int32Array::from(vec![ + Some(0), + None, + Some(2), + Some(3), + Some(4), + Some(5), + ])); + + let test_cases = TestCases::default() + .with_range(0..3) + .with_range(0..2) + .with_range(1..3) + .with_indices(vec![0, 1, 2]) + .with_indices(vec![1]) + .with_indices(vec![2]) + .with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![items], &test_cases, HashMap::default()).await; + } + + #[test_log::test(tokio::test)] + async fn test_simple_range() { + let items = Arc::new(Int32Array::from_iter( + (0..5000).map(|i| if i % 2 == 0 { Some(i) } else { None }), + )); + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![items], &test_cases, HashMap::default()).await; + } + + #[test_log::test(tokio::test)] + async fn test_value_primitive() { + for data_type in PRIMITIVE_TYPES { + log::info!("Testing encoding for {:?}", data_type); + let field = Field::new("", data_type.clone(), false); + check_basic_random(field).await; + } + } + + static LARGE_TYPES: LazyLock> = LazyLock::new(|| { + vec![DataType::FixedSizeList( + Arc::new(Field::new("", DataType::Int32, false)), + 128, + )] + }); + + #[test_log::test(tokio::test)] + async fn test_large_primitive() { + for data_type in LARGE_TYPES.iter() { + log::info!("Testing encoding for {:?}", data_type); + let field = Field::new("", data_type.clone(), false); + check_basic_random(field).await; + } + } + + #[test_log::test(tokio::test)] + async fn test_decimal128_dictionary_encoding() { + let test_cases = TestCases::default().with_structural_encodings(); + let decimals: Vec = (0..100).collect(); + let repeated_strings: Vec<_> = decimals + .iter() + .cycle() + .take(decimals.len() * 10000) + .map(|&v| Some(v as i128)) + .collect(); + let decimal_array = Arc::new(Decimal128Array::from(repeated_strings)) as ArrayRef; + check_round_trip_encoding_of_data(vec![decimal_array], &test_cases, HashMap::new()).await; + } + + #[test_log::test(tokio::test)] + async fn test_miniblock_stress() { + // Tests for strange page sizes and batch sizes and validity scenarios for miniblock + + // 10K integers, 100 per array, all valid + let data1 = (0..100) + .map(|_| Arc::new(Int32Array::from_iter_values(0..100)) as Arc) + .collect::>(); + + // Same as above but with mixed validity + let data2 = (0..100) + .map(|_| { + Arc::new(Int32Array::from_iter( + (0..100).map(|i| if i % 2 == 0 { Some(i) } else { None }), + )) as Arc + }) + .collect::>(); + + // Same as above but with all null for first half then all valid + // TODO: Re-enable once the all-null path is complete + let _data3 = (0..100) + .map(|chunk_idx| { + Arc::new(Int32Array::from_iter( + (0..100).map(|i| if chunk_idx < 50 { None } else { Some(i) }), + )) as Arc + }) + .collect::>(); + + for data in [data1, data2 /*data3*/] { + for batch_size in [10, 100, 1500, 15000] { + // 40000 bytes of data + let test_cases = TestCases::default() + .with_page_sizes(vec![1000, 2000, 3000, 60000]) + .with_batch_size(batch_size) + .with_structural_encodings(); + + check_round_trip_encoding_of_data(data.clone(), &test_cases, HashMap::new()).await; + } + } + } + + fn create_simple_fsl() -> FixedSizeListArray { + // [[0, 1], NULL], [NULL, NULL], [[8, 9], [NULL, 11]] + let items = Arc::new(Int32Array::from(vec![ + Some(0), + Some(1), + Some(2), + Some(3), + None, + None, + None, + None, + Some(8), + Some(9), + None, + Some(11), + ])); + let items_field = Arc::new(Field::new("item", DataType::Int32, true)); + let inner_list_nulls = BooleanBuffer::from(vec![true, false, false, false, true, true]); + let inner_list = Arc::new(FixedSizeListArray::new( + items_field.clone(), + 2, + items, + Some(NullBuffer::new(inner_list_nulls)), + )); + let inner_list_field = Arc::new(Field::new( + "item", + DataType::FixedSizeList(items_field, 2), + true, + )); + FixedSizeListArray::new(inner_list_field, 2, inner_list, None) + } + + #[test] + fn test_fsl_value_compression_miniblock() { + let sample_list = create_simple_fsl(); + + let starting_data = DataBlock::from_array(sample_list.clone()); + + let encoder = ValueEncoder::default(); + let (data, compression) = + MiniBlockCompressor::compress(&encoder, miniblock_context(), starting_data).unwrap(); + + assert_eq!(data.num_values, 3); + assert_eq!(data.data.len(), 3); + assert_eq!(data.chunks.len(), 1); + assert_eq!(data.chunks[0].buffer_sizes, vec![1, 2, 48]); + assert_eq!(data.chunks[0].log_num_values, 0); + + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!() + }; + + let decompressor = ValueDecompressor::from_fsl(fsl.as_ref()); + + let decompressed = + MiniBlockDecompressor::decompress(&decompressor, data.data, data.num_values).unwrap(); + + let decompressed = make_array( + decompressed + .into_arrow(sample_list.data_type().clone(), true) + .unwrap(), + ); + + assert_eq!(decompressed.as_ref(), &sample_list); + } + + fn wide_fixed_size_binary() -> ArrayRef { + let wide_value = vec![0xABu8; 5000]; + Arc::new( + arrow_array::FixedSizeBinaryArray::try_from_sparse_iter_with_size( + std::iter::repeat_n(Some(wide_value.as_slice()), 4), + 5000, + ) + .unwrap(), + ) + } + + fn wide_fixed_size_list_bool() -> ArrayRef { + // A wide FSL is sub-byte, so it chunks eight values per word and the + // smallest unit is 16 values rather than 2. + let dimension = 4095; + let values = arrow_array::BooleanArray::from(vec![false; dimension * 2]); + let field = Arc::new(Field::new("item", DataType::Boolean, true)); + Arc::new(FixedSizeListArray::new( + field, + dimension as i32, + Arc::new(values), + None, + )) + } + + #[rstest::rstest] + #[case::fixed_size_binary(wide_fixed_size_binary(), 2)] + #[case::fixed_size_list_bool(wide_fixed_size_list_bool(), 16)] + fn test_wide_value_miniblock_returns_error( + #[case] array: ArrayRef, + #[case] expected_min_values: u64, + ) { + let starting_data = DataBlock::from_array(array); + + let encoder = ValueEncoder::default(); + let result = MiniBlockCompressor::compress(&encoder, miniblock_context(), starting_data); + + let err = result.expect_err("wide values should not be encodable as miniblock"); + assert!( + matches!(err, lance_core::Error::InvalidInput { .. }), + "expected InvalidInput, got {err:?}" + ); + let msg = err.to_string(); + assert!( + msg.contains("too wide for miniblock encoding"), + "unexpected error message: {msg}" + ); + assert!( + msg.contains(&format!("{expected_min_values} values require")), + "unexpected error message: {msg}" + ); + } + + #[test] + fn test_fsl_value_compression_per_value() { + let sample_list = create_simple_fsl(); + + let starting_data = DataBlock::from_array(sample_list.clone()); + + let encoder = ValueEncoder::default(); + let (data, compression) = PerValueCompressor::compress(&encoder, starting_data).unwrap(); + + let PerValueDataBlock::Fixed(data) = data else { + panic!() + }; + + assert_eq!(data.bits_per_value, 144); + assert_eq!(data.num_values, 3); + assert_eq!(data.data.len(), 18 * 3); + + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!() + }; + + let decompressor = ValueDecompressor::from_fsl(fsl.as_ref()); + + let num_values = data.num_values; + assert_eq!( + FixedPerValueDecompressor::decoded_size_bytes(&decompressor, num_values), + None, + "nullable FSL output uses multiple buffers and requires the fallback estimate" + ); + let decompressed = + FixedPerValueDecompressor::decompress(&decompressor, data, num_values).unwrap(); + + let decompressed = make_array( + decompressed + .into_arrow(sample_list.data_type().clone(), true) + .unwrap(), + ); + + assert_eq!(decompressed.as_ref(), &sample_list); + } + + #[test_log::test(tokio::test)] + async fn test_fsl_all_null() { + let items = new_null_array(&DataType::Int32, 12); + let items_field = Arc::new(Field::new("item", DataType::Int32, true)); + let list_nulls = BooleanBuffer::from(vec![true, false, false, false, true, true]); + let list_array = + FixedSizeListArray::new(items_field, 2, items, Some(NullBuffer::new(list_nulls))); + + let test_cases = TestCases::default().with_structural_encodings(); + + check_round_trip_encoding_of_data(vec![Arc::new(list_array)], &test_cases, HashMap::new()) + .await; + } + + #[test_log::test(tokio::test)] + async fn regress_list_fsl() { + // This regresses a case where rows are large lists that span multiple + // mini-block chunks which gives us some all-premable mini-block chunks. + let offsets = ScalarBuffer::::from(vec![0, 393, 755, 1156, 1536]); + let data = UInt8Array::from(vec![0; 1536 * 16]); + let fsl_field = Arc::new(Field::new("item", DataType::UInt8, true)); + let fsl = FixedSizeListArray::new(fsl_field, 16, Arc::new(data), None); + let list_field = Arc::new(Field::new("item", fsl.data_type().clone(), false)); + let list_arr = ListArray::new(list_field, OffsetBuffer::new(offsets), Arc::new(fsl), None); + + let test_cases = TestCases::default() + .with_structural_encodings() + .with_batch_size(1); + + check_round_trip_encoding_of_data(vec![Arc::new(list_arr)], &test_cases, HashMap::new()) + .await; + } + + fn create_random_fsl() -> Arc { + // Several levels of def and multiple pages + let inner = array::rand_type(&DataType::Int32).with_random_nulls(0.1); + let list_one = array::cycle_vec(inner, Dimension::from(4)).with_random_nulls(0.1); + let list_two = array::cycle_vec(list_one, Dimension::from(4)).with_random_nulls(0.1); + let list_three = array::cycle_vec(list_two, Dimension::from(2)); + + // Should be 256Ki rows ~ 1MiB of data + let batch = gen_batch() + .anon_col(list_three) + .into_batch_rows(RowCount::from(8 * 1024)) + .unwrap(); + batch.column(0).clone() + } + + #[test] + fn fsl_value_miniblock_stress() { + let sample_array = create_random_fsl(); + + let starting_data = DataBlock::from_arrays( + std::slice::from_ref(&sample_array), + sample_array.len() as u64, + ); + + let encoder = ValueEncoder::default(); + let (data, compression) = + MiniBlockCompressor::compress(&encoder, miniblock_context(), starting_data).unwrap(); + + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!() + }; + + let decompressor = ValueDecompressor::from_fsl(fsl.as_ref()); + + let decompressed = + MiniBlockDecompressor::decompress(&decompressor, data.data, data.num_values).unwrap(); + + let decompressed = make_array( + decompressed + .into_arrow(sample_array.data_type().clone(), true) + .unwrap(), + ); + + assert_eq!(decompressed.as_ref(), sample_array.as_ref()); + } + + #[test] + fn fsl_value_per_value_stress() { + let sample_array = create_random_fsl(); + + let starting_data = DataBlock::from_arrays( + std::slice::from_ref(&sample_array), + sample_array.len() as u64, + ); + + let encoder = ValueEncoder::default(); + let (data, compression) = PerValueCompressor::compress(&encoder, starting_data).unwrap(); + + let Compression::FixedSizeList(fsl) = compression.compression.unwrap() else { + panic!() + }; + + let decompressor = ValueDecompressor::from_fsl(fsl.as_ref()); + + let PerValueDataBlock::Fixed(data) = data else { + panic!() + }; + + let num_values = data.num_values; + let decompressed = + FixedPerValueDecompressor::decompress(&decompressor, data, num_values).unwrap(); + + let decompressed = make_array( + decompressed + .into_arrow(sample_array.data_type().clone(), true) + .unwrap(), + ); + + assert_eq!(decompressed.as_ref(), sample_array.as_ref()); + } + + #[test_log::test(tokio::test)] + async fn test_fsl_nullable_items() { + let datagen = Box::new(FnArrayGeneratorProvider::new(move || { + lance_datagen::array::rand_vec_nullable::(Dimension::from(128), 0.5) + })); + + let field = Field::new( + "", + DataType::FixedSizeList(Arc::new(Field::new("item", DataType::UInt32, true)), 128), + false, + ); + check_round_trip_encoding_generated(field, datagen, TestCases::default()).await; + } + + #[test_log::test(tokio::test)] + async fn test_value_encoding_verification() { + use std::collections::HashMap; + + let test_cases = TestCases::default() + .with_expected_encoding("flat") + .with_structural_encodings(); + + // Test both explicit configuration and automatic fallback scenarios + // 1. Test explicit "none" compression to force flat encoding + // Also explicitly disable BSS to ensure value encoding is tested + let mut metadata_explicit = HashMap::new(); + metadata_explicit.insert("lance-encoding:compression".to_string(), "none".to_string()); + metadata_explicit.insert("lance-encoding:bss".to_string(), "off".to_string()); + + let arr_explicit = + Arc::new(Int32Array::from((0..1000).collect::>())) as Arc; + check_round_trip_encoding_of_data(vec![arr_explicit], &test_cases, metadata_explicit).await; + + // 2. Test automatic fallback to flat encoding when bitpacking conditions aren't met + // Use unique values to avoid RLE encoding + // Explicitly disable BSS to ensure value encoding is tested + let mut metadata = HashMap::new(); + metadata.insert("lance-encoding:bss".to_string(), "off".to_string()); + + let arr_fallback = Arc::new(Int32Array::from( + (0..100).map(|i| i * 73 + 19).collect::>(), + )) as Arc; + check_round_trip_encoding_of_data(vec![arr_fallback], &test_cases, metadata).await; + } + + #[test_log::test(tokio::test)] + async fn test_mixed_page_validity() { + let no_nulls = Arc::new(Int32Array::from_iter_values([1, 2])); + let has_nulls = Arc::new(Int32Array::from_iter([Some(3), None, Some(5)])); + + let test_cases = TestCases::default().with_page_sizes(vec![1]); + check_round_trip_encoding_of_data(vec![no_nulls, has_nulls], &test_cases, HashMap::new()) + .await; + } +} diff --git a/lance-artifact/rust/lance-encoding/src/format.rs b/lance-artifact/rust/lance-encoding/src/format.rs new file mode 100644 index 000000000..f37e69b02 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/format.rs @@ -0,0 +1,773 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Protobuf definitions for encodings +/// +/// These are the messages used for describing encoding in the 2.0 format +pub mod pb { + #![allow(clippy::all)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + #![allow(clippy::upper_case_acronyms)] + #![allow(clippy::use_self)] + include!(concat!(env!("OUT_DIR"), "/lance.encodings.rs")); +} + +/// Protobuf definitions for encodings21 +/// +/// These are the messages used for describing encoding in the 2.1 format +/// and any newer formats. +pub mod pb21 { + #![allow(clippy::all)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + #![allow(clippy::upper_case_acronyms)] + #![allow(clippy::use_self)] + include!(concat!(env!("OUT_DIR"), "/lance.encodings21.rs")); +} + +use pb::{ + ArrayEncoding, Binary, Bitpacked, BitpackedForNonNeg, Block, Dictionary, FixedSizeBinary, + FixedSizeList, Flat, Fsst, InlineBitpacking, Nullable, OutOfLineBitpacking, PackedStruct, + PackedStructFixedWidthMiniBlock, Rle, Variable, + array_encoding::ArrayEncoding as ArrayEncodingEnum, + buffer::BufferType, + nullable::{AllNull, NoNull, Nullability, SomeNull}, +}; + +use crate::{encodings::physical::block::CompressionConfig, repdef::DefinitionInterpretation}; + +use self::pb::Constant; +use lance_core::Result; + +// Utility functions for creating complex protobuf objects +pub struct ProtobufUtils {} + +impl ProtobufUtils { + pub fn constant(value: Vec) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Constant(Constant { + value: value.into(), + })), + } + } + + pub fn basic_all_null_encoding() -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Nullable(Box::new(Nullable { + nullability: Some(Nullability::AllNulls(AllNull {})), + }))), + } + } + + pub fn basic_some_null_encoding( + validity: ArrayEncoding, + values: ArrayEncoding, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Nullable(Box::new(Nullable { + nullability: Some(Nullability::SomeNulls(Box::new(SomeNull { + validity: Some(Box::new(validity)), + values: Some(Box::new(values)), + }))), + }))), + } + } + + pub fn basic_no_null_encoding(values: ArrayEncoding) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Nullable(Box::new(Nullable { + nullability: Some(Nullability::NoNulls(Box::new(NoNull { + values: Some(Box::new(values)), + }))), + }))), + } + } + + pub fn block(scheme: &str) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Block(Block { + scheme: scheme.to_string(), + })), + } + } + + pub fn flat_encoding( + bits_per_value: u64, + buffer_index: u32, + compression: Option, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Flat(Flat { + bits_per_value, + buffer: Some(pb::Buffer { + buffer_index, + buffer_type: BufferType::Page as i32, + }), + compression: compression.map(|compression_config| pb::Compression { + scheme: compression_config.scheme.to_string(), + level: compression_config.level, + }), + })), + } + } + + pub fn fsl_encoding(dimension: u64, items: ArrayEncoding, has_validity: bool) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::FixedSizeList(Box::new(FixedSizeList { + dimension: dimension.try_into().unwrap(), + items: Some(Box::new(items)), + has_validity, + }))), + } + } + + pub fn bitpacked_encoding( + compressed_bits_per_value: u64, + uncompressed_bits_per_value: u64, + buffer_index: u32, + signed: bool, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Bitpacked(Bitpacked { + compressed_bits_per_value, + buffer: Some(pb::Buffer { + buffer_index, + buffer_type: BufferType::Page as i32, + }), + uncompressed_bits_per_value, + signed, + })), + } + } + + pub fn bitpacked_for_non_neg_encoding( + compressed_bits_per_value: u64, + uncompressed_bits_per_value: u64, + buffer_index: u32, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::BitpackedForNonNeg(BitpackedForNonNeg { + compressed_bits_per_value, + buffer: Some(pb::Buffer { + buffer_index, + buffer_type: BufferType::Page as i32, + }), + uncompressed_bits_per_value, + })), + } + } + pub fn inline_bitpacking(uncompressed_bits_per_value: u64) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::InlineBitpacking(InlineBitpacking { + uncompressed_bits_per_value, + })), + } + } + pub fn out_of_line_bitpacking( + uncompressed_bits_per_value: u64, + compressed_bits_per_value: u64, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::OutOfLineBitpacking( + OutOfLineBitpacking { + uncompressed_bits_per_value, + compressed_bits_per_value, + }, + )), + } + } + + pub fn variable(bits_per_offset: u8) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Variable(Variable { + bits_per_offset: bits_per_offset as u32, + })), + } + } + + // Construct a `FsstMiniBlock` ArrayEncoding, the inner `binary_mini_block` encoding is actually + // not used and `FsstMiniBlockDecompressor` constructs a `binary_mini_block` in a `hard-coded` fashion. + // This can be an optimization later. + pub fn fsst(data: ArrayEncoding, symbol_table: Vec) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Fsst(Box::new(Fsst { + binary: Some(Box::new(data)), + symbol_table: symbol_table.into(), + }))), + } + } + + pub fn rle(bits_per_value: u64) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Rle(Rle { bits_per_value })), + } + } + + pub fn byte_stream_split(bits_per_value: u64) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::ByteStreamSplit(pb::ByteStreamSplit { + bits_per_value, + })), + } + } + + pub fn general_mini_block( + inner: ArrayEncoding, + compression: CompressionConfig, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::GeneralMiniBlock(Box::new( + pb::GeneralMiniBlock { + inner: Some(Box::new(inner)), + compression: Some(pb::Compression { + scheme: compression.scheme.to_string(), + level: compression.level, + }), + }, + ))), + } + } + + pub fn packed_struct( + child_encodings: Vec, + packed_buffer_index: u32, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::PackedStruct(PackedStruct { + inner: child_encodings, + buffer: Some(pb::Buffer { + buffer_index: packed_buffer_index, + buffer_type: BufferType::Page as i32, + }), + })), + } + } + + pub fn packed_struct_fixed_width_mini_block( + data: ArrayEncoding, + bits_per_values: Vec, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::PackedStructFixedWidthMiniBlock( + Box::new(PackedStructFixedWidthMiniBlock { + flat: Some(Box::new(data)), + bits_per_values, + }), + )), + } + } + + pub fn binary( + indices_encoding: ArrayEncoding, + bytes_encoding: ArrayEncoding, + null_adjustment: u64, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Binary(Box::new(Binary { + bytes: Some(Box::new(bytes_encoding)), + indices: Some(Box::new(indices_encoding)), + null_adjustment, + }))), + } + } + + pub fn dict_encoding( + indices: ArrayEncoding, + items: ArrayEncoding, + num_items: u32, + ) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::Dictionary(Box::new(Dictionary { + indices: Some(Box::new(indices)), + items: Some(Box::new(items)), + num_dictionary_items: num_items, + }))), + } + } + + pub fn fixed_size_binary(data: ArrayEncoding, byte_width: u32) -> ArrayEncoding { + ArrayEncoding { + array_encoding: Some(ArrayEncodingEnum::FixedSizeBinary(Box::new( + FixedSizeBinary { + bytes: Some(Box::new(data)), + byte_width, + }, + ))), + } + } +} + +macro_rules! impl_common_protobuf_utils { + ($module:ident, $struct_name:ident) => { + pub struct $struct_name {} + + impl $struct_name { + pub fn flat( + bits_per_value: u64, + values_compression: Option, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::Flat( + crate::format::$module::Flat { + bits_per_value, + data: values_compression, + }, + ), + ), + } + } + + pub fn constant( + value: Option, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::Constant( + crate::format::$module::Constant { value }, + ), + ), + } + } + + pub fn fsl( + items_per_value: u64, + has_validity: bool, + values: crate::format::$module::CompressiveEncoding, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::FixedSizeList( + Box::new(crate::format::$module::FixedSizeList { + items_per_value, + has_validity, + values: Some(Box::new(values)), + }), + ), + ), + } + } + + pub fn variable( + offsets_desc: crate::format::$module::CompressiveEncoding, + values_compression: Option, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::Variable( + Box::new(crate::format::$module::Variable { + offsets: Some(Box::new(offsets_desc)), + values: values_compression, + }), + ), + ), + } + } + + pub fn inline_bitpacking( + uncompressed_bits_per_value: u64, + values_compression: Option, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::InlineBitpacking( + crate::format::$module::InlineBitpacking { + uncompressed_bits_per_value, + values: values_compression, + }, + ), + ), + } + } + + pub fn out_of_line_bitpacking( + uncompressed_bits_per_value: u64, + values: crate::format::$module::CompressiveEncoding, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::OutOfLineBitpacking( + Box::new(crate::format::$module::OutOfLineBitpacking { + uncompressed_bits_per_value, + values: Some(Box::new(values)), + }), + ), + ), + } + } + + pub fn buffer_compression( + compression: CompressionConfig, + ) -> Result { + Ok(crate::format::$module::BufferCompression { + scheme: crate::format::$module::CompressionScheme::try_from( + compression.scheme, + )? as i32, + level: compression.level, + }) + } + + pub fn wrapped( + compression: CompressionConfig, + values: crate::format::$module::CompressiveEncoding, + ) -> Result { + Ok(crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::General( + Box::new(crate::format::$module::General { + compression: Some(Self::buffer_compression(compression)?), + values: Some(Box::new(values)), + }), + ), + ), + }) + } + + pub fn rle( + values: crate::format::$module::CompressiveEncoding, + run_lengths: crate::format::$module::CompressiveEncoding, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::Rle(Box::new( + crate::format::$module::Rle { + values: Some(Box::new(values)), + run_lengths: Some(Box::new(run_lengths)), + }, + )), + ), + } + } + + pub fn byte_stream_split( + values: crate::format::$module::CompressiveEncoding, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::ByteStreamSplit( + Box::new(crate::format::$module::ByteStreamSplit { + values: Some(Box::new(values)), + }), + ), + ), + } + } + + pub fn fsst( + data: crate::format::$module::CompressiveEncoding, + symbol_table: Vec, + ) -> crate::format::$module::CompressiveEncoding { + crate::format::$module::CompressiveEncoding { + compression: Some( + crate::format::$module::compressive_encoding::Compression::Fsst( + Box::new(crate::format::$module::Fsst { + symbol_table: symbol_table.into(), + values: Some(Box::new(data)), + }), + ), + ), + } + } + + fn def_inter_to_repdef_layer(def: DefinitionInterpretation) -> i32 { + match def { + DefinitionInterpretation::AllValidItem => { + crate::format::$module::RepDefLayer::RepdefAllValidItem as i32 + } + DefinitionInterpretation::AllValidList => { + crate::format::$module::RepDefLayer::RepdefAllValidList as i32 + } + DefinitionInterpretation::NullableItem => { + crate::format::$module::RepDefLayer::RepdefNullableItem as i32 + } + DefinitionInterpretation::NullableList => { + crate::format::$module::RepDefLayer::RepdefNullableList as i32 + } + DefinitionInterpretation::EmptyableList => { + crate::format::$module::RepDefLayer::RepdefEmptyableList as i32 + } + DefinitionInterpretation::NullableAndEmptyableList => { + crate::format::$module::RepDefLayer::RepdefNullAndEmptyList as i32 + } + } + } + + pub fn repdef_layer_to_def_interp( + layer: i32, + ) -> DefinitionInterpretation { + let layer = crate::format::$module::RepDefLayer::try_from(layer).unwrap(); + match layer { + crate::format::$module::RepDefLayer::RepdefAllValidItem => { + DefinitionInterpretation::AllValidItem + } + crate::format::$module::RepDefLayer::RepdefAllValidList => { + DefinitionInterpretation::AllValidList + } + crate::format::$module::RepDefLayer::RepdefNullableItem => { + DefinitionInterpretation::NullableItem + } + crate::format::$module::RepDefLayer::RepdefNullableList => { + DefinitionInterpretation::NullableList + } + crate::format::$module::RepDefLayer::RepdefEmptyableList => { + DefinitionInterpretation::EmptyableList + } + crate::format::$module::RepDefLayer::RepdefNullAndEmptyList => { + DefinitionInterpretation::NullableAndEmptyableList + } + crate::format::$module::RepDefLayer::RepdefUnspecified => { + panic!("Unspecified repdef layer") + } + } + } + + #[allow(clippy::too_many_arguments)] + pub fn miniblock_layout( + rep_encoding: Option, + def_encoding: Option, + value_encoding: crate::format::$module::CompressiveEncoding, + repetition_index_depth: u32, + num_buffers: u64, + dictionary_encoding: Option<( + crate::format::$module::CompressiveEncoding, + u64, + )>, + def_meaning: &[DefinitionInterpretation], + num_items: u64, + has_large_chunk: bool, + ) -> crate::format::$module::PageLayout { + assert!(!def_meaning.is_empty()); + let (dictionary, num_dictionary_items) = dictionary_encoding + .map(|(d, i)| (Some(d), i)) + .unwrap_or((None, 0)); + crate::format::$module::PageLayout { + layout: Some( + crate::format::$module::page_layout::Layout::MiniBlockLayout( + crate::format::$module::MiniBlockLayout { + def_compression: def_encoding, + rep_compression: rep_encoding, + value_compression: Some(value_encoding), + repetition_index_depth, + num_buffers, + dictionary, + num_dictionary_items, + layers: def_meaning + .iter() + .map(|&def| Self::def_inter_to_repdef_layer(def)) + .collect(), + num_items, + has_large_chunk, + }, + ), + ), + } + } + + fn full_zip_layout( + bits_rep: u8, + bits_def: u8, + details: crate::format::$module::full_zip_layout::Details, + value_encoding: crate::format::$module::CompressiveEncoding, + def_meaning: &[DefinitionInterpretation], + num_items: u32, + num_visible_items: u32, + ) -> crate::format::$module::PageLayout { + crate::format::$module::PageLayout { + layout: Some( + crate::format::$module::page_layout::Layout::FullZipLayout( + crate::format::$module::FullZipLayout { + bits_rep: bits_rep as u32, + bits_def: bits_def as u32, + details: Some(details), + value_compression: Some(value_encoding), + num_items, + num_visible_items, + layers: def_meaning + .iter() + .map(|&def| Self::def_inter_to_repdef_layer(def)) + .collect(), + }, + ), + ), + } + } + + pub fn fixed_full_zip_layout( + bits_rep: u8, + bits_def: u8, + bits_per_value: u32, + value_encoding: crate::format::$module::CompressiveEncoding, + def_meaning: &[DefinitionInterpretation], + num_items: u32, + num_visible_items: u32, + ) -> crate::format::$module::PageLayout { + Self::full_zip_layout( + bits_rep, + bits_def, + crate::format::$module::full_zip_layout::Details::BitsPerValue( + bits_per_value, + ), + value_encoding, + def_meaning, + num_items, + num_visible_items, + ) + } + + pub fn variable_full_zip_layout( + bits_rep: u8, + bits_def: u8, + bits_per_offset: u32, + value_encoding: crate::format::$module::CompressiveEncoding, + def_meaning: &[DefinitionInterpretation], + num_items: u32, + num_visible_items: u32, + ) -> crate::format::$module::PageLayout { + Self::full_zip_layout( + bits_rep, + bits_def, + crate::format::$module::full_zip_layout::Details::BitsPerOffset( + bits_per_offset, + ), + value_encoding, + def_meaning, + num_items, + num_visible_items, + ) + } + + pub fn blob_layout( + inner_layout: crate::format::$module::PageLayout, + def_meaning: &[DefinitionInterpretation], + ) -> crate::format::$module::PageLayout { + crate::format::$module::PageLayout { + layout: Some( + crate::format::$module::page_layout::Layout::BlobLayout(Box::new( + crate::format::$module::BlobLayout { + inner_layout: Some(Box::new(inner_layout)), + layers: def_meaning + .iter() + .map(|&def| Self::def_inter_to_repdef_layer(def)) + .collect(), + }, + )), + ), + } + } + + + } + }; +} + +impl_common_protobuf_utils!(pb21, ProtobufUtils21); + +impl ProtobufUtils21 { + pub fn constant_layout( + def_meaning: &[DefinitionInterpretation], + inline_value: Option>, + ) -> crate::format::pb21::PageLayout { + crate::format::pb21::PageLayout { + layout: Some(crate::format::pb21::page_layout::Layout::ConstantLayout( + crate::format::pb21::ConstantLayout { + inline_value: inline_value.map(bytes::Bytes::from), + rep_compression: None, + def_compression: None, + num_rep_values: 0, + num_def_values: 0, + layers: def_meaning + .iter() + .map(|&def| Self::def_inter_to_repdef_layer(def)) + .collect(), + }, + )), + } + } + + pub fn compressed_all_null_constant_layout( + def_meaning: &[DefinitionInterpretation], + rep_compression: Option, + def_compression: Option, + num_rep_values: u64, + num_def_values: u64, + ) -> crate::format::pb21::PageLayout { + crate::format::pb21::PageLayout { + layout: Some(crate::format::pb21::page_layout::Layout::ConstantLayout( + crate::format::pb21::ConstantLayout { + inline_value: None, + rep_compression, + def_compression, + num_rep_values, + num_def_values, + layers: def_meaning + .iter() + .map(|&def| Self::def_inter_to_repdef_layer(def)) + .collect(), + }, + )), + } + } + + pub fn packed_struct( + values: crate::format::pb21::CompressiveEncoding, + bits_per_values: Vec, + ) -> crate::format::pb21::CompressiveEncoding { + crate::format::pb21::CompressiveEncoding { + compression: Some( + crate::format::pb21::compressive_encoding::Compression::PackedStruct(Box::new( + crate::format::pb21::PackedStruct { + bits_per_value: bits_per_values, + values: Some(Box::new(values)), + }, + )), + ), + } + } + + pub fn packed_struct_variable( + fields: Vec, + ) -> crate::format::pb21::CompressiveEncoding { + crate::format::pb21::CompressiveEncoding { + compression: Some( + crate::format::pb21::compressive_encoding::Compression::VariablePackedStruct( + crate::format::pb21::VariablePackedStruct { fields }, + ), + ), + } + } + + pub fn packed_struct_field_fixed( + value_encoding: crate::format::pb21::CompressiveEncoding, + bits_per_value: u64, + ) -> crate::format::pb21::variable_packed_struct::FieldEncoding { + crate::format::pb21::variable_packed_struct::FieldEncoding { + value: Some(value_encoding), + layout: Some( + crate::format::pb21::variable_packed_struct::field_encoding::Layout::BitsPerValue( + bits_per_value, + ), + ), + } + } + + pub fn packed_struct_field_variable( + value_encoding: crate::format::pb21::CompressiveEncoding, + bits_per_length: u64, + ) -> crate::format::pb21::variable_packed_struct::FieldEncoding { + crate::format::pb21::variable_packed_struct::FieldEncoding { + value: Some(value_encoding), + layout: Some( + crate::format::pb21::variable_packed_struct::field_encoding::Layout::BitsPerLength( + bits_per_length, + ), + ), + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/lib.rs b/lance-artifact/rust/lance-encoding/src/lib.rs new file mode 100644 index 000000000..338028b79 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/lib.rs @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ops::Range, sync::Arc}; + +use bytes::Bytes; +use futures::{FutureExt, TryFutureExt, future::BoxFuture}; + +use lance_core::Result; + +mod array_encoding; +pub mod buffer; +pub mod compression; +pub mod compression_config; +pub mod constants; +pub mod data; +pub mod decoder; +pub mod encoder; +pub mod encodings; +pub mod format; +pub mod repdef; +pub mod statistics; +#[cfg(test)] +pub mod testing; +pub mod utils; + +// We can definitely add support for big-endian machines someday. However, it's not a priority and +// would involve extensive testing (probably through emulation) to ensure that the encodings are +// correct. +#[cfg(not(target_endian = "little"))] +compile_error!("Lance encodings only support little-endian systems."); + +/// A trait for an I/O service +/// +/// This represents the I/O API that the encoders and decoders need in order to operate. +/// We specify this as a trait so that lance-encodings does not need to depend on lance-io +/// +/// In general, it is assumed that this trait will be implemented by some kind of "file reader" +/// or "file scheduler". The encodings here are all limited to accessing a single file. +pub trait EncodingsIo: std::fmt::Debug + Send + Sync { + /// Submit an I/O request + /// + /// The response must contain a `Bytes` object for each range requested even if the underlying + /// I/O was coalesced into fewer actual requests. + /// + /// # Arguments + /// + /// * `ranges` - the byte ranges to request + /// * `priority` - the priority of the request + /// + /// Priority should be set to the lowest row number that this request is delivering data for. + /// This is important in cases where indirect I/O causes high priority requests to be submitted + /// after low priority requests. We want to fulfill the indirect I/O more quickly so that we + /// can decode as quickly as possible. + /// + /// The implementation should be able to handle empty ranges, and should return an empty + /// byte buffer for each empty range. + fn submit_request( + &self, + range: Vec>, + priority: u64, + ) -> BoxFuture<'static, Result>>; + + /// Submit an I/O request with a single range + /// + /// This is just a utitliy function that wraps [`EncodingsIo::submit_request`] for the common + /// case of a single range request. + fn submit_single( + &self, + range: std::ops::Range, + priority: u64, + ) -> BoxFuture<'static, lance_core::Result> { + self.submit_request(vec![range], priority) + .map_ok(|mut v| v.pop().unwrap()) + .boxed() + } + + /// Returns a version of this I/O service that bypasses backpressure for all requests. + /// + /// This is intended for indirect I/O (e.g. fetching items after decoding offsets) where + /// blocking on backpressure could cause deadlocks or excessive latency. + /// + /// Returns `None` if this implementation does not support bypass (e.g. in-memory or test + /// schedulers), in which case the caller should fall back to using self. + fn with_bypass_backpressure(&self) -> Option> { + None + } + + /// Returns a version of this I/O service that additionally records the I/O it + /// performs into `stats`, on top of any global accounting. This is the seam + /// used to measure exact per-scope (e.g. per-query) I/O without re-opening + /// files: wrap a reader's I/O service, perform the reads, then inspect the + /// recorder. + /// + /// Returns `None` if this implementation does not support per-scope I/O + /// statistics (e.g. in-memory or test schedulers), in which case the caller + /// should fall back to using self (and no statistics are recorded). + fn with_io_stats( + &self, + _stats: Arc, + ) -> Option> { + None + } +} + +/// An implementation of EncodingsIo that serves data from an in-memory buffer +#[derive(Debug)] +pub struct BufferScheduler { + data: Bytes, +} + +impl BufferScheduler { + pub fn new(data: Bytes) -> Self { + Self { data } + } + + fn satisfy_request(&self, req: Range) -> Bytes { + self.data.slice(req.start as usize..req.end as usize) + } +} + +impl EncodingsIo for BufferScheduler { + fn submit_request( + &self, + ranges: Vec>, + _priority: u64, + ) -> BoxFuture<'static, Result>> { + std::future::ready(Ok(ranges + .into_iter() + .map(|range| self.satisfy_request(range)) + .collect::>())) + .boxed() + } +} diff --git a/lance-artifact/rust/lance-encoding/src/repdef.rs b/lance-artifact/rust/lance-encoding/src/repdef.rs new file mode 100644 index 000000000..bb5d9cca3 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/repdef.rs @@ -0,0 +1,3853 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for rep-def levels +//! +//! Repetition and definition levels are a way to encode multipile validity / offsets arrays +//! into a single buffer. They are a form of "zipping" buffers together that takes advantage +//! of the fact that, if the outermost array is invalid, then the validity of the inner items +//! is irrelevant. +//! +//! Note: the concept of repetition & definition levels comes from the Dremel paper and has +//! been implemented in Apache Parquet. However, the implementation here is not necessarily +//! compatible with Parquet. For example, we use 0 to represent the "inner-most" item and +//! Parquet uses 0 to represent the "outer-most" item. +//! +//! # Repetition Levels +//! +//! With repetition levels we convert a sparse array of offsets into a dense array of levels. +//! These levels are marked non-zero whenever a new list begins. In other words, given the +//! list array with 3 rows [{<0,1>, <>, <2>}, {<3>}, {}], [], [{<4>}] we would have three +//! offsets arrays: +//! +//! Outer-most ([]): [0, 3, 3, 4] +//! Middle ({}): [0, 3, 4, 4, 5] +//! Inner (<>): [0, 2, 2, 3, 4, 5] +//! Values : [0, 1, 2, 3, 4] +//! +//! We can convert these into repetition levels as follows: +//! +//! | Values | Repetition | +//! | ------ | ---------- | +//! | 0 | 3 | // Start of outer-most list +//! | 1 | 0 | // Continues inner-most list (no new lists) +//! | - | 1 | // Start of new inner-most list (empty list) +//! | 2 | 1 | // Start of new inner-most list +//! | 3 | 2 | // Start of new middle list +//! | - | 2 | // Start of new inner-most list (empty list) +//! | - | 3 | // Start of new outer-most list (empty list) +//! | 4 | 0 | // Start of new outer-most list +//! +//! Note: We actually have MORE repetition levels than values. This is because the repetition +//! levels need to be able to represent empty lists. +//! +//! # Definition Levels +//! +//! Definition levels are simpler. We can think of them as zipping together various validity bitmaps +//! (from different levels of nesting) into a single buffer. For example, we could zip the arrays +//! [1, 1, 0, 0] and [1, 0, 1, 0] into [11, 10, 01, 00]. However, 00 and 01 are redundant. If the +//! outer level is null then the validity of the inner levels is irrelevant. To save space we instead +//! encode a "level" which is the "depth" of the null. Let's look at a more complete example: +//! +//! Array: [{"middle": {"inner": 1]}}, NULL, {"middle": NULL}, {"middle": {"inner": NULL}}] +//! +//! In Arrow we would have the following validity arrays: +//! Outer validity : 1, 0, 1, 1 +//! Middle validity: 1, ?, 0, 1 +//! Inner validity : 1, ?, ?, 0 +//! Values : 1, ?, ?, ? +//! +//! The ? values are undefined in the Arrow format. We can convert these into definition levels as follows: +//! +//! | Values | Definition | +//! | ------ | ---------- | +//! | 1 | 0 | // Valid at all levels +//! | - | 3 | // Null at outer level +//! | - | 2 | // Null at middle level +//! | - | 1 | // Null at inner level +//! +//! # Compression +//! +//! Note that we only need 2 bits of definition levels to represent 3 levels of nesting. Definition +//! levels are always more compact than the input validity arrays. However, compressed levels are not +//! necessarily more compact than the compressed validity arrays. +//! +//! Repetition levels are more complex. If there are very large lists then a sparse array of offsets +//! (which has one element per list) might be more compact than a dense array of repetition levels +//! (which has one element per list value, possibly even more if there are empty lists). +//! +//! However, both repetition levels and definition levels are typically very compressible with RLE. +//! +//! However, in Lance we don't always take advantage of that compression because we want to be able +//! to zip rep-def levels together with our values. This gives us fewer IOPS when accessing row values. +//! +//! # Utilities in this Module +//! +//! - `RepDefBuilder` - Extracts validity and offset information from Arrow arrays. We use this as we +//! shred the incoming data into primitive leaf arrays. We don't immediately convert into rep-def because +//! we need to share and cheaply clone the builder when we have structs (each struct child shares some parent +//! validity / offset information) The `serialize` method is called once all data has been received to create +//! the final rep-def levels. +//! +//! - `SerializerContext` - This is an internal utility that helps with serializing rep-def levels. +//! +//! - `CompositeRepDefUnraveler` - This structure is used to reverse the process. It starts with a set of +//! rep-def levels and then uses the `unravel_validity` and `unravel_offsets` methods to produce validity +//! buffers and offset buffers. It is "composite" because we may be combining sets of rep-def buffers from +//! multiple locations (e.g. multiple blocks in a mini-block encoded file). +//! +//! - `RepDefSlicer` - This is a utility that helps with slicing rep-def buffers. These buffers are "kind of" +//! transparent (maps 1:1 with the values in the array) but not exactly because of the special (empty/null) lists. +//! The slicer helps with this issue and is used when slicing a rep-def buffer into mini-blocks. +//! +//! - `build_control_word_iterator` - This takes in rep-def levels and returns an iterator that returns byte-padded +//! "control words" which are used when creating full-zip encoded data. +//! +//! - `ControlWordParser` - This parser can parse the control words returned by `build_control_word_iterator` and is +//! used when decoding full-zip encoded data. + +use std::{ + iter::{Copied, Zip}, + ops::Range, + sync::Arc, +}; + +use arrow_array::OffsetSizeTrait; +use arrow_buffer::{ + ArrowNativeType, BooleanBuffer, BooleanBufferBuilder, NullBuffer, OffsetBuffer, ScalarBuffer, +}; +use lance_core::{Error, Result, utils::bit::log_2_ceil}; + +use crate::{ + buffer::LanceBuffer, + encodings::logical::primitive::sparse::{SparseStructuralPlan, SparseStructuralUnraveler}, +}; + +pub type LevelBuffer = Vec; + +/// A top-level-row range whose dense rep/def stream fits one mini-block page. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct MiniBlockRepDefSplit { + /// Top-level row offset, relative to the original unsplit page. + pub(crate) row_start: u64, + /// Number of top-level rows in this split. + pub(crate) num_rows: u64, + /// Rep/def level range, relative to the original unsplit page. + pub(crate) level_range: Range, + /// Visible value offset, relative to the original unsplit page. + pub(crate) value_start: u64, + /// Number of visible values in this split. + pub(crate) num_values: u64, +} + +/// Dense mini-block rep/def budget result for one accumulated page. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum MiniBlockRepDefBudget { + /// The dense rep/def stream fits one mini-block structural page. + WithinBudget, + /// The dense rep/def stream fits after splitting on top-level row boundaries. + RequiresPageSplit(Vec), + /// A single top-level row has this many rep/def levels and exceeds the budget. + SingleRowOverBudget(u64), +} + +// As we build def levels we add this to special values to indicate that they +// are special so that we can skip over them when processing lower levels. +// +// We assume 16 bits is good enough for rep-def levels. This _would_ give +// us 65536 levels of struct nesting and list nesting. However, we cut that +// in half for SPECIAL_THRESHOLD because we use the top bit to indicate if an +// item is a special value (null list / empty list) during construction. +// +// We subtract this off at the end of construction to get the actual definition +// levels. +const SPECIAL_THRESHOLD: u16 = u16::MAX / 2; + +/// Represents information that we extract from a list array as we are +/// encoding +#[derive(Clone, Debug)] +struct OffsetDesc { + offsets: Arc<[i64]>, + validity: Option, + has_empty_lists: bool, + num_values: usize, + num_specials: usize, +} + +/// Represents validity information that we extract from non-list arrays (that +/// have nulls) as we are encoding +#[derive(Clone, Debug)] +struct ValidityDesc { + validity: Option, + num_values: usize, +} + +/// Represents validity information that we extract from FSL arrays. This is +/// just validity (no offsets) but we also record the dimension of the FSL array +/// as that will impact the next layer +#[derive(Clone, Debug)] +struct FslDesc { + validity: Option, + dimension: usize, + num_values: usize, +} + +// As we build up rep/def from arrow arrays we record a +// series of RawRepDef objects. Each one corresponds to layer +// in the array structure +#[derive(Clone, Debug)] +enum RawRepDef { + Offsets(OffsetDesc), + Validity(ValidityDesc), + Fsl(FslDesc), +} + +/// A normalized Arrow structural layer shared by dense and sparse serializers. +#[derive(Clone, Copy, Debug)] +pub(crate) enum NormalizedStructuralLayer<'a> { + List { + offsets: &'a [i64], + validity: Option<&'a BooleanBuffer>, + num_slots: usize, + }, + Validity { + validity: Option<&'a BooleanBuffer>, + num_slots: usize, + }, + FixedSizeList { + validity: Option<&'a BooleanBuffer>, + dimension: usize, + num_slots: usize, + }, +} + +/// Structural layers concatenated across input batches exactly once. +/// +/// Dense rep/def serialization and sparse metadata planning both consume this +/// representation so the Arrow nesting is not independently reconstructed. +#[derive(Debug)] +pub(crate) struct NormalizedStructuralPlan { + layers: Vec, + dense_all_valid: bool, +} + +impl NormalizedStructuralPlan { + pub(crate) fn layers(&self) -> impl ExactSizeIterator> { + self.layers.iter().map(|layer| match layer { + RawRepDef::Offsets(OffsetDesc { + offsets, + validity, + num_values, + .. + }) => NormalizedStructuralLayer::List { + offsets, + validity: validity.as_ref(), + num_slots: *num_values, + }, + RawRepDef::Validity(ValidityDesc { + validity, + num_values, + }) => NormalizedStructuralLayer::Validity { + validity: validity.as_ref(), + num_slots: *num_values, + }, + RawRepDef::Fsl(FslDesc { + validity, + dimension, + num_values, + }) => NormalizedStructuralLayer::FixedSizeList { + validity: validity.as_ref(), + dimension: *dimension, + num_slots: *num_values, + }, + }) + } + + fn to_serializer(&self) -> (SerializerContext, Option) { + if self.dense_all_valid { + let def_meaning = self + .layers + .iter() + .map(|_| DefinitionInterpretation::AllValidItem) + .collect::>(); + return ( + SerializerContext { + def_meaning, + rep_levels: LevelBuffer::default(), + spare_rep: LevelBuffer::default(), + def_levels: LevelBuffer::default(), + spare_def: LevelBuffer::default(), + current_rep: 0, + current_def: 0, + current_len: 0, + current_num_specials: 0, + has_fsl: false, + }, + None, + ); + } + + let total_len = self.layers.last().map_or(0, RawRepDef::num_values) + + self + .layers + .iter() + .map(RawRepDef::num_specials) + .sum::(); + let max_rep = self.layers.iter().map(RawRepDef::max_rep).sum::(); + let max_def = self.layers.iter().map(RawRepDef::max_def).sum::(); + let bits_per_rep = if max_rep > 0 { + u64::from(u16::BITS - max_rep.leading_zeros()) + } else { + 0 + }; + let bits_per_def = if max_def > 0 { + u64::from(u16::BITS - max_def.leading_zeros()) + } else { + 0 + }; + let bits_per_level = + (bits_per_rep + bits_per_def > 0).then_some(bits_per_rep + bits_per_def); + + let num_layers = self.layers.len(); + let mut context = SerializerContext::new(total_len, num_layers, max_rep, max_def); + for layer in &self.layers { + match layer { + RawRepDef::Validity(def) => context.record_validity(def), + RawRepDef::Offsets(rep) => context.record_offsets(rep), + RawRepDef::Fsl(fsl) => context.record_fsl(fsl), + } + } + (context, bits_per_level) + } + + pub(crate) fn serialize(&self) -> SerializedRepDefs { + self.to_serializer().0.build() + } + + pub(crate) fn serialize_with_miniblock_repdef_budget( + &self, + max_levels_for_bits: impl FnOnce(u64) -> u64, + num_rows: u64, + num_values: u64, + ) -> Result<(SerializedRepDefs, MiniBlockRepDefBudget)> { + let (context, bits_per_level) = self.to_serializer(); + context.build_with_miniblock_repdef_budget( + bits_per_level.map(max_levels_for_bits), + num_rows, + num_values, + ) + } +} + +impl RawRepDef { + // Are there any nulls in this layer + fn has_nulls(&self) -> bool { + match self { + Self::Offsets(OffsetDesc { validity, .. }) => validity.is_some(), + Self::Validity(ValidityDesc { validity, .. }) => validity.is_some(), + Self::Fsl(FslDesc { validity, .. }) => validity.is_some(), + } + } + + // How many values are in this layer + fn num_values(&self) -> usize { + match self { + Self::Offsets(OffsetDesc { num_values, .. }) => *num_values, + Self::Validity(ValidityDesc { num_values, .. }) => *num_values, + Self::Fsl(FslDesc { num_values, .. }) => *num_values, + } + } + + /// How many empty/null lists are in this layer + fn num_specials(&self) -> usize { + match self { + Self::Offsets(OffsetDesc { num_specials, .. }) => *num_specials, + _ => 0, + } + } + + /// How many definition levels do we need for this layer + fn max_def(&self) -> u16 { + match self { + Self::Offsets(OffsetDesc { + has_empty_lists, + validity, + .. + }) => { + let mut max_def = 0; + if *has_empty_lists { + max_def += 1; + } + if validity.is_some() { + max_def += 1; + } + max_def + } + Self::Validity(ValidityDesc { validity: None, .. }) => 0, + Self::Validity(ValidityDesc { .. }) => 1, + Self::Fsl(FslDesc { validity: None, .. }) => 0, + Self::Fsl(FslDesc { .. }) => 1, + } + } + + /// How many repetition levels do we need for this layer + fn max_rep(&self) -> u16 { + match self { + Self::Offsets(_) => 1, + _ => 0, + } + } +} + +/// Represents repetition and definition levels that have been +/// serialized into a pair of (optional) level buffers +#[derive(Debug)] +pub struct SerializedRepDefs { + /// The repetition levels, one per item + /// + /// If None, there are no lists + pub repetition_levels: Option>, + /// The definition levels, one per item + /// + /// If None, there are no nulls + pub definition_levels: Option>, + /// The meaning of each definition level + pub def_meaning: Vec, + /// The maximum level that is "visible" from the lowest level + /// + /// This is the last level before we encounter a list level of some kind. Once we've + /// hit a list level then nulls in any level beyond do not map to actual items. + /// + /// This is None if there are no lists + pub max_visible_level: Option, + has_fsl: bool, +} + +impl SerializedRepDefs { + fn max_visible_level(def_meaning: &[DefinitionInterpretation]) -> Option { + let first_list = def_meaning.iter().position(|level| level.is_list()); + first_list.map(|first_list| { + def_meaning + .iter() + .map(|level| level.num_def_levels()) + .take(first_list) + .sum::() + }) + } + + pub fn new( + repetition_levels: Option, + definition_levels: Option, + def_meaning: Vec, + ) -> Self { + Self::new_with_fixed_size_list_levels( + repetition_levels, + definition_levels, + def_meaning, + false, + ) + } + + pub(crate) fn new_with_fixed_size_list_levels( + repetition_levels: Option, + definition_levels: Option, + def_meaning: Vec, + has_fsl: bool, + ) -> Self { + let max_visible_level = Self::max_visible_level(&def_meaning); + Self { + repetition_levels: repetition_levels.map(Arc::from), + definition_levels: definition_levels.map(Arc::from), + def_meaning, + max_visible_level, + has_fsl, + } + } + + /// Creates an empty SerializedRepDefs (no repetition, all valid) + pub fn empty(def_meaning: Vec) -> Self { + Self { + repetition_levels: None, + definition_levels: None, + def_meaning, + max_visible_level: None, + has_fsl: false, + } + } + + pub fn rep_slicer(&self) -> Option> { + self.repetition_levels + .as_ref() + .map(|rep| RepDefSlicer::new(self, rep.clone())) + } + + pub fn def_slicer(&self) -> Option> { + self.definition_levels + .as_ref() + .map(|def| RepDefSlicer::new(self, def.clone())) + } + + pub(crate) fn has_fixed_size_list_levels(&self) -> bool { + self.has_fsl + } +} + +/// Slices a level buffer into pieces +/// +/// This is needed to handle the fact that a level buffer may have more +/// levels than values due to special (empty/null) lists. +/// +/// As a result, a call to `slice_next(10)` may return 10 levels or it may +/// return more than 10 levels if any special values are encountered. +#[derive(Debug)] +pub struct RepDefSlicer<'a> { + repdef: &'a SerializedRepDefs, + to_slice: LanceBuffer, + current: usize, +} + +// TODO: All of this logic will need some changing when we compress rep/def levels. +impl<'a> RepDefSlicer<'a> { + fn new(repdef: &'a SerializedRepDefs, levels: Arc<[u16]>) -> Self { + Self { + repdef, + to_slice: LanceBuffer::reinterpret_slice(levels), + current: 0, + } + } + + pub fn num_levels(&self) -> usize { + self.to_slice.len() / 2 + } + + pub fn num_levels_remaining(&self) -> usize { + self.num_levels() - self.current + } + + pub fn all_levels(&self) -> &LanceBuffer { + &self.to_slice + } + + /// Returns the rest of the levels not yet sliced + /// + /// This must be called instead of `slice_next` on the final iteration. + /// This is because anytime we slice there may be empty/null lists on the + /// boundary that are "free" and the current behavior in `slice_next` is to + /// leave them for the next call. + /// + /// `slice_rest` will slice all remaining levels and return them. + pub fn slice_rest(&mut self) -> LanceBuffer { + let start = self.current; + let remaining = self.num_levels_remaining(); + self.current = self.num_levels(); + self.to_slice.slice_with_length(start * 2, remaining * 2) + } + + /// Returns enough levels to satisfy the next `num_values` values + pub fn slice_next(&mut self, num_values: usize) -> LanceBuffer { + let start = self.current; + let Some(max_visible_level) = self.repdef.max_visible_level else { + // No lists, should be 1:1 mapping from levels to values + self.current = start + num_values; + return self.to_slice.slice_with_length(start * 2, num_values * 2); + }; + if let Some(def) = self.repdef.definition_levels.as_ref() { + // There are lists and there are def levels. That means there may be + // more rep/def levels than values. We need to scan the def levels to figure + // out which items are "invisible" and skip over them + let mut def_itr = def[start..].iter(); + let mut num_taken = 0; + let mut num_passed = 0; + while num_taken < num_values { + let def_level = *def_itr.next().unwrap(); + if def_level <= max_visible_level { + num_taken += 1; + } + num_passed += 1; + } + self.current = start + num_passed; + self.to_slice.slice_with_length(start * 2, num_passed * 2) + } else { + // No def levels, should be 1:1 mapping from levels to values + self.current = start + num_values; + self.to_slice.slice_with_length(start * 2, num_values * 2) + } + } +} + +/// This tells us how an array handles definition. Given a stack of +/// these and a nested array and a set of definition levels we can calculate +/// how we should interpret the definition levels. +/// +/// For example, if the interpretation is [AllValidItem, NullableItem] then +/// a 0 means "valid item" and a 1 means "null struct". If the interpretation +/// is [NullableItem, NullableItem] then a 0 means "valid item" and a 1 means +/// "null item" and a 2 means "null struct". +/// +/// Lists are tricky because we might use up to two definition levels for a +/// single layer of list nesting because we need one value to indicate "empty list" +/// and another value to indicate "null list". +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum DefinitionInterpretation { + AllValidItem, + AllValidList, + NullableItem, + NullableList, + EmptyableList, + NullableAndEmptyableList, +} + +impl DefinitionInterpretation { + /// How many definition levels do we need for this layer + pub fn num_def_levels(&self) -> u16 { + match self { + Self::AllValidItem => 0, + Self::AllValidList => 0, + Self::NullableItem => 1, + Self::NullableList => 1, + Self::EmptyableList => 1, + Self::NullableAndEmptyableList => 2, + } + } + + /// Does this layer have nulls? + pub fn is_all_valid(&self) -> bool { + matches!( + self, + Self::AllValidItem | Self::AllValidList | Self::EmptyableList + ) + } + + /// Does this layer represent a list? + pub fn is_list(&self) -> bool { + matches!( + self, + Self::AllValidList + | Self::NullableList + | Self::EmptyableList + | Self::NullableAndEmptyableList + ) + } +} + +/// The RepDefBuilder is used to collect offsets & validity buffers +/// from arrow structures. Once we have those we use the SerializerContext +/// to build the actual repetition and definition levels. +/// +/// We know ahead of time how many rep/def levels we will need (number of items +/// in inner-most array + the number of empty/null lists in any parent arrays). +/// +/// As a result we try and avoid any re-allocations by pre-allocating the buffers +/// up front. We allocate two copies of each buffer which allows us to avoid unsafe +/// code caused by reading and writing to the same buffer (also, it's unavoidable +/// because there are times we need to write 'faster' than we read) +#[derive(Debug)] +struct SerializerContext { + // This is built from outer-to-inner and then reversed at the end + def_meaning: Vec, + rep_levels: LevelBuffer, + spare_rep: LevelBuffer, + def_levels: LevelBuffer, + spare_def: LevelBuffer, + current_rep: u16, + current_def: u16, + current_len: usize, + current_num_specials: usize, + has_fsl: bool, +} + +impl SerializerContext { + fn new(len: usize, num_layers: usize, max_rep: u16, max_def: u16) -> Self { + let def_meaning = Vec::with_capacity(num_layers); + Self { + rep_levels: if max_rep > 0 { + vec![0; len] + } else { + LevelBuffer::default() + }, + spare_rep: if max_rep > 0 { + vec![0; len] + } else { + LevelBuffer::default() + }, + def_levels: if max_def > 0 { + vec![0; len] + } else { + LevelBuffer::default() + }, + spare_def: if max_def > 0 { + vec![0; len] + } else { + LevelBuffer::default() + }, + def_meaning, + current_rep: max_rep, + current_def: max_def, + current_len: 0, + current_num_specials: 0, + has_fsl: false, + } + } + + fn checkout_def(&mut self, meaning: DefinitionInterpretation) -> u16 { + let def = self.current_def; + self.current_def -= meaning.num_def_levels(); + self.def_meaning.push(meaning); + def + } + + fn record_offsets(&mut self, offset_desc: &OffsetDesc) { + let rep_level = self.current_rep; + let (null_list_level, empty_list_level) = + match (offset_desc.validity.is_some(), offset_desc.has_empty_lists) { + (true, true) => { + let level = + self.checkout_def(DefinitionInterpretation::NullableAndEmptyableList); + (level - 1, level) + } + (true, false) => (self.checkout_def(DefinitionInterpretation::NullableList), 0), + (false, true) => ( + 0, + self.checkout_def(DefinitionInterpretation::EmptyableList), + ), + (false, false) => { + self.checkout_def(DefinitionInterpretation::AllValidList); + (0, 0) + } + }; + self.current_rep -= 1; + + if let Some(validity) = &offset_desc.validity { + self.do_record_validity(validity, null_list_level); + } + + // We write into the spare buffers and read from the active buffers + // and then swap at the end. This way we don't write over what we + // are reading. + + let mut new_len = 0; + let expected_len = offset_desc.num_values + self.current_num_specials; + if expected_len == 0 { + // Offsets [0] mean no list values, so no levels. + self.current_len = 0; + return; + } + assert!(self.rep_levels.len() >= expected_len - 1); + if self.def_levels.is_empty() { + let mut write_itr = self.spare_rep.iter_mut(); + let mut read_iter = self.rep_levels.iter().copied(); + for w in offset_desc.offsets.windows(2) { + let len = w[1] - w[0]; + // len can't be 0 because then we'd have def levels + assert!(len > 0); + let rep = read_iter.next().unwrap(); + let list_level = if rep == 0 { rep_level } else { rep }; + *write_itr.next().unwrap() = list_level; + + for _ in 1..len { + *write_itr.next().unwrap() = 0; + } + new_len += len as usize; + } + std::mem::swap(&mut self.rep_levels, &mut self.spare_rep); + } else { + assert!(self.def_levels.len() >= expected_len - 1); + let mut def_write_itr = self.spare_def.iter_mut(); + let mut rep_write_itr = self.spare_rep.iter_mut(); + let mut rep_read_itr = self.rep_levels.iter().copied(); + let mut def_read_itr = self.def_levels.iter().copied(); + let specials_to_pass = self.current_num_specials; + let mut specials_passed = 0; + + for w in offset_desc.offsets.windows(2) { + let mut def = def_read_itr.next().unwrap(); + // Copy over any higher-level special values in place + while def > SPECIAL_THRESHOLD { + *def_write_itr.next().unwrap() = def; + *rep_write_itr.next().unwrap() = rep_read_itr.next().unwrap(); + def = def_read_itr.next().unwrap(); + new_len += 1; + specials_passed += 1; + } + + let len = w[1] - w[0]; + let rep = rep_read_itr.next().unwrap(); + + // If the rep_level is 0 then we are the first list level + // otherwise we are starting a higher level list so keep + // existing rep level + let list_level = if rep == 0 { rep_level } else { rep }; + + if def == 0 && len > 0 { + // New valid list, write a rep level and then add new 0/0 items + *def_write_itr.next().unwrap() = 0; + *rep_write_itr.next().unwrap() = list_level; + + for _ in 1..len { + *def_write_itr.next().unwrap() = 0; + *rep_write_itr.next().unwrap() = 0; + } + + new_len += len as usize; + } else if def == 0 { + // Empty list, insert new special + *def_write_itr.next().unwrap() = empty_list_level + SPECIAL_THRESHOLD; + *rep_write_itr.next().unwrap() = list_level; + new_len += 1; + } else { + // Either the list is null or one of its struct parents + // is null. Promote it to a special value. + *def_write_itr.next().unwrap() = def + SPECIAL_THRESHOLD; + *rep_write_itr.next().unwrap() = list_level; + new_len += 1; + } + } + + // If we have any special values at the end, we need to copy them over + while specials_passed < specials_to_pass { + *def_write_itr.next().unwrap() = def_read_itr.next().unwrap(); + *rep_write_itr.next().unwrap() = rep_read_itr.next().unwrap(); + new_len += 1; + specials_passed += 1; + } + std::mem::swap(&mut self.def_levels, &mut self.spare_def); + std::mem::swap(&mut self.rep_levels, &mut self.spare_rep); + } + + self.current_len = new_len; + self.current_num_specials += offset_desc.num_specials; + } + + fn do_record_validity(&mut self, validity: &BooleanBuffer, null_level: u16) { + assert!(self.def_levels.len() >= validity.len() + self.current_num_specials); + debug_assert!( + self.current_len == 0 || self.current_len == validity.len() + self.current_num_specials + ); + self.current_len = validity.len(); + + let mut def_read_itr = self.def_levels.iter().copied(); + let mut def_write_itr = self.spare_def.iter_mut(); + + let specials_to_pass = self.current_num_specials; + let mut specials_passed = 0; + + for incoming_validity in validity.iter() { + let mut def = def_read_itr.next().unwrap(); + while def > SPECIAL_THRESHOLD { + *def_write_itr.next().unwrap() = def; + def = def_read_itr.next().unwrap(); + specials_passed += 1; + } + if def == 0 && !incoming_validity { + *def_write_itr.next().unwrap() = null_level; + } else { + *def_write_itr.next().unwrap() = def; + } + } + + while specials_passed < specials_to_pass { + *def_write_itr.next().unwrap() = def_read_itr.next().unwrap(); + specials_passed += 1; + } + + std::mem::swap(&mut self.def_levels, &mut self.spare_def); + } + + fn multiply_levels(&mut self, multiplier: usize) { + let old_len = self.current_len; + // All non-special values will be broadcasted by the multiplier. Special values are copied as-is. + self.current_len = + (self.current_len - self.current_num_specials) * multiplier + self.current_num_specials; + + if self.rep_levels.is_empty() && self.def_levels.is_empty() { + // All valid with no rep/def levels, nothing to do + return; + } else if self.rep_levels.is_empty() { + assert!(self.def_levels.len() >= self.current_len); + // No rep levels, just multiply the def levels + let mut def_read_itr = self.def_levels.iter().copied(); + let mut def_write_itr = self.spare_def.iter_mut(); + for _ in 0..old_len { + let mut def = def_read_itr.next().unwrap(); + while def > SPECIAL_THRESHOLD { + *def_write_itr.next().unwrap() = def; + def = def_read_itr.next().unwrap(); + } + for _ in 0..multiplier { + *def_write_itr.next().unwrap() = def; + } + } + } else if self.def_levels.is_empty() { + assert!(self.rep_levels.len() >= self.current_len); + // No def levels, just multiply the rep levels + let mut rep_read_itr = self.rep_levels.iter().copied(); + let mut rep_write_itr = self.spare_rep.iter_mut(); + for _ in 0..old_len { + let rep = rep_read_itr.next().unwrap(); + for _ in 0..multiplier { + *rep_write_itr.next().unwrap() = rep; + } + } + } else { + assert!(self.rep_levels.len() >= self.current_len); + assert!(self.def_levels.len() >= self.current_len); + let mut rep_read_itr = self.rep_levels.iter().copied(); + let mut def_read_itr = self.def_levels.iter().copied(); + let mut rep_write_itr = self.spare_rep.iter_mut(); + let mut def_write_itr = self.spare_def.iter_mut(); + for _ in 0..old_len { + let mut def = def_read_itr.next().unwrap(); + while def > SPECIAL_THRESHOLD { + *def_write_itr.next().unwrap() = def; + *rep_write_itr.next().unwrap() = rep_read_itr.next().unwrap(); + def = def_read_itr.next().unwrap(); + } + let rep = rep_read_itr.next().unwrap(); + for _ in 0..multiplier { + *def_write_itr.next().unwrap() = def; + *rep_write_itr.next().unwrap() = rep; + } + } + } + std::mem::swap(&mut self.def_levels, &mut self.spare_def); + std::mem::swap(&mut self.rep_levels, &mut self.spare_rep); + } + + fn record_validity_buf(&mut self, validity: &Option) { + if let Some(validity) = validity { + let def_level = self.checkout_def(DefinitionInterpretation::NullableItem); + self.do_record_validity(validity, def_level); + } else { + self.checkout_def(DefinitionInterpretation::AllValidItem); + } + } + + fn record_validity(&mut self, validity_desc: &ValidityDesc) { + self.record_validity_buf(&validity_desc.validity) + } + + fn record_fsl(&mut self, fsl_desc: &FslDesc) { + self.has_fsl = true; + self.record_validity_buf(&fsl_desc.validity); + self.multiply_levels(fsl_desc.dimension); + } + + fn normalize_specials(&mut self) { + for def in self.def_levels.iter_mut() { + if *def > SPECIAL_THRESHOLD { + *def -= SPECIAL_THRESHOLD; + } + } + } + + fn normalize_specials_and_plan_splits( + &mut self, + def_meaning: &[DefinitionInterpretation], + max_levels_per_page: Option, + num_rows: u64, + num_values: u64, + ) -> Result { + // Extremely sparse lists can have many rep/def levels for very few + // visible leaf values. If this ratio becomes too skewed then a + // mini-block rep/def chunk can exceed its packed metadata budget even + // though the value buffers are small. We detect that case while + // normalizing special def levels and split on top-level row boundaries + // so each emitted dense mini-block page stays within the budget. + if self.def_levels.is_empty() { + return Ok(MiniBlockRepDefBudget::WithinBudget); + } + + if self.rep_levels.is_empty() { + self.normalize_specials(); + return Ok(MiniBlockRepDefBudget::WithinBudget); + } + + if self.rep_levels.len() != self.def_levels.len() { + return Err(Error::internal(format!( + "Cannot plan structural page splits with mismatched rep/def lengths: rep={}, def={}", + self.rep_levels.len(), + self.def_levels.len() + ))); + } + + let Some(max_levels_per_page) = max_levels_per_page else { + self.normalize_specials(); + return Ok(MiniBlockRepDefBudget::WithinBudget); + }; + + if num_values == 0 { + self.normalize_specials(); + return Ok(MiniBlockRepDefBudget::WithinBudget); + } + + let max_schema_rep = def_meaning.iter().filter(|level| level.is_list()).count() as u16; + let max_visible_level = SerializedRepDefs::max_visible_level(def_meaning); + let should_plan = !self.has_fsl && max_schema_rep > 0 && max_visible_level.is_some(); + + if !should_plan { + self.normalize_specials(); + return Ok(MiniBlockRepDefBudget::WithinBudget); + } + + let max_visible_level = max_visible_level.unwrap(); + let mut splits = Vec::new(); + let mut counted_rows = 0u64; + let mut counted_values = 0u64; + let mut saw_structural_overhead = false; + let mut single_row_over_budget_levels = None; + + let mut current_row_level_start = None; + let mut current_row_num_values = 0u64; + + let mut current_page_row_start = 0u64; + let mut current_page_num_rows = 0u64; + let mut current_page_level_start = 0usize; + let mut current_page_level_end = 0usize; + let mut current_page_value_start = 0u64; + let mut current_page_num_values = 0u64; + let mut current_page_num_levels = 0u64; + let mut current_page_has_structural_overhead = false; + + let mut finish_row = + |row_level_start: usize, row_level_end: usize, row_num_values: u64| -> Result<()> { + let row_num_levels = (row_level_end - row_level_start) as u64; + let row_has_structural_overhead = row_num_levels > row_num_values; + saw_structural_overhead |= row_has_structural_overhead; + + if row_has_structural_overhead && row_num_levels > max_levels_per_page { + single_row_over_budget_levels = Some(row_num_levels); + } + + if current_page_num_rows > 0 + && (current_page_has_structural_overhead || row_has_structural_overhead) + && current_page_num_levels + row_num_levels > max_levels_per_page + { + splits.push(MiniBlockRepDefSplit { + row_start: current_page_row_start, + num_rows: current_page_num_rows, + level_range: current_page_level_start..current_page_level_end, + value_start: current_page_value_start, + num_values: current_page_num_values, + }); + current_page_row_start = counted_rows; + current_page_num_rows = 0; + current_page_level_start = row_level_start; + current_page_value_start = counted_values; + current_page_num_values = 0; + current_page_num_levels = 0; + current_page_has_structural_overhead = false; + } + + if current_page_num_rows == 0 { + current_page_level_start = row_level_start; + } + current_page_num_rows += 1; + current_page_level_end = row_level_end; + current_page_num_values += row_num_values; + current_page_num_levels += row_num_levels; + current_page_has_structural_overhead |= row_has_structural_overhead; + counted_rows += 1; + counted_values += row_num_values; + Ok(()) + }; + + for (idx, (rep_level, def_level)) in self + .rep_levels + .iter() + .copied() + .zip(self.def_levels.iter_mut()) + .enumerate() + { + if *def_level > SPECIAL_THRESHOLD { + *def_level -= SPECIAL_THRESHOLD; + } + + if rep_level == max_schema_rep { + if let Some(level_start) = current_row_level_start { + finish_row(level_start, idx, current_row_num_values)?; + current_row_num_values = 0; + } else if idx != 0 { + return Err(Error::internal(format!( + "Cannot plan structural page splits: first top-level row starts at level {}, expected 0", + idx + ))); + } + current_row_level_start = Some(idx); + } + + if current_row_level_start.is_none() { + return Err(Error::internal( + "Cannot plan structural page splits: found levels before the first top-level row start", + )); + } + if *def_level <= max_visible_level { + current_row_num_values += 1; + } + } + + let Some(level_start) = current_row_level_start else { + return Err(Error::internal( + "Cannot plan structural page splits: found no top-level row starts", + )); + }; + finish_row(level_start, self.rep_levels.len(), current_row_num_values)?; + + if counted_rows != num_rows { + return Err(Error::internal(format!( + "Cannot plan structural page splits: expected {} top-level row starts, found {}", + num_rows, counted_rows + ))); + } + if counted_values != num_values { + return Err(Error::internal(format!( + "Cannot plan structural page splits: counted {} visible values, expected {}", + counted_values, num_values + ))); + } + if !saw_structural_overhead { + return Ok(MiniBlockRepDefBudget::WithinBudget); + } + if let Some(row_num_levels) = single_row_over_budget_levels { + return Ok(MiniBlockRepDefBudget::SingleRowOverBudget(row_num_levels)); + } + + if current_page_num_rows > 0 { + splits.push(MiniBlockRepDefSplit { + row_start: current_page_row_start, + num_rows: current_page_num_rows, + level_range: current_page_level_start..current_page_level_end, + value_start: current_page_value_start, + num_values: current_page_num_values, + }); + } + + if splits.len() > 1 { + Ok(MiniBlockRepDefBudget::RequiresPageSplit(splits)) + } else { + Ok(MiniBlockRepDefBudget::WithinBudget) + } + } + + fn build(mut self) -> SerializedRepDefs { + if self.current_len == 0 { + return SerializedRepDefs::new_with_fixed_size_list_levels( + None, + None, + self.def_meaning, + self.has_fsl, + ); + } + + self.normalize_specials(); + + let definition_levels = if self.def_levels.is_empty() { + None + } else { + Some(self.def_levels) + }; + let repetition_levels = if self.rep_levels.is_empty() { + None + } else { + Some(self.rep_levels) + }; + + // Need to reverse the def meaning since we build rep / def levels in reverse + let def_meaning = self.def_meaning.into_iter().rev().collect::>(); + + SerializedRepDefs::new_with_fixed_size_list_levels( + repetition_levels, + definition_levels, + def_meaning, + self.has_fsl, + ) + } + + fn build_with_miniblock_repdef_budget( + mut self, + max_levels_per_page: Option, + num_rows: u64, + num_values: u64, + ) -> Result<(SerializedRepDefs, MiniBlockRepDefBudget)> { + if self.current_len == 0 { + return Ok(( + SerializedRepDefs::new_with_fixed_size_list_levels( + None, + None, + self.def_meaning, + self.has_fsl, + ), + MiniBlockRepDefBudget::WithinBudget, + )); + } + + // Need to reverse the def meaning since we build rep / def levels in reverse + let def_meaning = std::mem::take(&mut self.def_meaning) + .into_iter() + .rev() + .collect::>(); + let budget = self.normalize_specials_and_plan_splits( + &def_meaning, + max_levels_per_page, + num_rows, + num_values, + )?; + + let definition_levels = if self.def_levels.is_empty() { + None + } else { + Some(self.def_levels) + }; + let repetition_levels = if self.rep_levels.is_empty() { + None + } else { + Some(self.rep_levels) + }; + + Ok(( + SerializedRepDefs::new_with_fixed_size_list_levels( + repetition_levels, + definition_levels, + def_meaning, + self.has_fsl, + ), + budget, + )) + } +} + +/// A structure used to collect validity buffers and offsets from arrow +/// arrays and eventually create repetition and definition levels +/// +/// As we are encoding the structural encoders are given this struct and +/// will record the arrow information into it. Once we hit a leaf node we +/// serialize the data into rep/def levels and write these into the page. +#[derive(Clone, Default, Debug)] +pub struct RepDefBuilder { + // The rep/def info we have collected so far + repdefs: Vec, + // The current length, can get larger as we traverse lists (e.g. an + // array might have 5 lists which results in 50 items) + // + // Starts uninitialized until we see the first rep/def item + len: Option, +} + +impl RepDefBuilder { + fn check_validity_len(&mut self, incoming_len: usize) { + if let Some(len) = self.len { + assert_eq!(incoming_len, len); + } else { + // First validity buffer we've seen + self.len = Some(incoming_len); + } + } + + fn num_layers(&self) -> usize { + self.repdefs.len() + } + + /// The builder is "empty" if there is no repetition and no nulls. In this case we don't need + /// to store anything to disk (except the description) + pub fn is_empty(&self) -> bool { + self.repdefs + .iter() + .all(|r| matches!(r, RawRepDef::Validity(ValidityDesc { validity: None, .. }))) + } + + /// Returns true if there is only a single layer of definition + pub fn is_simple_validity(&self) -> bool { + self.repdefs.len() == 1 && matches!(self.repdefs[0], RawRepDef::Validity(_)) + } + + /// Registers a nullable validity bitmap + pub fn add_validity_bitmap(&mut self, validity: NullBuffer) { + self.check_validity_len(validity.len()); + if validity.null_count() == 0 { + self.add_no_null(validity.len()); + return; + } + self.repdefs.push(RawRepDef::Validity(ValidityDesc { + num_values: validity.len(), + validity: Some(validity.into_inner()), + })); + } + + /// Registers an all-valid validity layer + pub fn add_no_null(&mut self, len: usize) { + self.check_validity_len(len); + self.repdefs.push(RawRepDef::Validity(ValidityDesc { + validity: None, + num_values: len, + })); + } + + pub fn add_fsl(&mut self, validity: Option, dimension: usize, num_values: usize) { + if let Some(len) = self.len { + assert_eq!(num_values, len); + } + self.len = Some(num_values * dimension); + debug_assert!(validity.is_none() || validity.as_ref().unwrap().len() == num_values); + self.repdefs.push(RawRepDef::Fsl(FslDesc { + num_values, + validity: validity.map(|v| v.into_inner()), + dimension, + })) + } + + fn check_offset_len(&mut self, offsets: &[i64]) { + if let Some(len) = self.len { + assert!(offsets.len() == len + 1); + } + self.len = Some(offsets[offsets.len() - 1] as usize); + } + + fn do_add_offsets( + &mut self, + lengths: impl Iterator, + validity: Option, + capacity: usize, + ) -> bool { + let mut num_specials = 0; + let mut has_empty_lists = false; + let mut has_garbage_values = false; + let mut last_off: i64 = 0; + + let mut normalized_offsets = Vec::with_capacity(capacity); + normalized_offsets.push(0); + + if let Some(ref validity) = validity { + for (len, is_valid) in lengths.zip(validity.iter()) { + match (is_valid, len == 0) { + (false, is_empty) => { + num_specials += 1; + has_garbage_values |= !is_empty; + } + (true, true) => { + num_specials += 1; + has_empty_lists = true; + } + _ => { + last_off += len; + } + } + normalized_offsets.push(last_off); + } + } else { + for len in lengths { + if len == 0 { + num_specials += 1; + has_empty_lists = true; + } + last_off += len; + normalized_offsets.push(last_off); + } + } + + self.check_offset_len(&normalized_offsets); + self.repdefs.push(RawRepDef::Offsets(OffsetDesc { + num_values: normalized_offsets.len() - 1, + offsets: normalized_offsets.into(), + validity: validity.map(|v| v.into_inner()), + has_empty_lists, + num_specials: num_specials as usize, + })); + + has_garbage_values + } + + /// Adds a layer of offsets + /// + /// Offsets are casted to a common type (i64) and also normalized. Null lists are + /// always represented by a zero-length (identical) pair of offsets and so the caller + /// should filter out any garbage items before encoding them. To assist with this the + /// method will return true if any non-empty null lists were found. + pub fn add_offsets( + &mut self, + offsets: OffsetBuffer, + validity: Option, + ) -> bool { + let inner = offsets.into_inner(); + let buffer_len = inner.len(); + + if O::IS_LARGE { + let i64_buff = ScalarBuffer::::new(inner.into_inner(), 0, buffer_len); + let lengths = i64_buff.windows(2).map(|off| off[1] - off[0]); + self.do_add_offsets(lengths, validity, buffer_len) + } else { + let i32_buff = ScalarBuffer::::new(inner.into_inner(), 0, buffer_len); + let lengths = i32_buff.windows(2).map(|off| (off[1] - off[0]) as i64); + self.do_add_offsets(lengths, validity, buffer_len) + } + } + + // When we are encoding data it arrives in batches. For each batch we create a RepDefBuilder and collect the + // various validity buffers and offset buffers from that batch. Once we have enough batches to write a page we + // need to take this collection of RepDefBuilders and concatenate them and then serialize them into rep/def levels. + // + // TODO: In the future, we may concatenate and serialize at the same time? + // + // This method takes care of the concatenation part. First we collect all of layer 0 from each builder, then we + // call this method. Then we collect all of layer 1 from each builder and call this method. And so on. + // + // That means this method should get a collection of `RawRepDef` where each item is the same kind (all validity or + // all offsets) though the nullability / lengths may be different in each layer. + fn concat_layers<'a>( + layers: impl Iterator, + num_layers: usize, + ) -> RawRepDef { + enum LayerKind { + Validity, + Fsl, + Offsets, + } + + // We make two passes through the layers. The first determines if we need to pay the cost of allocating + // buffers. The second pass actually adds the values. + let mut collected = Vec::with_capacity(num_layers); + let mut has_nulls = false; + let mut layer_kind = LayerKind::Validity; + let mut total_num_specials = 0; + let mut all_dimension = 0; + let mut all_has_empty_lists = false; + let mut all_num_values = 0; + for layer in layers { + has_nulls |= layer.has_nulls(); + match layer { + RawRepDef::Validity(_) => { + layer_kind = LayerKind::Validity; + } + RawRepDef::Offsets(OffsetDesc { + num_specials, + has_empty_lists, + .. + }) => { + all_has_empty_lists |= *has_empty_lists; + layer_kind = LayerKind::Offsets; + total_num_specials += num_specials; + } + RawRepDef::Fsl(FslDesc { dimension, .. }) => { + layer_kind = LayerKind::Fsl; + all_dimension = *dimension; + } + } + collected.push(layer); + all_num_values += layer.num_values(); + } + + // Shortcut if there are no nulls + if !has_nulls { + match layer_kind { + LayerKind::Validity => { + return RawRepDef::Validity(ValidityDesc { + validity: None, + num_values: all_num_values, + }); + } + LayerKind::Fsl => { + return RawRepDef::Fsl(FslDesc { + validity: None, + num_values: all_num_values, + dimension: all_dimension, + }); + } + LayerKind::Offsets => {} + } + } + + // Only allocate if needed + let mut validity_builder = if has_nulls { + BooleanBufferBuilder::new(all_num_values) + } else { + BooleanBufferBuilder::new(0) + }; + let mut all_offsets = if matches!(layer_kind, LayerKind::Offsets) { + let mut all_offsets = Vec::with_capacity(all_num_values); + all_offsets.push(0); + all_offsets + } else { + Vec::new() + }; + + for layer in collected { + match layer { + RawRepDef::Validity(ValidityDesc { + validity: Some(validity), + .. + }) => { + validity_builder.append_buffer(validity); + } + RawRepDef::Validity(ValidityDesc { + validity: None, + num_values, + }) => { + validity_builder.append_n(*num_values, true); + } + RawRepDef::Fsl(FslDesc { + validity, + num_values, + .. + }) => { + if let Some(validity) = validity { + validity_builder.append_buffer(validity); + } else { + validity_builder.append_n(*num_values, true); + } + } + RawRepDef::Offsets(OffsetDesc { + offsets, + validity: Some(validity), + has_empty_lists, + .. + }) => { + all_has_empty_lists |= has_empty_lists; + validity_builder.append_buffer(validity); + let last = *all_offsets.last().unwrap(); + all_offsets.extend(offsets.iter().skip(1).map(|off| *off + last)); + } + RawRepDef::Offsets(OffsetDesc { + offsets, + validity: None, + has_empty_lists, + num_values, + .. + }) => { + all_has_empty_lists |= has_empty_lists; + if has_nulls { + validity_builder.append_n(*num_values, true); + } + let last = *all_offsets.last().unwrap(); + all_offsets.extend(offsets.iter().skip(1).map(|off| *off + last)); + } + } + } + let validity = if has_nulls { + Some(validity_builder.finish()) + } else { + None + }; + match layer_kind { + LayerKind::Fsl => RawRepDef::Fsl(FslDesc { + validity, + num_values: all_num_values, + dimension: all_dimension, + }), + LayerKind::Validity => RawRepDef::Validity(ValidityDesc { + validity, + num_values: all_num_values, + }), + LayerKind::Offsets => RawRepDef::Offsets(OffsetDesc { + offsets: all_offsets.into(), + validity, + has_empty_lists: all_has_empty_lists, + num_values: all_num_values, + num_specials: total_num_specials, + }), + } + } + + /// Converts the validity / offsets buffers that have been gathered so far + /// into repetition and definition levels + pub fn serialize(builders: Vec) -> SerializedRepDefs { + Self::normalize(builders).serialize() + } + + pub(crate) fn normalize(builders: Vec) -> NormalizedStructuralPlan { + assert!(!builders.is_empty()); + let num_layers = builders[0].num_layers(); + debug_assert!( + builders + .iter() + .all(|builder| builder.num_layers() == num_layers) + ); + let layers = (0..num_layers) + .map(|layer_index| { + Self::concat_layers( + builders.iter().map(|b| &b.repdefs[layer_index]), + builders.len(), + ) + }) + .collect::>(); + NormalizedStructuralPlan { + layers, + dense_all_valid: builders.iter().all(Self::is_empty), + } + } +} + +/// Starts with serialized repetition and definition levels and unravels +/// them into validity buffers and offsets buffers +/// +/// This is used during decoding to create the necessary arrow structures +#[derive(Debug)] +pub struct RepDefUnraveler { + sparse: Option, + rep_levels: Option, + def_levels: Option, + // Maps from definition level to the rep level at which that definition level is visible + levels_to_rep: Vec, + def_meaning: Arc<[DefinitionInterpretation]>, + // Current definition level to compare to. + current_def_cmp: u16, + // Current rep level, determines which specials we can see + current_rep_cmp: u16, + // Current layer index, 0 means inner-most layer and it counts up from there. Used to index + // into special_defs + current_layer: usize, + // Number of items in the inner-most layer (needed if the definition levels are not present) + num_items: u64, +} + +impl RepDefUnraveler { + /// Creates a new unraveler from serialized repetition and definition information + pub fn new( + rep_levels: Option, + def_levels: Option, + def_meaning: Arc<[DefinitionInterpretation]>, + num_items: u64, + ) -> Self { + let mut levels_to_rep = Vec::with_capacity(def_meaning.len()); + let mut rep_counter = 0; + // Level=0 is always visible and means valid item + levels_to_rep.push(0); + for meaning in def_meaning.as_ref() { + match meaning { + DefinitionInterpretation::AllValidItem | DefinitionInterpretation::AllValidList => { + // There is no corresponding level, so nothing to put in levels_to_rep + } + DefinitionInterpretation::NullableItem => { + // Some null structs are not visible at inner rep levels in cases like LIST>> + levels_to_rep.push(rep_counter); + } + DefinitionInterpretation::NullableList => { + rep_counter += 1; + levels_to_rep.push(rep_counter); + } + DefinitionInterpretation::EmptyableList => { + rep_counter += 1; + levels_to_rep.push(rep_counter); + } + DefinitionInterpretation::NullableAndEmptyableList => { + rep_counter += 1; + levels_to_rep.push(rep_counter); + levels_to_rep.push(rep_counter); + } + } + } + Self { + sparse: None, + rep_levels, + def_levels, + current_def_cmp: 0, + current_rep_cmp: 0, + levels_to_rep, + current_layer: 0, + def_meaning, + num_items, + } + } + + pub(crate) fn new_sparse(plan: SparseStructuralPlan) -> Self { + Self { + sparse: Some(SparseStructuralUnraveler::new(plan)), + rep_levels: None, + def_levels: None, + levels_to_rep: Vec::new(), + def_meaning: Arc::new([]), + current_def_cmp: 0, + current_rep_cmp: 0, + current_layer: 0, + num_items: 0, + } + } + + fn ensure_exhausted(&self) -> Result<()> { + if let Some(sparse) = &self.sparse { + sparse.ensure_exhausted()?; + } + Ok(()) + } + + fn is_sparse(&self) -> bool { + self.sparse.is_some() + } + + pub fn is_all_valid(&self) -> bool { + if let Some(sparse) = &self.sparse { + return sparse.is_all_valid(); + } + self.def_levels.is_none() || self.def_meaning[self.current_layer].is_all_valid() + } + + /// If the current level is a repetition layer then this returns the number of lists + /// at this level. + /// + /// This is not valid to call when the current level is a struct/primitive layer because + /// in some cases there may be no rep or def information to know this. + pub fn max_lists(&self) -> Result { + if let Some(sparse) = &self.sparse { + return sparse.max_lists(); + } + debug_assert!( + self.def_meaning[self.current_layer] != DefinitionInterpretation::NullableItem + ); + Ok(self + .rep_levels + .as_ref() + // Worst case every rep item is max_rep and a new list + .map(|levels| levels.len()) + .unwrap_or(0)) + } + + /// Unravels a layer of offsets from the unraveler into the given offset width + /// + /// When decoding a list the caller should first unravel the offsets and then + /// unravel the validity (this is the opposite order used during encoding) + pub fn unravel_offsets( + &mut self, + offsets: &mut Vec, + validity: Option<&mut BooleanBufferBuilder>, + ) -> Result<()> { + if let Some(sparse) = self.sparse.as_mut() { + return sparse.unravel_offsets(offsets, validity); + } + let rep_levels = self + .rep_levels + .as_mut() + .expect("Expected repetition level but data didn't contain repetition"); + let valid_level = self.current_def_cmp; + let (null_level, empty_level) = match self.def_meaning[self.current_layer] { + DefinitionInterpretation::NullableList => { + self.current_def_cmp += 1; + (valid_level + 1, 0) + } + DefinitionInterpretation::EmptyableList => { + self.current_def_cmp += 1; + (0, valid_level + 1) + } + DefinitionInterpretation::NullableAndEmptyableList => { + self.current_def_cmp += 2; + (valid_level + 1, valid_level + 2) + } + DefinitionInterpretation::AllValidList => (0, 0), + _ => unreachable!(), + }; + self.current_layer += 1; + + // This is the highest def level that is still visible. Once we hit a list then + // we stop looking because any null / empty list (or list masked by a higher level + // null) will not be visible + let mut max_level = null_level.max(empty_level).max(valid_level); + // Anything higher than this (but less than max_level) is a null struct masking our + // list. We will materialize this is a null list. + let upper_null = max_level; + for level in self.def_meaning[self.current_layer..].iter() { + match level { + DefinitionInterpretation::NullableItem => { + max_level += 1; + } + DefinitionInterpretation::AllValidItem => {} + _ => { + break; + } + } + } + + let mut curlen: usize = offsets.last().map(|o| o.as_usize()).unwrap_or(0); + + // If offsets is empty this is a no-op. If offsets is not empty that means we already + // added a set of offsets. For example, we might have added [0, 3, 5] (2 lists). Now + // say we want to add [0, 1, 4] (2 lists). We should get [0, 3, 5, 6, 9] (4 lists). If + // we don't pop here we get [0, 3, 5, 5, 6, 9] which is wrong. + // + // Or, to think about it another way, if every unraveler adds the starting 0 and the trailing + // length then we have N + unravelers.len() values instead of N + 1. + offsets.pop(); + + let to_offset = |val: usize| { + T::from_usize(val) + .ok_or_else(|| Error::invalid_input("A single batch had more than i32::MAX values and so a large container type is required")) + }; + self.current_rep_cmp += 1; + if let Some(def_levels) = &mut self.def_levels { + assert!(rep_levels.len() == def_levels.len()); + // It's possible validity is None even if we have def levels. For example, we might have + // empty lists (which require def levels) but no nulls. + let mut push_validity: Box = if let Some(validity) = validity { + Box::new(|is_valid| validity.append(is_valid)) + } else { + Box::new(|_| {}) + }; + // This is a strange access pattern. We are iterating over the rep/def levels and + // at the same time writing the rep/def levels. This means we need both a mutable + // and immutable reference to the rep/def levels. + let mut read_idx = 0; + let mut write_idx = 0; + while read_idx < rep_levels.len() { + // SAFETY: We assert that rep_levels and def_levels have the same + // len and read_idx and write_idx can never go past the end. + unsafe { + let rep_val = *rep_levels.get_unchecked(read_idx); + if rep_val != 0 { + let def_val = *def_levels.get_unchecked(read_idx); + // Copy over + *rep_levels.get_unchecked_mut(write_idx) = rep_val - 1; + *def_levels.get_unchecked_mut(write_idx) = def_val; + write_idx += 1; + + if def_val == 0 { + // This is a valid list + offsets.push(to_offset(curlen)?); + curlen += 1; + push_validity(true); + } else if def_val > max_level { + // This is not visible at this rep level, do not add to offsets, but keep in repdef + } else if def_val == null_level || def_val > upper_null { + // This is a null list (or a list masked by a null struct) + offsets.push(to_offset(curlen)?); + push_validity(false); + } else if def_val == empty_level { + // This is an empty list + offsets.push(to_offset(curlen)?); + push_validity(true); + } else { + // New valid list starting with null item + offsets.push(to_offset(curlen)?); + curlen += 1; + push_validity(true); + } + } else { + curlen += 1; + } + read_idx += 1; + } + } + offsets.push(to_offset(curlen)?); + rep_levels.truncate(write_idx); + def_levels.truncate(write_idx); + Ok(()) + } else { + // SAFETY: See above loop + let mut read_idx = 0; + let mut write_idx = 0; + let old_offsets_len = offsets.len(); + while read_idx < rep_levels.len() { + // SAFETY: read_idx / write_idx cannot go past rep_levels.len() + unsafe { + let rep_val = *rep_levels.get_unchecked(read_idx); + if rep_val != 0 { + // Finish the current list + offsets.push(to_offset(curlen)?); + *rep_levels.get_unchecked_mut(write_idx) = rep_val - 1; + write_idx += 1; + } + curlen += 1; + read_idx += 1; + } + } + let num_new_lists = offsets.len() - old_offsets_len; + offsets.push(to_offset(curlen)?); + // Truncate to the number of lists THIS unraveler produced (write_idx), + // not `offsets.len() - 1` — the latter includes offsets contributed by + // earlier unravelers in a multi-page read, which would leave too many + // rep levels for the next (outer) layer and over-count its lists. + rep_levels.truncate(write_idx); + if let Some(validity) = validity { + // Even though we don't have validity it is possible another unraveler did and so we need + // to push all valids + validity.append_n(num_new_lists, true); + } + Ok(()) + } + } + + pub fn skip_validity(&mut self) -> Result<()> { + if let Some(sparse) = self.sparse.as_mut() { + return sparse.skip_validity(); + } + debug_assert!(self.is_all_valid()); + self.current_layer += 1; + Ok(()) + } + + /// Unravels a layer of validity from the definition levels + pub fn unravel_validity(&mut self, validity: &mut BooleanBufferBuilder) -> Result<()> { + if let Some(sparse) = self.sparse.as_mut() { + return sparse.unravel_validity(validity); + } + let meaning = self.def_meaning[self.current_layer]; + if meaning == DefinitionInterpretation::AllValidItem || self.def_levels.is_none() { + self.current_layer += 1; + validity.append_n(self.num_items as usize, true); + return Ok(()); + } + + self.current_layer += 1; + let def_levels = &self.def_levels.as_ref().unwrap(); + + let current_def_cmp = self.current_def_cmp; + self.current_def_cmp += 1; + + for is_valid in def_levels.iter().filter_map(|&level| { + if self.levels_to_rep[level as usize] <= self.current_rep_cmp { + Some(level <= current_def_cmp) + } else { + None + } + }) { + validity.append(is_valid); + } + Ok(()) + } + + pub fn decimate(&mut self, dimension: usize) -> Result<()> { + if let Some(sparse) = self.sparse.as_mut() { + return sparse.decimate(dimension); + } + if self.rep_levels.is_some() { + // If we need to support this then I think we need to walk through the rep def levels to find + // the spots at which we keep. E.g. if we have: + // rep: 1 0 0 1 0 1 0 0 0 1 0 0 + // def: 1 1 1 0 1 0 1 1 0 1 1 0 + // dimension: 2 + // + // The output should be: + // rep: 1 0 0 1 0 0 0 + // def: 1 1 1 0 1 1 0 + // + // Maybe there's some special logic for empty/null lists? I'll save the headache for future me. + todo!("Not yet supported FSL<...List<...>>"); + } + let Some(def_levels) = self.def_levels.as_mut() else { + return Ok(()); + }; + let mut read_idx = 0; + let mut write_idx = 0; + while read_idx < def_levels.len() { + unsafe { + *def_levels.get_unchecked_mut(write_idx) = *def_levels.get_unchecked(read_idx); + } + write_idx += 1; + read_idx += dimension; + } + def_levels.truncate(write_idx); + Ok(()) + } +} + +/// As we decode we may extract rep/def information from multiple pages (or multiple +/// chunks within a page). +/// +/// For each chunk we create an unraveler. Each unraveler can have a completely different +/// interpretation (e.g. one page might contain null items but no null structs and the next +/// page might have null structs but no null items). +/// +/// Concatenating these unravelers would be tricky and expensive so instead we have a +/// composite unraveler which unravels across multiple unravelers. +/// +/// Note: this class should be used even if there is only one page / unraveler. This is +/// because the `RepDefUnraveler`'s API is more complex (it's meant to be called by this +/// class) +#[derive(Debug)] +pub struct CompositeRepDefUnraveler { + unravelers: Vec, + comparisons: Vec, +} + +impl CompositeRepDefUnraveler { + pub fn new(unravelers: Vec) -> Self { + Self { + unravelers, + comparisons: Vec::new(), + } + } + + pub(crate) fn add_compatibility_check(&mut self, other: Self) { + self.comparisons.push(other); + } + + pub(crate) fn has_sparse(&self) -> bool { + self.unravelers.iter().any(RepDefUnraveler::is_sparse) + || self.comparisons.iter().any(Self::has_sparse) + } + + pub(crate) fn ensure_exhausted(&self) -> Result<()> { + for unraveler in &self.unravelers { + unraveler.ensure_exhausted()?; + } + for comparison in &self.comparisons { + comparison.ensure_exhausted()?; + } + Ok(()) + } + + fn null_buffers_equal( + left: &Option, + right: &Option, + expected_len: usize, + ) -> bool { + match (left, right) { + (None, None) => true, + (Some(left), Some(right)) => { + left.len() == expected_len + && right.len() == expected_len + && left.iter().eq(right.iter()) + } + (None, Some(right)) => right.len() == expected_len && right.null_count() == 0, + (Some(left), None) => left.len() == expected_len && left.null_count() == 0, + } + } + + fn decimate(&mut self, dimension: usize) -> Result<()> { + for unraveler in &mut self.unravelers { + unraveler.decimate(dimension)?; + } + for comparison in &mut self.comparisons { + comparison.decimate(dimension)?; + } + Ok(()) + } + + /// Unravels a layer of validity + /// + /// Returns None if there are no null items in this layer + pub fn unravel_validity(&mut self, num_values: usize) -> Result> { + let is_all_valid = self + .unravelers + .iter() + .all(|unraveler| unraveler.is_all_valid()); + + let validity = if is_all_valid { + for unraveler in self.unravelers.iter_mut() { + unraveler.skip_validity()?; + } + None + } else { + let mut validity = BooleanBufferBuilder::new(num_values); + for unraveler in self.unravelers.iter_mut() { + unraveler.unravel_validity(&mut validity)?; + } + Some(NullBuffer::new(validity.finish())) + }; + for comparison in &mut self.comparisons { + let other = comparison.unravel_validity(num_values)?; + if !Self::null_buffers_equal(&validity, &other, num_values) { + return Err(Error::invalid_input_source( + format!( + "Structural sibling fields have incompatible validity metadata for {num_values} values" + ) + .into(), + )); + } + } + Ok(validity) + } + + pub fn unravel_fsl_validity( + &mut self, + num_values: usize, + dimension: usize, + ) -> Result> { + self.decimate(dimension)?; + self.unravel_validity(num_values) + } + + /// Unravels a layer of offsets (and the validity for that layer) + pub fn unravel_offsets( + &mut self, + ) -> Result<(OffsetBuffer, Option)> { + let mut is_all_valid = true; + let mut max_num_lists: usize = 0; + for unraveler in self.unravelers.iter() { + is_all_valid &= unraveler.is_all_valid(); + max_num_lists = max_num_lists + .checked_add(unraveler.max_lists()?) + .ok_or_else(|| { + Error::invalid_input_source( + "Combined repetition/definition list count exceeds usize::MAX".into(), + ) + })?; + } + + let mut validity = if is_all_valid { + None + } else { + // Note: This is probably an over-estimate and potentially even an under-estimate. We only know + // right now how many items we have and not how many rows. (TODO: Shouldn't we know the # of rows?) + Some(BooleanBufferBuilder::new(max_num_lists)) + }; + + let mut offsets = Vec::with_capacity(max_num_lists + 1); + + for unraveler in self.unravelers.iter_mut() { + unraveler.unravel_offsets(&mut offsets, validity.as_mut())?; + } + + let offsets = OffsetBuffer::new(ScalarBuffer::from(offsets)); + let validity = validity.map(|mut v| NullBuffer::new(v.finish())); + for comparison in &mut self.comparisons { + let (other_offsets, other_validity) = comparison.unravel_offsets::()?; + if offsets.as_ref() != other_offsets.as_ref() + || !Self::null_buffers_equal( + &validity, + &other_validity, + offsets.len().saturating_sub(1), + ) + { + return Err(Error::invalid_input_source( + format!( + "Structural sibling fields have incompatible list metadata for {} slots", + offsets.len().saturating_sub(1) + ) + .into(), + )); + } + } + + Ok((offsets, validity)) + } +} + +/// A [`ControlWordIterator`] when there are both repetition and definition levels +/// +/// The iterator will put the repetition level in the upper bits and the definition +/// level in the lower bits. The number of bits used for each level is determined +/// by the width of the repetition and definition levels. +#[derive(Debug)] +pub struct BinaryControlWordIterator, W> { + repdef: I, + def_width: usize, + max_rep: u16, + max_visible_def: u16, + rep_mask: u16, + def_mask: u16, + bits_rep: u8, + bits_def: u8, + phantom: std::marker::PhantomData, +} + +impl> BinaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next()?; + let control_word: u8 = + (((next.0 & self.rep_mask) as u8) << self.def_width) + ((next.1 & self.def_mask) as u8); + buf.push(control_word); + let is_new_row = next.0 == self.max_rep; + let is_visible = next.1 <= self.max_visible_def; + let is_valid_item = next.1 == 0; + Some(ControlWordDesc { + is_new_row, + is_visible, + is_valid_item, + }) + } +} + +impl> BinaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next()?; + let control_word: u16 = + ((next.0 & self.rep_mask) << self.def_width) + (next.1 & self.def_mask); + let control_word = control_word.to_le_bytes(); + buf.push(control_word[0]); + buf.push(control_word[1]); + let is_new_row = next.0 == self.max_rep; + let is_visible = next.1 <= self.max_visible_def; + let is_valid_item = next.1 == 0; + Some(ControlWordDesc { + is_new_row, + is_visible, + is_valid_item, + }) + } +} + +impl> BinaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next()?; + let control_word: u32 = (((next.0 & self.rep_mask) as u32) << self.def_width) + + ((next.1 & self.def_mask) as u32); + let control_word = control_word.to_le_bytes(); + buf.push(control_word[0]); + buf.push(control_word[1]); + buf.push(control_word[2]); + buf.push(control_word[3]); + let is_new_row = next.0 == self.max_rep; + let is_visible = next.1 <= self.max_visible_def; + let is_valid_item = next.1 == 0; + Some(ControlWordDesc { + is_new_row, + is_visible, + is_valid_item, + }) + } +} + +/// A [`ControlWordIterator`] when there are only definition levels or only repetition levels +#[derive(Debug)] +pub struct UnaryControlWordIterator, W> { + repdef: I, + level_mask: u16, + bits_rep: u8, + bits_def: u8, + max_rep: u16, + phantom: std::marker::PhantomData, +} + +impl> UnaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next()?; + buf.push((next & self.level_mask) as u8); + let is_new_row = self.max_rep == 0 || next == self.max_rep; + let is_valid_item = next == 0 || self.bits_def == 0; + Some(ControlWordDesc { + is_new_row, + // Either there is no rep, in which case there are no invisible items + // or there is no def, in which case there are no invisible items + is_visible: true, + is_valid_item, + }) + } +} + +impl> UnaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next().unwrap() & self.level_mask; + let control_word = next.to_le_bytes(); + buf.push(control_word[0]); + buf.push(control_word[1]); + let is_new_row = self.max_rep == 0 || next == self.max_rep; + let is_valid_item = next == 0 || self.bits_def == 0; + Some(ControlWordDesc { + is_new_row, + is_visible: true, + is_valid_item, + }) + } +} + +impl> UnaryControlWordIterator { + fn append_next(&mut self, buf: &mut Vec) -> Option { + let next = self.repdef.next()?; + let next = (next & self.level_mask) as u32; + let control_word = next.to_le_bytes(); + buf.push(control_word[0]); + buf.push(control_word[1]); + buf.push(control_word[2]); + buf.push(control_word[3]); + let is_new_row = self.max_rep == 0 || next as u16 == self.max_rep; + let is_valid_item = next == 0 || self.bits_def == 0; + Some(ControlWordDesc { + is_new_row, + is_visible: true, + is_valid_item, + }) + } +} + +/// A [`ControlWordIterator`] when there are no repetition or definition levels +#[derive(Debug)] +pub struct NilaryControlWordIterator { + len: usize, + idx: usize, +} + +impl NilaryControlWordIterator { + fn append_next(&mut self) -> Option { + if self.idx == self.len { + None + } else { + self.idx += 1; + Some(ControlWordDesc { + is_new_row: true, + is_visible: true, + is_valid_item: true, + }) + } + } +} + +/// Helper function to get a bit mask of the given width +fn get_mask(width: u16) -> u16 { + (1 << width) - 1 +} + +// We're really going out of our way to avoid boxing here but this will be called on a per-value basis +// so it is in the critical path. +type SpecificBinaryControlWordIterator<'a, T> = BinaryControlWordIterator< + Zip>, Copied>>, + T, +>; + +/// An iterator that generates control words from repetition and definition levels +/// +/// "Control word" is just a fancy term for a single u8/u16/u32 that contains both +/// the repetition and definition in it. +/// +/// In the large majority of case we only need a single byte to represent both the +/// repetition and definition levels. However, if there is deep nesting then we may +/// need two bytes. In the worst case we need 4 bytes though this suggests hundreds of +/// levels of nesting which seems unlikely to encounter in practice. +#[derive(Debug)] +pub enum ControlWordIterator<'a> { + Binary8(SpecificBinaryControlWordIterator<'a, u8>), + Binary16(SpecificBinaryControlWordIterator<'a, u16>), + Binary32(SpecificBinaryControlWordIterator<'a, u32>), + Unary8(UnaryControlWordIterator>, u8>), + Unary16(UnaryControlWordIterator>, u16>), + Unary32(UnaryControlWordIterator>, u32>), + Nilary(NilaryControlWordIterator), +} + +/// Describes the properties of a control word +#[derive(Debug)] +pub struct ControlWordDesc { + pub is_new_row: bool, + pub is_visible: bool, + pub is_valid_item: bool, +} + +impl ControlWordIterator<'_> { + /// Appends the next control word to the buffer + /// + /// Returns true if this is the start of a new item (i.e. the repetition level is maxed out) + pub fn append_next(&mut self, buf: &mut Vec) -> Option { + match self { + Self::Binary8(iter) => iter.append_next(buf), + Self::Binary16(iter) => iter.append_next(buf), + Self::Binary32(iter) => iter.append_next(buf), + Self::Unary8(iter) => iter.append_next(buf), + Self::Unary16(iter) => iter.append_next(buf), + Self::Unary32(iter) => iter.append_next(buf), + Self::Nilary(iter) => iter.append_next(), + } + } + + /// Return true if the control word iterator has repetition levels + pub fn has_repetition(&self) -> bool { + match self { + Self::Binary8(_) | Self::Binary16(_) | Self::Binary32(_) => true, + Self::Unary8(iter) => iter.bits_rep > 0, + Self::Unary16(iter) => iter.bits_rep > 0, + Self::Unary32(iter) => iter.bits_rep > 0, + Self::Nilary(_) => false, + } + } + + /// Returns the number of bytes per control word + pub fn bytes_per_word(&self) -> usize { + match self { + Self::Binary8(_) => 1, + Self::Binary16(_) => 2, + Self::Binary32(_) => 4, + Self::Unary8(_) => 1, + Self::Unary16(_) => 2, + Self::Unary32(_) => 4, + Self::Nilary(_) => 0, + } + } + + /// Returns the number of bits used for the repetition level + pub fn bits_rep(&self) -> u8 { + match self { + Self::Binary8(iter) => iter.bits_rep, + Self::Binary16(iter) => iter.bits_rep, + Self::Binary32(iter) => iter.bits_rep, + Self::Unary8(iter) => iter.bits_rep, + Self::Unary16(iter) => iter.bits_rep, + Self::Unary32(iter) => iter.bits_rep, + Self::Nilary(_) => 0, + } + } + + /// Returns the number of bits used for the definition level + pub fn bits_def(&self) -> u8 { + match self { + Self::Binary8(iter) => iter.bits_def, + Self::Binary16(iter) => iter.bits_def, + Self::Binary32(iter) => iter.bits_def, + Self::Unary8(iter) => iter.bits_def, + Self::Unary16(iter) => iter.bits_def, + Self::Unary32(iter) => iter.bits_def, + Self::Nilary(_) => 0, + } + } +} + +/// Builds a [`ControlWordIterator`] from repetition and definition levels +/// by first calculating the width needed and then creating the iterator +/// with the appropriate width +pub fn build_control_word_iterator<'a>( + rep: Option<&'a [u16]>, + max_rep: u16, + def: Option<&'a [u16]>, + max_def: u16, + max_visible_def: u16, + len: usize, +) -> ControlWordIterator<'a> { + let rep_width = if max_rep == 0 { + 0 + } else { + log_2_ceil(max_rep as u32) as u16 + }; + let rep_mask = if max_rep == 0 { 0 } else { get_mask(rep_width) }; + let def_width = if max_def == 0 { + 0 + } else { + log_2_ceil(max_def as u32) as u16 + }; + let def_mask = if max_def == 0 { 0 } else { get_mask(def_width) }; + let total_width = rep_width + def_width; + match (rep, def) { + (Some(rep), Some(def)) => { + let iter = rep.iter().copied().zip(def.iter().copied()); + let def_width = def_width as usize; + if total_width <= 8 { + ControlWordIterator::Binary8(BinaryControlWordIterator { + repdef: iter, + rep_mask, + def_mask, + def_width, + max_rep, + max_visible_def, + bits_rep: rep_width as u8, + bits_def: def_width as u8, + phantom: std::marker::PhantomData, + }) + } else if total_width <= 16 { + ControlWordIterator::Binary16(BinaryControlWordIterator { + repdef: iter, + rep_mask, + def_mask, + def_width, + max_rep, + max_visible_def, + bits_rep: rep_width as u8, + bits_def: def_width as u8, + phantom: std::marker::PhantomData, + }) + } else { + ControlWordIterator::Binary32(BinaryControlWordIterator { + repdef: iter, + rep_mask, + def_mask, + def_width, + max_rep, + max_visible_def, + bits_rep: rep_width as u8, + bits_def: def_width as u8, + phantom: std::marker::PhantomData, + }) + } + } + (Some(lev), None) => { + let iter = lev.iter().copied(); + if total_width <= 8 { + ControlWordIterator::Unary8(UnaryControlWordIterator { + repdef: iter, + level_mask: rep_mask, + bits_rep: total_width as u8, + bits_def: 0, + max_rep, + phantom: std::marker::PhantomData, + }) + } else if total_width <= 16 { + ControlWordIterator::Unary16(UnaryControlWordIterator { + repdef: iter, + level_mask: rep_mask, + bits_rep: total_width as u8, + bits_def: 0, + max_rep, + phantom: std::marker::PhantomData, + }) + } else { + ControlWordIterator::Unary32(UnaryControlWordIterator { + repdef: iter, + level_mask: rep_mask, + bits_rep: total_width as u8, + bits_def: 0, + max_rep, + phantom: std::marker::PhantomData, + }) + } + } + (None, Some(lev)) => { + let iter = lev.iter().copied(); + if total_width <= 8 { + ControlWordIterator::Unary8(UnaryControlWordIterator { + repdef: iter, + level_mask: def_mask, + bits_rep: 0, + bits_def: total_width as u8, + max_rep: 0, + phantom: std::marker::PhantomData, + }) + } else if total_width <= 16 { + ControlWordIterator::Unary16(UnaryControlWordIterator { + repdef: iter, + level_mask: def_mask, + bits_rep: 0, + bits_def: total_width as u8, + max_rep: 0, + phantom: std::marker::PhantomData, + }) + } else { + ControlWordIterator::Unary32(UnaryControlWordIterator { + repdef: iter, + level_mask: def_mask, + bits_rep: 0, + bits_def: total_width as u8, + max_rep: 0, + phantom: std::marker::PhantomData, + }) + } + } + (None, None) => ControlWordIterator::Nilary(NilaryControlWordIterator { len, idx: 0 }), + } +} + +/// A parser to unwrap control words into repetition and definition levels +/// +/// This is the inverse of the [`ControlWordIterator`]. +#[derive(Copy, Clone, Debug)] +pub enum ControlWordParser { + // First item is the bits to shift, second is the mask to apply (the mask can be + // calculated from the bits to shift but we don't want to calculate it each time) + BOTH8(u8, u32), + BOTH16(u8, u32), + BOTH32(u8, u32), + REP8, + REP16, + REP32, + DEF8, + DEF16, + DEF32, + NIL, +} + +impl ControlWordParser { + fn parse_both( + src: &[u8], + dst_rep: &mut Vec, + dst_def: &mut Vec, + bits_to_shift: u8, + mask_to_apply: u32, + ) { + match WORD_SIZE { + 1 => { + let word = src[0]; + let rep = word >> bits_to_shift; + let def = word & (mask_to_apply as u8); + dst_rep.push(rep as u16); + dst_def.push(def as u16); + } + 2 => { + let word = u16::from_le_bytes([src[0], src[1]]); + let rep = word >> bits_to_shift; + let def = word & mask_to_apply as u16; + dst_rep.push(rep); + dst_def.push(def); + } + 4 => { + let word = u32::from_le_bytes([src[0], src[1], src[2], src[3]]); + let rep = word >> bits_to_shift; + let def = word & mask_to_apply; + dst_rep.push(rep as u16); + dst_def.push(def as u16); + } + _ => unreachable!(), + } + } + + fn parse_desc_both( + src: &[u8], + bits_to_shift: u8, + mask_to_apply: u32, + max_rep: u16, + max_visible_def: u16, + ) -> ControlWordDesc { + match WORD_SIZE { + 1 => { + let word = src[0]; + let rep = word >> bits_to_shift; + let def = word & (mask_to_apply as u8); + let is_visible = def as u16 <= max_visible_def; + let is_new_row = rep as u16 == max_rep; + let is_valid_item = def == 0; + ControlWordDesc { + is_visible, + is_new_row, + is_valid_item, + } + } + 2 => { + let word = u16::from_le_bytes([src[0], src[1]]); + let rep = word >> bits_to_shift; + let def = word & mask_to_apply as u16; + let is_visible = def <= max_visible_def; + let is_new_row = rep == max_rep; + let is_valid_item = def == 0; + ControlWordDesc { + is_visible, + is_new_row, + is_valid_item, + } + } + 4 => { + let word = u32::from_le_bytes([src[0], src[1], src[2], src[3]]); + let rep = word >> bits_to_shift; + let def = word & mask_to_apply; + let is_visible = def as u16 <= max_visible_def; + let is_new_row = rep as u16 == max_rep; + let is_valid_item = def == 0; + ControlWordDesc { + is_visible, + is_new_row, + is_valid_item, + } + } + _ => unreachable!(), + } + } + + fn parse_one(src: &[u8], dst: &mut Vec) { + match WORD_SIZE { + 1 => { + let word = src[0]; + dst.push(word as u16); + } + 2 => { + let word = u16::from_le_bytes([src[0], src[1]]); + dst.push(word); + } + 4 => { + let word = u32::from_le_bytes([src[0], src[1], src[2], src[3]]); + dst.push(word as u16); + } + _ => unreachable!(), + } + } + + fn parse_rep_desc_one(src: &[u8], max_rep: u16) -> ControlWordDesc { + match WORD_SIZE { + 1 => ControlWordDesc { + is_new_row: src[0] as u16 == max_rep, + is_visible: true, + is_valid_item: true, + }, + 2 => ControlWordDesc { + is_new_row: u16::from_le_bytes([src[0], src[1]]) == max_rep, + is_visible: true, + is_valid_item: true, + }, + 4 => ControlWordDesc { + is_new_row: u32::from_le_bytes([src[0], src[1], src[2], src[3]]) as u16 == max_rep, + is_visible: true, + is_valid_item: true, + }, + _ => unreachable!(), + } + } + + fn parse_def_desc_one(src: &[u8]) -> ControlWordDesc { + match WORD_SIZE { + 1 => ControlWordDesc { + is_new_row: true, + is_visible: true, + is_valid_item: src[0] == 0, + }, + 2 => ControlWordDesc { + is_new_row: true, + is_visible: true, + is_valid_item: u16::from_le_bytes([src[0], src[1]]) == 0, + }, + 4 => ControlWordDesc { + is_new_row: true, + is_visible: true, + is_valid_item: u32::from_le_bytes([src[0], src[1], src[2], src[3]]) as u16 == 0, + }, + _ => unreachable!(), + } + } + + /// Returns the number of bytes per control word + pub fn bytes_per_word(&self) -> usize { + match self { + Self::BOTH8(..) => 1, + Self::BOTH16(..) => 2, + Self::BOTH32(..) => 4, + Self::REP8 => 1, + Self::REP16 => 2, + Self::REP32 => 4, + Self::DEF8 => 1, + Self::DEF16 => 2, + Self::DEF32 => 4, + Self::NIL => 0, + } + } + + /// Appends the next control word to the rep & def buffers + /// + /// `src` should be pointing at the first byte (little endian) of the control word + /// + /// `dst_rep` and `dst_def` are the buffers to append the rep and def levels to. + /// They will not be appended to if not needed. + pub fn parse(&self, src: &[u8], dst_rep: &mut Vec, dst_def: &mut Vec) { + match self { + Self::BOTH8(bits_to_shift, mask_to_apply) => { + Self::parse_both::<1>(src, dst_rep, dst_def, *bits_to_shift, *mask_to_apply) + } + Self::BOTH16(bits_to_shift, mask_to_apply) => { + Self::parse_both::<2>(src, dst_rep, dst_def, *bits_to_shift, *mask_to_apply) + } + Self::BOTH32(bits_to_shift, mask_to_apply) => { + Self::parse_both::<4>(src, dst_rep, dst_def, *bits_to_shift, *mask_to_apply) + } + Self::REP8 => Self::parse_one::<1>(src, dst_rep), + Self::REP16 => Self::parse_one::<2>(src, dst_rep), + Self::REP32 => Self::parse_one::<4>(src, dst_rep), + Self::DEF8 => Self::parse_one::<1>(src, dst_def), + Self::DEF16 => Self::parse_one::<2>(src, dst_def), + Self::DEF32 => Self::parse_one::<4>(src, dst_def), + Self::NIL => {} + } + } + + /// Return true if the control words contain repetition information + pub fn has_rep(&self) -> bool { + match self { + Self::BOTH8(..) + | Self::BOTH16(..) + | Self::BOTH32(..) + | Self::REP8 + | Self::REP16 + | Self::REP32 => true, + Self::DEF8 | Self::DEF16 | Self::DEF32 | Self::NIL => false, + } + } + + /// Temporarily parses the control word to inspect its properties but does not append to any buffers + pub fn parse_desc(&self, src: &[u8], max_rep: u16, max_visible_def: u16) -> ControlWordDesc { + match self { + Self::BOTH8(bits_to_shift, mask_to_apply) => Self::parse_desc_both::<1>( + src, + *bits_to_shift, + *mask_to_apply, + max_rep, + max_visible_def, + ), + Self::BOTH16(bits_to_shift, mask_to_apply) => Self::parse_desc_both::<2>( + src, + *bits_to_shift, + *mask_to_apply, + max_rep, + max_visible_def, + ), + Self::BOTH32(bits_to_shift, mask_to_apply) => Self::parse_desc_both::<4>( + src, + *bits_to_shift, + *mask_to_apply, + max_rep, + max_visible_def, + ), + Self::REP8 => Self::parse_rep_desc_one::<1>(src, max_rep), + Self::REP16 => Self::parse_rep_desc_one::<2>(src, max_rep), + Self::REP32 => Self::parse_rep_desc_one::<4>(src, max_rep), + Self::DEF8 => Self::parse_def_desc_one::<1>(src), + Self::DEF16 => Self::parse_def_desc_one::<2>(src), + Self::DEF32 => Self::parse_def_desc_one::<4>(src), + Self::NIL => ControlWordDesc { + is_new_row: true, + is_valid_item: true, + is_visible: true, + }, + } + } + + /// Creates a new parser from the number of bits used for the repetition and definition levels + pub fn new(bits_rep: u8, bits_def: u8) -> Self { + let total_bits = bits_rep + bits_def; + + enum WordSize { + One, + Two, + Four, + } + + let word_size = if total_bits <= 8 { + WordSize::One + } else if total_bits <= 16 { + WordSize::Two + } else { + WordSize::Four + }; + + match (bits_rep > 0, bits_def > 0, word_size) { + (false, false, _) => Self::NIL, + (false, true, WordSize::One) => Self::DEF8, + (false, true, WordSize::Two) => Self::DEF16, + (false, true, WordSize::Four) => Self::DEF32, + (true, false, WordSize::One) => Self::REP8, + (true, false, WordSize::Two) => Self::REP16, + (true, false, WordSize::Four) => Self::REP32, + (true, true, WordSize::One) => Self::BOTH8(bits_def, get_mask(bits_def as u16) as u32), + (true, true, WordSize::Two) => Self::BOTH16(bits_def, get_mask(bits_def as u16) as u32), + (true, true, WordSize::Four) => { + Self::BOTH32(bits_def, get_mask(bits_def as u16) as u32) + } + } + } +} + +#[cfg(test)] +mod tests { + use arrow_buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; + + use crate::encodings::logical::primitive::sparse::{ + SparsePositionSet, SparseStructuralLayerPlan, SparseStructuralPlan, SparseValidityMeaning, + SparseValiditySet, + }; + use crate::repdef::{ + CompositeRepDefUnraveler, DefinitionInterpretation, RepDefUnraveler, SerializedRepDefs, + }; + + use super::RepDefBuilder; + + fn validity(values: &[bool]) -> NullBuffer { + NullBuffer::from_iter(values.iter().copied()) + } + + fn offsets_32(values: &[i32]) -> OffsetBuffer { + OffsetBuffer::::new(ScalarBuffer::from_iter(values.iter().copied())) + } + + fn offsets_64(values: &[i64]) -> OffsetBuffer { + OffsetBuffer::::new(ScalarBuffer::from_iter(values.iter().copied())) + } + + #[test] + fn sparse_sibling_validity_mismatch_is_invalid_input() { + let sparse = |positions| { + RepDefUnraveler::new_sparse(SparseStructuralPlan { + layers: vec![SparseStructuralLayerPlan::Validity { + num_slots: 2, + validity: SparseValiditySet { + meaning: SparseValidityMeaning::NullPositions, + positions, + }, + }], + num_items: 2, + num_visible_items: 2, + }) + }; + let mut repdef = CompositeRepDefUnraveler::new(vec![sparse(SparsePositionSet::Empty)]); + repdef.add_compatibility_check(CompositeRepDefUnraveler::new(vec![sparse( + SparsePositionSet::Explicit(vec![0]), + )])); + + let err = repdef.unravel_validity(2).unwrap_err(); + assert!(matches!(err, lance_core::Error::InvalidInput { .. })); + assert!(err.to_string().contains("incompatible validity metadata")); + } + + #[test] + fn test_repdef_empty_offsets() { + // Empty offsets should serialize without panicking. + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_32(&[0]), None); + let repdefs = RepDefBuilder::serialize(vec![builder]); + assert!(repdefs.repetition_levels.is_none()); + assert!(repdefs.definition_levels.is_none()); + } + + #[test] + fn test_repdef_basic() { + // Basic case, rep & def + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_64(&[0, 2, 2, 5]), + Some(validity(&[true, false, true])), + ); + builder.add_offsets( + offsets_64(&[0, 1, 3, 5, 5, 9]), + Some(validity(&[true, true, true, false, true])), + ); + builder.add_validity_bitmap(validity(&[ + true, true, true, false, false, false, true, true, false, + ])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!(vec![0, 0, 0, 3, 1, 1, 2, 1, 0, 0, 1], *def); + assert_eq!(vec![2, 1, 0, 2, 2, 0, 1, 1, 0, 0, 0], *rep); + + // [[I], [I, I]], NULL, [[NULL, NULL], NULL, [NULL, I, I, NULL]] + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 9, + )]); + + // Note: validity doesn't exactly round-trip because repdef normalizes some of the + // redundant validity values + assert_eq!( + unraveler.unravel_validity(9).unwrap(), + Some(validity(&[ + true, true, true, false, false, false, true, true, false + ])) + ); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 1, 3, 5, 5, 9]).inner()); + assert_eq!(val, Some(validity(&[true, true, true, false, true]))); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 2, 2, 5]).inner()); + assert_eq!(val, Some(validity(&[true, false, true]))); + } + + #[test] + fn test_repdef_simple_null_empty_list() { + let check = |repdefs: SerializedRepDefs, last_def: DefinitionInterpretation| { + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 1, 1, 0, 0], *rep); + assert_eq!([0, 0, 2, 0, 1, 0], *def); + assert_eq!( + vec![DefinitionInterpretation::NullableItem, last_def,], + repdefs.def_meaning + ); + }; + + // Null list and empty list should be serialized mostly the same + + // Null case + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[0, 2, 2, 5]), + Some(validity(&[true, false, true])), + ); + builder.add_validity_bitmap(validity(&[true, true, true, false, true])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + check(repdefs, DefinitionInterpretation::NullableList); + + // Empty case + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_32(&[0, 2, 2, 5]), None); + builder.add_validity_bitmap(validity(&[true, true, true, false, true])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + check(repdefs, DefinitionInterpretation::EmptyableList); + } + + #[test] + fn test_repdef_empty_list_at_end() { + // Regresses a failure we encountered when the last item was an empty list + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_32(&[0, 2, 5, 5]), None); + builder.add_validity_bitmap(validity(&[true, true, true, false, true])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 1, 0, 0, 1], *rep); + assert_eq!([0, 0, 0, 1, 0, 2], *def); + assert_eq!( + vec![ + DefinitionInterpretation::NullableItem, + DefinitionInterpretation::EmptyableList, + ], + repdefs.def_meaning + ); + } + + #[test] + fn test_repdef_abnormal_nulls() { + // List nulls are allowed to have non-empty offsets and garbage values + // and the add_offsets call should normalize this + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[0, 2, 5, 8]), + Some(validity(&[true, false, true])), + ); + // Note: we pass 5 here and not 8. If add_offsets tells us there is garbage nulls they + // should be removed before continuing + builder.add_no_null(5); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 1, 1, 0, 0], *rep); + assert_eq!([0, 0, 1, 0, 0, 0], *def); + + assert_eq!( + vec![ + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::NullableList, + ], + repdefs.def_meaning + ); + } + + #[test] + fn test_repdef_fsl() { + let mut builder = RepDefBuilder::default(); + builder.add_fsl(Some(validity(&[true, false])), 2, 2); + builder.add_fsl(None, 2, 4); + builder.add_validity_bitmap(validity(&[ + true, false, true, false, true, false, true, false, + ])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + assert_eq!( + vec![ + DefinitionInterpretation::NullableItem, + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::NullableItem + ], + repdefs.def_meaning + ); + + assert!(repdefs.repetition_levels.is_none()); + + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([0, 1, 0, 1, 2, 2, 2, 2], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + None, + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!( + unraveler.unravel_validity(8).unwrap(), + Some(validity(&[ + true, false, true, false, false, false, false, false + ])) + ); + assert_eq!(unraveler.unravel_fsl_validity(4, 2).unwrap(), None); + assert_eq!( + unraveler.unravel_fsl_validity(2, 2).unwrap(), + Some(validity(&[true, false])) + ); + } + + #[test] + fn test_repdef_fsl_allvalid_item() { + let mut builder = RepDefBuilder::default(); + builder.add_fsl(Some(validity(&[true, false])), 2, 2); + builder.add_fsl(None, 2, 4); + builder.add_no_null(8); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + assert_eq!( + vec![ + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::NullableItem + ], + repdefs.def_meaning + ); + + assert!(repdefs.repetition_levels.is_none()); + + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([0, 0, 0, 0, 1, 1, 1, 1], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + None, + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!(unraveler.unravel_validity(8).unwrap(), None); + assert_eq!(unraveler.unravel_fsl_validity(4, 2).unwrap(), None); + assert_eq!( + unraveler.unravel_fsl_validity(2, 2).unwrap(), + Some(validity(&[true, false])) + ); + } + + #[test] + fn test_repdef_sliced_offsets() { + // Sliced lists may have offsets that don't start with zero. The + // add_offsets call needs to normalize these to operate correctly. + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[5, 7, 7, 10]), + Some(validity(&[true, false, true])), + ); + builder.add_no_null(5); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 1, 1, 0, 0], *rep); + assert_eq!([0, 0, 1, 0, 0, 0], *def); + + assert_eq!( + vec![ + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::NullableList, + ], + repdefs.def_meaning + ); + } + + #[test] + fn test_repdef_complex_null_empty() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[0, 4, 4, 4, 6]), + Some(validity(&[true, false, true, true])), + ); + builder.add_offsets( + offsets_32(&[0, 1, 1, 2, 2, 2, 3]), + Some(validity(&[true, false, true, false, true, true])), + ); + builder.add_no_null(3); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([2, 1, 1, 1, 2, 2, 2, 1], *rep); + assert_eq!([0, 1, 0, 1, 3, 4, 2, 0], *def); + } + + #[test] + fn test_repdef_empty_list_no_null() { + // Tests when we have some empty lists but no null lists. This case + // caused some bugs because we have definition but no nulls + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_32(&[0, 4, 4, 4, 6]), None); + builder.add_no_null(6); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 0, 0, 1, 1, 1, 0], *rep); + assert_eq!([0, 0, 0, 0, 1, 1, 0, 0], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!(unraveler.unravel_validity(6).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 4, 4, 4, 6]).inner()); + assert_eq!(val, None); + } + + #[test] + fn test_repdef_all_valid() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_64(&[0, 2, 3, 5]), None); + builder.add_offsets(offsets_64(&[0, 1, 3, 5, 7, 9]), None); + builder.add_no_null(9); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + let rep = repdefs.repetition_levels.unwrap(); + assert!(repdefs.definition_levels.is_none()); + + assert_eq!([2, 1, 0, 2, 0, 2, 0, 1, 0], *rep); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + None, + repdefs.def_meaning.into(), + 9, + )]); + + assert_eq!(unraveler.unravel_validity(9).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 1, 3, 5, 7, 9]).inner()); + assert_eq!(val, None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 2, 3, 5]).inner()); + assert_eq!(val, None); + } + + #[test] + fn test_repdef_nested_list_multibatch_matches_single() { + // Single builder: List>, 3 rows. + // outer [0,2,3,5] -> rows have 2,1,2 inner lists + // inner [0,1,3,5,7,9] -> 5 inner lists, lengths 1,2,2,2,2 (9 leaf) + let mut single = RepDefBuilder::default(); + single.add_offsets(offsets_64(&[0, 2, 3, 5]), None); + single.add_offsets(offsets_64(&[0, 1, 3, 5, 7, 9]), None); + single.add_no_null(9); + let single_rep = RepDefBuilder::serialize(vec![single]) + .repetition_levels + .unwrap(); + + // Same logical data split into two batches: + // batch0 = rows 0,1 : outer [0,2,3], inner [0,1,3,5] (3 inner, 5 leaf) + // batch1 = row 2 : outer [0,2], inner [0,2,4] (2 inner, 4 leaf) + let mut b0 = RepDefBuilder::default(); + b0.add_offsets(offsets_64(&[0, 2, 3]), None); + b0.add_offsets(offsets_64(&[0, 1, 3, 5]), None); + b0.add_no_null(5); + let mut b1 = RepDefBuilder::default(); + b1.add_offsets(offsets_64(&[0, 2]), None); + b1.add_offsets(offsets_64(&[0, 2, 4]), None); + b1.add_no_null(4); + let multi_rep = RepDefBuilder::serialize(vec![b0, b1]) + .repetition_levels + .unwrap(); + + assert_eq!( + *single_rep, *multi_rep, + "multi-batch nested-list rep levels must equal single-batch" + ); + } + + #[test] + fn test_only_empty_lists() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_32(&[0, 4, 4, 4, 6]), None); + builder.add_no_null(6); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 0, 0, 1, 1, 1, 0], *rep); + assert_eq!([0, 0, 0, 0, 1, 1, 0, 0], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!(unraveler.unravel_validity(6).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 4, 4, 4, 6]).inner()); + assert_eq!(val, None); + } + + #[test] + fn test_only_null_lists() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[0, 4, 4, 4, 6]), + Some(validity(&[true, false, false, true])), + ); + builder.add_no_null(6); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 0, 0, 1, 1, 1, 0], *rep); + assert_eq!([0, 0, 0, 0, 1, 1, 0, 0], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!(unraveler.unravel_validity(6).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 4, 4, 4, 6]).inner()); + assert_eq!(val, Some(validity(&[true, false, false, true]))); + } + + #[test] + fn test_null_and_empty_lists() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_32(&[0, 4, 4, 4, 6]), + Some(validity(&[true, false, true, true])), + ); + builder.add_no_null(6); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 0, 0, 0, 1, 1, 1, 0], *rep); + assert_eq!([0, 0, 0, 0, 1, 2, 0, 0], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 8, + )]); + + assert_eq!(unraveler.unravel_validity(6).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 4, 4, 4, 6]).inner()); + assert_eq!(val, Some(validity(&[true, false, true, true]))); + } + + #[test] + fn test_repdef_null_struct_valid_list() { + // This regresses a bug + + let rep = vec![1, 0, 0, 0]; + let def = vec![2, 0, 2, 2]; + // AllValidList> + let def_meaning = vec![ + DefinitionInterpretation::NullableItem, + DefinitionInterpretation::NullableItem, + DefinitionInterpretation::AllValidList, + ]; + let num_items = 4; + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep), + Some(def), + def_meaning.into(), + num_items, + )]); + + assert_eq!( + unraveler.unravel_validity(4).unwrap(), + Some(validity(&[false, true, false, false])) + ); + assert_eq!( + unraveler.unravel_validity(4).unwrap(), + Some(validity(&[false, true, false, false])) + ); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 4]).inner()); + assert_eq!(val, None); + } + + #[test] + fn test_repdef_no_rep() { + let mut builder = RepDefBuilder::default(); + builder.add_no_null(5); + builder.add_validity_bitmap(validity(&[false, false, true, true, true])); + builder.add_validity_bitmap(validity(&[false, true, true, true, false])); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + assert!(repdefs.repetition_levels.is_none()); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([2, 2, 0, 0, 1], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + None, + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 5, + )]); + + assert_eq!( + unraveler.unravel_validity(5).unwrap(), + Some(validity(&[false, false, true, true, false])) + ); + assert_eq!( + unraveler.unravel_validity(5).unwrap(), + Some(validity(&[false, false, true, true, true])) + ); + assert_eq!(unraveler.unravel_validity(5).unwrap(), None); + } + + #[test] + fn test_composite_unravel() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_64(&[0, 2, 2, 5]), + Some(validity(&[true, false, true])), + ); + builder.add_no_null(5); + let repdef1 = RepDefBuilder::serialize(vec![builder]); + + let mut builder = RepDefBuilder::default(); + builder.add_offsets(offsets_64(&[0, 1, 3, 5, 7, 9]), None); + builder.add_no_null(9); + let repdef2 = RepDefBuilder::serialize(vec![builder]); + + let rep1 = repdef1.repetition_levels.clone().unwrap(); + let def1 = repdef1.definition_levels.clone().unwrap(); + let rep2 = repdef2.repetition_levels.clone().unwrap(); + assert!(repdef2.definition_levels.is_none()); + + assert_eq!([1, 0, 1, 1, 0, 0], *rep1); + assert_eq!([0, 0, 1, 0, 0, 0], *def1); + assert_eq!([1, 1, 0, 1, 0, 1, 0, 1, 0], *rep2); + + let unravel1 = RepDefUnraveler::new( + repdef1.repetition_levels.map(|l| l.to_vec()), + repdef1.definition_levels.map(|l| l.to_vec()), + repdef1.def_meaning.into(), + 5, + ); + let unravel2 = RepDefUnraveler::new( + repdef2.repetition_levels.map(|l| l.to_vec()), + repdef2.definition_levels.map(|l| l.to_vec()), + repdef2.def_meaning.into(), + 9, + ); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![unravel1, unravel2]); + + assert!(unraveler.unravel_validity(9).unwrap().is_none()); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!( + off.inner(), + offsets_32(&[0, 2, 2, 5, 6, 8, 10, 12, 14]).inner() + ); + assert_eq!( + val, + Some(validity(&[true, false, true, true, true, true, true, true])) + ); + } + + #[test] + fn test_repdef_multiple_builders() { + // Basic case, rep & def + let mut builder1 = RepDefBuilder::default(); + builder1.add_offsets(offsets_64(&[0, 2]), None); + builder1.add_offsets(offsets_64(&[0, 1, 3]), None); + builder1.add_validity_bitmap(validity(&[true, true, true])); + + let mut builder2 = RepDefBuilder::default(); + builder2.add_offsets(offsets_64(&[0, 0, 3]), Some(validity(&[false, true]))); + builder2.add_offsets( + offsets_64(&[0, 2, 2, 6]), + Some(validity(&[true, false, true])), + ); + builder2.add_validity_bitmap(validity(&[false, false, false, true, true, false])); + + let repdefs = RepDefBuilder::serialize(vec![builder1, builder2]); + + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([2, 1, 0, 2, 2, 0, 1, 1, 0, 0, 0], *rep); + assert_eq!([0, 0, 0, 3, 1, 1, 2, 1, 0, 0, 1], *def); + } + + #[test] + fn test_all_valid_validity_bitmap_serializes_as_no_null() { + let mut from_bitmap = RepDefBuilder::default(); + from_bitmap.add_validity_bitmap(validity(&[true, true, true, true])); + + let mut from_no_null = RepDefBuilder::default(); + from_no_null.add_no_null(4); + + let from_bitmap = RepDefBuilder::serialize(vec![from_bitmap]); + let from_no_null = RepDefBuilder::serialize(vec![from_no_null]); + + assert!(from_bitmap.repetition_levels.is_none()); + assert!(from_bitmap.definition_levels.is_none()); + assert_eq!(from_bitmap.def_meaning, from_no_null.def_meaning); + assert_eq!( + from_bitmap.max_visible_level, + from_no_null.max_visible_level + ); + } + + #[test] + fn test_slicer() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_64(&[0, 2, 2, 30, 30]), + Some(validity(&[true, false, true, true])), + ); + builder.add_no_null(30); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + + let mut rep_slicer = repdefs.rep_slicer().unwrap(); + + // First 5 items include a null list so we get 6 levels (12 bytes) + assert_eq!(rep_slicer.slice_next(5).len(), 12); + // Next 20 are all plain + assert_eq!(rep_slicer.slice_next(20).len(), 40); + // Last 5 include an empty list so we get 6 levels (12 bytes) + assert_eq!(rep_slicer.slice_rest().len(), 12); + + let mut def_slicer = repdefs.rep_slicer().unwrap(); + + // First 5 items include a null list so we get 6 levels (12 bytes) + assert_eq!(def_slicer.slice_next(5).len(), 12); + // Next 20 are all plain + assert_eq!(def_slicer.slice_next(20).len(), 40); + // Last 5 include an empty list so we get 6 levels (12 bytes) + assert_eq!(def_slicer.slice_rest().len(), 12); + } + + #[test] + fn test_control_words() { + // Convert to control words, verify expected, convert back, verify same as original + fn check( + rep: &[u16], + def: &[u16], + expected_values: Vec, + expected_bytes_per_word: usize, + expected_bits_rep: u8, + expected_bits_def: u8, + ) { + let num_vals = rep.len().max(def.len()); + let max_rep = rep.iter().max().copied().unwrap_or(0); + let max_def = def.iter().max().copied().unwrap_or(0); + + let in_rep = if rep.is_empty() { None } else { Some(rep) }; + let in_def = if def.is_empty() { None } else { Some(def) }; + + let mut iter = super::build_control_word_iterator( + in_rep, + max_rep, + in_def, + max_def, + max_def + 1, + expected_values.len(), + ); + assert_eq!(iter.bytes_per_word(), expected_bytes_per_word); + assert_eq!(iter.bits_rep(), expected_bits_rep); + assert_eq!(iter.bits_def(), expected_bits_def); + let mut cw_vec = Vec::with_capacity(num_vals * iter.bytes_per_word()); + + for _ in 0..num_vals { + iter.append_next(&mut cw_vec); + } + assert!(iter.append_next(&mut cw_vec).is_none()); + + assert_eq!(expected_values, cw_vec); + + let parser = super::ControlWordParser::new(expected_bits_rep, expected_bits_def); + + let mut rep_out = Vec::with_capacity(num_vals); + let mut def_out = Vec::with_capacity(num_vals); + + if expected_bytes_per_word > 0 { + for slice in cw_vec.chunks_exact(expected_bytes_per_word) { + parser.parse(slice, &mut rep_out, &mut def_out); + } + } + + assert_eq!(rep, rep_out.as_slice()); + assert_eq!(def, def_out.as_slice()); + } + + // Each will need 4 bits and so we should get 1-byte control words + let rep = &[0_u16, 7, 3, 2, 9, 8, 12, 5]; + let def = &[5_u16, 3, 1, 2, 12, 15, 0, 2]; + let expected = vec![ + 0b00000101, // 0, 5 + 0b01110011, // 7, 3 + 0b00110001, // 3, 1 + 0b00100010, // 2, 2 + 0b10011100, // 9, 12 + 0b10001111, // 8, 15 + 0b11000000, // 12, 0 + 0b01010010, // 5, 2 + ]; + check(rep, def, expected, 1, 4, 4); + + // Now we need 5 bits for def so we get 2-byte control words + let rep = &[0_u16, 7, 3, 2, 9, 8, 12, 5]; + let def = &[5_u16, 3, 1, 2, 12, 22, 0, 2]; + let expected = vec![ + 0b00000101, 0b00000000, // 0, 5 + 0b11100011, 0b00000000, // 7, 3 + 0b01100001, 0b00000000, // 3, 1 + 0b01000010, 0b00000000, // 2, 2 + 0b00101100, 0b00000001, // 9, 12 + 0b00010110, 0b00000001, // 8, 22 + 0b10000000, 0b00000001, // 12, 0 + 0b10100010, 0b00000000, // 5, 2 + ]; + check(rep, def, expected, 2, 4, 5); + + // Just rep, 4 bits so 1 byte each + let levels = &[0_u16, 7, 3, 2, 9, 8, 12, 5]; + let expected = vec![ + 0b00000000, // 0 + 0b00000111, // 7 + 0b00000011, // 3 + 0b00000010, // 2 + 0b00001001, // 9 + 0b00001000, // 8 + 0b00001100, // 12 + 0b00000101, // 5 + ]; + check(levels, &[], expected.clone(), 1, 4, 0); + + // Just def + check(&[], levels, expected, 1, 0, 4); + + // No rep, no def, no bytes + check(&[], &[], Vec::default(), 0, 0, 0); + } + + #[test] + fn test_control_words_rep_index() { + fn check( + rep: &[u16], + def: &[u16], + expected_new_rows: Vec, + expected_is_visible: Vec, + ) { + let num_vals = rep.len().max(def.len()); + let max_rep = rep.iter().max().copied().unwrap_or(0); + let max_def = def.iter().max().copied().unwrap_or(0); + + let in_rep = if rep.is_empty() { None } else { Some(rep) }; + let in_def = if def.is_empty() { None } else { Some(def) }; + + let mut iter = super::build_control_word_iterator( + in_rep, + max_rep, + in_def, + max_def, + /*max_visible_def=*/ 2, + expected_new_rows.len(), + ); + + let mut cw_vec = Vec::with_capacity(num_vals * iter.bytes_per_word()); + let mut expected_new_rows = expected_new_rows.iter().copied(); + let mut expected_is_visible = expected_is_visible.iter().copied(); + for _ in 0..expected_new_rows.len() { + let word_desc = iter.append_next(&mut cw_vec).unwrap(); + assert_eq!(word_desc.is_new_row, expected_new_rows.next().unwrap()); + assert_eq!(word_desc.is_visible, expected_is_visible.next().unwrap()); + } + assert!(iter.append_next(&mut cw_vec).is_none()); + } + + // 2 means new list + let rep = &[2_u16, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0]; + // These values don't matter for this test + let def = &[0_u16, 0, 0, 3, 1, 1, 2, 1, 0, 0, 1]; + + // Rep & def + check( + rep, + def, + vec![ + true, false, false, true, true, false, false, false, false, true, false, + ], + vec![ + true, true, true, false, true, true, true, true, true, true, true, + ], + ); + // Rep only + check( + rep, + &[], + vec![ + true, false, false, true, true, false, false, false, false, true, false, + ], + vec![true; 11], + ); + // No repetition + check( + &[], + def, + vec![ + true, true, true, true, true, true, true, true, true, true, true, + ], + vec![true; 11], + ); + // No repetition, no definition + check( + &[], + &[], + vec![ + true, true, true, true, true, true, true, true, true, true, true, + ], + vec![true; 11], + ); + } + + #[test] + fn regress_empty_list_case() { + // This regresses a case where we had 3 null lists inside a struct + let mut builder = RepDefBuilder::default(); + builder.add_validity_bitmap(validity(&[true, false, true])); + builder.add_offsets( + offsets_32(&[0, 0, 0, 0]), + Some(validity(&[false, false, false])), + ); + builder.add_no_null(0); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([1, 1, 1], *rep); + assert_eq!([1, 2, 1], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 0, + )]); + + assert_eq!(unraveler.unravel_validity(0).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 0, 0, 0]).inner()); + assert_eq!(val, Some(validity(&[false, false, false]))); + let val = unraveler.unravel_validity(3).unwrap().unwrap(); + assert_eq!(val.inner(), validity(&[true, false, true]).inner()); + } + + #[test] + fn regress_list_ends_null_case() { + let mut builder = RepDefBuilder::default(); + builder.add_offsets( + offsets_64(&[0, 1, 2, 2]), + Some(validity(&[true, true, false])), + ); + builder.add_offsets(offsets_64(&[0, 1, 1]), Some(validity(&[true, false]))); + builder.add_no_null(1); + + let repdefs = RepDefBuilder::serialize(vec![builder]); + let rep = repdefs.repetition_levels.unwrap(); + let def = repdefs.definition_levels.unwrap(); + + assert_eq!([2, 2, 2], *rep); + assert_eq!([0, 1, 2], *def); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![RepDefUnraveler::new( + Some(rep.as_ref().to_vec()), + Some(def.as_ref().to_vec()), + repdefs.def_meaning.into(), + 1, + )]); + + assert_eq!(unraveler.unravel_validity(1).unwrap(), None); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 1, 1]).inner()); + assert_eq!(val, Some(validity(&[true, false]))); + let (off, val) = unraveler.unravel_offsets::().unwrap(); + assert_eq!(off.inner(), offsets_32(&[0, 1, 2, 2]).inner()); + assert_eq!(val, Some(validity(&[true, true, false]))); + } + + #[test] + fn test_mixed_unraveler() { + // This tests cases where the validity is different between two different pages + // because one page has nulls and the other doesn't. + + // Simple case with one layer of validity and no repetition + let mut unraveler = CompositeRepDefUnraveler::new(vec![ + RepDefUnraveler::new( + None, + Some(vec![0, 1, 0, 1]), + vec![DefinitionInterpretation::NullableItem].into(), + 4, + ), + RepDefUnraveler::new( + None, + None, + vec![DefinitionInterpretation::AllValidItem].into(), + 4, + ), + ]); + + assert_eq!( + unraveler.unravel_validity(8).unwrap(), + Some(validity(&[ + true, false, true, false, true, true, true, true + ])) + ); + + // More complex case with two layers of validity and repetition + let def1 = Some(vec![0, 1, 2]); + let rep1 = Some(vec![1, 0, 1]); + + let def2 = Some(vec![1, 0, 0]); + let rep2 = Some(vec![1, 1, 0]); + + let mut unraveler = CompositeRepDefUnraveler::new(vec![ + RepDefUnraveler::new( + rep1, + def1, + vec![ + DefinitionInterpretation::NullableItem, + DefinitionInterpretation::EmptyableList, + ] + .into(), + 2, + ), + RepDefUnraveler::new( + rep2, + def2, + vec![ + DefinitionInterpretation::AllValidItem, + DefinitionInterpretation::NullableList, + ] + .into(), + 2, + ), + ]); + + assert_eq!( + unraveler.unravel_validity(4).unwrap(), + Some(validity(&[true, false, true, true])) + ); + assert_eq!( + unraveler.unravel_offsets::().unwrap(), + ( + offsets_32(&[0, 2, 2, 2, 4]), + Some(validity(&[true, true, false, true])) + ) + ); + } + + #[test] + fn test_mixed_unraveler_nullable_without_def_levels() { + // A page can keep nullable layer metadata even when all definition levels are 0 + // and no definition buffer needs to be materialized. This should decode as all-valid. + let mut unraveler = CompositeRepDefUnraveler::new(vec![ + RepDefUnraveler::new( + None, + Some(vec![0, 1, 0, 1]), + vec![DefinitionInterpretation::NullableItem].into(), + 4, + ), + RepDefUnraveler::new( + None, + None, + vec![DefinitionInterpretation::NullableItem].into(), + 4, + ), + ]); + + assert_eq!( + unraveler.unravel_validity(8).unwrap(), + Some(validity(&[ + true, false, true, false, true, true, true, true + ])) + ); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/statistics.rs b/lance-artifact/rust/lance-encoding/src/statistics.rs new file mode 100644 index 000000000..e312bc513 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/statistics.rs @@ -0,0 +1,1269 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + fmt::{self}, + hash::{Hash, RandomState}, + sync::Arc, +}; + +use arrow_array::{Array, ArrowPrimitiveType, UInt64Array, cast::AsArray, types::UInt64Type}; +use hyperloglogplus::{HyperLogLog, HyperLogLogPlus}; +use num_traits::PrimInt; + +use crate::data::{ + AllNullDataBlock, DataBlock, DictionaryDataBlock, FixedSizeListBlock, FixedWidthDataBlock, + NullableDataBlock, OpaqueBlock, StructDataBlock, VariableWidthBlock, +}; + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub enum Stat { + BitWidth, + DataSize, + Cardinality, + FixedSize, + NullCount, + MaxLength, + RunCount, + BytePositionEntropy, +} + +impl fmt::Debug for Stat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::BitWidth => write!(f, "BitWidth"), + Self::DataSize => write!(f, "DataSize"), + Self::Cardinality => write!(f, "Cardinality"), + Self::FixedSize => write!(f, "FixedSize"), + Self::NullCount => write!(f, "NullCount"), + Self::MaxLength => write!(f, "MaxLength"), + Self::RunCount => write!(f, "RunCount"), + Self::BytePositionEntropy => write!(f, "BytePositionEntropy"), + } + } +} + +impl fmt::Display for Stat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +pub trait ComputeStat { + fn compute_stat(&mut self); +} + +impl ComputeStat for DataBlock { + fn compute_stat(&mut self) { + match self { + Self::Empty() => {} + Self::Constant(_) => {} + Self::AllNull(_) => {} + Self::Nullable(data_block) => data_block.data.compute_stat(), + Self::FixedWidth(data_block) => data_block.compute_stat(), + Self::FixedSizeList(data_block) => data_block.compute_stat(), + Self::VariableWidth(data_block) => data_block.compute_stat(), + Self::Opaque(data_block) => data_block.compute_stat(), + Self::Struct(data_block) => data_block.compute_stat(), + Self::Dictionary(_) => {} + } + } +} + +impl ComputeStat for VariableWidthBlock { + fn compute_stat(&mut self) { + if !self.block_info.0.read().unwrap().is_empty() { + panic!("compute_stat should only be called once during DataBlock construction"); + } + let data_size = self.data_size(); + let data_size_array = Arc::new(UInt64Array::from(vec![data_size])); + + let max_length_array = self.max_length(); + + let mut info = self.block_info.0.write().unwrap(); + info.insert(Stat::DataSize, data_size_array); + info.insert(Stat::MaxLength, max_length_array); + } +} + +impl ComputeStat for FixedWidthDataBlock { + fn compute_stat(&mut self) { + // compute this datablock's data_size + let data_size = self.data_size(); + let data_size_array = Arc::new(UInt64Array::from(vec![data_size])); + + // compute this datablock's max_bit_width + let max_bit_widths = self.max_bit_widths(); + + // the MaxLength of FixedWidthDataBlock is it's self.bits_per_value / 8 + let max_len = self.bits_per_value / 8; + let max_len_array = Arc::new(UInt64Array::from(vec![max_len])); + + // compute run count + let run_count_array = self.run_count(); + + // compute byte position entropy + let byte_position_entropy = self.byte_position_entropy(); + + let mut info = self.block_info.0.write().unwrap(); + info.insert(Stat::DataSize, data_size_array); + info.insert(Stat::BitWidth, max_bit_widths); + info.insert(Stat::MaxLength, max_len_array); + info.insert(Stat::RunCount, run_count_array); + info.insert(Stat::BytePositionEntropy, byte_position_entropy); + } +} + +impl ComputeStat for FixedSizeListBlock { + fn compute_stat(&mut self) { + // We leave the child stats unchanged. This may seem odd (e.g. should bit width be the + // bit width of the child * dimension?) but it's because we use these stats to determine + // compression and we are currently just compressing the child data. + // + // There is a potential opportunity here to do better. For example, if we have a FSL of + // 4 32-bit integers then we should probably treat them as a single 128-bit integer or maybe + // even 4 columns of 32-bit integers. This might yield better compression. + self.child.compute_stat(); + } +} + +impl ComputeStat for OpaqueBlock { + fn compute_stat(&mut self) { + // compute this datablock's data_size + let data_size = self.data_size(); + let data_size_array = Arc::new(UInt64Array::from(vec![data_size])); + let mut info = self.block_info.0.write().unwrap(); + info.insert(Stat::DataSize, data_size_array); + } +} + +pub trait GetStat: fmt::Debug { + fn get_stat(&self, stat: Stat) -> Option>; + + fn expect_stat(&self, stat: Stat) -> Arc { + self.get_stat(stat) + .unwrap_or_else(|| panic!("{:?} DataBlock does not have `{}` statistics.", self, stat)) + } + + fn expect_single_stat(&self, stat: Stat) -> T::Native { + let stat_value = self.expect_stat(stat); + let stat_value = stat_value.as_primitive::(); + if stat_value.len() != 1 { + panic!( + "{:?} DataBlock does not have exactly one value for `{} statistics.", + self, stat + ); + } + stat_value.value(0) + } +} + +impl GetStat for DataBlock { + fn get_stat(&self, stat: Stat) -> Option> { + match self { + Self::Empty() => None, + Self::Constant(_) => None, + Self::AllNull(data_block) => data_block.get_stat(stat), + Self::Nullable(data_block) => data_block.get_stat(stat), + Self::FixedWidth(data_block) => data_block.get_stat(stat), + Self::FixedSizeList(data_block) => data_block.get_stat(stat), + Self::VariableWidth(data_block) => data_block.get_stat(stat), + Self::Opaque(data_block) => data_block.get_stat(stat), + Self::Struct(data_block) => data_block.get_stat(stat), + Self::Dictionary(data_block) => data_block.get_stat(stat), + } + } +} + +// NullableDataBlock will be deprecated in Lance 2.1. +impl GetStat for NullableDataBlock { + // This function simply returns the statistics of the inner `DataBlock` of `NullableDataBlock`, + // this is not accurate but `NullableDataBlock` is going to be deprecated in Lance 2.1 anyway. + fn get_stat(&self, stat: Stat) -> Option> { + self.data.get_stat(stat) + } +} + +impl GetStat for VariableWidthBlock { + fn get_stat(&self, stat: Stat) -> Option> { + { + let block_info = self.block_info.0.read().unwrap(); + if block_info.is_empty() { + panic!("get_stat should be called after statistics are computed."); + } + if let Some(stat_value) = block_info.get(&stat) { + return Some(stat_value.clone()); + } + } + + if stat != Stat::Cardinality { + return None; + } + + let computed = self.compute_cardinality(); + let mut block_info = self.block_info.0.write().unwrap(); + if block_info.is_empty() { + panic!("get_stat should be called after statistics are computed."); + } + Some( + block_info + .entry(stat) + .or_insert_with(|| computed.clone()) + .clone(), + ) + } +} + +impl GetStat for FixedSizeListBlock { + fn get_stat(&self, stat: Stat) -> Option> { + let child_stat = self.child.get_stat(stat); + match stat { + Stat::MaxLength => child_stat.map(|max_length| { + // this is conservative when working with variable length data as we shouldn't assume + // that we have a list of all max-length elements but it's cheap and easy to calculate + let max_length = max_length.as_primitive::().value(0); + Arc::new(UInt64Array::from(vec![max_length * self.dimension])) as Arc + }), + _ => child_stat, + } + } +} + +impl VariableWidthBlock { + // Caveat: the computation here assumes VariableWidthBlock.offsets maps directly to VariableWidthBlock.data + // without any adjustment(for example, no null_adjustment for offsets) + fn compute_cardinality(&self) -> Arc { + const PRECISION: u8 = 4; + // The default hasher (currently sip hash 1-3) does not seem to give good results + // with HLL. + // + // In particular, when using randomly generated 12-byte strings, the HLL count was + // suggested a cardinality of 500 (out of 1000 unique items and hashes) at least 10% + // of the time. + // + // Using xxhash3 consistently gives better results. + let mut hll: HyperLogLogPlus<&[u8], xxhash_rust::xxh3::Xxh3Builder> = + HyperLogLogPlus::new(PRECISION, xxhash_rust::xxh3::Xxh3Builder::default()).unwrap(); + + match self.bits_per_offset { + 32 => { + let offsets_ref = self.offsets.borrow_to_typed_slice::(); + let offsets: &[u32] = offsets_ref.as_ref(); + + offsets + .iter() + .zip(offsets.iter().skip(1)) + .for_each(|(&start, &end)| { + hll.insert(&self.data[start as usize..end as usize]); + }); + let cardinality = hll.count() as u64; + Arc::new(UInt64Array::from(vec![cardinality])) + } + 64 => { + let offsets_ref = self.offsets.borrow_to_typed_slice::(); + let offsets: &[u64] = offsets_ref.as_ref(); + + offsets + .iter() + .zip(offsets.iter().skip(1)) + .for_each(|(&start, &end)| { + hll.insert(&self.data[start as usize..end as usize]); + }); + + let cardinality = hll.count() as u64; + Arc::new(UInt64Array::from(vec![cardinality])) + } + _ => { + unreachable!("the bits_per_offset of VariableWidthBlock can only be 32 or 64") + } + } + } + + fn max_length(&mut self) -> Arc { + match self.bits_per_offset { + 32 => { + let offsets = self.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + let max_len = offsets + .windows(2) + .map(|pair| pair[1] - pair[0]) + .max() + .unwrap_or(0); + Arc::new(UInt64Array::from(vec![max_len as u64])) + } + 64 => { + let offsets = self.offsets.borrow_to_typed_slice::(); + let offsets = offsets.as_ref(); + let max_len = offsets + .windows(2) + .map(|pair| pair[1] - pair[0]) + .max() + .unwrap_or(0); + Arc::new(UInt64Array::from(vec![max_len])) + } + _ => { + unreachable!("the type of offsets in VariableWidth can only be u32 or u64"); + } + } + } +} + +impl GetStat for AllNullDataBlock { + fn get_stat(&self, stat: Stat) -> Option> { + match stat { + Stat::NullCount => { + let null_count = self.num_values; + Some(Arc::new(UInt64Array::from(vec![null_count]))) + } + Stat::DataSize => Some(Arc::new(UInt64Array::from(vec![0]))), + _ => None, + } + } +} + +impl GetStat for FixedWidthDataBlock { + fn get_stat(&self, stat: Stat) -> Option> { + { + let block_info = self.block_info.0.read().unwrap(); + + if block_info.is_empty() { + panic!("get_stat should be called after statistics are computed."); + } + + if let Some(stat_value) = block_info.get(&stat) { + return Some(stat_value.clone()); + } + } + + if stat == Stat::Cardinality && (self.bits_per_value == 64 || self.bits_per_value == 128) { + let computed = self.cardinality(); + let mut block_info = self.block_info.0.write().unwrap(); + Some( + block_info + .entry(stat) + .or_insert_with(|| computed.clone()) + .clone(), + ) + } else { + None + } + } +} + +impl FixedWidthDataBlock { + fn max_bit_widths(&mut self) -> Arc { + if self.num_values == 0 { + return Arc::new(UInt64Array::from(vec![0u64])); + } + + const CHUNK_SIZE: usize = 1024; + + fn calculate_max_bit_width(slice: &[T], bits_per_value: u64) -> Vec { + slice + .chunks(CHUNK_SIZE) + .map(|chunk| { + let max_value = chunk.iter().fold(T::zero(), |acc, &x| acc | x); + bits_per_value - max_value.leading_zeros() as u64 + }) + .collect() + } + + match self.bits_per_value { + 8 => { + let u8_slice = self.data.borrow_to_typed_slice::(); + let u8_slice = u8_slice.as_ref(); + Arc::new(UInt64Array::from(calculate_max_bit_width( + u8_slice, + self.bits_per_value, + ))) + } + 16 => { + let u16_slice = self.data.borrow_to_typed_slice::(); + let u16_slice = u16_slice.as_ref(); + Arc::new(UInt64Array::from(calculate_max_bit_width( + u16_slice, + self.bits_per_value, + ))) + } + 32 => { + let u32_slice = self.data.borrow_to_typed_slice::(); + let u32_slice = u32_slice.as_ref(); + Arc::new(UInt64Array::from(calculate_max_bit_width( + u32_slice, + self.bits_per_value, + ))) + } + 64 => { + let u64_slice = self.data.borrow_to_typed_slice::(); + let u64_slice = u64_slice.as_ref(); + Arc::new(UInt64Array::from(calculate_max_bit_width( + u64_slice, + self.bits_per_value, + ))) + } + _ => Arc::new(UInt64Array::from(vec![self.bits_per_value])), + } + } + + fn cardinality(&self) -> Arc { + match self.bits_per_value { + 64 => { + let u64_slice_ref = self.data.borrow_to_typed_slice::(); + let u64_slice = u64_slice_ref.as_ref(); + + const PRECISION: u8 = 4; + let mut hll: HyperLogLogPlus = + HyperLogLogPlus::new(PRECISION, xxhash_rust::xxh3::Xxh3Builder::default()) + .unwrap(); + for val in u64_slice { + hll.insert(val); + } + let cardinality = hll.count() as u64; + Arc::new(UInt64Array::from(vec![cardinality])) + } + 128 => { + let u128_slice_ref = self.data.borrow_to_typed_slice::(); + let u128_slice = u128_slice_ref.as_ref(); + + const PRECISION: u8 = 4; + let mut hll: HyperLogLogPlus = + HyperLogLogPlus::new(PRECISION, RandomState::new()).unwrap(); + for val in u128_slice { + hll.insert(val); + } + let cardinality = hll.count() as u64; + Arc::new(UInt64Array::from(vec![cardinality])) + } + _ => unreachable!(), + } + } + + /// Counts the number of runs (consecutive sequences of equal values) in the data. + /// + /// A "run" is defined as a sequence of one or more consecutive equal values. + /// For example: + /// - `[1, 1, 2, 2, 2, 3]` has 3 runs: [1,1], [2,2,2], and [3] + /// - `[1, 2, 3, 4]` has 4 runs (each value is its own run) + /// - `[5, 5, 5, 5]` has 1 run + /// + /// This count is used to determine if RLE compression would be effective. + /// Fewer runs relative to the total number of values indicates better RLE compression potential. + fn run_count(&mut self) -> Arc { + if self.num_values == 0 { + return Arc::new(UInt64Array::from(vec![0u64])); + } + + // Inner function to count runs in typed data + fn count_runs(slice: &[T]) -> u64 { + if slice.is_empty() { + return 0; + } + + // Start with 1 run (the first value) + let mut runs = 1u64; + let mut prev = slice[0]; + + // Count value transitions (each transition indicates a new run) + for &val in &slice[1..] { + if val != prev { + runs += 1; + prev = val; + } + } + + runs + } + + let run_count = match self.bits_per_value { + 8 => { + let u8_slice = self.data.borrow_to_typed_slice::(); + count_runs(u8_slice.as_ref()) + } + 16 => { + let u16_slice = self.data.borrow_to_typed_slice::(); + count_runs(u16_slice.as_ref()) + } + 32 => { + let u32_slice = self.data.borrow_to_typed_slice::(); + count_runs(u32_slice.as_ref()) + } + 64 => { + let u64_slice = self.data.borrow_to_typed_slice::(); + count_runs(u64_slice.as_ref()) + } + 128 => { + let u128_slice = self.data.borrow_to_typed_slice::(); + count_runs(u128_slice.as_ref()) + } + _ => self.num_values, // For other bit widths, assume no runs + }; + + Arc::new(UInt64Array::from(vec![run_count])) + } + + /// Calculates entropy for each byte position. + /// Returns an array with entropy values for each byte position (scaled by 1000 for integer storage). + /// Lower entropy in specific byte positions indicates better suitability for BSS. + fn byte_position_entropy(&mut self) -> Arc { + const SAMPLE_SIZE: usize = 64; // Sample more values for better entropy estimation + + // Get sample size (min of data length and SAMPLE_SIZE) + let sample_count = (self.num_values as usize).min(SAMPLE_SIZE); + + if sample_count == 0 { + // Return empty array for empty data + return Arc::new(UInt64Array::from(vec![] as Vec)); + } + + let bytes_per_value = (self.bits_per_value / 8) as usize; + let mut entropies = Vec::with_capacity(bytes_per_value); + + // Calculate entropy for each byte position + for pos in 0..bytes_per_value { + let mut byte_counts = [0u32; 256]; + + // Count occurrences of each byte value at this position + for i in 0..sample_count { + let byte_offset = i * bytes_per_value + pos; + if byte_offset < self.data.len() { + byte_counts[self.data[byte_offset] as usize] += 1; + } + } + + // Calculate Shannon entropy for this position + let mut entropy = 0.0f64; + let total = sample_count as f64; + + for &count in &byte_counts { + if count > 0 { + let p = count as f64 / total; + entropy -= p * p.log2(); + } + } + + // Scale by 1000 and store as integer for efficient storage + entropies.push((entropy * 1000.0) as u64); + } + + Arc::new(UInt64Array::from(entropies)) + } +} + +impl GetStat for OpaqueBlock { + fn get_stat(&self, stat: Stat) -> Option> { + let block_info = self.block_info.0.read().unwrap(); + + if block_info.is_empty() { + panic!("get_stat should be called after statistics are computed."); + } + block_info.get(&stat).cloned() + } +} + +impl GetStat for DictionaryDataBlock { + fn get_stat(&self, _stat: Stat) -> Option> { + None + } +} + +impl GetStat for StructDataBlock { + fn get_stat(&self, stat: Stat) -> Option> { + let block_info = self.block_info.0.read().unwrap(); + if block_info.is_empty() { + panic!("get_stat should be called after statistics are computed.") + } + block_info.get(&stat).cloned() + } +} + +impl ComputeStat for StructDataBlock { + fn compute_stat(&mut self) { + let data_size = self.data_size(); + let data_size_array = Arc::new(UInt64Array::from(vec![data_size])); + + let max_len = self + .children + .iter() + .map(|child| child.expect_single_stat::(Stat::MaxLength)) + .sum::(); + let max_len_array = Arc::new(UInt64Array::from(vec![max_len])); + + let mut info = self.block_info.0.write().unwrap(); + info.insert(Stat::DataSize, data_size_array); + info.insert(Stat::MaxLength, max_len_array); + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + + use arrow_array::{ + ArrayRef, Int8Array, Int16Array, Int32Array, Int64Array, LargeStringArray, StringArray, + UInt8Array, UInt16Array, UInt32Array, UInt64Array, + }; + use arrow_schema::{DataType, Field}; + use lance_arrow::DataTypeExt; + use lance_datagen::{ArrayGeneratorExt, DEFAULT_SEED, RowCount, array}; + use rand::SeedableRng; + + use crate::statistics::{GetStat, Stat}; + + use super::DataBlock; + + use arrow_array::{ + Array, + cast::AsArray, + types::{Int32Type, UInt64Type}, + }; + use arrow_select::concat::concat; + #[test] + fn test_data_size_stat() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = array::rand::().with_nulls(&[false, false, false]); + let arr1 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let arr2 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let arr3 = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_arrays(&[arr1.clone(), arr2.clone(), arr3.clone()], 9); + + let concatenated_array = concat(&[ + &*Arc::new(arr1.clone()) as &dyn Array, + &*Arc::new(arr2.clone()) as &dyn Array, + &*Arc::new(arr3.clone()) as &dyn Array, + ]) + .unwrap(); + + let data_size = block.expect_single_stat::(Stat::DataSize); + + let total_buffer_size: usize = concatenated_array + .to_data() + .buffers() + .iter() + .map(|buffer| buffer.len()) + .sum(); + assert!(data_size == total_buffer_size as u64); + + // test DataType::Binary + let mut genn = lance_datagen::array::rand_type(&DataType::Binary); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + let data_size = block.expect_single_stat::(Stat::DataSize); + + let total_buffer_size: usize = arr + .to_data() + .buffers() + .iter() + .map(|buffer| buffer.len()) + .sum(); + assert!(data_size == total_buffer_size as u64); + + // test DataType::Struct + let fields = vec![ + Arc::new(Field::new("int_field", DataType::Int32, false)), + Arc::new(Field::new("float_field", DataType::Float32, false)), + ] + .into(); + + let mut genn = lance_datagen::array::rand_type(&DataType::Struct(fields)); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + let (_, arr_parts, _) = arr.as_struct().clone().into_parts(); + let total_buffer_size: usize = arr_parts + .iter() + .map(|arr| { + arr.to_data() + .buffers() + .iter() + .map(|buffer| buffer.len()) + .sum::() + }) + .sum(); + let data_size = block.expect_single_stat::(Stat::DataSize); + assert!(data_size == total_buffer_size as u64); + + // test DataType::Dictionary + let mut genn = array::rand_type(&DataType::Dictionary( + Box::new(DataType::Int32), + Box::new(DataType::Utf8), + )); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + assert!(block.get_stat(Stat::DataSize).is_none()); + + let mut genn = array::rand::().with_nulls(&[false, true, false]); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + let data_size = block.expect_single_stat::(Stat::DataSize); + let total_buffer_size: usize = arr + .to_data() + .buffers() + .iter() + .map(|buffer| buffer.len()) + .sum(); + + assert!(data_size == total_buffer_size as u64); + } + + #[test] + fn test_bit_width_stat_for_integers() { + let int8_array = Int8Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref(),); + + let int8_array = Int8Array::from(vec![0x1, 0x2, 0x3, 0x7F]); + let array_ref: ArrayRef = Arc::new(int8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![7])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref(),); + + let int8_array = Int8Array::from(vec![0x1, 0x2, 0x3, 0xF, 0x1F]); + let array_ref: ArrayRef = Arc::new(int8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![5])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref(),); + + let int8_array = Int8Array::from(vec![-1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![0x1, 0x2, 0x3, 0x7F]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![7])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![0x1, 0x2, 0x3, 0x1FF]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![0x1, 0x2, 0x3, 0xF, 0x1F]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![5])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int16_array = Int16Array::from(vec![-1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![16])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int32_array = Int32Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int32_array = Int32Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(int32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int32_array = Int32Array::from(vec![0x1, 0x2, 0x3, 0xFF, 0x1FF]); + let array_ref: ArrayRef = Arc::new(int32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int32_array = Int32Array::from(vec![-1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![32])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int32_array = Int32Array::from(vec![-1, 2, 3, -88]); + let array_ref: ArrayRef = Arc::new(int32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![32])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int64_array = Int64Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int64_array = Int64Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(int64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int64_array = Int64Array::from(vec![0x1, 0x2, 0x3, 0xFF, 0x1FF]); + let array_ref: ArrayRef = Arc::new(int64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int64_array = Int64Array::from(vec![-1, 2, 3]); + let array_ref: ArrayRef = Arc::new(int64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![64])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let int64_array = Int64Array::from(vec![-1, 2, 3, -88]); + let array_ref: ArrayRef = Arc::new(int64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![64])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint8_array = UInt8Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(uint8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint8_array = UInt8Array::from(vec![0x1, 0x2, 0x3, 0x7F]); + let array_ref: ArrayRef = Arc::new(uint8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![7])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint8_array = UInt8Array::from(vec![0x1, 0x2, 0x3, 0xF, 0x1F]); + let array_ref: ArrayRef = Arc::new(uint8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![5])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint8_array = UInt8Array::from(vec![1, 2, 3, 0xF]); + let array_ref: ArrayRef = Arc::new(uint8_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![4])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![0x1, 0x2, 0x3, 0x7F]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![7])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![0x1, 0x2, 0x3, 0x1FF]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![0x1, 0x2, 0x3, 0xF, 0x1F]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![5])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint16_array = UInt16Array::from(vec![1, 2, 3, 0xFFFF]); + let array_ref: ArrayRef = Arc::new(uint16_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![16])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint32_array = UInt32Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(uint32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint32_array = UInt32Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(uint32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref(),); + + let uint32_array = UInt32Array::from(vec![0x1, 0x2, 0x3, 0xFF, 0x1FF]); + let array_ref: ArrayRef = Arc::new(uint32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint32_array = UInt32Array::from(vec![1, 2, 3, 0xF]); + let array_ref: ArrayRef = Arc::new(uint32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![4])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint32_array = UInt32Array::from(vec![1, 2, 3, 0x77]); + let array_ref: ArrayRef = Arc::new(uint32_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![7])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint64_array = UInt64Array::from(vec![1, 2, 3]); + let array_ref: ArrayRef = Arc::new(uint64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![2])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint64_array = UInt64Array::from(vec![0x1, 0x2, 0x3, 0xFF]); + let array_ref: ArrayRef = Arc::new(uint64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![8])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint64_array = UInt64Array::from(vec![0x1, 0x2, 0x3, 0xFF, 0x1FF]); + let array_ref: ArrayRef = Arc::new(uint64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![9])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint64_array = UInt64Array::from(vec![0, 2, 3, 0xFFFF]); + let array_ref: ArrayRef = Arc::new(uint64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![16])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + + let uint64_array = UInt64Array::from(vec![1, 2, 3, 0xFFFF_FFFF_FFFF_FFFF]); + let array_ref: ArrayRef = Arc::new(uint64_array); + let block = DataBlock::from_array(array_ref); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![64])) as ArrayRef; + let actual_bit_width = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_width.as_ref(), expected_bit_width.as_ref()); + } + + #[test] + fn test_bit_width_stat_more_than_1024() { + for data_type in [ + DataType::Int8, + DataType::Int16, + DataType::Int32, + DataType::Int64, + ] { + let array1 = Int64Array::from(vec![3; 1024]); + let array2 = Int64Array::from(vec![8; 1024]); + let array3 = Int64Array::from(vec![-1; 10]); + let array1 = arrow_cast::cast(&array1, &data_type).unwrap(); + let array2 = arrow_cast::cast(&array2, &data_type).unwrap(); + let array3 = arrow_cast::cast(&array3, &data_type).unwrap(); + + let arrays: Vec<&dyn arrow_array::Array> = + vec![array1.as_ref(), array2.as_ref(), array3.as_ref()]; + let concatenated = concat(&arrays).unwrap(); + let block = DataBlock::from_array(concatenated.clone()); + + let expected_bit_width = Arc::new(UInt64Array::from(vec![ + 2, + 4, + (data_type.byte_width() * 8) as u64, + ])) as ArrayRef; + let actual_bit_widths = block.expect_stat(Stat::BitWidth); + assert_eq!(actual_bit_widths.as_ref(), expected_bit_width.as_ref(),); + } + } + + #[test] + fn test_bit_width_when_none() { + let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(DEFAULT_SEED.0); + let mut genn = lance_datagen::array::rand_type(&DataType::Binary); + let arr = genn.generate(RowCount::from(3), &mut rng).unwrap(); + let block = DataBlock::from_array(arr.clone()); + assert!(block.get_stat(Stat::BitWidth).is_none(),); + } + + #[test] + fn test_cardinality_variable_width_datablock() { + let string_array = StringArray::from(vec![Some("hello"), Some("world")]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 2; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(actual_cardinality, expected_cardinality,); + + let string_array = StringArray::from(vec![ + Some("to be named by variables"), + Some("to be passed as arguments to procedures"), + Some("to be returned as values of procedures"), + ]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 3; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + + assert_eq!(actual_cardinality, expected_cardinality,); + + let string_array = StringArray::from(vec![ + Some("Samuel Eilenberg"), + Some("Saunders Mac Lane"), + Some("Samuel Eilenberg"), + ]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 2; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(actual_cardinality, expected_cardinality,); + + let string_array = LargeStringArray::from(vec![Some("hello"), Some("world")]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 2; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(actual_cardinality, expected_cardinality,); + + let string_array = LargeStringArray::from(vec![ + Some("to be named by variables"), + Some("to be passed as arguments to procedures"), + Some("to be returned as values of procedures"), + ]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 3; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(actual_cardinality, expected_cardinality,); + + let string_array = LargeStringArray::from(vec![ + Some("Samuel Eilenberg"), + Some("Saunders Mac Lane"), + Some("Samuel Eilenberg"), + ]); + let block = DataBlock::from_array(string_array); + let expected_cardinality = 2; + let actual_cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(actual_cardinality, expected_cardinality,); + } + + #[test] + fn test_max_length_variable_width_datablock() { + let string_array = StringArray::from(vec![Some("hello"), Some("world")]); + let block = DataBlock::from_array(string_array.clone()); + let expected_max_length = string_array.value_length(0) as u64; + let actual_max_length = block.expect_single_stat::(Stat::MaxLength); + assert_eq!(actual_max_length, expected_max_length); + + let string_array = StringArray::from(vec![ + Some("to be named by variables"), + Some("to be passed as arguments to procedures"), // string that has max length + Some("to be returned as values of procedures"), + ]); + let block = DataBlock::from_array(string_array.clone()); + let expected_max_length = string_array.value_length(1) as u64; + let actual_max_length = block.expect_single_stat::(Stat::MaxLength); + assert_eq!(actual_max_length, expected_max_length); + + let string_array = StringArray::from(vec![ + Some("Samuel Eilenberg"), + Some("Saunders Mac Lane"), // string that has max length + Some("Samuel Eilenberg"), + ]); + let block = DataBlock::from_array(string_array.clone()); + let expected_max_length = string_array.value_length(1) as u64; + let actual_max_length = block.expect_single_stat::(Stat::MaxLength); + assert_eq!(actual_max_length, expected_max_length); + + let string_array = LargeStringArray::from(vec![Some("hello"), Some("world")]); + let block = DataBlock::from_array(string_array.clone()); + let expected_max_length = string_array.value_length(1) as u64; + let actual_max_length = block.expect_single_stat::(Stat::MaxLength); + assert_eq!(actual_max_length, expected_max_length); + + let string_array = LargeStringArray::from(vec![ + Some("to be named by variables"), + Some("to be passed as arguments to procedures"), // string that has max length + Some("to be returned as values of procedures"), + ]); + let block = DataBlock::from_array(string_array.clone()); + let expected_max_length = string_array.value(1).len() as u64; + let actual_max_length = block.expect_single_stat::(Stat::MaxLength); + + assert_eq!(actual_max_length, expected_max_length); + } + + #[test] + fn test_run_count_stat() { + // Test with highly repetitive data + let int32_array = Int32Array::from(vec![1, 1, 1, 2, 2, 2, 3, 3, 3]); + let block = DataBlock::from_array(int32_array); + let expected_run_count = 3; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + + // Test with no repetition + let int32_array = Int32Array::from(vec![1, 2, 3, 4, 5]); + let block = DataBlock::from_array(int32_array); + let expected_run_count = 5; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + + // Test with mixed pattern + let int32_array = Int32Array::from(vec![1, 1, 2, 3, 3, 3, 4, 5, 5]); + let block = DataBlock::from_array(int32_array); + let expected_run_count = 5; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + + // Test with single value + let int32_array = Int32Array::from(vec![42, 42, 42, 42, 42]); + let block = DataBlock::from_array(int32_array); + let expected_run_count = 1; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + + // Test with different data types + let uint8_array = UInt8Array::from(vec![1, 1, 2, 2, 3, 3]); + let block = DataBlock::from_array(uint8_array); + let expected_run_count = 3; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + + let int64_array = Int64Array::from(vec![100, 100, 200, 300, 300]); + let block = DataBlock::from_array(int64_array); + let expected_run_count = 3; + let actual_run_count = block.expect_single_stat::(Stat::RunCount); + assert_eq!(actual_run_count, expected_run_count); + } + + #[test] + fn test_fixed_width_cardinality_is_lazy() { + let int64_array = Int64Array::from(vec![1, 2, 3, 1, 2, 3, 1]); + let block = DataBlock::from_array(int64_array); + + let DataBlock::FixedWidth(fixed) = &block else { + panic!("Expected FixedWidth datablock"); + }; + + let info = fixed.block_info.0.read().unwrap(); + assert!(info.contains_key(&Stat::DataSize)); + assert!(info.contains_key(&Stat::BitWidth)); + assert!(!info.contains_key(&Stat::Cardinality)); + } + + #[test] + fn test_fixed_width_cardinality_computed_on_demand() { + let int64_array = Int64Array::from(vec![1, 2, 3, 1, 2, 3, 1]); + let block = DataBlock::from_array(int64_array); + + let cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(cardinality, 3); + + let DataBlock::FixedWidth(fixed) = &block else { + panic!("Expected FixedWidth datablock"); + }; + + let info = fixed.block_info.0.read().unwrap(); + assert!(info.contains_key(&Stat::Cardinality)); + } + + #[test] + fn test_variable_width_cardinality_is_lazy() { + let string_array = StringArray::from(vec!["a", "b", "a"]); + let block = DataBlock::from_array(string_array); + + let DataBlock::VariableWidth(var) = &block else { + panic!("Expected VariableWidth datablock"); + }; + + { + let info = var.block_info.0.read().unwrap(); + assert!(info.contains_key(&Stat::DataSize)); + assert!(info.contains_key(&Stat::MaxLength)); + assert!(!info.contains_key(&Stat::Cardinality)); + } + + let cardinality = block.expect_single_stat::(Stat::Cardinality); + assert_eq!(cardinality, 2); + + let info = var.block_info.0.read().unwrap(); + assert!(info.contains_key(&Stat::Cardinality)); + } +} diff --git a/lance-artifact/rust/lance-encoding/src/testing.rs b/lance-artifact/rust/lance-encoding/src/testing.rs new file mode 100644 index 000000000..90c5c0bb6 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/testing.rs @@ -0,0 +1,1534 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{cmp::Ordering, collections::HashMap, ops::Range, sync::Arc}; + +use crate::{ + decoder::DecoderConfig, + encodings::physical::block::CompressionScheme, + format::pb21::{ + BufferCompression, CompressiveEncoding, PageLayout, compressive_encoding::Compression, + }, +}; + +use arrow_array::{Array, StructArray, UInt64Array, make_array}; +use arrow_data::transform::{Capacities, MutableArrayData}; +use arrow_ord::ord::make_comparator; +use arrow_schema::{DataType, Field, Field as ArrowField, FieldRef, Schema, SortOptions}; +use arrow_select::concat::concat; +use bytes::{Bytes, BytesMut}; +use futures::{FutureExt, StreamExt, future::BoxFuture}; +use log::{debug, info, trace}; +use tokio::sync::mpsc::{self, UnboundedSender}; + +use lance_core::{Error, Result, datatypes::Field as LanceField, utils::bit::pad_bytes}; +use lance_datagen::{ArrayGenerator, RowCount, Seed, array, gen_batch}; + +use crate::{ + EncodingsIo, + buffer::LanceBuffer, + compression::{ + BlockCompressor, CompressionStrategy, field_metadata_params, finalize_miniblock_compressor, + reject_packed_struct_per_value, try_bitpacking_block, try_bitpacking_miniblock, + try_byte_stream_split_miniblock, try_child_rle_miniblock, + try_fixed_packed_struct_miniblock, try_fixed_u8_rle_block, try_fixed_u8_rle_miniblock, + try_general_block, try_raw_block, try_raw_fixed_size_list_miniblock, + try_raw_fixed_width_miniblock, try_raw_per_value, try_uncompressed_fixed_width_miniblock, + try_variable_packed_struct_per_value, try_variable_rle_block, try_variable_width_miniblock, + try_variable_width_per_value, + }, + compression_config::{CompressionFieldParams, CompressionParams}, + data::DataBlock, + decoder::{ + ColumnInfo, DecodeBatchScheduler, DecoderMessage, DecoderPlugins, FilterExpression, + PageInfo, create_decode_stream, + }, + encoder::{ + ColumnIndexSequence, EncodedColumn, EncodedPage, EncodingOptions, FieldEncoder, + FieldEncodingContext, FieldEncodingStrategy, MIN_PAGE_BUFFER_ALIGNMENT, OutOfLineBuffers, + structural::{ + PrimitiveFieldEncoding, PrimitivePageEncoding, try_create_binary_blob, try_create_list, + try_create_map, try_create_struct, try_create_structural_blob, + try_create_structural_fixed_size_list, + }, + }, + encodings::logical::primitive::{fullzip::PerValueCompressor, miniblock::MiniBlockCompressor}, + repdef::RepDefBuilder, +}; + +const MAX_PAGE_BYTES: u64 = 32 * 1024 * 1024; +const TEST_ALIGNMENT: usize = MIN_PAGE_BUFFER_ALIGNMENT as usize; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TestEncoding { + Array, + StructuralU16, + StructuralU32, + StructuralSparse, +} + +impl TestEncoding { + fn all() -> impl Iterator { + [ + Self::Array, + Self::StructuralU16, + Self::StructuralU32, + Self::StructuralSparse, + ] + .into_iter() + } + + fn is_structural(self) -> bool { + self != Self::Array + } +} + +impl std::fmt::Display for TestEncoding { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Array => write!(f, "array"), + Self::StructuralU16 => write!(f, "structural-u16"), + Self::StructuralU32 => write!(f, "structural-u32"), + Self::StructuralSparse => write!(f, "structural-sparse"), + } + } +} + +#[derive(Debug, Clone)] +struct TestCompressionStrategy { + encoding: TestEncoding, + params: CompressionParams, +} + +impl TestCompressionStrategy { + fn field_params(&self, field: &LanceField) -> CompressionFieldParams { + let mut params = self + .params + .get_field_params(&field.name, &field.data_type()); + let mut metadata = field_metadata_params(field); + if self.encoding == TestEncoding::StructuralU16 + && metadata + .minichunk_size + .is_some_and(|size| size >= 32 * 1024) + { + metadata.minichunk_size = None; + } + params.merge(&metadata); + params + } +} + +impl CompressionStrategy for TestCompressionStrategy { + fn create_miniblock_compressor( + &self, + field: &LanceField, + data: &DataBlock, + ) -> Result> { + let params = self.field_params(field); + let compressor = + if let Some(compressor) = try_uncompressed_fixed_width_miniblock(data, ¶ms) { + compressor + } else if let Some(compressor) = try_byte_stream_split_miniblock(data, ¶ms) { + compressor + } else if let Some(compressor) = match self.encoding { + TestEncoding::StructuralSparse => try_child_rle_miniblock(data, ¶ms), + TestEncoding::Array | TestEncoding::StructuralU16 | TestEncoding::StructuralU32 => { + try_fixed_u8_rle_miniblock(data, ¶ms) + } + } { + compressor + } else if let Some(compressor) = try_bitpacking_miniblock(data) { + compressor + } else if let Some(compressor) = try_raw_fixed_width_miniblock(data) { + compressor + } else if let Some(compressor) = try_variable_width_miniblock(field, data, ¶ms)? { + compressor + } else if let Some(compressor) = try_fixed_packed_struct_miniblock(data)? { + compressor + } else if let Some(compressor) = try_raw_fixed_size_list_miniblock(data) { + compressor + } else { + return Err(lance_core::Error::not_supported_source( + format!( + "Mini-block compression not yet supported for block type {}", + data.name() + ) + .into(), + )); + }; + finalize_miniblock_compressor(data, compressor, ¶ms) + } + + fn create_per_value( + &self, + field: &LanceField, + data: &DataBlock, + ) -> Result> { + let params = self.field_params(field); + if let Some(compressor) = try_raw_per_value(data) { + return Ok(compressor); + } + let packed = match self.encoding { + TestEncoding::Array | TestEncoding::StructuralU16 => { + reject_packed_struct_per_value(field, data)? + } + TestEncoding::StructuralU32 | TestEncoding::StructuralSparse => { + try_variable_packed_struct_per_value(Arc::new(self.clone()), field, data)? + } + }; + if let Some(compressor) = packed { + return Ok(compressor); + } + if let Some(compressor) = try_variable_width_per_value(field, data, ¶ms)? { + return Ok(compressor); + } + Err(lance_core::Error::not_supported_source( + format!( + "Per-value compression not yet supported for block type {}", + data.name() + ) + .into(), + )) + } + + fn create_block_compressor( + &self, + field: &LanceField, + data: &DataBlock, + ) -> Result<(Box, CompressiveEncoding)> { + let params = self.field_params(field); + let rle = match self.encoding { + TestEncoding::Array | TestEncoding::StructuralU16 => None, + TestEncoding::StructuralU32 => try_fixed_u8_rle_block(data, ¶ms)?, + TestEncoding::StructuralSparse => try_variable_rle_block(data, ¶ms)?, + }; + if let Some(compressor) = rle { + return Ok(compressor); + } + if let Some(compressor) = try_bitpacking_block(data) { + return Ok(compressor); + } + if matches!( + self.encoding, + TestEncoding::StructuralU32 | TestEncoding::StructuralSparse + ) && let Some(compressor) = try_general_block(data, ¶ms)? + { + return Ok(compressor); + } + if let Some(compressor) = try_raw_block(data) { + return Ok(compressor); + } + Err(lance_core::Error::not_supported_source( + format!( + "Block compression not yet supported for block type {}", + data.name() + ) + .into(), + )) + } +} + +pub fn test_compression_strategy( + encoding: TestEncoding, + params: CompressionParams, +) -> Arc { + Arc::new(TestCompressionStrategy { encoding, params }) +} + +#[derive(Debug)] +struct TestFieldEncodingStrategy { + encoding: TestEncoding, + primitive: PrimitiveFieldEncoding, +} + +impl FieldEncodingStrategy for TestFieldEncodingStrategy { + fn create_field_encoder( + &self, + field: &LanceField, + column_index: &mut ColumnIndexSequence, + context: &FieldEncodingContext<'_>, + ) -> Result> { + if let Some(encoder) = + try_create_binary_blob(&self.primitive, field, column_index, context)? + { + return Ok(encoder); + } + if self.encoding != TestEncoding::StructuralU16 + && let Some(encoder) = + try_create_structural_blob(&self.primitive, field, column_index, context)? + { + return Ok(encoder); + } + if field.is_blob() { + return Err(Error::invalid_input_source( + format!( + "Blob encoding is not available for field '{}' with data type {}", + field.name, + field.data_type() + ) + .into(), + )); + } + if self.encoding != TestEncoding::StructuralU16 { + if let Some(encoder) = try_create_map(field, column_index, context)? { + return Ok(encoder); + } + if let Some(encoder) = + try_create_structural_fixed_size_list(field, column_index, context)? + { + return Ok(encoder); + } + } + if let Some(encoder) = self.primitive.try_create(field, column_index, context)? { + return Ok(encoder); + } + if self.encoding == TestEncoding::StructuralU16 { + if matches!( + field.data_type(), + DataType::FixedSizeList(item, _) + if matches!(item.data_type(), DataType::Struct(_)) + ) { + return Err(Error::not_supported_source( + "FixedSizeList is not enabled by the selected file format".into(), + )); + } + if matches!(field.data_type(), DataType::Map(_, _)) { + return Err(Error::not_supported_source( + "Map data type is not enabled by the selected file format".into(), + )); + } + } + if let Some(encoder) = try_create_list(field, column_index, context)? { + return Ok(encoder); + } + if let Some(encoder) = try_create_struct(field, column_index, context)? { + return Ok(encoder); + } + Err(Error::not_supported_source( + format!( + "{} has no field encoding for '{}' with data type {}", + self.encoding, + field.name, + field.data_type() + ) + .into(), + )) + } +} + +pub fn test_encoding_strategy(encoding: TestEncoding) -> Box { + if encoding == TestEncoding::Array { + return Box::new(crate::array_encoding::ArrayFieldEncodingStrategy::new()); + } + + let compression = test_compression_strategy(encoding, CompressionParams::default()); + let page_encodings = match encoding { + TestEncoding::StructuralU16 => vec![ + PrimitivePageEncoding::reject_sparse(), + PrimitivePageEncoding::dense_u16(compression), + ], + TestEncoding::StructuralU32 => vec![ + PrimitivePageEncoding::reject_sparse(), + PrimitivePageEncoding::constant(), + PrimitivePageEncoding::dense_u32(compression), + ], + TestEncoding::StructuralSparse => vec![ + PrimitivePageEncoding::sparse(compression.clone()), + PrimitivePageEncoding::constant(), + PrimitivePageEncoding::dense_u32(compression), + ], + TestEncoding::Array => unreachable!(), + }; + Box::new(TestFieldEncodingStrategy { + encoding, + primitive: PrimitiveFieldEncoding::new(page_encodings), + }) +} + +pub fn create_test_field_encoder( + strategy: &dyn FieldEncodingStrategy, + field: &lance_core::datatypes::Field, + column_index: &mut ColumnIndexSequence, + options: &EncodingOptions, +) -> Result> { + let context = FieldEncodingContext { + strategy, + options, + root_field_metadata: &field.metadata, + }; + strategy.create_field_encoder(field, column_index, &context) +} + +#[derive(Debug)] +pub(crate) struct SimulatedScheduler { + data: Bytes, +} + +impl SimulatedScheduler { + pub fn new(data: Bytes) -> Self { + Self { data } + } +} + +impl EncodingsIo for SimulatedScheduler { + fn submit_request( + &self, + ranges: Vec>, + priority: u64, + ) -> BoxFuture<'static, Result>> { + let data = ranges + .into_iter() + .map(|range| self.data.slice(range.start as usize..range.end as usize)) + .collect(); + + log::trace!("Scheduled request with priority {}", priority); + std::future::ready(data) + .map(move |data| { + log::trace!("Decoded request with priority {}", priority); + Ok(data) + }) + .boxed() + } +} + +fn column_indices_from_schema_helper( + fields: &[FieldRef], + column_indices: &mut Vec, + column_counter: &mut u32, + is_structural_encoding: bool, +) { + // In the old style, every field except FSL gets its own column. In the new style only primitive + // leaf fields get their own column. + for field in fields { + if is_structural_encoding && field.metadata().contains_key("lance-encoding:packed") { + column_indices.push(*column_counter); + *column_counter += 1; + continue; + } + + match field.data_type() { + DataType::Struct(fields) => { + if !is_structural_encoding { + column_indices.push(*column_counter); + *column_counter += 1; + } + column_indices_from_schema_helper( + fields.as_ref(), + column_indices, + column_counter, + is_structural_encoding, + ); + } + DataType::List(inner) => { + if !is_structural_encoding { + column_indices.push(*column_counter); + *column_counter += 1; + } + column_indices_from_schema_helper( + std::slice::from_ref(inner), + column_indices, + column_counter, + is_structural_encoding, + ); + } + DataType::LargeList(inner) => { + if !is_structural_encoding { + column_indices.push(*column_counter); + *column_counter += 1; + } + column_indices_from_schema_helper( + std::slice::from_ref(inner), + column_indices, + column_counter, + is_structural_encoding, + ); + } + DataType::Map(entries, _) => { + column_indices_from_schema_helper( + std::slice::from_ref(entries), + column_indices, + column_counter, + is_structural_encoding, + ); + } + DataType::FixedSizeList(inner, _) => { + // FSL(primitive) does not get its own column in either approach + column_indices_from_schema_helper( + std::slice::from_ref(inner), + column_indices, + column_counter, + is_structural_encoding, + ); + } + _ => { + column_indices.push(*column_counter); + *column_counter += 1; + + column_indices_from_schema_helper( + &[], + column_indices, + column_counter, + is_structural_encoding, + ); + } + } + } +} + +fn column_indices_from_schema(schema: &Schema, is_structural_encoding: bool) -> Vec { + let mut column_indices = Vec::new(); + let mut column_counter = 0; + column_indices_from_schema_helper( + schema.fields(), + &mut column_indices, + &mut column_counter, + is_structural_encoding, + ); + column_indices +} + +#[allow(clippy::too_many_arguments)] +async fn test_decode( + num_rows: u64, + batch_size: u32, + schema: &Schema, + column_infos: &[Arc], + expected: Option>, + io: Arc, + is_structural_encoding: bool, + schedule_fn: impl FnOnce( + DecodeBatchScheduler, + UnboundedSender>, + ) -> BoxFuture<'static, ()>, +) { + let lance_schema = lance_core::datatypes::Schema::try_from(schema).unwrap(); + let cache = Arc::new(lance_core::cache::LanceCache::with_capacity( + 128 * 1024 * 1024, + )); + let column_indices = column_indices_from_schema(schema, is_structural_encoding); + let decode_scheduler = DecodeBatchScheduler::try_new( + &lance_schema, + &column_indices, + column_infos, + &Vec::new(), + num_rows, + Arc::::default(), + io, + cache, + &FilterExpression::no_filter(), + &DecoderConfig::default(), + ) + .await + .unwrap(); + + let (tx, rx) = mpsc::unbounded_channel(); + + let scheduler_fut = schedule_fn(decode_scheduler, tx); + + scheduler_fut.await; + + let mut decode_stream = create_decode_stream( + &lance_schema, + num_rows, + batch_size, + is_structural_encoding, + /*should_validate=*/ true, + /*spawn_structural_batch_decode_tasks=*/ is_structural_encoding, + rx, + /*batch_size_bytes=*/ None, + ) + .unwrap(); + + let mut offset = 0; + while let Some(batch) = decode_stream.next().await { + let batch = batch.task.await.unwrap(); + if let Some(expected) = expected.as_ref() { + let actual = batch.column(0); + let expected_size = (batch_size as usize).min(expected.len() - offset); + let expected = expected.slice(offset, expected_size); + assert_eq!(expected.data_type(), actual.data_type()); + if expected.len() != actual.len() { + panic!( + "Mismatch in length (at offset={}) expected {} but got {}", + offset, + expected.len(), + actual.len() + ); + } + if &expected != actual { + if let Ok(comparator) = make_comparator(&expected, &actual, SortOptions::default()) + { + // We can't just assert_eq! because the error message is not very helpful. This gives us a bit + // more information about where the mismatch is. + for i in 0..expected.len() { + if !matches!(comparator(i, i), Ordering::Equal) { + panic!( + "Mismatch at index {} (offset={}) expected {:?} but got {:?} first mismatch is expected {:?} but got {:?}", + i, + offset, + expected, + actual, + expected.slice(i, 1), + actual.slice(i, 1) + ); + } + } + } else { + // Some arrays (like the null type) don't have a comparator so we just re-run the normal comparison + // and let it assert + assert_eq!(&expected, actual); + } + } + } + offset += batch.num_rows(); + } + if let Some(expected) = expected.as_ref() { + assert_eq!(offset, expected.len()); + } +} + +pub trait ArrayGeneratorProvider { + fn provide(&self) -> Box; + fn copy(&self) -> Box; +} +struct RandomArrayGeneratorProvider { + field: Field, +} + +impl ArrayGeneratorProvider for RandomArrayGeneratorProvider { + fn provide(&self) -> Box { + array::rand_type(self.field.data_type()) + } + + fn copy(&self) -> Box { + Box::new(Self { + field: self.field.clone(), + }) + } +} + +/// Given a field this will test the round trip encoding and decoding of random data +pub async fn check_basic_random(field: Field) { + check_specific_random(field, TestCases::basic()).await; +} + +pub async fn check_specific_random(field: Field, test_cases: TestCases) { + let array_generator_provider = RandomArrayGeneratorProvider { + field: field.clone(), + }; + check_round_trip_encoding_generated(field, Box::new(array_generator_provider), test_cases) + .await; +} + +pub struct FnArrayGeneratorProvider Box + Clone + 'static> { + provider_fn: F, +} + +impl Box + Clone + 'static> FnArrayGeneratorProvider { + pub fn new(provider_fn: F) -> Self { + Self { provider_fn } + } +} + +impl Box + Clone + 'static> ArrayGeneratorProvider + for FnArrayGeneratorProvider +{ + fn provide(&self) -> Box { + (self.provider_fn)() + } + + fn copy(&self) -> Box { + Box::new(Self { + provider_fn: self.provider_fn.clone(), + }) + } +} + +pub async fn check_basic_generated( + field: Field, + array_generator_provider: Box, +) { + check_round_trip_encoding_generated(field, array_generator_provider, TestCases::basic()).await; +} + +pub async fn check_round_trip_encoding_generated( + field: Field, + array_generator_provider: Box, + test_cases: TestCases, +) { + let lance_field = lance_core::datatypes::Field::try_from(&field).unwrap(); + for page_size in test_cases.page_sizes.iter().copied() { + debug!("Testing random data with a page size of {}", page_size); + let encoder_factory = |encoding: TestEncoding| { + let encoding_strategy = test_encoding_strategy(encoding); + let mut column_index_seq = ColumnIndexSequence::default(); + let encoding_options = EncodingOptions { + max_page_bytes: MAX_PAGE_BYTES, + cache_bytes_per_column: page_size, + keep_original_array: true, + buffer_alignment: MIN_PAGE_BUFFER_ALIGNMENT, + }; + create_test_field_encoder( + encoding_strategy.as_ref(), + &lance_field, + &mut column_index_seq, + &encoding_options, + ) + .unwrap() + }; + + check_round_trip_random( + encoder_factory, + field.clone(), + array_generator_provider.copy(), + &test_cases, + ) + .await + } +} + +fn supports_nulls(data_type: &DataType, encoding: TestEncoding) -> bool { + if let DataType::Struct(fields) = data_type { + if encoding == TestEncoding::Array { + // 2.0 doesn't support nullability for structs + false + } else if fields.is_empty() { + // Even in 2.1 we don't support nulls for struct if there are no children because + // we have no spot to stick the repdef info (there is no column) + false + } else { + true + } + } else { + true + } +} + +type EncodingVerificationFn = dyn Fn(&[EncodedColumn], &TestEncoding); + +// The default will just test the full read +#[derive(Clone)] +pub struct TestCases { + ranges: Vec>, + indices: Vec>, + batch_size: u32, + skip_validation: bool, + max_page_size: Option, + page_sizes: Vec, + encodings: Vec, + verify_encoding: Option>, + expected_encoding: Option>, +} + +impl Default for TestCases { + fn default() -> Self { + Self { + batch_size: 100, + ranges: Vec::new(), + indices: Vec::new(), + skip_validation: false, + max_page_size: None, + page_sizes: vec![4096, 1024 * 1024], + encodings: TestEncoding::all().collect(), + verify_encoding: None, + expected_encoding: None, + } + } +} + +impl TestCases { + pub fn basic() -> Self { + Self::default() + .with_range(0..500) + .with_range(100..1100) + .with_range(8000..8500) + .with_indices(vec![100]) + .with_indices(vec![0]) + .with_indices(vec![9999]) + .with_indices(vec![100, 1100, 5000]) + .with_indices(vec![1000, 2000, 3000]) + .with_indices(vec![2000, 2001, 2002, 2003, 2004]) + // Big take that spans multiple pages and generates multiple output batches + .with_indices((100..500).map(|i| i * 3).collect::>()) + } + + pub fn with_range(mut self, range: Range) -> Self { + self.ranges.push(range); + self + } + + pub fn with_indices(mut self, indices: Vec) -> Self { + self.indices.push(indices); + self + } + + pub fn with_batch_size(mut self, batch_size: u32) -> Self { + self.batch_size = batch_size; + self + } + + pub fn without_validation(mut self) -> Self { + self.skip_validation = true; + self + } + + pub fn with_encoding(mut self, encoding: TestEncoding) -> Self { + self.encodings = vec![encoding]; + self + } + + pub fn with_encodings(mut self, encodings: impl IntoIterator) -> Self { + self.encodings = encodings.into_iter().collect(); + self + } + + pub fn with_structural_encodings(self) -> Self { + self.with_encodings([ + TestEncoding::StructuralU16, + TestEncoding::StructuralU32, + TestEncoding::StructuralSparse, + ]) + } + + pub fn with_u32_structural_encodings(self) -> Self { + self.with_encodings([TestEncoding::StructuralU32, TestEncoding::StructuralSparse]) + } + + pub fn with_dense_encodings(self) -> Self { + self.with_encodings([ + TestEncoding::Array, + TestEncoding::StructuralU16, + TestEncoding::StructuralU32, + ]) + } + + pub fn with_array_and_u16_encodings(self) -> Self { + self.with_encodings([TestEncoding::Array, TestEncoding::StructuralU16]) + } + + pub fn with_page_sizes(mut self, page_sizes: Vec) -> Self { + self.page_sizes = page_sizes; + self + } + + pub fn with_max_page_size(mut self, max_page_size: u64) -> Self { + self.max_page_size = Some(max_page_size); + self + } + + fn get_max_page_size(&self) -> u64 { + self.max_page_size.unwrap_or(MAX_PAGE_BYTES) + } + + fn encodings(&self) -> impl Iterator + '_ { + self.encodings.iter().copied() + } + + pub fn with_verify_encoding(mut self, verify_encoding: Arc) -> Self { + self.verify_encoding = Some(verify_encoding); + self + } + + fn verify_encoding(&self, columns: &[EncodedColumn], encoding: &TestEncoding) { + if let Some(verify_encoding) = self.verify_encoding.as_ref() { + verify_encoding(columns, encoding); + } + } + + pub fn with_expected_encoding(mut self, encoding: impl Into) -> Self { + self.expected_encoding = Some(vec![encoding.into()]); + self + } + + pub fn with_expected_encoding_chain, T: Into>( + mut self, + encodings: I, + ) -> Self { + self.expected_encoding = Some(encodings.into_iter().map(Into::into).collect()); + self + } +} + +/// Maps encoding enum variant to its string tag +/// Uses exhaustive match to ensure compile-time checking when variants are added/removed +fn tag(e: &Compression) -> &'static str { + use Compression::*; + + match e { + Flat(_) => "flat", + Variable(_) => "variable", + Constant(_) => "constant", + OutOfLineBitpacking(_) => "out_of_line_bitpacking", + InlineBitpacking(_) => "inline_bitpacking", + Fsst(_) => "fsst", + Dictionary(_) => "dictionary", + Rle(_) => "rle", + ByteStreamSplit(_) => "byte_stream_split", + General(_) => "general", + FixedSizeList(_) => "fixed_size_list", + PackedStruct(_) => "packed_struct", + VariablePackedStruct(_) => "variable_packed_struct", + } +} + +/// Returns any buffer outputs of this encoding +fn buffer(c: &Compression) -> Option> { + use Compression::*; + + match c { + Flat(f) => f.data.as_ref().map(|b| vec![b]), + Variable(v) => v.values.as_ref().map(|b| vec![b]), + InlineBitpacking(i) => i.values.as_ref().map(|b| vec![b]), + General(g) => g.compression.as_ref().map(|c| vec![c]), + _ => None, + } +} + +/// Returns the child encoding if this variant contains nested ArrayEncoding +fn child(c: &Compression) -> Option> { + use Compression::*; + + match c { + Variable(v) => v.offsets.as_ref().map(|b| vec![b.as_ref()]), + OutOfLineBitpacking(o) => o.values.as_ref().map(|b| vec![b.as_ref()]), + Fsst(f) => f.values.as_ref().map(|b| vec![b.as_ref()]), + ByteStreamSplit(b) => b.values.as_ref().map(|b| vec![b.as_ref()]), + General(g) => g.values.as_ref().map(|b| vec![b.as_ref()]), + Dictionary(d) => { + let mut children = Vec::new(); + if let Some(values) = d.items.as_ref() { + children.push(values.as_ref()); + } + if let Some(indices) = d.indices.as_ref() { + children.push(indices.as_ref()); + } + Some(children) + } + Rle(r) => { + let mut children = Vec::new(); + if let Some(values) = r.values.as_ref() { + children.push(values.as_ref()); + } + if let Some(run_lengths) = r.run_lengths.as_ref() { + children.push(run_lengths.as_ref()); + } + Some(children) + } + FixedSizeList(f) => f.values.as_ref().map(|b| vec![b.as_ref()]), + VariablePackedStruct(v) => { + let nested: Vec<&CompressiveEncoding> = v + .fields + .iter() + .filter_map(|field| field.value.as_ref()) + .collect(); + if nested.is_empty() { + None + } else { + Some(nested) + } + } + _ => None, + } +} + +/// Extract encoding types from array encoding (helper for nested encodings) +/// Returns the encoding chain including compression schemes for Block variants +pub fn extract_array_encoding_chain(enc: &CompressiveEncoding) -> Vec { + let mut chain = Vec::with_capacity(8); + let mut stack = vec![enc]; + + while let Some(cur) = stack.pop() { + if let Some(inner) = &cur.compression { + // 1. Add current layer's tag + chain.push(tag(inner).to_string()); + + // 2. Extract any buffer output + if let Some(buffer) = buffer(inner) { + chain.extend(buffer.into_iter().map(|b| { + let scheme = CompressionScheme::try_from(b.scheme()).unwrap(); + scheme.to_string() + })); + } + + // 3. Process child encoding if exists + if let Some(children) = child(inner) { + stack.extend(children); + } + } + } + chain +} + +fn collect_page_encoding(layout: &PageLayout, actual_chain: &mut Vec) -> Result<()> { + // Extract encodings from the page layout + use crate::format::pb21::page_layout::Layout; + if let Some(ref layout_type) = layout.layout { + match layout_type { + Layout::MiniBlockLayout(mini_block) => { + if mini_block.dictionary.is_some() { + actual_chain.push("dictionary".to_string()); + } + // Check value compression + if let Some(ref value_comp) = mini_block.value_compression { + let chain = extract_array_encoding_chain(value_comp); + actual_chain.extend(chain); + } + } + Layout::FullZipLayout(full_zip) => { + // Check value compression in full zip layout + if let Some(ref value_comp) = full_zip.value_compression { + let chain = extract_array_encoding_chain(value_comp); + actual_chain.extend(chain); + } + } + Layout::ConstantLayout(_) => { + // Constant layout does not describe a value encoding chain. + } + Layout::BlobLayout(blob) => { + if let Some(inner_layout) = &blob.inner_layout { + collect_page_encoding(inner_layout.as_ref(), actual_chain)? + } + } + Layout::SparseLayout(sparse) => { + if let Some(value_compression) = &sparse.value_compression { + actual_chain.extend(extract_array_encoding_chain(value_compression)); + } + } + } + } + + Ok(()) +} + +/// Verify that a single page contains the expected encoding +fn verify_page_encoding( + page: &EncodedPage, + expected_chain: &[String], + col_idx: usize, +) -> Result<()> { + use crate::decoder::PageEncoding; + use lance_core::Error; + + let mut actual_chain = Vec::new(); + + match &page.description { + PageEncoding::Structural(layout) => { + collect_page_encoding(layout, &mut actual_chain)?; + + // All-null structural pages may legitimately contain no encodings to verify. + // This can happen even when compression is configured because there is no value data + // (and rep/def compression is not currently described in the page layout). + if actual_chain.is_empty() + && page.data.is_empty() + && let Some(crate::format::pb21::page_layout::Layout::ConstantLayout(cl)) = + layout.layout.as_ref() + && cl.inline_value.is_none() + { + return Ok(()); + } + } + PageEncoding::Legacy(_) => { + // We don't need to care about the v2.0 array encoding. + } + } + + // Check that all expected encodings appear in the actual chain + for expected in expected_chain { + if !actual_chain.iter().any(|actual| actual.contains(expected)) { + return Err(Error::invalid_input_source( + format!( + "Column {} expected encoding chain {:?} but got {:?}", + col_idx, expected_chain, actual_chain + ) + .into(), + )); + } + } + Ok(()) +} + +/// Given specific data and test cases we check round trip encoding and decoding +/// +/// Note that the input `data` is a `Vec` to simulate multiple calls to `maybe_encode`. +/// In other words, these are multiple chunks of one long array and not multiple columns +/// in a record batch. To feed a "record batch" you should first convert the record batch +/// to a struct array. +pub async fn check_round_trip_encoding_of_data( + data: Vec>, + test_cases: &TestCases, + metadata: HashMap, +) { + check_round_trip_encoding_of_data_with_expected(data, None, test_cases, metadata).await +} + +pub async fn check_round_trip_encoding_of_data_with_expected( + data: Vec>, + expected_override: Option>, + test_cases: &TestCases, + metadata: HashMap, +) { + let example_data = data.first().expect("Data must have at least one array"); + let mut field = Field::new("", example_data.data_type().clone(), true); + field = field.with_metadata(metadata); + let lance_field = lance_core::datatypes::Field::try_from(&field).unwrap(); + for encoding in test_cases.encodings() { + for page_size in test_cases.page_sizes.iter() { + let encoding_strategy = test_encoding_strategy(encoding); + let mut column_index_seq = ColumnIndexSequence::default(); + let encoding_options = EncodingOptions { + cache_bytes_per_column: *page_size, + max_page_bytes: test_cases.get_max_page_size(), + keep_original_array: true, + buffer_alignment: MIN_PAGE_BUFFER_ALIGNMENT, + }; + let encoder = create_test_field_encoder( + encoding_strategy.as_ref(), + &lance_field, + &mut column_index_seq, + &encoding_options, + ) + .unwrap(); + info!( + "Testing round trip encoding of data with test encoding {} and page size {}", + encoding, page_size + ); + check_round_trip_encoding_inner( + encoder, + &field, + data.clone(), + expected_override.clone(), + test_cases, + encoding, + ) + .await + } + } +} + +struct SimulatedWriter { + page_infos: Vec>, + encoded_data: BytesMut, +} + +impl SimulatedWriter { + fn new(num_columns: u32) -> Self { + let mut page_infos = Vec::with_capacity(num_columns as usize); + page_infos.resize_with(num_columns as usize, Default::default); + Self { + page_infos, + encoded_data: BytesMut::new(), + } + } + + fn write_buffer(&mut self, buffer: LanceBuffer) -> (u64, u64) { + let offset = self.encoded_data.len() as u64; + self.encoded_data.extend_from_slice(&buffer); + let size = self.encoded_data.len() as u64 - offset; + let pad_bytes = pad_bytes::(self.encoded_data.len()); + self.encoded_data.extend(std::iter::repeat_n(0, pad_bytes)); + (offset, size) + } + + fn write_lance_buffer(&mut self, buffer: LanceBuffer) { + self.encoded_data.extend_from_slice(&buffer); + let pad_bytes = pad_bytes::(self.encoded_data.len()); + self.encoded_data.extend(std::iter::repeat_n(0, pad_bytes)); + } + + fn write_page(&mut self, encoded_page: EncodedPage) { + trace!("Encoded page {:?}", encoded_page); + let page_buffers = encoded_page.data; + let page_encoding = encoded_page.description; + let buffer_offsets_and_sizes = page_buffers + .into_iter() + .map(|b| { + let (offset, size) = self.write_buffer(b); + trace!("Encoded buffer offset={} size={}", offset, size); + (offset, size) + }) + .collect::>(); + + let page_info = PageInfo { + num_rows: encoded_page.num_rows, + encoding: page_encoding, + buffer_offsets_and_sizes: Arc::from(buffer_offsets_and_sizes), + priority: encoded_page.row_number, + }; + + let col_idx = encoded_page.column_idx as usize; + self.page_infos[col_idx].push(page_info); + } + + fn new_external_buffers(&self) -> OutOfLineBuffers { + OutOfLineBuffers::new(self.encoded_data.len() as u64, MIN_PAGE_BUFFER_ALIGNMENT) + } +} + +/// This is the inner-most check function that actually runs the round trip and tests it +async fn check_round_trip_encoding_inner( + mut encoder: Box, + field: &Field, + data: Vec>, + expected_override: Option>, + test_cases: &TestCases, + encoding: TestEncoding, +) { + let mut writer = SimulatedWriter::new(encoder.num_columns()); + + let log_page = |encoded_page: &EncodedPage| { + debug!( + "Encoded page on column {} with {} rows and start row {} and buffer sizes [{}]", + encoded_page.column_idx, + encoded_page.num_rows, + encoded_page.row_number, + encoded_page + .data + .iter() + .map(|buf| buf.len().to_string()) + .collect::>() + .join(", ") + ); + }; + + let mut row_number = 0; + for arr in &data { + let mut external_buffers = writer.new_external_buffers(); + let repdef = RepDefBuilder::default(); + let num_rows = arr.len() as u64; + let encode_tasks = encoder + .maybe_encode( + arr.clone(), + &mut external_buffers, + repdef, + row_number, + num_rows, + ) + .unwrap(); + for buffer in external_buffers.take_buffers() { + writer.write_lance_buffer(buffer); + } + for encode_task in encode_tasks { + let encoded_page = encode_task.await.unwrap(); + log_page(&encoded_page); + + // For V2.1, verify encoding in the page if expected + if encoding.is_structural() + && let Some(ref expected) = test_cases.expected_encoding + { + verify_page_encoding(&encoded_page, expected, encoded_page.column_idx as usize) + .unwrap(); + } + + writer.write_page(encoded_page); + } + row_number += arr.len() as u64; + } + + let mut external_buffers = writer.new_external_buffers(); + let encode_tasks = encoder.flush(&mut external_buffers).unwrap(); + for buffer in external_buffers.take_buffers() { + writer.write_lance_buffer(buffer); + } + for task in encode_tasks { + let encoded_page = task.await.unwrap(); + log_page(&encoded_page); + + // For V2.1, verify encoding in the page if expected + if encoding.is_structural() + && let Some(ref expected) = test_cases.expected_encoding + { + verify_page_encoding(&encoded_page, expected, encoded_page.column_idx as usize) + .unwrap(); + } + + writer.write_page(encoded_page); + } + + let mut external_buffers = writer.new_external_buffers(); + let encoded_columns = encoder.finish(&mut external_buffers).await.unwrap(); + test_cases.verify_encoding(&encoded_columns, &encoding); + for buffer in external_buffers.take_buffers() { + writer.write_lance_buffer(buffer); + } + let mut column_infos = Vec::new(); + for (col_idx, encoded_column) in encoded_columns.into_iter().enumerate() { + // Keep track of pages for encoding verification + for page in encoded_column.final_pages { + writer.write_page(page); + } + + let col_buffer_off_and_size = encoded_column + .column_buffers + .into_iter() + .map(|b| writer.write_buffer(b)) + .collect::>(); + + let column_info = ColumnInfo::new( + col_idx as u32, + Arc::from(std::mem::take(&mut writer.page_infos[col_idx])), + col_buffer_off_and_size, + encoded_column.encoding, + ); + + column_infos.push(Arc::new(column_info)); + } + + let encoded_data = writer.encoded_data.freeze(); + + let scheduler = Arc::new(SimulatedScheduler::new(encoded_data)) as Arc; + + let num_rows = data.iter().map(|arr| arr.len() as u64).sum::(); + let concat_data = if test_cases.skip_validation { + None + } else if let Some(DataType::Struct(_)) = data.first().map(|datum| datum.data_type()) { + // TODO(tsaucer) When arrow upgrades to 56, remove this if statement + // This is due to a check for concat_struct in arrow-rs. See https://github.com/lance-format/lance/pull/4598 + let capacities = Capacities::Array(num_rows as usize); + let array_data: Vec<_> = data.iter().map(|a| a.to_data()).collect::>(); + let array_data = array_data.iter().collect(); + let mut mutable = MutableArrayData::with_capacities(array_data, false, capacities); + + for (i, a) in data.iter().enumerate() { + mutable.extend(i, 0, a.len()) + } + + Some(make_array(mutable.freeze())) + } else { + Some(concat(&data.iter().map(|arr| arr.as_ref()).collect::>()).unwrap()) + }; + + let expected_data = expected_override.clone().or_else(|| concat_data.clone()); + + let is_structural_encoding = encoding.is_structural(); + + let decode_field = if is_structural_encoding { + let mut lance_field = lance_core::datatypes::Field::try_from(field).unwrap(); + if lance_field.is_blob() && matches!(lance_field.data_type(), DataType::Struct(_)) { + lance_field.unloaded_mut(); + let mut arrow_field = ArrowField::from(&lance_field); + let mut metadata = arrow_field.metadata().clone(); + metadata.insert("lance-encoding:packed".to_string(), "true".to_string()); + arrow_field = arrow_field.with_metadata(metadata); + arrow_field + } else { + field.clone() + } + } else { + field.clone() + }; + + let schema = Schema::new(vec![decode_field]); + + debug!("Testing full decode"); + let scheduler_copy = scheduler.clone(); + test_decode( + num_rows, + test_cases.batch_size, + &schema, + &column_infos, + expected_data.clone(), + scheduler_copy.clone(), + is_structural_encoding, + |mut decode_scheduler, tx| { + async move { + decode_scheduler.schedule_range( + 0..num_rows, + &FilterExpression::no_filter(), + tx, + scheduler_copy, + ) + } + .boxed() + }, + ) + .await; + + // Test range scheduling + for range in &test_cases.ranges { + debug!("Testing decode of range {:?}", range); + let num_rows = range.end - range.start; + let expected = expected_data + .as_ref() + .map(|arr| arr.slice(range.start as usize, num_rows as usize)); + let scheduler = scheduler.clone(); + let range = range.clone(); + test_decode( + num_rows, + test_cases.batch_size, + &schema, + &column_infos, + expected, + scheduler.clone(), + is_structural_encoding, + |mut decode_scheduler, tx| { + async move { + decode_scheduler.schedule_range( + range, + &FilterExpression::no_filter(), + tx, + scheduler, + ) + } + .boxed() + }, + ) + .await; + } + + // Test take scheduling + for indices in &test_cases.indices { + if indices.len() == 1 { + debug!("Testing decode of index {}", indices[0]); + } else { + debug!( + "Testing decode of {} indices spread across range [{}..{}]", + indices.len(), + indices[0], + indices[indices.len() - 1] + ); + } + let num_rows = indices.len() as u64; + let indices_arr = UInt64Array::from(indices.clone()); + + // There is a bug in arrow_select::take::take that causes it to return empty arrays + // if the data type is an empty struct. This is a workaround for that. + let is_empty_struct = if let DataType::Struct(fields) = field.data_type() { + fields.is_empty() + } else { + false + }; + + let expected = if is_empty_struct { + Some(Arc::new(StructArray::new_empty_fields(indices_arr.len(), None)) as Arc) + } else { + concat_data.as_ref().map(|concat_data| { + arrow_select::take::take(&concat_data, &indices_arr, None).unwrap() + }) + }; + + let scheduler = scheduler.clone(); + let indices = indices.clone(); + test_decode( + num_rows, + test_cases.batch_size, + &schema, + &column_infos, + expected, + scheduler.clone(), + is_structural_encoding, + |mut decode_scheduler, tx| { + async move { + decode_scheduler.schedule_take( + &indices, + &FilterExpression::no_filter(), + tx, + scheduler, + ) + } + .boxed() + }, + ) + .await; + } +} + +const NUM_RANDOM_ROWS: u32 = 10000; + +/// Generates random data (parameterized by null rate, slicing, and # ingest batches) +/// and tests with that against default test cases. +/// +/// To test specific test cases use the +async fn check_round_trip_random( + encoder_factory: impl Fn(TestEncoding) -> Box, + field: Field, + array_generator_provider: Box, + test_cases: &TestCases, +) { + for null_rate in [None, Some(0.5), Some(1.0)] { + for use_slicing in [false, true] { + for encoding in test_cases.encodings() { + if null_rate != Some(1.0) && matches!(field.data_type(), DataType::Null) { + continue; + } + + let field = if null_rate.is_some() { + if !supports_nulls(field.data_type(), encoding) { + continue; + } + field.clone().with_nullable(true) + } else { + field.clone().with_nullable(false) + }; + + for num_ingest_batches in [1, 5, 10] { + let rows_per_batch = NUM_RANDOM_ROWS / num_ingest_batches; + let mut data = Vec::new(); + + // Test both ingesting one big array sliced into smaller arrays and smaller + // arrays independently generated. These behave slightly differently. For + // example, a list array sliced into smaller arrays will have arrays whose + // starting offset is not 0. + if use_slicing { + let mut generator = + gen_batch().anon_col(array_generator_provider.provide()); + if let Some(null_rate) = null_rate { + // The null generator is the only generator that already inserts nulls + // and attempting to do so again makes arrow-rs grumpy + if !matches!(field.data_type(), DataType::Null) { + generator.with_random_nulls(null_rate); + } + } + let all_data = generator + .into_batch_rows(RowCount::from(10000)) + .unwrap() + .column(0) + .clone(); + let mut offset = 0; + for _ in 0..num_ingest_batches { + data.push(all_data.slice(offset, rows_per_batch as usize)); + offset += rows_per_batch as usize; + } + } else { + for i in 0..num_ingest_batches { + let mut generator = gen_batch() + .with_seed(Seed::from(i as u64)) + .anon_col(array_generator_provider.provide()); + if let Some(null_rate) = null_rate { + // The null generator is the only generator that already inserts nulls + // and attempting to do so again makes arrow-rs grumpy + if !matches!(field.data_type(), DataType::Null) { + generator.with_random_nulls(null_rate); + } + } + let arr = generator + .into_batch_rows(RowCount::from(rows_per_batch as u64)) + .unwrap() + .column(0) + .clone(); + data.push(arr); + } + } + + info!( + "Testing encoding {} with {} rows divided across {} batches for {} rows per batch with null_rate={:?} and use_slicing={}", + encoding, + NUM_RANDOM_ROWS, + num_ingest_batches, + rows_per_batch, + null_rate, + use_slicing + ); + check_round_trip_encoding_inner( + encoder_factory(encoding), + &field, + data, + None, + test_cases, + encoding, + ) + .await + } + } + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/utils.rs b/lance-artifact/rust/lance-encoding/src/utils.rs new file mode 100644 index 000000000..4163277d2 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/utils.rs @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Miscellaneous utility functions that don't have a home elsewhere. + +pub mod accumulation; +pub mod bytepack; diff --git a/lance-artifact/rust/lance-encoding/src/utils/accumulation.rs b/lance-artifact/rust/lance-encoding/src/utils/accumulation.rs new file mode 100644 index 000000000..f6255d12e --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/utils/accumulation.rs @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! An accumulation queue accumulates arrays until we have enough data to flush. + +use arrow_array::ArrayRef; +use lance_arrow::deepcopy::deep_copy_array; +use log::{debug, trace}; + +#[derive(Debug)] +pub struct AccumulationQueue { + cache_bytes: u64, + keep_original_array: bool, + buffered_arrays: Vec, + current_bytes: u64, + // Row number of the first item in buffered_arrays, reset on flush + row_number: u64, + // Number of top level rows represented in buffered_arrays, reset on flush + num_rows: u64, + // This is only for logging / debugging purposes + column_index: u32, +} + +impl AccumulationQueue { + pub fn new(cache_bytes: u64, column_index: u32, keep_original_array: bool) -> Self { + Self { + cache_bytes, + buffered_arrays: Vec::new(), + current_bytes: 0, + column_index, + keep_original_array, + row_number: u64::MAX, + num_rows: 0, + } + } + + /// Adds an array to the queue, if there is enough data then the queue is flushed + /// and returned + pub fn insert( + &mut self, + array: ArrayRef, + row_number: u64, + num_rows: u64, + ) -> Option<(Vec, u64, u64)> { + if self.row_number == u64::MAX { + self.row_number = row_number; + } + self.num_rows += num_rows; + self.current_bytes += array.get_array_memory_size() as u64; + if self.current_bytes > self.cache_bytes { + debug!( + "Flushing column {} page of size {} bytes (unencoded)", + self.column_index, self.current_bytes + ); + // Push into buffered_arrays without copy since we are about to flush anyways + self.buffered_arrays.push(array); + self.current_bytes = 0; + let row_number = self.row_number; + self.row_number = u64::MAX; + let num_rows = self.num_rows; + self.num_rows = 0; + Some(( + std::mem::take(&mut self.buffered_arrays), + row_number, + num_rows, + )) + } else { + trace!( + "Accumulating data for column {}. Now at {} bytes", + self.column_index, self.current_bytes + ); + if self.keep_original_array { + self.buffered_arrays.push(array); + } else { + self.buffered_arrays.push(deep_copy_array(array.as_ref())) + } + None + } + } + + pub fn flush(&mut self) -> Option<(Vec, u64, u64)> { + if self.buffered_arrays.is_empty() { + trace!( + "No final flush since no data at column {}", + self.column_index + ); + None + } else { + trace!( + "Final flush of column {} which has {} bytes", + self.column_index, self.current_bytes + ); + self.current_bytes = 0; + let row_number = self.row_number; + self.row_number = u64::MAX; + let num_rows = self.num_rows; + self.num_rows = 0; + Some(( + std::mem::take(&mut self.buffered_arrays), + row_number, + num_rows, + )) + } + } +} diff --git a/lance-artifact/rust/lance-encoding/src/utils/bytepack.rs b/lance-artifact/rust/lance-encoding/src/utils/bytepack.rs new file mode 100644 index 000000000..1b2c805b5 --- /dev/null +++ b/lance-artifact/rust/lance-encoding/src/utils/bytepack.rs @@ -0,0 +1,263 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +//! Utilities for byte (not bit) packing for situations where saving a few +//! bits is less important than simplicity and speed. + +pub struct U8BytePacker { + data: Vec, +} + +impl U8BytePacker { + fn with_capacity(capacity: usize) -> Self { + Self { + data: Vec::with_capacity(capacity), + } + } + + fn append(&mut self, value: u64) { + self.data.push(value as u8); + } +} + +pub struct U16BytePacker { + data: Vec, +} + +impl U16BytePacker { + fn with_capacity(capacity: usize) -> Self { + Self { + data: Vec::with_capacity(capacity * 2), + } + } + + fn append(&mut self, value: u64) { + self.data.extend_from_slice(&(value as u16).to_le_bytes()); + } +} + +pub struct U32BytePacker { + data: Vec, +} + +impl U32BytePacker { + fn with_capacity(capacity: usize) -> Self { + Self { + data: Vec::with_capacity(capacity * 4), + } + } + + fn append(&mut self, value: u64) { + self.data.extend_from_slice(&(value as u32).to_le_bytes()); + } +} + +pub struct U64BytePacker { + data: Vec, +} + +impl U64BytePacker { + fn with_capacity(capacity: usize) -> Self { + Self { + data: Vec::with_capacity(capacity * 8), + } + } + + fn append(&mut self, value: u64) { + self.data.extend_from_slice(&value.to_le_bytes()); + } +} + +/// A bytepacked integer encoder that automatically chooses the smallest +/// possible integer type to store the given values. +/// +/// This is byte packing (not bit packing). Not even that, we only fit things into +/// sizes of 1,2,4,8 bytes. It's simple, fast, and easy but doesn't provide the +/// maximum possible compression. +/// +/// Still, it's useful for things like offsets which are often small and fit into a +/// u16 or u32 but sometimes might need the full u64 range. +/// +/// In the future we can investigate replacing this with something more sophisticated. +pub enum BytepackedIntegerEncoder { + U8(U8BytePacker), + U16(U16BytePacker), + U32(U32BytePacker), + U64(U64BytePacker), + Zero, +} + +impl BytepackedIntegerEncoder { + /// Create a new encoder with the given capacity and maximum value. + pub fn with_capacity(capacity: usize, max_value: u64) -> Self { + if max_value == 0 { + Self::Zero + } else if max_value <= u8::MAX as u64 { + Self::U8(U8BytePacker::with_capacity(capacity)) + } else if max_value <= u16::MAX as u64 { + Self::U16(U16BytePacker::with_capacity(capacity)) + } else if max_value <= u32::MAX as u64 { + Self::U32(U32BytePacker::with_capacity(capacity)) + } else { + Self::U64(U64BytePacker::with_capacity(capacity)) + } + } + + /// Append a value to the encoder. + /// + /// # Safety + /// + /// This function is unsafe because it doesn't check for overflow. If the + /// value is too large to fit in the chosen integer type, it will be silently + /// truncated. + pub unsafe fn append(&mut self, value: u64) { + match self { + Self::U8(packer) => packer.append(value), + Self::U16(packer) => packer.append(value), + Self::U32(packer) => packer.append(value), + Self::U64(packer) => packer.append(value), + Self::Zero => {} + } + } + + /// Convert the encoder into a vector of bytes. + pub fn into_data(self) -> Vec { + match self { + Self::U8(packer) => packer.data, + Self::U16(packer) => packer.data, + Self::U32(packer) => packer.data, + Self::U64(packer) => packer.data, + Self::Zero => Vec::new(), + } + } +} + +/// An iterator that unpacks bytes into integers (currently only u64) +pub enum ByteUnpacker> { + U8(I), + U16(I), + U32(I), + U64(I), +} + +impl> ByteUnpacker { + #[allow(clippy::new_ret_no_self)] + pub fn new>(data: I, size: usize) -> impl Iterator { + match size { + 1 => Self::U8(data.into_iter()), + 2 => Self::U16(data.into_iter()), + 4 => Self::U32(data.into_iter()), + 8 => Self::U64(data.into_iter()), + _ => panic!("Invalid size"), + } + } +} + +impl> Iterator for ByteUnpacker { + type Item = u64; + + fn next(&mut self) -> Option { + match self { + Self::U8(iter) => iter.next().map(|v| v as u64), + Self::U16(iter) => { + let first_byte = iter.next()?; + Some(u16::from_le_bytes([first_byte, iter.next().unwrap()]) as u64) + } + Self::U32(iter) => { + let first_byte = iter.next()?; + Some(u32::from_le_bytes([ + first_byte, + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + ]) as u64) + } + Self::U64(iter) => { + let first_byte = iter.next()?; + Some(u64::from_le_bytes([ + first_byte, + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + ])) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_bytepacked_integer_encoder() { + // Fits in u8 + let mut encoder = BytepackedIntegerEncoder::with_capacity(10, 100); + unsafe { + encoder.append(50); + encoder.append(20); + encoder.append(30); + } + let data = encoder.into_data(); + assert_eq!(data, vec![50, 20, 30]); + + assert_eq!( + ByteUnpacker::new(data, 1).collect::>(), + vec![50, 20, 30] + ); + + // Requires u16 + let mut encoder = BytepackedIntegerEncoder::with_capacity(10, 1000); + unsafe { + encoder.append(500); + encoder.append(200); + encoder.append(300); + } + let data = encoder.into_data(); + assert_eq!(data, vec![244, 1, 200, 0, 44, 1]); + + assert_eq!( + ByteUnpacker::new(data, 2).collect::>(), + vec![500, 200, 300] + ); + + // Requires u32 + let mut encoder = BytepackedIntegerEncoder::with_capacity(10, 1000000); + unsafe { + encoder.append(500000); + encoder.append(200000); + encoder.append(300000); + } + let data = encoder.into_data(); + assert_eq!(data, vec![32, 161, 7, 0, 64, 13, 3, 0, 224, 147, 4, 0]); + + assert_eq!( + ByteUnpacker::new(data, 4).collect::>(), + vec![500000, 200000, 300000] + ); + + // Requires u64 + let mut encoder = BytepackedIntegerEncoder::with_capacity(10, 0x10000000000); + unsafe { + encoder.append(0x5000000000); + encoder.append(0x2000000000); + encoder.append(0x3000000000); + } + let data = encoder.into_data(); + assert_eq!( + data, + vec![ + 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0 + ] + ); + + assert_eq!( + ByteUnpacker::new(data, 8).collect::>(), + vec![0x5000000000, 0x2000000000, 0x3000000000] + ); + } +} diff --git a/lance-artifact/rust/lance-file/Cargo.toml b/lance-artifact/rust/lance-file/Cargo.toml new file mode 100644 index 000000000..f9e98f60c --- /dev/null +++ b/lance-artifact/rust/lance-file/Cargo.toml @@ -0,0 +1,71 @@ +[package] +name = "lance-file" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +readme = "README.md" +description = "Utilities for the Lance file format" +keywords.workspace = true +categories.workspace = true +rust-version.workspace = true + +[dependencies] +lance-arrow.workspace = true +lance-core.workspace = true +lance-encoding.workspace = true +lance-io.workspace = true +arrow-arith.workspace = true +arrow-array.workspace = true +arrow-buffer.workspace = true +arrow-cast.workspace = true +arrow-data.workspace = true +arrow-schema.workspace = true +arrow-select.workspace = true +async-recursion.workspace = true +async-trait.workspace = true +byteorder.workspace = true +bytes.workspace = true +datafusion-common.workspace = true +futures.workspace = true +log.workspace = true +num-traits.workspace = true +object_store.workspace = true +prost.workspace = true +prost-types.workspace = true +tokio.workspace = true +tracing.workspace = true + +[dev-dependencies] +lance-datagen.workspace = true +lance-testing.workspace = true +criterion.workspace = true +rstest.workspace = true +proptest.workspace = true +pretty_assertions.workspace = true +rand.workspace = true +test-log.workspace = true +libc.workspace = true + +[build-dependencies] +prost-build.workspace = true +protobuf-src = { version = "2.1", optional = true } + +[features] +protoc = ["dep:protobuf-src"] + +[package.metadata.docs.rs] +# docs.rs uses an older version of Ubuntu that does not have the necessary protoc version +features = ["protoc"] + +[[bench]] +name = "reader" +harness = false + +[[bench]] +name = "schema" +harness = false + +[lints] +workspace = true diff --git a/lance-artifact/rust/lance-file/README.md b/lance-artifact/rust/lance-file/README.md new file mode 100644 index 000000000..a63f2d164 --- /dev/null +++ b/lance-artifact/rust/lance-file/README.md @@ -0,0 +1,6 @@ +# lance-file + +`lance-file` is an internal sub-crate, containing readers and writers for the +[Lance file format](https://lance.org/format/file/). + +**Important Note**: This crate is **not intended for external usage**. diff --git a/lance-artifact/rust/lance-file/benches/reader.rs b/lance-artifact/rust/lance-file/benches/reader.rs new file mode 100644 index 000000000..5d280f00d --- /dev/null +++ b/lance-artifact/rust/lance-file/benches/reader.rs @@ -0,0 +1,477 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors +use std::sync::{Arc, Mutex}; + +use arrow_array::{UInt32Array, cast::AsArray, types::Int32Type}; +use arrow_schema::DataType; +use std::hint::black_box; + +use criterion::{BatchSize, Criterion, Throughput, criterion_group, criterion_main}; +use futures::{FutureExt, StreamExt}; +use lance_core::utils::{tempfile::TempDir, tokio::get_num_compute_intensive_cpus}; +use lance_datagen::ArrayGeneratorExt; +use lance_encoding::decoder::{DecoderConfig, DecoderPlugins, FilterExpression}; +use lance_file::{ + reader::{FileReader, FileReaderOptions}, + testing::test_cache, + version::ConcreteFileVersion, + versions as file_versions, + writer::FileWriterOptions, +}; +use lance_io::{ + object_store::ObjectStore, + scheduler::{ScanScheduler, SchedulerConfig}, + utils::CachedFileSize, +}; +use object_store::path::Path; +use std::collections::HashMap; +use tokio::runtime::Runtime; + +fn bench_reader(c: &mut Criterion) { + for version in [ + ConcreteFileVersion::V2_0, + ConcreteFileVersion::V2_1, + ConcreteFileVersion::V2_2, + ] { + let mut group = c.benchmark_group(format!("reader_{}", version)); + let data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(&DataType::Int32)) + .into_batch_rows(lance_datagen::RowCount::from(2 * 1024 * 1024)) + .unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); + + let tmpdir = TempDir::default(); + let (object_store, base_path) = rt + .block_on(ObjectStore::from_uri(&tmpdir.path_str())) + .unwrap(); + + let file_path = base_path.clone().join("foo.lance"); + let object_writer = rt.block_on(object_store.create(&file_path)).unwrap(); + + let mut writer = file_versions::create_writer( + version, + object_writer, + data.schema().as_ref().try_into().unwrap(), + FileWriterOptions::default(), + ) + .unwrap(); + rt.block_on(writer.write_batch(&data)).unwrap(); + rt.block_on(writer.finish()).unwrap(); + group.throughput(criterion::Throughput::Bytes( + data.get_array_memory_size() as u64 + )); + group.bench_function("decode", |b| { + b.iter(|| { + let object_store = &object_store; + let file_path = &file_path; + let data = &data; + rt.block_on(async move { + let store_scheduler = ScanScheduler::new( + object_store.clone(), + SchedulerConfig::default_for_testing(), + ); + let scheduler = store_scheduler + .open_file(file_path, &CachedFileSize::unknown()) + .await + .unwrap(); + let reader = FileReader::try_open( + scheduler.clone(), + None, + Arc::::default(), + &test_cache(), + FileReaderOptions::default(), + ) + .await + .unwrap(); + let stream = reader + .read_tasks( + lance_io::ReadBatchParams::RangeFull, + 16 * 1024, + None, + FilterExpression::no_filter(), + ) + .await + .unwrap(); + let stats = Arc::new(Mutex::new((0, 0))); + let mut stream = stream + .map(|batch_task| { + let stats = stats.clone(); + async move { + let batch = batch_task.task.await.unwrap(); + let row_count = batch.num_rows(); + let sum = batch + .column(0) + .as_primitive::() + .values() + .iter() + .map(|v| *v as i64) + .sum::(); + let mut stats = stats.lock().unwrap(); + stats.0 += row_count; + stats.1 += sum; + } + .boxed() + }) + .buffer_unordered(16); + while (stream.next().await).is_some() {} + let stats = stats.lock().unwrap(); + let row_count = stats.0; + let sum = stats.1; + assert_eq!(data.num_rows(), row_count); + black_box(sum); + }); + }) + }); + } +} + +#[cfg(not(target_os = "linux"))] +pub fn drop_file_from_cache(_path: impl AsRef) -> std::io::Result<()> { + Ok(()) +} + +#[cfg(target_os = "linux")] +pub fn drop_file_from_cache(path: impl AsRef) -> std::io::Result<()> { + use std::os::unix::io::AsRawFd; + + let file = std::fs::File::open(path.as_ref())?; + let fd = file.as_raw_fd(); + + // POSIX_FADV_DONTNEED = 4 + // This tells the kernel to drop the file from the page cache + let result = unsafe { libc::posix_fadvise(fd, 0, 0, libc::POSIX_FADV_DONTNEED) }; + + if result != 0 { + return Err(std::io::Error::from_raw_os_error(result)); + } + + Ok(()) +} + +const MAX_PARALLELISM: usize = 64; +// Need at least 5K rows between indices to spread data across disk pages +const ROW_GAP: usize = 1024 * 5; +const TOTAL_ROWS: usize = 100_000; + +struct CachedReader { + reader: Arc, + indices: UInt32Array, + runtime: Arc, +} + +struct CachedReaders { + all_indices: UInt32Array, + readers: Vec, +} + +type FileCache = HashMap<(String, String), Arc>; + +/// Get or create a lance file for benchmarking. +/// +/// This function caches the results so files are only created once per (filesystem, version) combination. +/// The version and filesystem are encoded in the filename to avoid collisions. +fn get_cached_readers( + tmpdir: &TempDir, + filesystem: &str, + rt: &Runtime, + version: ConcreteFileVersion, +) -> Arc { + use std::sync::{LazyLock, Mutex}; + + static FILE_CACHE: LazyLock> = LazyLock::new(|| Mutex::new(HashMap::new())); + + let key = (filesystem.to_string(), version.to_string()); + + // Check cache first + { + let cache = FILE_CACHE.lock().unwrap(); + if let Some(cached) = cache.get(&key) { + return cached.clone(); + } + } + + let num_threads = get_num_compute_intensive_cpus(); + + // Create object store + let (object_store, base_path) = if filesystem == "mem" { + rt.block_on(ObjectStore::from_uri("memory://")).unwrap() + } else { + rt.block_on(ObjectStore::from_uri(&tmpdir.path_str())) + .unwrap() + }; + + // Create filename with version to avoid collisions + let filename = format!("bench_{}.lance", version); + let file_path = base_path.join(filename.as_str()); + + // Generate data + let data = lance_datagen::gen_batch() + .anon_col(lance_datagen::array::rand_type(&DataType::Int32).with_random_nulls(0.1)) + .into_batch_rows(lance_datagen::RowCount::from(500 * 1024 * 1024)) + .unwrap(); + + // Write file + let object_writer = rt.block_on(object_store.create(&file_path)).unwrap(); + let mut writer = file_versions::create_writer( + version, + object_writer, + data.schema().as_ref().try_into().unwrap(), + FileWriterOptions::default(), + ) + .unwrap(); + rt.block_on(writer.write_batch(&data)).unwrap(); + rt.block_on(writer.finish()).unwrap(); + + let indices = (0..TOTAL_ROWS as u32) + .map(|i| i * ROW_GAP as u32) + .collect::>(); + let all_indices = UInt32Array::from(indices); + + let rows_per_thread = TOTAL_ROWS / num_threads; + + let mut readers = Vec::with_capacity(num_threads); + for i in 0..num_threads { + let indices = all_indices.slice(i * rows_per_thread, rows_per_thread); + let runtime = Arc::new( + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(), + ); + let reader = open_reader(&runtime, &object_store, &file_path); + // Warm up reader + read_task( + &runtime, + reader.clone(), + indices.clone(), + /*rows_at_a_time=*/ 100, + ); + readers.push(CachedReader { + reader, + indices, + runtime, + }); + } + + let cached_readers = Arc::new(CachedReaders { + all_indices, + readers, + }); + + let mut cache = FILE_CACHE.lock().unwrap(); + cache.insert(key, cached_readers.clone()); + cached_readers +} + +fn open_reader(rt: &Runtime, object_store: &Arc, file_path: &Path) -> Arc { + rt.block_on(async { + let store_scheduler = + ScanScheduler::new(object_store.clone(), SchedulerConfig::default_for_testing()); + let scheduler = store_scheduler + .open_file(file_path, &CachedFileSize::unknown()) + .await + .unwrap(); + Arc::new( + FileReader::try_open( + scheduler.clone(), + None, + Arc::::default(), + &test_cache(), + FileReaderOptions { + decoder_config: DecoderConfig { + ..Default::default() + }, + ..Default::default() + }, + ) + .await + .unwrap(), + ) + }) +} + +fn read_task( + runtime: &Runtime, + reader: Arc, + indices: UInt32Array, + rows_at_a_time: usize, +) { + let num_rows = indices.len(); + + let read_batch = |reader: Arc, indices: UInt32Array| async move { + let stream = reader + .read_tasks( + lance_io::ReadBatchParams::Indices(indices), + rows_at_a_time as u32, + None, + FilterExpression::no_filter(), + ) + .await + .unwrap(); + let stats = Arc::new(Mutex::new((0, 0))); + let mut stream = stream.then(|batch_task| { + let stats = stats.clone(); + async move { + let batch = batch_task.task.await.unwrap(); + let row_count = batch.num_rows(); + let sum = batch + .column(0) + .as_primitive::() + .values() + .iter() + .map(|v| *v as i64) + .sum::(); + let mut stats = stats.lock().unwrap(); + stats.0 += row_count; + stats.1 += sum; + } + .boxed() + }); + while (stream.next().await).is_some() {} + let stats = stats.lock().unwrap(); + let row_count = stats.0; + let sum = stats.1; + assert_eq!(rows_at_a_time, row_count); + black_box(sum); + }; + + runtime.block_on(async move { + futures::stream::iter(0..num_rows / rows_at_a_time) + .map(|i| { + let reader = reader.clone(); + let indices = indices.clone(); + async move { + let reader = reader.clone(); + let indices = indices.slice(i * rows_at_a_time, rows_at_a_time); + read_batch(reader, indices).await; + } + }) + .buffer_unordered(MAX_PARALLELISM) + .collect::>() + .await; + }); +} + +fn bench_random_access(c: &mut Criterion) { + let filesystems = ["mem", "disk"]; + + let global_runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + + let tmpdir = TempDir::default(); + + let mut group = c.benchmark_group("take"); + + let versions = [ + ConcreteFileVersion::V2_0, + ConcreteFileVersion::V2_1, + ConcreteFileVersion::V2_2, + ]; + + for filesystem in filesystems { + for version in versions { + // Get or create the file (cached) + let cached_readers = get_cached_readers(&tmpdir, filesystem, &global_runtime, version); + + for multithreaded in [false, true] { + for rows_at_a_time in [1, 100] { + for cached in [true, false] { + if !cached && (filesystem == "mem" || version == ConcreteFileVersion::V2_0) + { + continue; + } + + let num_threads = if multithreaded { + get_num_compute_intensive_cpus() + } else { + 1 + }; + let rows_per_thread = TOTAL_ROWS / num_threads; + group.throughput(Throughput::Elements( + rows_per_thread as u64 * num_threads as u64, + )); + + group.bench_function( + format!( + "{}_{}_{}thread_{}_{}", + filesystem, + version, + num_threads, + rows_at_a_time, + if cached { "cached" } else { "nocache" }, + ), + |b| { + b.iter_batched( + || { + if !cached { + let filename = tmpdir + .std_path() + .join(format!("bench_{}.lance", version)); + drop_file_from_cache(tmpdir.std_path().join(&filename)) + .unwrap(); + } + }, + |_| { + let cached_readers = cached_readers.clone(); + global_runtime.block_on(async move { + let mut handles = Vec::with_capacity(num_threads); + if multithreaded { + for reader in &cached_readers.readers { + let runtime = reader.runtime.clone(); + let indices = reader.indices.clone(); + let reader = reader.reader.clone(); + handles.push(tokio::task::spawn_blocking( + move || { + read_task( + &runtime, + reader, + indices, + rows_at_a_time, + ); + }, + )); + } + for handle in handles { + handle.await.unwrap(); + } + } else { + tokio::task::spawn_blocking(move || { + read_task( + &cached_readers.readers[0].runtime, + cached_readers.readers[0].reader.clone(), + cached_readers.all_indices.clone(), + rows_at_a_time, + ) + }) + .await + .unwrap(); + } + }); + }, + // We have at least 0.1 seconds of work per iteration so don't need to worry about + // overhead of BatchSize::PerIteration + BatchSize::PerIteration, + ); + }, + ); + } + } + } + } + } +} + +#[cfg(target_os = "linux")] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10) + .with_profiler(lance_testing::pprof::PProfProfiler::new(100, lance_testing::pprof::Output::Flamegraph(None))); + targets = bench_reader, bench_random_access); + +// Non-linux version does not support pprof. +#[cfg(not(target_os = "linux"))] +criterion_group!( + name=benches; + config = Criterion::default().significance_level(0.1).sample_size(10); + targets = bench_reader, bench_random_access); +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-file/benches/schema.rs b/lance-artifact/rust/lance-file/benches/schema.rs new file mode 100644 index 000000000..b8da23d97 --- /dev/null +++ b/lance-artifact/rust/lance-file/benches/schema.rs @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::hint::black_box; + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use lance_core::datatypes::Schema; +use lance_file::{datatypes::Fields, format::pb}; + +fn proto_field(id: i32, parent_id: i32, name: String, logical_type: &str) -> pb::Field { + pb::Field { + id, + parent_id, + name, + logical_type: logical_type.to_owned(), + ..Default::default() + } +} + +/// Builds a pre-order flat schema with `num_physical_columns` physical leaves. +/// +/// Each struct contributes one parent and two `int32` leaves. Root fields use +/// `-1` as `parent_id`, and each struct consumes a three-ID block. +fn wide_two_leaf_structs(num_physical_columns: usize) -> Fields { + assert_eq!(num_physical_columns % 2, 0); + let num_structs = num_physical_columns / 2; + let mut fields = Vec::with_capacity(num_structs + num_physical_columns); + + for struct_index in 0..num_structs { + let parent_id = (struct_index * 3) as i32; + fields.push(proto_field( + parent_id, + -1, + format!("struct_{struct_index}"), + "struct", + )); + fields.push(proto_field( + parent_id + 1, + parent_id, + format!("left_{struct_index}"), + "int32", + )); + fields.push(proto_field( + parent_id + 2, + parent_id, + format!("right_{struct_index}"), + "int32", + )); + } + + Fields(fields) +} + +fn bench_schema_reconstruction(c: &mut Criterion) { + let mut group = c.benchmark_group("schema_from_flat_fields"); + + for num_physical_columns in [1024, 4096, 16_384, 65_536] { + let fields = wide_two_leaf_structs(num_physical_columns); + group.throughput(Throughput::Elements(fields.0.len() as u64)); + group.bench_with_input( + BenchmarkId::new("physical_columns", num_physical_columns), + &fields, + |bencher, fields| { + bencher.iter(|| Schema::try_from(black_box(fields)).unwrap()); + }, + ); + } + + group.finish(); +} + +criterion_group!(benches, bench_schema_reconstruction); +criterion_main!(benches); diff --git a/lance-artifact/rust/lance-file/build.rs b/lance-artifact/rust/lance-file/build.rs new file mode 100644 index 000000000..70ccdc250 --- /dev/null +++ b/lance-artifact/rust/lance-file/build.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::io::Result; + +fn main() -> Result<()> { + println!("cargo:rerun-if-changed=protos"); + + #[cfg(feature = "protoc")] + // Use vendored protobuf compiler if requested. + unsafe { + std::env::set_var("PROTOC", protobuf_src::protoc()); + } + + let mut prost_build = prost_build::Config::new(); + prost_build.protoc_arg("--experimental_allow_proto3_optional"); + prost_build.extern_path(".lance.encodings", "::lance_encoding::format::pb"); + prost_build.compile_protos( + &[ + "./protos/file.proto", + "./protos/file2.proto", + "./protos/encodings_v2_0.proto", + "./protos/encodings_v2_1.proto", + ], + &["./protos"], + )?; + + Ok(()) +} diff --git a/lance-artifact/rust/lance-file/protos b/lance-artifact/rust/lance-file/protos new file mode 120000 index 000000000..3d021e597 --- /dev/null +++ b/lance-artifact/rust/lance-file/protos @@ -0,0 +1 @@ +../../protos/ \ No newline at end of file diff --git a/lance-artifact/rust/lance-file/src/compatibility_tests.rs b/lance-artifact/rust/lance-file/src/compatibility_tests.rs new file mode 100644 index 000000000..c63137ec6 --- /dev/null +++ b/lance-artifact/rust/lance-file/src/compatibility_tests.rs @@ -0,0 +1,524 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::collections::HashMap; +use std::sync::Arc; + +use arrow_array::builder::StringDictionaryBuilder; +use arrow_array::cast::AsArray; +use arrow_array::types::{Int8Type, Int32Type}; +use arrow_array::{ + Array, ArrayRef, Int32Array, LargeBinaryArray, ListArray, RecordBatch, StringArray, +}; +use arrow_schema::{DataType, Field, Schema as ArrowSchema}; +use bytes::Bytes; +use futures::TryStreamExt; +use lance_core::cache::LanceCache; +use lance_core::datatypes::Schema as LanceSchema; +use lance_encoding::decoder::{DecoderPlugins, EncodedBatchLayout, FilterExpression, decode_batch}; +use lance_encoding::encoder::{EncodedBatch, EncodingOptions, encode_batch}; +use lance_io::ReadBatchParams; +use lance_io::traits::Writer; +use lance_io::utils::CachedFileSize; +use rstest::rstest; +use tokio::io::AsyncWriteExt; + +use crate::reader::{EncodedBatchReaderExt, FileReader, FileReaderOptions}; +use crate::testing::FsFixture; +use crate::version::ConcreteFileVersion; +use crate::versions; +use crate::versions::v1::reader::FileReader as V1Reader; +use crate::versions::v1::writer::{ + FileWriter as V1Writer, FileWriterOptions as V1WriterOptions, NotSelfDescribing, +}; +use crate::writer::FileWriterOptions; + +fn compatibility_fixture_batch() -> RecordBatch { + let row_count = 4097; + let ids = Arc::new(Int32Array::from_iter_values(0..row_count)) as ArrayRef; + let names = Arc::new(StringArray::from_iter((0..row_count).map(|index| { + (index % 7 != 0).then(|| format!("value-{index:04}-deterministic-fixture")) + }))) as ArrayRef; + let items = Arc::new(ListArray::from_iter_primitive::( + (0..row_count).map(|index| { + (index % 11 != 0).then(|| { + vec![ + Some(index), + (index % 5 != 0).then_some(index * 2), + Some(index * 3), + ] + }) + }), + )) as ArrayRef; + let mut categories = StringDictionaryBuilder::::new(); + for index in 0..row_count { + if index % 13 == 0 { + categories.append_null(); + } else { + categories + .append(match index % 3 { + 0 => "red", + 1 => "green", + _ => "blue", + }) + .unwrap(); + } + } + let categories = Arc::new(categories.finish()) as ArrayRef; + let blobs = Arc::new(LargeBinaryArray::from_iter_values( + (0..row_count).map(|index| format!("blob-{index:04}-deterministic-payload").into_bytes()), + )) as ArrayRef; + + let schema = Arc::new(ArrowSchema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new("name", DataType::Utf8, true).with_metadata(HashMap::from([( + "lance-encoding:compression".to_string(), + "none".to_string(), + )])), + Field::new( + "items", + DataType::List(Arc::new(Field::new("item", DataType::Int32, true))), + true, + ), + Field::new( + "category", + DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)), + true, + ) + .with_metadata(HashMap::from([( + "lance-encoding:dict-values-compression".to_string(), + "none".to_string(), + )])), + Field::new("blob", DataType::LargeBinary, true).with_metadata(HashMap::from([( + "lance-encoding:blob".to_string(), + "true".to_string(), + )])), + ])); + RecordBatch::try_new(schema, vec![ids, names, items, categories, blobs]).unwrap() +} + +fn v1_reader_expected_batch(batch: &RecordBatch) -> RecordBatch { + // The V1 reader historically materializes null lists as empty, null child integers as zero, + // and null dictionary keys as the first dictionary value. + let items = Arc::new(ListArray::from_iter_primitive::( + (0..batch.num_rows() as i32).map(|index| { + Some(if index % 11 == 0 { + Vec::new() + } else { + vec![ + Some(index), + Some(if index % 5 == 0 { 0 } else { index * 2 }), + Some(index * 3), + ] + }) + }), + )) as ArrayRef; + let mut categories = StringDictionaryBuilder::::new(); + for index in 0..batch.num_rows() { + categories + .append(if index % 13 == 0 { + "green" + } else { + match index % 3 { + 0 => "red", + 1 => "green", + _ => "blue", + } + }) + .unwrap(); + } + let categories = Arc::new(categories.finish()) as ArrayRef; + let mut columns = batch.columns().to_vec(); + columns[2] = items; + columns[3] = categories; + RecordBatch::try_new(batch.schema(), columns).unwrap() +} + +fn stable_fixture(version: ConcreteFileVersion) -> &'static [u8] { + match version { + ConcreteFileVersion::V1 => include_bytes!("../test_data/exact_versions/v1.lance"), + ConcreteFileVersion::V2_0 => { + include_bytes!("../test_data/exact_versions/v2_0.lance") + } + ConcreteFileVersion::V2_1 => { + include_bytes!("../test_data/exact_versions/v2_1.lance") + } + ConcreteFileVersion::V2_2 => { + include_bytes!("../test_data/exact_versions/v2_2.lance") + } + ConcreteFileVersion::V2_3 => { + unreachable!("v2.3 is unstable and has no compatibility fixture") + } + } +} + +fn assert_blob_column_eq(actual: &dyn Array, expected: &dyn Array) { + let actual = actual.as_binary::(); + let expected = expected.as_binary::(); + assert_eq!(actual.len(), expected.len()); + for index in 0..actual.len() { + assert_eq!( + actual.is_null(index), + expected.is_null(index), + "blob validity differs at row {index}" + ); + if actual.is_valid(index) { + assert_eq!( + actual.value(index), + expected.value(index), + "blob payload differs at row {index}" + ); + } + } +} + +fn assert_record_batch_eq(actual: &RecordBatch, expected: &RecordBatch) { + assert_eq!(actual.schema_ref(), expected.schema_ref()); + assert_eq!(actual.num_rows(), expected.num_rows()); + assert_eq!(actual.num_columns(), expected.num_columns()); + + for column_index in 0..actual.num_columns() { + if expected.schema().field(column_index).name() == "blob" { + assert_blob_column_eq( + actual.column(column_index).as_ref(), + expected.column(column_index).as_ref(), + ); + } else if actual.column(column_index).to_data() != expected.column(column_index).to_data() { + let row_index = (0..actual.num_rows()) + .find(|row_index| { + actual.column(column_index).slice(*row_index, 1).to_data() + != expected.column(column_index).slice(*row_index, 1).to_data() + }) + .unwrap(); + panic!( + "column {} ({}) differs at row {}: actual={:?}, expected={:?}", + column_index, + expected.schema().field(column_index).name(), + row_index, + actual.column(column_index).slice(row_index, 1), + expected.column(column_index).slice(row_index, 1) + ); + } + } +} + +fn footer_version(bytes: &[u8]) -> (u16, u16) { + let version_start = bytes.len() - 8; + ( + u16::from_le_bytes([bytes[version_start], bytes[version_start + 1]]), + u16::from_le_bytes([bytes[version_start + 2], bytes[version_start + 3]]), + ) +} + +fn assert_wire_bytes_equal(actual: &[u8], expected: &[u8]) { + if let Some(offset) = actual + .iter() + .zip(expected) + .position(|(actual, expected)| actual != expected) + { + panic!( + "wire fixture first differs at byte {offset}: actual={}, expected={}", + actual[offset], expected[offset] + ); + } + assert_eq!( + actual.len(), + expected.len(), + "wire fixture length changed after a common {}-byte prefix", + actual.len().min(expected.len()) + ); +} + +async fn write_current_fixture( + version: ConcreteFileVersion, + batch: &RecordBatch, + schema: &LanceSchema, +) -> Vec { + let fs = FsFixture::default(); + let object_writer = fs.object_store.create(&fs.tmp_path).await.unwrap(); + let options = FileWriterOptions { + data_cache_bytes: Some(1), + max_page_bytes: Some(1024), + ..Default::default() + }; + let summary = match version { + ConcreteFileVersion::V1 => { + unreachable!("legacy fixtures use the legacy writer") + } + ConcreteFileVersion::V2_0 => { + let mut writer = + versions::v2_0::create_writer(object_writer, schema.clone(), options).unwrap(); + for offset in (0..batch.num_rows()).step_by(1024) { + let slice = batch.slice(offset, (batch.num_rows() - offset).min(1024)); + writer.write_batch(&slice).await.unwrap(); + } + writer.finish().await.unwrap() + } + ConcreteFileVersion::V2_1 => { + let mut writer = + versions::v2_1::create_writer(object_writer, schema.clone(), options).unwrap(); + for offset in (0..batch.num_rows()).step_by(1024) { + let slice = batch.slice(offset, (batch.num_rows() - offset).min(1024)); + writer.write_batch(&slice).await.unwrap(); + } + writer.finish().await.unwrap() + } + ConcreteFileVersion::V2_2 => { + let mut writer = + versions::v2_2::create_writer(object_writer, schema.clone(), options).unwrap(); + for offset in (0..batch.num_rows()).step_by(1024) { + let slice = batch.slice(offset, (batch.num_rows() - offset).min(1024)); + writer.write_batch(&slice).await.unwrap(); + } + writer.finish().await.unwrap() + } + ConcreteFileVersion::V2_3 => { + let mut writer = + versions::v2_3::create_writer(object_writer, schema.clone(), options).unwrap(); + for offset in (0..batch.num_rows()).step_by(1024) { + let slice = batch.slice(offset, (batch.num_rows() - offset).min(1024)); + writer.write_batch(&slice).await.unwrap(); + } + writer.finish().await.unwrap() + } + }; + fs.object_store + .open(&fs.tmp_path) + .await + .unwrap() + .get_range(0..summary.size_bytes as usize) + .await + .unwrap() + .to_vec() +} + +async fn write_v2_0_embedded_fixtures(batch: &RecordBatch, schema: &LanceSchema) -> (Bytes, Bytes) { + let options = EncodingOptions { + cache_bytes_per_column: 1, + max_page_bytes: 1024, + keep_original_array: true, + buffer_alignment: 64, + }; + let encoding_strategy = crate::versions::v2_0::encoding_strategy(); + let encoded_batch = encode_batch( + batch, + Arc::new(schema.clone()), + encoding_strategy.as_ref(), + &options, + ) + .await + .unwrap(); + + ( + versions::v2_0::encode_self_described_batch(&encoded_batch).unwrap(), + versions::v2_0::encode_mini_batch(&encoded_batch).unwrap(), + ) +} + +async fn assert_current_reader_roundtrip( + fixture: &[u8], + version: ConcreteFileVersion, + expected: &RecordBatch, +) { + let fs = FsFixture::default(); + let mut fixture_writer = fs.object_store.create(&fs.tmp_path).await.unwrap(); + fixture_writer.write_all(fixture).await.unwrap(); + Writer::shutdown(fixture_writer.as_mut()).await.unwrap(); + let scheduler = fs + .scheduler + .open_file(&fs.tmp_path, &CachedFileSize::new(fixture.len() as u64)) + .await + .unwrap(); + let reader = FileReader::try_open( + scheduler, + None, + Arc::::default(), + &LanceCache::no_cache(), + FileReaderOptions::default(), + ) + .await + .unwrap(); + assert_eq!(reader.metadata().version(), version); + assert!( + reader + .metadata() + .column_metadatas + .iter() + .any(|metadata| metadata.pages.len() > 1) + ); + let batches = reader + .read_stream( + ReadBatchParams::RangeFull, + 1024, + 16, + FilterExpression::no_filter(), + ) + .await + .unwrap() + .try_collect::>() + .await + .unwrap(); + assert_eq!( + batches.iter().map(RecordBatch::num_rows).sum::(), + expected.num_rows() + ); + assert!( + batches + .iter() + .all(|actual| actual.schema_ref() == expected.schema_ref()) + ); + let mut row_offset = 0; + for actual in &batches { + let expected = expected.slice(row_offset, actual.num_rows()); + assert_record_batch_eq(actual, &expected); + row_offset += actual.num_rows(); + } + assert_eq!(row_offset, expected.num_rows()); +} + +#[rstest] +#[case::v2_0(ConcreteFileVersion::V2_0)] +#[case::v2_1(ConcreteFileVersion::V2_1)] +#[case::v2_2(ConcreteFileVersion::V2_2)] +#[tokio::test] +async fn stable_current_writer_and_reader_are_wire_compatible( + #[case] version: ConcreteFileVersion, +) { + let batch = compatibility_fixture_batch(); + let mut schema = LanceSchema::try_from(batch.schema().as_ref()).unwrap(); + schema.set_dictionary(&batch).unwrap(); + + let actual = write_current_fixture(version, &batch, &schema).await; + let expected = stable_fixture(version); + assert_wire_bytes_equal(&actual, expected); + assert_eq!( + footer_version(expected), + version.to_standard_footer_numbers() + ); + assert_current_reader_roundtrip(expected, version, &batch).await; +} + +#[tokio::test] +async fn v2_0_embedded_writer_and_reader_are_wire_compatible() { + let batch = compatibility_fixture_batch() + .project(&[0, 1]) + .unwrap() + .slice(0, 257); + let mut schema = LanceSchema::try_from(batch.schema().as_ref()).unwrap(); + schema.set_dictionary(&batch).unwrap(); + + let (actual_self_described, actual_mini) = write_v2_0_embedded_fixtures(&batch, &schema).await; + let expected_self_described = + include_bytes!("../test_data/exact_versions/v2_0_self_described.lance"); + let expected_mini = include_bytes!("../test_data/exact_versions/v2_0_mini.lance"); + assert_wire_bytes_equal(&actual_self_described, expected_self_described); + assert_wire_bytes_equal(&actual_mini, expected_mini); + + let expected_footer = ConcreteFileVersion::V2_0.to_embedded_footer_numbers(); + assert_eq!(footer_version(expected_self_described), expected_footer); + assert_eq!(footer_version(expected_mini), expected_footer); + + let self_described = + EncodedBatch::try_from_self_described_lance(Bytes::from_static(expected_self_described)) + .unwrap(); + let decoded = decode_batch( + &self_described, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Array, + None, + ) + .await + .unwrap(); + assert_record_batch_eq(&decoded, &batch); + + let mini = + EncodedBatch::try_from_mini_lance(Bytes::from_static(expected_mini), &schema).unwrap(); + let decoded = decode_batch( + &mini, + &FilterExpression::no_filter(), + Arc::::default(), + false, + EncodedBatchLayout::Array, + None, + ) + .await + .unwrap(); + assert_record_batch_eq(&decoded, &batch); +} + +#[tokio::test] +async fn v2_3_output_is_deterministic_within_the_current_revision() { + let batch = compatibility_fixture_batch(); + let mut schema = LanceSchema::try_from(batch.schema().as_ref()).unwrap(); + schema.set_dictionary(&batch).unwrap(); + + let first = write_current_fixture(ConcreteFileVersion::V2_3, &batch, &schema).await; + let second = write_current_fixture(ConcreteFileVersion::V2_3, &batch, &schema).await; + assert_eq!(first, second); + assert_eq!( + footer_version(&first), + ConcreteFileVersion::V2_3.to_standard_footer_numbers() + ); + assert_current_reader_roundtrip(&first, ConcreteFileVersion::V2_3, &batch).await; +} + +#[tokio::test] +async fn v1_writer_and_reader_are_wire_compatible() { + let expected = stable_fixture(ConcreteFileVersion::V1); + let batch = compatibility_fixture_batch(); + let mut schema = LanceSchema::try_from(batch.schema().as_ref()).unwrap(); + schema.set_dictionary(&batch).unwrap(); + let fs = FsFixture::default(); + let mut writer = V1Writer::::try_new( + fs.object_store.as_ref(), + &fs.tmp_path, + schema.clone(), + &V1WriterOptions { + collect_stats_for_fields: Some(Vec::new()), + }, + ) + .await + .unwrap(); + for offset in (0..batch.num_rows()).step_by(1024) { + let slice = batch.slice(offset, (batch.num_rows() - offset).min(1024)); + writer.write(std::slice::from_ref(&slice)).await.unwrap(); + } + let summary = writer.finish().await.unwrap(); + let actual = fs + .object_store + .open(&fs.tmp_path) + .await + .unwrap() + .get_range(0..summary.size_bytes as usize) + .await + .unwrap(); + assert_wire_bytes_equal(actual.as_ref(), expected); + assert_eq!( + footer_version(expected), + ConcreteFileVersion::V1.to_standard_footer_numbers() + ); + + let fixture_fs = FsFixture::default(); + let mut fixture_writer = fixture_fs + .object_store + .create(&fixture_fs.tmp_path) + .await + .unwrap(); + fixture_writer.write_all(expected).await.unwrap(); + Writer::shutdown(fixture_writer.as_mut()).await.unwrap(); + let reader = V1Reader::try_new( + fixture_fs.object_store.as_ref(), + &fixture_fs.tmp_path, + schema.clone(), + ) + .await + .unwrap(); + let actual_batch = reader + .read_range(0..batch.num_rows(), &schema) + .await + .unwrap(); + assert_eq!(reader.num_batches(), 5); + assert_record_batch_eq(&actual_batch, &v1_reader_expected_batch(&batch)); +} diff --git a/lance-artifact/rust/lance-file/src/datatypes.rs b/lance-artifact/rust/lance-file/src/datatypes.rs new file mode 100644 index 000000000..3d84e9926 --- /dev/null +++ b/lance-artifact/rust/lance-file/src/datatypes.rs @@ -0,0 +1,565 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use lance_arrow::ARROW_EXT_NAME_KEY; +use lance_core::datatypes::{Dictionary, Encoding, Field, LogicalType, Schema}; +use lance_core::{Error, Result}; +use std::collections::HashMap; + +use crate::format::pb; + +#[allow(clippy::fallible_impl_from)] +impl From<&pb::Field> for Field { + fn from(field: &pb::Field) -> Self { + let lance_metadata: HashMap = field + .metadata + .iter() + .map(|(key, value)| { + let string_value = String::from_utf8_lossy(value).to_string(); + (key.clone(), string_value) + }) + .collect(); + let mut lance_metadata = lance_metadata; + if !field.extension_name.is_empty() { + lance_metadata.insert(ARROW_EXT_NAME_KEY.to_string(), field.extension_name.clone()); + } + Self { + name: field.name.clone(), + id: field.id, + parent_id: field.parent_id, + logical_type: LogicalType::from(field.logical_type.as_str()), + metadata: lance_metadata, + encoding: match field.encoding { + 1 => Some(Encoding::Plain), + 2 => Some(Encoding::VarBinary), + 3 => Some(Encoding::Dictionary), + 4 => Some(Encoding::RLE), + _ => None, + }, + nullable: field.nullable, + children: vec![], + dictionary: field.dictionary.as_ref().map(Dictionary::from), + unenforced_primary_key_position: if field.unenforced_primary_key_position > 0 { + Some(field.unenforced_primary_key_position) + } else if field.unenforced_primary_key { + Some(0) + } else { + None + }, + unenforced_clustering_key_position: if field.unenforced_clustering_key_position > 0 { + Some(field.unenforced_clustering_key_position) + } else { + None + }, + } + } +} + +impl From<&Field> for pb::Field { + fn from(field: &Field) -> Self { + let pb_metadata = field + .metadata + .iter() + .map(|(key, value)| (key.clone(), value.clone().into_bytes())) + .collect(); + Self { + id: field.id, + parent_id: field.parent_id, + name: field.name.clone(), + logical_type: field.logical_type.to_string(), + encoding: match field.encoding { + Some(Encoding::Plain) => 1, + Some(Encoding::VarBinary) => 2, + Some(Encoding::Dictionary) => 3, + Some(Encoding::RLE) => 4, + _ => 0, + }, + nullable: field.nullable, + dictionary: field.dictionary.as_ref().map(pb::Dictionary::from), + metadata: pb_metadata, + extension_name: field + .extension_name() + .map(|name| name.to_owned()) + .unwrap_or_default(), + r#type: 0, + unenforced_primary_key: field.unenforced_primary_key_position.is_some(), + unenforced_primary_key_position: field.unenforced_primary_key_position.unwrap_or(0), + unenforced_clustering_key: false, + unenforced_clustering_key_position: field + .unenforced_clustering_key_position + .unwrap_or(0), + } + } +} + +pub struct Fields(pub Vec); + +struct FieldNode { + field: Field, + child_indices: Vec, +} + +/// Searches in pre-order depth-first order and returns the first matching node, +/// preserving the legacy parent tie-break for duplicate field IDs. +fn first_field_index_by_id( + nodes: &[FieldNode], + root_indices: &[usize], + field_id: i32, +) -> Option { + let mut to_visit = Vec::with_capacity(nodes.len()); + to_visit.extend(root_indices.iter().rev().copied()); + + while let Some(node_index) = to_visit.pop() { + let node = &nodes[node_index]; + if node.field.id == field_id { + return Some(node_index); + } + to_visit.extend(node.child_indices.iter().rev().copied()); + } + + None +} + +impl From<&Field> for Fields { + fn from(field: &Field) -> Self { + let mut protos = vec![pb::Field::from(field)]; + protos.extend(field.children.iter().flat_map(|val| Self::from(val).0)); + Self(protos) + } +} + +/// Reconstruct a schema from a flat, pre-order protobuf field list. +/// +/// Parent fields must appear before their children. Historical manifests may +/// contain duplicate field IDs, so an ID may not identify a unique parent. For +/// those references, reconstruction preserves the legacy +/// [`Schema::mut_field_by_id`] tie-break by selecting the first matching field +/// in pre-order depth-first traversal. +/// +/// # Examples +/// +/// ``` +/// use lance_core::datatypes::Schema; +/// use lance_file::{datatypes::Fields, format::pb}; +/// +/// let field = pb::Field { +/// id: 0, +/// parent_id: -1, +/// name: "value".to_owned(), +/// logical_type: "int32".to_owned(), +/// ..Default::default() +/// }; +/// let fields = Fields(vec![field]); +/// let schema = Schema::try_from(&fields)?; +/// assert_eq!(schema.fields[0].name, "value"); +/// # Ok::<(), lance_core::Error>(()) +/// ``` +impl TryFrom<&Fields> for Schema { + type Error = Error; + + fn try_from(fields: &Fields) -> Result { + let mut nodes: Vec = Vec::with_capacity(fields.0.len()); + let mut root_indices = Vec::with_capacity(fields.0.len()); + let mut field_indices: HashMap> = HashMap::with_capacity(fields.0.len()); + + for proto_field in &fields.0 { + let parent_index = if proto_field.parent_id == -1 { + None + } else { + let parent_index = match field_indices.get(&proto_field.parent_id) { + Some(Some(parent_index)) => *parent_index, + Some(None) => { + // Duplicate IDs are invalid but occur in historical + // manifests. Match the legacy tree traversal only for + // these ambiguous parent references so valid schemas + // retain the linear fast path. + first_field_index_by_id(&nodes, &root_indices, proto_field.parent_id) + .ok_or_else(|| { + Error::internal(format!( + "Duplicate field id {} has no existing arena node", + proto_field.parent_id + )) + })? + } + None => { + return Err(Error::schema(format!( + "Field '{}' (id={}) references parent id {}, which must appear earlier in the protobuf field list", + proto_field.name, proto_field.id, proto_field.parent_id + ))); + } + }; + Some(parent_index) + }; + + let node_index = nodes.len(); + if let Some(parent_index) = parent_index { + nodes[parent_index].child_indices.push(node_index); + } else { + root_indices.push(node_index); + } + nodes.push(FieldNode { + field: Field::from(proto_field), + child_indices: Vec::new(), + }); + + field_indices + .entry(proto_field.id) + .and_modify(|field_index| *field_index = None) + .or_insert(Some(node_index)); + } + + let mut fields_by_node = Vec::with_capacity(nodes.len()); + fields_by_node.resize_with(nodes.len(), || None); + for (node_index, mut node) in nodes.into_iter().enumerate().rev() { + node.field.children.reserve(node.child_indices.len()); + for child_index in node.child_indices { + let child = fields_by_node + .get_mut(child_index) + .and_then(Option::take) + .ok_or_else(|| { + Error::internal(format!( + "Schema field arena node {child_index} was not materialized before its parent" + )) + })?; + node.field.children.push(child); + } + fields_by_node[node_index] = Some(node.field); + } + + let fields = root_indices + .into_iter() + .map(|root_index| { + fields_by_node[root_index].take().ok_or_else(|| { + Error::internal(format!( + "Schema field arena root node {root_index} was not materialized" + )) + }) + }) + .collect::>>()?; + + Ok(Self { + fields, + metadata: HashMap::default(), + }) + } +} + +pub struct FieldsWithMeta { + pub fields: Fields, + pub metadata: HashMap>, +} + +/// Reconstruct a schema from flat protobuf fields and schema metadata. +/// +/// # Examples +/// +/// ``` +/// use std::collections::HashMap; +/// +/// use lance_core::datatypes::Schema; +/// use lance_file::datatypes::{Fields, FieldsWithMeta}; +/// +/// let fields = FieldsWithMeta { +/// fields: Fields(Vec::new()), +/// metadata: HashMap::from([("owner".to_owned(), b"lance".to_vec())]), +/// }; +/// let schema = Schema::try_from(fields)?; +/// assert_eq!(schema.metadata["owner"], "lance"); +/// # Ok::<(), lance_core::Error>(()) +/// ``` +impl TryFrom for Schema { + type Error = Error; + + fn try_from(fields_with_meta: FieldsWithMeta) -> Result { + let lance_metadata = fields_with_meta + .metadata + .into_iter() + .map(|(key, value)| { + let string_value = String::from_utf8_lossy(&value).to_string(); + (key, string_value) + }) + .collect(); + + let schema_with_fields = Self::try_from(&fields_with_meta.fields)?; + Ok(Self { + fields: schema_with_fields.fields, + metadata: lance_metadata, + }) + } +} + +/// Convert a Schema to a list of protobuf Field. +impl From<&Schema> for Fields { + fn from(schema: &Schema) -> Self { + let mut protos = vec![]; + schema.fields.iter().for_each(|f| { + protos.extend(Self::from(f).0); + }); + Self(protos) + } +} + +/// Convert a Schema to a list of protobuf Field and Metadata +impl From<&Schema> for FieldsWithMeta { + fn from(schema: &Schema) -> Self { + let fields = schema.into(); + let metadata = schema + .metadata + .clone() + .into_iter() + .map(|(key, value)| (key, value.into_bytes())) + .collect(); + Self { fields, metadata } + } +} + +impl From<&pb::Dictionary> for Dictionary { + fn from(proto: &pb::Dictionary) -> Self { + Self { + offset: proto.offset as usize, + length: proto.length as usize, + values: None, + } + } +} + +impl From<&Dictionary> for pb::Dictionary { + fn from(d: &Dictionary) -> Self { + Self { + offset: d.offset as i64, + length: d.length as i64, + } + } +} + +impl From for pb::Encoding { + fn from(e: Encoding) -> Self { + match e { + Encoding::Plain => Self::Plain, + Encoding::VarBinary => Self::VarBinary, + Encoding::Dictionary => Self::Dictionary, + Encoding::RLE => Self::Rle, + } + } +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use arrow_schema::DataType; + use arrow_schema::Field as ArrowField; + use arrow_schema::Fields as ArrowFields; + use arrow_schema::Schema as ArrowSchema; + use lance_core::Error; + use lance_core::datatypes::Schema; + + use super::{Fields, FieldsWithMeta}; + use crate::format::pb; + + fn proto_field(id: i32, parent_id: i32, name: String, logical_type: &str) -> pb::Field { + pb::Field { + id, + parent_id, + name, + logical_type: logical_type.to_owned(), + ..Default::default() + } + } + + #[test] + fn test_schema_set_ids() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("a", DataType::Int32, false), + ArrowField::new( + "b", + DataType::Struct(ArrowFields::from(vec![ + ArrowField::new("f1", DataType::Utf8, true), + ArrowField::new("f2", DataType::Boolean, false), + ArrowField::new("f3", DataType::Float32, false), + ])), + true, + ), + ArrowField::new("c", DataType::Float64, false), + ]); + let schema = Schema::try_from(&arrow_schema).unwrap(); + + let protos: Fields = (&schema).into(); + assert_eq!( + protos.0.iter().map(|p| p.id).collect::>(), + (0..6).collect::>() + ); + } + + #[test] + fn test_schema_metadata() { + let mut metadata: HashMap = HashMap::new(); + metadata.insert(String::from("k1"), String::from("v1")); + metadata.insert(String::from("k2"), String::from("v2")); + + let arrow_schema = ArrowSchema::new_with_metadata( + vec![ArrowField::new("a", DataType::Int32, false)], + metadata, + ); + + let expected_schema = Schema::try_from(&arrow_schema).unwrap(); + let fields_with_meta: FieldsWithMeta = (&expected_schema).into(); + + let schema = Schema::try_from(fields_with_meta).unwrap(); + assert_eq!(expected_schema, schema); + } + + #[test] + fn test_reconstruct_wide_nested_schema() { + const NUM_STRUCTS: usize = 4096; + + let mut proto_fields = Vec::with_capacity(NUM_STRUCTS * 3); + for struct_index in 0..NUM_STRUCTS { + let parent_id = (struct_index * 3) as i32; + proto_fields.push(proto_field( + parent_id, + -1, + format!("struct_{struct_index}"), + "struct", + )); + proto_fields.push(proto_field( + parent_id + 1, + parent_id, + format!("left_{struct_index}"), + "int32", + )); + proto_fields.push(proto_field( + parent_id + 2, + parent_id, + format!("right_{struct_index}"), + "int32", + )); + } + + let fields = Fields(proto_fields); + let schema = Schema::try_from(&fields).unwrap(); + assert_eq!(schema.fields.len(), NUM_STRUCTS); + for (struct_index, field) in schema.fields.iter().enumerate() { + let parent_id = (struct_index * 3) as i32; + assert_eq!(field.id, parent_id); + assert_eq!(field.name, format!("struct_{struct_index}")); + assert_eq!(field.children.len(), 2); + assert_eq!(field.children[0].id, parent_id + 1); + assert_eq!(field.children[0].name, format!("left_{struct_index}")); + assert_eq!(field.children[1].id, parent_id + 2); + assert_eq!(field.children[1].name, format!("right_{struct_index}")); + } + } + + #[test] + fn test_reconstruct_deep_nested_schema() { + const DEPTH: usize = 1024; + + let proto_fields = (0..DEPTH) + .map(|depth| { + proto_field( + depth as i32, + if depth == 0 { -1 } else { depth as i32 - 1 }, + format!("level_{depth}"), + if depth + 1 == DEPTH { + "int32" + } else { + "struct" + }, + ) + }) + .collect(); + + let fields = Fields(proto_fields); + let schema = Schema::try_from(&fields).unwrap(); + assert_eq!(schema.fields.len(), 1); + let mut field = &schema.fields[0]; + for depth in 0..DEPTH { + assert_eq!(field.id, depth as i32); + assert_eq!(field.name, format!("level_{depth}")); + if depth + 1 == DEPTH { + assert!(field.children.is_empty()); + } else { + assert_eq!(field.children.len(), 1); + field = &field.children[0]; + } + } + } + + #[test] + fn test_reconstruct_schema_reports_missing_parent() { + let fields = Fields(vec![proto_field(7, 42, "child".to_owned(), "int32")]); + + let error = Schema::try_from(&fields).unwrap_err(); + assert!(matches!(&error, Error::Schema { .. })); + assert!( + error.to_string().contains( + "Field 'child' (id=7) references parent id 42, which must appear earlier" + ) + ); + } + + #[test] + fn test_reconstruct_schema_preserves_legacy_duplicate_id_match() { + let fields = Fields(vec![ + proto_field(1, -1, "root_a".to_owned(), "struct"), + proto_field(2, -1, "root_b".to_owned(), "struct"), + proto_field(2, 1, "nested_duplicate".to_owned(), "struct"), + proto_field(3, 2, "child".to_owned(), "int32"), + ]); + + let schema = Schema::try_from(&fields).unwrap(); + assert_eq!(schema.fields.len(), 2); + assert_eq!(schema.fields[0].name, "root_a"); + assert_eq!(schema.fields[0].children.len(), 1); + assert_eq!(schema.fields[0].children[0].name, "nested_duplicate"); + assert_eq!(schema.fields[0].children[0].children.len(), 1); + assert_eq!(schema.fields[0].children[0].children[0].name, "child"); + assert_eq!(schema.fields[1].name, "root_b"); + assert!(schema.fields[1].children.is_empty()); + } + + #[test] + fn test_clustering_key_roundtrip() { + let arrow_schema = ArrowSchema::new(vec![ + ArrowField::new("region", DataType::Utf8, true).with_metadata( + vec![( + "lance-schema:unenforced-clustering-key:position".to_owned(), + "1".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("date", DataType::Int32, false).with_metadata( + vec![( + "lance-schema:unenforced-clustering-key:position".to_owned(), + "2".to_owned(), + )] + .into_iter() + .collect::>(), + ), + ArrowField::new("value", DataType::Float64, true), + ]); + + let schema = Schema::try_from(&arrow_schema).unwrap(); + let ck = schema.unenforced_clustering_key(); + assert_eq!(ck.len(), 2); + assert_eq!(ck[0].name, "region"); + assert_eq!(ck[1].name, "date"); + + // Round-trip through protobuf + let fields_with_meta: FieldsWithMeta = (&schema).into(); + let restored = Schema::try_from(fields_with_meta).unwrap(); + + let ck2 = restored.unenforced_clustering_key(); + assert_eq!(ck2.len(), 2); + assert_eq!(ck2[0].name, "region"); + assert_eq!(ck2[1].name, "date"); + assert_eq!(ck2[0].unenforced_clustering_key_position, Some(1)); + assert_eq!(ck2[1].unenforced_clustering_key_position, Some(2)); + + // Non-clustering-key field should not have position + let value_field = restored.field("value").unwrap(); + assert!(!value_field.is_unenforced_clustering_key()); + } +} diff --git a/lance-artifact/rust/lance-file/src/format.rs b/lance-artifact/rust/lance-file/src/format.rs new file mode 100644 index 000000000..d7bc9c423 --- /dev/null +++ b/lance-artifact/rust/lance-file/src/format.rs @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +/// Protobuf definitions for Lance Format +pub mod pb { + #![allow(clippy::all)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + #![allow(clippy::upper_case_acronyms)] + #![allow(clippy::use_self)] + include!(concat!(env!("OUT_DIR"), "/lance.file.rs")); +} + +/// Protobuf definitions for Lance Format v2 +pub mod pbfile { + #![allow(clippy::all)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + #![allow(clippy::upper_case_acronyms)] + #![allow(clippy::use_self)] + include!(concat!(env!("OUT_DIR"), "/lance.file.v2.rs")); +} + +/// These version/magic values are written at the end of Lance files (e.g. versions/1.version) +pub const MAJOR_VERSION: i16 = 0; +pub const MINOR_VERSION: i16 = 2; +pub const MAGIC: &[u8; 4] = b"LANC"; diff --git a/lance-artifact/rust/lance-file/src/io.rs b/lance-artifact/rust/lance-file/src/io.rs new file mode 100644 index 000000000..86e5189a8 --- /dev/null +++ b/lance-artifact/rust/lance-file/src/io.rs @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::sync::Arc; + +use futures::{FutureExt, future::BoxFuture}; +use lance_encoding::EncodingsIo; +use lance_io::scheduler::FileScheduler; + +use super::reader::DEFAULT_READ_CHUNK_SIZE; + +#[derive(Debug)] +pub struct LanceEncodingsIo { + scheduler: FileScheduler, + /// Size of chunks when reading large pages + read_chunk_size: u64, +} + +impl LanceEncodingsIo { + pub fn new(scheduler: FileScheduler) -> Self { + Self { + scheduler, + read_chunk_size: DEFAULT_READ_CHUNK_SIZE, + } + } + + pub fn with_read_chunk_size(mut self, read_chunk_size: u64) -> Self { + self.read_chunk_size = read_chunk_size; + self + } +} + +impl EncodingsIo for LanceEncodingsIo { + fn with_bypass_backpressure(&self) -> Option> { + Some(Arc::new(Self { + scheduler: self.scheduler.with_bypass_backpressure(), + read_chunk_size: self.read_chunk_size, + })) + } + + fn with_io_stats( + &self, + stats: Arc, + ) -> Option> { + Some(Arc::new(Self { + scheduler: self.scheduler.with_io_stats(stats), + read_chunk_size: self.read_chunk_size, + })) + } + + fn submit_request( + &self, + ranges: Vec>, + priority: u64, + ) -> BoxFuture<'static, lance_core::Result>> { + let mut split_ranges = Vec::new(); + let mut split_indices = Vec::new(); // Track which original range each split came from + // Large ranges (above read_chunk_size) will be split into + // multiple reads. Empty ranges will skip the I/O layer + // entirely. If we have either of these we will need to + // reassemble our results, inserting empties and merging parts + let mut needs_reassembly = false; + + // Split large ranges into smaller chunks + // + // TODO: consider read_chunk_size before submitting requests. + for (idx, range) in ranges.iter().enumerate() { + if range.start == range.end { + // EncodingsIo requires one result per input range. Zero-length + // ranges schedule no I/O, so their empty results are restored + // after the non-empty requests complete. + needs_reassembly = true; + continue; + } + let range_size = range.end - range.start; + + if range_size > self.read_chunk_size { + needs_reassembly = true; + let num_chunks = range_size.div_ceil(self.read_chunk_size); + let chunk_size = range_size / num_chunks; + + for i in 0..num_chunks { + let start = range.start + i * chunk_size; + let end = if i == num_chunks - 1 { + range.end // Last chunk gets any remaining bytes + } else { + start + chunk_size + }; + split_ranges.push(start..end); + split_indices.push(idx); + } + } else { + split_ranges.push(range.clone()); + split_indices.push(idx); + } + } + + let fut = self.scheduler.submit_request(split_ranges, priority); + + async move { + let split_results = fut.await?; + + if split_results.len() != split_indices.len() { + return Err(lance_core::Error::internal(format!( + "Encoding I/O returned {} results for {} requested range chunks", + split_results.len(), + split_indices.len() + ))); + } + if !needs_reassembly { + return Ok(split_results); + } + + let mut results = vec![Vec::new(); ranges.len()]; + + for (split_result, orig_idx) in split_results.into_iter().zip(split_indices) { + results[orig_idx].push(split_result); + } + + let mut reassembled = Vec::with_capacity(ranges.len()); + for (range, chunks) in ranges.iter().zip(results) { + if chunks.is_empty() { + if range.start == range.end { + reassembled.push(bytes::Bytes::new()); + continue; + } + return Err(lance_core::Error::internal(format!( + "Encoding I/O returned no data for non-empty range {}..{}", + range.start, range.end + ))); + } + if chunks.len() == 1 { + reassembled.push(chunks[0].clone()); + continue; + } + + let total_size: usize = chunks.iter().map(|c| c.len()).sum(); + let mut combined = Vec::with_capacity(total_size); + for chunk in chunks { + combined.extend_from_slice(&chunk); + } + reassembled.push(bytes::Bytes::from(combined)); + } + Ok(reassembled) + } + .boxed() + } +} diff --git a/lance-artifact/rust/lance-file/src/lib.rs b/lance-artifact/rust/lance-file/src/lib.rs new file mode 100644 index 000000000..32dfdb89f --- /dev/null +++ b/lance-artifact/rust/lance-file/src/lib.rs @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +pub mod datatypes; +pub mod format; +pub(crate) mod io; +pub mod reader; +pub mod testing; +pub mod version; +pub mod versions; +pub mod writer; + +#[cfg(test)] +mod compatibility_tests; + +pub use io::LanceEncodingsIo; + +use format::MAGIC; +use lance_core::{Error, Result}; +use lance_io::object_store::ObjectStore; +use object_store::path::Path; +use version::ConcreteFileVersion; + +pub async fn determine_file_version( + store: &ObjectStore, + path: &Path, + known_size: Option, +) -> Result { + let size = match known_size { + None => usize::try_from(store.size(path).await?).map_err(|_| { + Error::invalid_input(format!("file {} is too large for this platform", path)) + })?, + Some(size) => size, + }; + if size < 8 { + return Err(Error::invalid_input_source( + format!( + "the file {} does not appear to be a lance file (too small)", + path + ) + .into(), + )); + } + let reader = store.open_with_size(path, size).await?; + let footer = reader.get_range((size - 8)..size).await?; + if &footer[4..] != MAGIC { + return Err(Error::invalid_input_source( + format!( + "the file {} does not appear to be a lance file (magic mismatch)", + path + ) + .into(), + )); + } + let major_version = u16::from_le_bytes([footer[0], footer[1]]); + let minor_version = u16::from_le_bytes([footer[2], footer[3]]); + + ConcreteFileVersion::from_footer_numbers(major_version, minor_version) +} diff --git a/lance-artifact/rust/lance-file/src/reader.rs b/lance-artifact/rust/lance-file/src/reader.rs new file mode 100644 index 000000000..9a4b09b93 --- /dev/null +++ b/lance-artifact/rust/lance-file/src/reader.rs @@ -0,0 +1,4606 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright The Lance Authors + +use std::{ + borrow::Cow, + collections::{BTreeMap, BTreeSet}, + fmt::Debug, + io::Cursor, + ops::Range, + pin::Pin, + sync::Arc, +}; + +use arrow_array::RecordBatchReader; +use arrow_schema::Schema as ArrowSchema; +use async_trait::async_trait; +use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; +use bytes::{Bytes, BytesMut}; +use futures::{Stream, StreamExt, stream::BoxStream}; +use lance_core::deepsize::{Context, DeepSizeOf}; +use lance_encoding::{ + EncodingsIo, + decoder::{ + ColumnInfo, DecoderConfig, DecoderPlugins, FilterExpression, PageEncoding, ReadBatchTask, + RequestedRows, SchedulerDecoderConfig, schedule_and_decode, schedule_and_decode_blocking, + }, + encoder::EncodedBatch, +}; +use log::debug; +use object_store::path::Path; +use prost::Message; + +use lance_core::{ + Error, Result, + cache::{CacheKey, CacheKeySchema, KeyBuilder, LanceCache}, + datatypes::{Field, Schema}, +}; +use lance_encoding::format::pb as pbenc; +use lance_encoding::format::pb21 as pbenc21; +use lance_io::{ + ReadBatchParams, + scheduler::FileScheduler, + stream::{RecordBatchStream, RecordBatchStreamAdapter}, +}; + +use crate::{ + datatypes::{Fields, FieldsWithMeta}, + format::{MAGIC, pb, pbfile}, + io::LanceEncodingsIo, + version::ConcreteFileVersion, + versions, +}; + +pub(crate) mod structural; + +/// Default chunk size for reading large pages (8MiB) +/// Pages larger than this will be split into multiple chunks during read +pub const DEFAULT_READ_CHUNK_SIZE: u64 = 8 * 1024 * 1024; + +// For now, we don't use global buffers for anything other than schema. If we +// use these later we should make them lazily loaded and then cached once loaded. +// +// We store their position / length for debugging purposes +#[derive(Debug, DeepSizeOf)] +pub struct BufferDescriptor { + pub position: u64, + pub size: u64, +} + +impl BufferDescriptor { + fn checked_range(&self, buffer_index: usize, file_len: u64) -> Result> { + let end = self.position.checked_add(self.size).ok_or_else(|| { + Error::invalid_input_source( + format!( + "Global buffer {} range overflows: position={}, size={}", + buffer_index, self.position, self.size + ) + .into(), + ) + })?; + if self.position > file_len { + return Err(Error::invalid_input_source( + format!( + "Global buffer {} position {} is outside file of size {}", + buffer_index, self.position, file_len + ) + .into(), + )); + } + if end > file_len { + return Err(Error::invalid_input_source( + format!( + "Global buffer {} range {}..{} is outside file of size {}", + buffer_index, self.position, end, file_len + ) + .into(), + )); + } + Ok(self.position..end) + } +} + +/// Statistics summarize some of the file metadata for quick summary info +#[derive(Debug)] +pub struct FileStatistics { + /// Statistics about each of the columns in the file + pub columns: Vec, +} + +/// Summary information describing a column +#[derive(Debug)] +pub struct ColumnStatistics { + /// The number of pages in the column + pub num_pages: usize, + /// The total number of data & metadata bytes in the column + /// + /// This is the compressed on-disk size + pub size_bytes: u64, +} + +// TODO: Caching +#[derive(Debug)] +pub struct CachedFileMetadata { + /// The schema of the file + pub file_schema: Arc, + /// The column metadatas + pub column_metadatas: Vec, + pub column_infos: Vec>, + /// The number of rows in the file + pub num_rows: u64, + pub file_buffers: Vec, + /// The number of bytes contained in the data page section of the file + pub num_data_bytes: u64, + /// The number of bytes contained in the column metadata (not including buffers + /// referenced by the metadata) + pub num_column_metadata_bytes: u64, + /// The number of bytes contained in global buffers + pub num_global_buffer_bytes: u64, + /// The number of bytes contained in the CMO and GBO tables + pub num_footer_bytes: u64, + /// The major version number stored in the file footer. + pub major_version: u16, + /// The minor version number stored in the file footer. + pub minor_version: u16, + pub version: ConcreteFileVersion, + /// The actual total file size in bytes, as reported by the object store. + pub file_size_bytes: u64, + /// User global buffers (index >= 1) whose bytes were already captured by the + /// tail read that `read_all_metadata` performs at open, keyed by buffer index. + /// + /// All global buffers are laid out contiguously starting at the schema, so on + /// small/medium files they land inside the captured tail window. Retaining + /// those bytes lets `read_global_buffer` serve them with zero additional I/O. + /// The bytes are copied out of the tail (rather than sliced) so the much + /// larger tail allocation can be dropped — we only hold what we will serve. + /// + /// The schema (buffer 0) is excluded: it is already decoded at open and is + /// not fetched through `read_global_buffer`. Buffers that fall outside the + /// window (large files) are absent here and fall back to a dedicated read. + pub retained_global_buffers: BTreeMap, +} + +impl CachedFileMetadata { + /// Total file size in bytes. + pub fn file_size(&self) -> u64 { + self.file_size_bytes + } +} + +fn column_metadata_deep_size(column_metadatas: &[pbfile::ColumnMetadata]) -> usize { + column_metadatas + .iter() + .map(|cm| cm.encoded_len() * 4) + .sum::() + + std::mem::size_of_val(column_metadatas) +} + +impl DeepSizeOf for CachedFileMetadata { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + let schema_size = self.file_schema.deep_size_of_children(context); + + let buffers_size: usize = self + .file_buffers + .iter() + .map(|fb| fb.deep_size_of_children(context)) + .sum(); + + // column_metadatas is Vec (protobuf generated, + // does not implement DeepSizeOf). We use prost::Message::encoded_len() + // as a proxy for in-memory size. The decoded representation is typically + // several times larger than the wire format due to heap-allocated + // repeated/string/bytes fields, so we apply a 4x multiplier. + let column_metadatas_size = column_metadata_deep_size(self.column_metadatas.as_slice()); + + // column_infos is Vec>. Each ColumnInfo contains + // page_infos (with protobuf PageEncoding), buffer offsets, and a + // column-level ColumnEncoding protobuf. + let column_infos_size = self.column_infos.deep_size_of_children(context); + + // Global buffer bytes retained for zero-IO reads (copied out of the tail). + let retained_buffers_size = self.retained_global_buffers.deep_size_of_children(context); + + schema_size + + buffers_size + + column_metadatas_size + + column_infos_size + + retained_buffers_size + } +} + +/// Lightweight file metadata used to locate per-column metadata on demand. +/// +/// This contains the file-level schema, row count, global buffer descriptors, +/// and column metadata offset table. Unlike [`CachedFileMetadata`], it does not +/// hold decoded metadata for every column. +#[derive(Debug, DeepSizeOf)] +pub struct FileMetadataIndex { + pub(crate) file_schema: Arc, + pub(crate) num_rows: u64, + pub(crate) file_buffers: Vec, + pub(crate) column_metadata_offsets: Arc<[(u64, u64)]>, + pub(crate) num_columns: u32, + pub(crate) version: ConcreteFileVersion, + pub(crate) file_size_bytes: u64, + pub(crate) retained_global_buffers: BTreeMap, +} + +impl FileMetadataIndex { + /// Returns the total size of the file in bytes. + pub fn file_size(&self) -> u64 { + self.file_size_bytes + } + + /// Returns the number of physical columns in the file. + pub fn num_columns(&self) -> u32 { + self.num_columns + } +} + +#[derive(Debug)] +struct CachedColumnMetadata { + column_metadata: pbfile::ColumnMetadata, + column_info: Arc, +} + +impl DeepSizeOf for CachedColumnMetadata { + fn deep_size_of_children(&self, context: &mut Context) -> usize { + column_metadata_deep_size(std::slice::from_ref(&self.column_metadata)) + + self.column_info.deep_size_of_children(context) + } +} + +#[derive(Debug, Clone)] +struct ColumnMetadataCacheKey { + column_index: u32, +} + +impl CacheKey for ColumnMetadataCacheKey { + type ValueType = CachedColumnMetadata; + + fn key(&self) -> Cow<'_, str> { + Cow::Owned(format!("column_metadata/{}", self.column_index)) + } + + fn type_name() -> &'static str { + "ColumnMetadata" + } + + fn schema() -> CacheKeySchema { + CacheKeySchema::new("lance.file.column-metadata-key", 1) + } + + fn write_key(&self, builder: &mut KeyBuilder) { + builder.write_u32(self.column_index); + } +} + +impl CachedFileMetadata { + pub fn version(&self) -> ConcreteFileVersion { + self.version + } +} + +/// Selecting columns from a lance file requires specifying both the +/// index of the column and the data type of the column +/// +/// Partly, this is because it is not strictly required that columns +/// be read into the same type. For example, a string column may be +/// read as a string, large_string or string_view type. +/// +/// A read will only succeed if the decoder for a column is capable +/// of decoding into the requested type. +/// +/// Note that this should generally be limited to different in-memory +/// representations of the same semantic type. An encoding could +/// theoretically support "casting" (e.g. int to string, etc.) but +/// there is little advantage in doing so here. +/// +/// Note: in order to specify a projection the user will need some way +/// to figure out the column indices. In the table format we do this +/// using field IDs and keeping track of the field id->column index mapping. +/// +/// If users are not using the table format then they will need to figure +/// out some way to do this themselves. +#[derive(Debug, Clone)] +pub struct ReaderProjection { + /// The data types (schema) of the selected columns. The names + /// of the schema are arbitrary and ignored. + pub schema: Arc, + /// The indices of the columns to load. + /// + /// The content of this vector depends on the file version. + /// + /// In Lance File Version 2.0 we need ids for structural fields as + /// well as leaf fields: + /// + /// - Primitive: the index of the column in the schema + /// - List: the index of the list column in the schema + /// followed by the column indices of the children + /// - FixedSizeList (of primitive): the index of the column in the schema + /// (this case is not nested) + /// - FixedSizeList (of non-primitive): not yet implemented + /// - Dictionary: same as primitive + /// - Struct: the index of the struct column in the schema + /// followed by the column indices of the children + /// + /// In other words, this should be a DFS listing of the desired schema. + /// + /// In Lance File Version 2.1 we only need ids for leaf fields. Any structural + /// fields are completely transparent. + /// + /// For example, if the goal is to load: + /// + /// x: int32 + /// y: `struct` + /// z: `list` + /// + /// and the schema originally used to store the data was: + /// + /// a: `struct` + /// b: int64 + /// y: `struct` + /// z: `list` + /// + /// Then the column_indices should be: + /// + /// - 2.0: [1, 3, 4, 6, 7, 8] + /// - 2.1: [0, 2, 4, 5] + pub column_indices: Vec, +} + +impl ReaderProjection { + /// Returns whether this projection is selective enough to benefit from + /// loading column metadata through the file's metadata index. + /// + /// The caller must already have selected a file format that supports indexed + /// metadata. This method only evaluates the projection shape and selectivity. + pub fn prefers_indexed_metadata(&self, total_columns: usize) -> bool { + FileMetadataProvider::projection_matches_indexed_metadata(self) + && self.column_indices.len().saturating_mul(4) < total_columns + } +} + +/// File Reader Options that can control reading behaviors, such as whether to enable caching on repetition indices +#[derive(Clone, Debug)] +pub struct FileReaderOptions { + pub decoder_config: DecoderConfig, + /// Size of chunks when reading large pages. Pages larger than this + /// will be read in multiple chunks to control memory usage. + /// Default: 8MB (DEFAULT_READ_CHUNK_SIZE) + pub read_chunk_size: u64, + /// If set, the reader will produce batches whose total size in bytes + /// is approximately this value. The row-based `batch_size` remains an + /// independent upper bound, and the limit reached first determines the batch size. + /// + /// This can be set at the dataset level (via `ReadParams::file_reader_options`) + /// to provide a default for all scans, or at the scanner level (via + /// `Scanner::batch_size_bytes`) to override per scan. + pub batch_size_bytes: Option, +} + +impl Default for FileReaderOptions { + fn default() -> Self { + Self { + decoder_config: DecoderConfig::default(), + read_chunk_size: DEFAULT_READ_CHUNK_SIZE, + batch_size_bytes: None, + } + } +} + +#[derive(Debug, Clone)] +pub(crate) struct PreparedProjection { + pub column_infos: Vec>, + pub decoder_projection: ReaderProjection, +} + +#[derive(Debug, Clone)] +pub(crate) enum FileMetadataProvider { + Full(Arc), + Indexed(Arc), +} + +/// Executable projection behavior selected by an exact file-version module. +/// +/// The shared reader invokes this behavior but never interprets a version or +/// accepted-grammar profile. +#[async_trait] +pub(crate) trait ReadProjection: Debug + Send + Sync { + fn validate_indexed( + &self, + projection: &ReaderProjection, + metadata_index: &FileMetadataIndex, + ) -> Result<()>; + + fn read_length(&self, prepared: &PreparedProjection) -> Result; + + async fn prepare( + &self, + metadata_provider: &FileMetadataProvider, + projection: &ReaderProjection, + io: &Arc, + cache: &Arc, + ) -> Result<(PreparedProjection, u64)>; +} + +#[derive(Debug, Clone)] +pub(crate) struct DecodeEngine { + pub scheduler: Arc, + pub base_projection: ReaderProjection, + pub metadata_provider: FileMetadataProvider, + pub read_projection: Arc, + pub decoder_plugins: Arc, + pub cache: Arc, + pub options: FileReaderOptions, +} + +/// A projection-scoped reader for a current-format Lance file. +/// +/// This reader fixes a base projection at construction time. All later reads +/// must stay within that projection, which lets the reader load only the column +/// metadata needed by the base projection when opening from a [`FileMetadataIndex`]. +/// It intentionally does not expose APIs that require synchronous access to full +/// file metadata. +#[derive(Debug, Clone)] +pub struct ProjectedFileReader { + core: DecodeEngine, +} + +/// A current-format Lance file reader backed by fully decoded metadata. +#[derive(Debug, Clone)] +pub struct FileReader { + pub(crate) core: DecodeEngine, + pub(crate) metadata: Arc, +} + +pub(crate) fn tasks_to_record_batch_stream( + schema: Arc, + tasks: Pin + Send>>, + batch_readahead: u32, +) -> Pin> { + let arrow_schema = Arc::new(ArrowSchema::from(schema.as_ref())); + let batches = tasks + .map(|task| task.task) + .buffered(batch_readahead as usize) + .boxed(); + Box::pin(RecordBatchStreamAdapter::new(arrow_schema, batches)) +} + +pub(crate) enum RawFileMetadataOpen { + Legacy { + major_version: u16, + minor_version: u16, + }, + Current { + version: ConcreteFileVersion, + metadata: RawFileMetadata, + }, +} + +pub(crate) struct RawFileMetadata { + pub file_schema: Arc, + pub column_metadatas: Vec, + pub num_rows: u64, + pub file_buffers: Vec, + pub num_data_bytes: u64, + pub num_column_metadata_bytes: u64, + pub num_global_buffer_bytes: u64, + pub num_footer_bytes: u64, + pub footer: Footer, + pub file_size_bytes: u64, + pub retained_global_buffers: BTreeMap, +} + +#[derive(Debug)] +pub(crate) struct Footer { + #[allow(dead_code)] + pub column_meta_start: u64, + // We don't use this today because we always load metadata for every column + // and don't yet support "metadata projection" + #[allow(dead_code)] + pub column_meta_offsets_start: u64, + pub global_buff_offsets_start: u64, + pub num_global_buffers: u32, + pub num_columns: u32, + pub major_version: u16, + pub minor_version: u16, +} + +const FOOTER_LEN: usize = 40; + +// Count the V2.1 physical columns required to reconstruct a projected field. +// This is the same DFS shape consumed by `ColumnInfoIter`: ordinary structural +// nodes are transparent and leaves contribute columns. Indexed metadata loading +// can therefore compact any ordinary structural projection into 0..N while +// preserving this order. +// +// Blob and packed-struct fields remain unsupported by indexed projection. Their +// opaque decode semantics are handled by the existing full-metadata reader. +fn indexed_projection_column_count(field: &Field) -> Option { + if field.is_blob() || field.is_packed_struct() { + return None; + } + + if field.children.is_empty() { + return Some(1); + } + + field.children.iter().try_fold(0usize, |count, child| { + count.checked_add(indexed_projection_column_count(child)?) + }) +} + +// The reader combines a projection's columns into rectangular batches, so they +// must all have the same length. Returns that common length, or a descriptive +// error (naming each column's length) when they differ. Ordinary files always +// pass; only files written with `FileWriter::write_column` whose columns ended up +// unequal can fail, and those must be read separately. +pub(crate) fn normalized_column_num_rows(info: &ColumnInfo) -> Result { + info.page_infos.iter().try_fold(0_u64, |rows, page| { + let page_rows = match &page.encoding { + PageEncoding::Structural(layout) => match &layout.layout { + Some(pbenc21::page_layout::Layout::SparseLayout(sparse)) => sparse + .structural_layers + .first() + .and_then(|layer| layer.layer.as_ref()) + .map_or(page.num_rows, |layer| match layer { + pbenc21::sparse_structural_layer::Layer::Validity(layer) => layer.num_slots, + pbenc21::sparse_structural_layer::Layer::List(layer) => layer.num_slots, + pbenc21::sparse_structural_layer::Layer::FixedSizeList(layer) => { + layer.num_slots + } + }), + _ => page.num_rows, + }, + _ => page.num_rows, + }; + rows.checked_add(page_rows) + .ok_or_else(|| Error::invalid_input_source("Column row count overflows u64".into())) + }) +} + +pub(crate) fn verify_uniform_lengths(field_lengths: &[(&str, u64)]) -> Result { + let first = field_lengths.first().map_or(0, |&(_, len)| len); + if field_lengths.iter().all(|&(_, len)| len == first) { + return Ok(first); + } + let columns = field_lengths + .iter() + .map(|(name, len)| format!("{name}={len}")) + .collect::>() + .join(", "); + Err(Error::invalid_input(format!( + "cannot read columns of differing lengths together ({columns}); \ + read each column (or equal-length group) separately" + ))) +} + +impl FileReader { + pub(crate) fn base_projection(&self) -> &ReaderProjection { + &self.core.base_projection + } + + pub(crate) fn full_projection(&self, projection: ReaderProjection) -> PreparedProjection { + PreparedProjection { + column_infos: self.metadata.column_infos.clone(), + decoder_projection: projection, + } + } + + pub(crate) async fn read_prepared_tasks( + &self, + params: ReadBatchParams, + batch_size: u32, + prepared: PreparedProjection, + read_len: u64, + filter: FilterExpression, + ) -> Result + Send>>> { + self.core + .read_prepared_tasks(params, batch_size, prepared, read_len, filter) + .await + } + + pub fn with_scheduler(&self, scheduler: Arc) -> Self { + Self { + core: self.core.with_scheduler(scheduler), + metadata: self.metadata.clone(), + } + } + + /// Returns a clone of this reader whose I/O is additionally recorded into + /// `stats`, on top of the scheduler's global accounting. + /// + /// All cached metadata is shared with `self`, so no file is re-opened and + /// only a few `Arc` clones are performed. If the underlying I/O service + /// does not support per-scope statistics (e.g. an in-memory scheduler), the + /// returned reader is an ordinary, uninstrumented clone. + pub fn with_io_stats( + &self, + stats: Arc, + ) -> Self { + match self.core.scheduler.with_io_stats(stats) { + Some(scheduler) => self.with_scheduler(scheduler), + None => self.clone(), + } + } + + pub fn num_rows(&self) -> u64 { + self.core.num_rows() + } + + /// The number of rows stored in a single physical column. + /// + /// For ordinary (rectangular) files every column has the same length, equal + /// to [`num_rows`](Self::num_rows). Files written with + /// [`FileWriter::write_column`](crate::writer::FileWriter::write_column) + /// may have columns of differing lengths; this returns the length of one + /// such column, derived by summing its pages' row counts. Errors if + /// `column_index` is out of bounds. + pub fn column_num_rows(&self, column_index: usize) -> Result { + let column = self + .metadata + .column_metadatas + .get(column_index) + .ok_or_else(|| { + Error::invalid_input(format!( + "column index {} is out of bounds (file has {} columns)", + column_index, + self.metadata.column_metadatas.len() + )) + })?; + Ok(column.pages.iter().map(|page| page.length).sum()) + } + + pub fn metadata(&self) -> &Arc { + &self.metadata + } + + fn statistics_from_column_metadata( + column_metadatas: &[pbfile::ColumnMetadata], + ) -> FileStatistics { + let column_stats = column_metadatas + .iter() + .map(|col_metadata| { + let num_pages = col_metadata.pages.len(); + let size_bytes = col_metadata + .pages + .iter() + .map(|page| page.buffer_sizes.iter().sum::()) + .sum::(); + ColumnStatistics { + num_pages, + size_bytes, + } + }) + .collect(); + + FileStatistics { + columns: column_stats, + } + } + + pub fn file_statistics(&self) -> FileStatistics { + Self::statistics_from_column_metadata(&self.metadata().column_metadatas) + } + + pub async fn read_global_buffer(&self, index: u32) -> Result { + self.core.read_global_buffer(index).await + } + + async fn read_tail(scheduler: &FileScheduler) -> Result<(Bytes, u64)> { + let file_size = scheduler.reader().size().await? as u64; + let begin = if file_size < scheduler.reader().block_size() as u64 { + 0 + } else { + file_size - scheduler.reader().block_size() as u64 + }; + let tail_bytes = scheduler.submit_single(begin..file_size, 0).await?; + Ok((tail_bytes, file_size)) + } + + async fn read_range_from_tail_or_scheduler( + tail_bytes: &Bytes, + tail_offset: u64, + scheduler: &FileScheduler, + range: Range, + ) -> Result { + let tail_end = tail_offset + tail_bytes.len() as u64; + if range.start >= tail_offset && range.end <= tail_end { + let rel_start = (range.start - tail_offset) as usize; + let rel_end = (range.end - tail_offset) as usize; + Ok(tail_bytes.slice(rel_start..rel_end)) + } else { + scheduler.submit_single(range, 0).await + } + } + + fn retained_global_buffers_from_tail( + gbo_table: &[BufferDescriptor], + tail_bytes: &Bytes, + tail_offset: u64, + file_len: u64, + ) -> Result> { + let tail_end = tail_offset + .checked_add(tail_bytes.len() as u64) + .ok_or_else(|| Error::invalid_input_source("Tail byte range overflows".into()))?; + let mut retained_buffers = BTreeMap::new(); + for (index, buffer) in gbo_table.iter().enumerate().skip(1) { + let range = buffer.checked_range(index, file_len)?; + if range.start >= tail_offset && range.end <= tail_end { + let rel_start = (range.start - tail_offset) as usize; + let rel_end = (range.end - tail_offset) as usize; + let bytes = Bytes::copy_from_slice(&tail_bytes[rel_start..rel_end]); + retained_buffers.insert(index as u32, bytes); + } + } + Ok(retained_buffers) + } + + // Checks to make sure the footer is written correctly and returns the + // position of the file descriptor (which comes from the footer) + fn decode_footer(footer_bytes: &Bytes) -> Result