# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright The LanceDB Authors from datetime import timedelta import lancedb def test_attached_snapshot_cache(tmp_db): db = tmp_db table = db.create_table("samples", [{"sample_id": 101}]) table.add([{"sample_id": 102}]) table_name = table.name namespace_path = table.namespace branch = table.current_branch() cached_version = table.version cached_address = ( table.search() .where("sample_id = 102") .select(["_rowaddr"]) .limit(1) .to_list()[0]["_rowaddr"] ) table.add([{"sample_id": 103}]) # --8<-- [start:attach_tag_cached_version] tag = "training-run-42" table = db.open_table(table_name, namespace_path=namespace_path, branch=branch) table.tags.create(tag, cached_version) # --8<-- [end:attach_tag_cached_version] table.optimize() assert table.count_rows() == 3 assert table.search().where(f"_rowaddr = {cached_address}").limit(1).to_list() == [] dataset = table.to_lance() dataset.cleanup_old_versions( older_than=timedelta(0), error_if_tagged_old_versions=False ) assert {version["version"] for version in dataset.versions()} == { cached_version, table.version, } # --8<-- [start:attach_read_cached_snapshot] snapshot = db.open_table(table_name, namespace_path=namespace_path, branch=branch) snapshot.checkout(tag) if snapshot.version != cached_version: raise RuntimeError(f"Tag {tag} no longer points to version {cached_version}") rows = snapshot.search().where(f"_rowaddr = {cached_address}").limit(1).to_list() # --8<-- [end:attach_read_cached_snapshot] assert rows == [{"sample_id": 102}] assert snapshot.count_rows() == 2 def test_attached_stable_id_cache(tmp_db): db = tmp_db writer = db.create_table( "samples", [{"sample_id": 101}, {"sample_id": 102}], storage_options={"new_table_enable_stable_row_ids": "true"}, ) writer.add([{"sample_id": 103}]) table_name = writer.name namespace_path = writer.namespace branch = writer.current_branch() # --8<-- [start:attach_build_stable_id_cache] table = db.open_table(table_name, namespace_path=namespace_path, branch=branch) table.checkout_latest() cached_rows = ( table.search() .where("sample_id IN (101, 103)") .select(["sample_id"]) .with_row_id(True) .limit(2) .to_list() ) ids_by_key = {row["sample_id"]: row["_rowid"] for row in cached_rows} # --8<-- [end:attach_build_stable_id_cache] writer.delete("sample_id = 101") writer.optimize() assert writer.stats()["fragment_stats"]["num_fragments"] == 1 # --8<-- [start:attach_read_stable_id_cache] table.checkout_latest() row_ids = [ids_by_key[101], ids_by_key[103]] rows = table.take_row_ids(row_ids, with_row_id=True).to_list() rows_by_id = {row["_rowid"]: row for row in rows} batch = [rows_by_id.get(row_id) for row_id in row_ids] # --8<-- [end:attach_read_stable_id_cache] assert batch == [None, {"sample_id": 103, "_rowid": ids_by_key[103]}] def test_attached_reader_freshness(tmp_path): database_uri = str(tmp_path) connection_options = {} writer = lancedb.connect(database_uri).create_table("samples", [{"sample_id": 101}]) table_name = writer.name namespace_path = writer.namespace branch = writer.current_branch() reader = lancedb.connect(database_uri).open_table(table_name) assert reader.count_rows() == 1 writer.add([{"sample_id": 102}]) assert reader.count_rows() == 1 # --8<-- [start:attach_read_zero_interval] from datetime import timedelta fresh_db = lancedb.connect( database_uri, read_consistency_interval=timedelta(0), **connection_options, ) fresh_reader = fresh_db.open_table( table_name, namespace_path=namespace_path, branch=branch ) latest_count = fresh_reader.count_rows() # --8<-- [end:attach_read_zero_interval] # --8<-- [start:attach_refresh_reader] reader.checkout_latest() refreshed_count = reader.count_rows() # --8<-- [end:attach_refresh_reader] assert latest_count == 2 assert refreshed_count == 2