code: #  Copyright 2023 LanceDB Developers
  #
  #  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.
  
  import functools
  from copy import copy
  from datetime import date, datetime, timedelta
  from pathlib import Path
  from time import sleep
  from typing import List
  from unittest.mock import PropertyMock, patch
  
  import lance
  import lancedb
  import numpy as np
  import pandas as pd
  import polars as pl
  import pyarrow as pa
  import pytest
  import pytest_asyncio
  from lancedb.conftest import MockTextEmbeddingFunction
  from lancedb.db import AsyncConnection, LanceDBConnection
  from lancedb.embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry
  from lancedb.pydantic import LanceModel, Vector
  from lancedb.table import LanceTable
  from pydantic import BaseModel
  
  
  class MockDB:
      def __init__(self, uri: Path):
          self.uri = uri
          self.read_consistency_interval = None
  
      @functools.cached_property
      def is_managed_remote(self) -> bool:
          return False
  
  
  @pytest.fixture
  def db(tmp_path) -> MockDB:
      return MockDB(tmp_path)
  
  
  @pytest_asyncio.fixture
  async def db_async(tmp_path) -> AsyncConnection:
      return await lancedb.connect_async(
          tmp_path, read_consistency_interval=timedelta(seconds=0)
      )
  
  
  def test_basic(db):
      ds = LanceTable.create(
          db,
          "test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ],
      ).to_lance()
  
      table = LanceTable(db, "test")
      assert table.name == "test"
      assert table.schema == ds.schema
      assert table.to_lance().to_table() == ds.to_table()
  
  
  @pytest.mark.asyncio
  async def test_close(db_async: AsyncConnection):
      table = await db_async.create_table("some_table", data=[{"id": 0}])
      assert table.is_open()
      table.close()
      assert not table.is_open()
  
      with pytest.raises(Exception, match="Table some_table is closed"):
          await table.count_rows()
      assert str(table) == "ClosedTable(some_table)"
  
  
  @pytest.mark.asyncio
  async def test_update_async(db_async: AsyncConnection):
      table = await db_async.create_table("some_table", data=[{"id": 0}])
      assert await table.count_rows("id == 0") == 1
      assert await table.count_rows("id == 7") == 0
      await table.update({"id": 7})
      assert await table.count_rows("id == 7") == 1
      assert await table.count_rows("id == 0") == 0
      await table.add([{"id": 2}])
      await table.update(where="id % 2 == 0", updates_sql={"id": "5"})
      assert await table.count_rows("id == 7") == 1
      assert await table.count_rows("id == 2") == 0
      assert await table.count_rows("id == 5") == 1
      await table.update({"id": 10}, where="id == 5")
      assert await table.count_rows("id == 10") == 1
  
  
  def test_create_table(db):
      schema = pa.schema(
          [
              pa.field("vector", pa.list_(pa.float32(), 2)),
              pa.field("item", pa.string()),
              pa.field("price", pa.float32()),
          ]
      )
      expected = pa.Table.from_arrays(
          [
              pa.FixedSizeListArray.from_arrays(pa.array([3.1, 4.1, 5.9, 26.5]), 2),
              pa.array(["foo", "bar"]),
              pa.array([10.0, 20.0]),
          ],
          schema=schema,
      )
      data = [
          [
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ]
      ]
      df = pd.DataFrame(data[0])
      data.append(df)
      data.append(pa.Table.from_pandas(df, schema=schema))
  
      for i, d in enumerate(data):
          tbl = (
              LanceTable.create(db, f"test_{i}", data=d, schema=schema)
              .to_lance()
              .to_table()
          )
          assert expected == tbl
  
  
  def test_empty_table(db):
      schema = pa.schema(
          [
              pa.field("vector", pa.list_(pa.float32(), 2)),
              pa.field("item", pa.string()),
              pa.field("price", pa.float32()),
          ]
      )
      tbl = LanceTable.create(db, "test", schema=schema)
      data = [
          {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
          {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
      ]
      tbl.add(data=data)
  
  
  def test_add(db):
      schema = pa.schema(
          [
              pa.field("vector", pa.list_(pa.float32(), 2)),
              pa.field("item", pa.string()),
              pa.field("price", pa.float64()),
          ]
      )
  
      table = LanceTable.create(
          db,
          "test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ],
      )
      _add(table, schema)
  
      table = LanceTable.create(db, "test2", schema=schema)
      table.add(
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ],
      )
      _add(table, schema)
  
  
  def test_add_pydantic_model(db):
      # https://github.com/lancedb/lancedb/issues/562
  
      class Metadata(BaseModel):
          source: str
          timestamp: datetime
  
      class Document(BaseModel):
          content: str
          meta: Metadata
  
      class LanceSchema(LanceModel):
          id: str
          vector: Vector(2)
          li: List[int]
          payload: Document
  
      tbl = LanceTable.create(db, "mytable", schema=LanceSchema, mode="overwrite")
      assert tbl.schema == LanceSchema.to_arrow_schema()
  
      # add works
      expected = LanceSchema(
          id="id",
          vector=[0.0, 0.0],
          li=[1, 2, 3],
          payload=Document(
              content="foo", meta=Metadata(source="bar", timestamp=datetime.now())
          ),
      )
      tbl.add([expected])
  
      result = tbl.search([0.0, 0.0]).limit(1).to_pydantic(LanceSchema)[0]
      assert result == expected
  
      flattened = tbl.search([0.0, 0.0]).limit(1).to_pandas(flatten=1)
      assert len(flattened.columns) == 6  # _distance is automatically added
  
      really_flattened = tbl.search([0.0, 0.0]).limit(1).to_pandas(flatten=True)
      assert len(really_flattened.columns) == 7
  
  
  @pytest.mark.asyncio
  async def test_add_async(db_async: AsyncConnection):
      table = await db_async.create_table(
          "test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ],
      )
      assert await table.count_rows() == 2
      await table.add(
          data=[
              {"vector": [10.0, 11.0], "item": "baz", "price": 30.0},
          ],
      )
      table = await db_async.open_table("test")
      assert await table.count_rows() == 3
  
  
  def test_polars(db):
      data = {
          "vector": [[3.1, 4.1], [5.9, 26.5]],
          "item": ["foo", "bar"],
          "price": [10.0, 20.0],
      }
      # Ingest polars dataframe
      table = LanceTable.create(db, "test", data=pl.DataFrame(data))
      assert len(table) == 2
  
      result = table.to_pandas()
      assert np.allclose(result["vector"].tolist(), data["vector"])
      assert result["item"].tolist() == data["item"]
      assert np.allclose(result["price"].tolist(), data["price"])
  
      schema = pa.schema(
          [
              pa.field("vector", pa.list_(pa.float32(), 2)),
              pa.field("item", pa.large_string()),
              pa.field("price", pa.float64()),
          ]
      )
      assert table.schema == schema
  
      # search results to polars dataframe
      q = [3.1, 4.1]
      result = table.search(q).limit(1).to_polars()
      assert np.allclose(result["vector"][0], q)
      assert result["item"][0] == "foo"
      assert np.allclose(result["price"][0], 10.0)
  
      # enter table to polars dataframe
      result = table.to_polars()
      assert np.allclose(result.collect()["vector"].to_list(), data["vector"])
  
      # make sure filtering isn't broken
      filtered_result = result.filter(pl.col("item").is_in(["foo", "bar"])).collect()
      assert len(filtered_result) == 2
  
  
  def _add(table, schema):
      # table = LanceTable(db, "test")
      assert len(table) == 2
  
      table.add([{"vector": [6.3, 100.5], "item": "new", "price": 30.0}])
      assert len(table) == 3
  
      expected = pa.Table.from_arrays(
          [
              pa.FixedSizeListArray.from_arrays(
                  pa.array([3.1, 4.1, 5.9, 26.5, 6.3, 100.5]), 2
              ),
              pa.array(["foo", "bar", "new"]),
              pa.array([10.0, 20.0, 30.0]),
          ],
          schema=schema,
      )
      assert expected == table.to_arrow()
  
  
  def test_versioning(db):
      table = LanceTable.create(
          db,
          "test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [5.9, 26.5], "item": "bar", "price": 20.0},
          ],
      )
  
      assert len(table.list_versions()) == 2
      assert table.version == 2
  
      table.add([{"vector": [6.3, 100.5], "item": "new", "price": 30.0}])
      assert len(table.list_versions()) == 3
      assert table.version == 3
      assert len(table) == 3
  
      table.checkout(2)
      assert table.version == 2
      assert len(table) == 2
  
  
  def test_create_index_method():
      with patch.object(
          LanceTable, "_dataset_mut", new_callable=PropertyMock
      ) as mock_dataset:
          # Setup mock responses
          mock_dataset.return_value.create_index.return_value = None
  
          # Create a LanceTable object
          connection = LanceDBConnection(uri="mock.uri")
          table = LanceTable(connection, "test_table")
  
          # Call the create_index method
          table.create_index(
              metric="L2",
              num_partitions=256,
              num_sub_vectors=96,
              vector_column_name="vector",
              replace=True,
              index_cache_size=256,
          )
  
          # Check that the _dataset.create_index method was called
          # with the right parameters
          mock_dataset.return_value.create_index.assert_called_once_with(
              column="vector",
              index_type="IVF_PQ",
              metric="L2",
              num_partitions=256,
              num_sub_vectors=96,
              replace=True,
              accelerator=None,
              index_cache_size=256,
          )
  
  
  def test_add_with_nans(db):
      # by default we raise an error on bad input vectors
      bad_data = [
          {"vector": [np.nan], "item": "bar", "price": 20.0},
          {"vector": [5], "item": "bar", "price": 20.0},
          {"vector": [np.nan, np.nan], "item": "bar", "price": 20.0},
          {"vector": [np.nan, 5.0], "item": "bar", "price": 20.0},
      ]
      for row in bad_data:
          with pytest.raises(ValueError):
              LanceTable.create(
                  db,
                  "error_test",
                  data=[{"vector": [3.1, 4.1], "item": "foo", "price": 10.0}, row],
              )
  
      table = LanceTable.create(
          db,
          "drop_test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [np.nan], "item": "bar", "price": 20.0},
              {"vector": [5], "item": "bar", "price": 20.0},
              {"vector": [np.nan, np.nan], "item": "bar", "price": 20.0},
          ],
          on_bad_vectors="drop",
      )
      assert len(table) == 1
  
      # We can fill bad input with some value
      table = LanceTable.create(
          db,
          "fill_test",
          data=[
              {"vector": [3.1, 4.1], "item": "foo", "price": 10.0},
              {"vector": [np.nan], "item": "bar", "price": 20.0},
              {"vector": [np.nan, np.nan], "item": "bar", "price": 20.0},
          ],
          on_bad_vectors="fill",
          fill_value=0.0,
      )
      assert len(table) == 3
      arrow_tbl = table.to_lance().to_table(filter="item == 'bar'")
      v = arrow_tbl["vector"].to_pylist()[0]
      assert np.allclose(v, np.array([0.0, 0.0]))
  
  
  def test_restore(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"vector": [1.1, 0.9], "type": "vector"}],
      )
      table.add([{"vector": [0.5, 0.2], "type": "vector"}])
      table.restore(2)
      assert len(table.list_versions()) == 4
      assert len(table) == 1
  
      expected = table.to_arrow()
      table.checkout(2)
      table.restore()
      assert len(table.list_versions()) == 5
      assert table.to_arrow() == expected
  
      table.restore(5)  # latest version should be no-op
      assert len(table.list_versions()) == 5
  
      with pytest.raises(ValueError):
          table.restore(6)
  
      with pytest.raises(ValueError):
          table.restore(0)
  
  
  def test_merge(db, tmp_path):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"vector": [1.1, 0.9], "id": 0}, {"vector": [1.2, 1.9], "id": 1}],
      )
      other_table = pa.table({"document": ["foo", "bar"], "id": [0, 1]})
      table.merge(other_table, left_on="id")
      assert len(table.list_versions()) == 3
      expected = pa.table(
          {"vector": [[1.1, 0.9], [1.2, 1.9]], "id": [0, 1], "document": ["foo", "bar"]},
          schema=table.schema,
      )
      assert table.to_arrow() == expected
  
      other_dataset = lance.write_dataset(other_table, tmp_path / "other_table.lance")
      table.restore(1)
      table.merge(other_dataset, left_on="id")
  
  
  def test_delete(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"vector": [1.1, 0.9], "id": 0}, {"vector": [1.2, 1.9], "id": 1}],
      )
      assert len(table) == 2
      assert len(table.list_versions()) == 2
      table.delete("id=0")
      assert len(table.list_versions()) == 3
      assert table.version == 3
      assert len(table) == 1
      assert table.to_pandas()["id"].tolist() == [1]
  
  
  def test_update(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"vector": [1.1, 0.9], "id": 0}, {"vector": [1.2, 1.9], "id": 1}],
      )
      assert len(table) == 2
      assert len(table.list_versions()) == 2
      table.update(where="id=0", values={"vector": [1.1, 1.1]})
      assert len(table.list_versions()) == 3
      assert table.version == 3
      assert len(table) == 2
      v = table.to_arrow()["vector"].combine_chunks()
      v = v.values.to_numpy().reshape(2, 2)
      assert np.allclose(v, np.array([[1.2, 1.9], [1.1, 1.1]]))
  
  
  def test_update_types(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[
              {
                  "id": 0,
                  "str": "foo",
                  "float": 1.1,
                  "timestamp": datetime(2021, 1, 1),
                  "date": date(2021, 1, 1),
                  "vector1": [1.0, 0.0],
                  "vector2": [1.0, 1.0],
              }
          ],
      )
      # Update with SQL
      table.update(
          values_sql=dict(
              id="1",
              str="'bar'",
              float="2.2",
              timestamp="TIMESTAMP '2021-01-02 00:00:00'",
              date="DATE '2021-01-02'",
              vector1="[2.0, 2.0]",
              vector2="[3.0, 3.0]",
          )
      )
      actual = table.to_arrow().to_pylist()[0]
      expected = dict(
          id=1,
          str="bar",
          float=2.2,
          timestamp=datetime(2021, 1, 2),
          date=date(2021, 1, 2),
          vector1=[2.0, 2.0],
          vector2=[3.0, 3.0],
      )
      assert actual == expected
  
      # Update with values
      table.update(
          values=dict(
              id=2,
              str="baz",
              float=3.3,
              timestamp=datetime(2021, 1, 3),
              date=date(2021, 1, 3),
              vector1=[3.0, 3.0],
              vector2=np.array([4.0, 4.0]),
          )
      )
      actual = table.to_arrow().to_pylist()[0]
      expected = dict(
          id=2,
          str="baz",
          float=3.3,
          timestamp=datetime(2021, 1, 3),
          date=date(2021, 1, 3),
          vector1=[3.0, 3.0],
          vector2=[4.0, 4.0],
      )
      assert actual == expected
  
  
  def test_merge_insert(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=pa.table({"a": [1, 2, 3], "b": ["a", "b", "c"]}),
      )
      assert len(table) == 3
      version = table.version
  
      new_data = pa.table({"a": [2, 3, 4], "b": ["x", "y", "z"]})
  
      # upsert
      table.merge_insert(
          "a"
      ).when_matched_update_all().when_not_matched_insert_all().execute(new_data)
  
      expected = pa.table({"a": [1, 2, 3, 4], "b": ["a", "x", "y", "z"]})
      assert table.to_arrow().sort_by("a") == expected
  
      table.restore(version)
  
      # conditional update
      table.merge_insert("a").when_matched_update_all(where="target.b = 'b'").execute(
          new_data
      )
      expected = pa.table({"a": [1, 2, 3], "b": ["a", "x", "c"]})
      assert table.to_arrow().sort_by("a") == expected
  
      table.restore(version)
  
      # insert-if-not-exists
      table.merge_insert("a").when_not_matched_insert_all().execute(new_data)
  
      expected = pa.table({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "z"]})
      assert table.to_arrow().sort_by("a") == expected
  
      table.restore(version)
  
      new_data = pa.table({"a": [2, 4], "b": ["x", "z"]})
  
      # replace-range
      table.merge_insert(
          "a"
      ).when_matched_update_all().when_not_matched_insert_all().when_not_matched_by_source_delete(
          "a > 2"
      ).execute(new_data)
  
      expected = pa.table({"a": [1, 2, 4], "b": ["a", "x", "z"]})
      assert table.to_arrow().sort_by("a") == expected
  
      table.restore(version)
  
      # replace-range no condition
      table.merge_insert(
          "a"
      ).when_matched_update_all().when_not_matched_insert_all().when_not_matched_by_source_delete().execute(
          new_data
      )
  
      expected = pa.table({"a": [2, 4], "b": ["x", "z"]})
      assert table.to_arrow().sort_by("a") == expected
  
  
  def test_create_with_embedding_function(db):
      class MyTable(LanceModel):
          text: str
          vector: Vector(10)
  
      func = MockTextEmbeddingFunction()
      texts = ["hello world", "goodbye world", "foo bar baz fizz buzz"]
      df = pd.DataFrame({"text": texts, "vector": func.compute_source_embeddings(texts)})
  
      conf = EmbeddingFunctionConfig(
          source_column="text", vector_column="vector", function=func
      )
      table = LanceTable.create(
          db,
          "my_table",
          schema=MyTable,
          embedding_functions=[conf],
      )
      table.add(df)
  
      query_str = "hi how are you?"
      query_vector = func.compute_query_embeddings(query_str)[0]
      expected = table.search(query_vector).limit(2).to_arrow()
  
      actual = table.search(query_str).limit(2).to_arrow()
      assert actual == expected
  
  
  def test_create_f16_table(db):
      class MyTable(LanceModel):
          text: str
          vector: Vector(128, value_type=pa.float16())
  
      df = pd.DataFrame(
          {
              "text": [f"s-{i}" for i in range(10000)],
              "vector": [np.random.randn(128).astype(np.float16) for _ in range(10000)],
          }
      )
      table = LanceTable.create(
          db,
          "f16_tbl",
          schema=MyTable,
      )
      table.add(df)
      table.create_index(num_partitions=2, num_sub_vectors=8)
  
      query = df.vector.iloc[2]
      expected = table.search(query).limit(2).to_arrow()
  
      assert "s-2" in expected["text"].to_pylist()
  
  
  def test_add_with_embedding_function(db):
      emb = EmbeddingFunctionRegistry.get_instance().get("test")()
  
      class MyTable(LanceModel):
          text: str = emb.SourceField()
          vector: Vector(emb.ndims()) = emb.VectorField()
  
      table = LanceTable.create(db, "my_table", schema=MyTable)
  
      texts = ["hello world", "goodbye world", "foo bar baz fizz buzz"]
      df = pd.DataFrame({"text": texts})
      table.add(df)
  
      texts = ["the quick brown fox", "jumped over the lazy dog"]
      table.add([{"text": t} for t in texts])
  
      query_str = "hi how are you?"
      query_vector = emb.compute_query_embeddings(query_str)[0]
      expected = table.search(query_vector).limit(2).to_arrow()
  
      actual = table.search(query_str).limit(2).to_arrow()
      assert actual == expected
  
  
  def test_multiple_vector_columns(db):
      class MyTable(LanceModel):
          text: str
          vector1: Vector(10)
          vector2: Vector(10)
  
      table = LanceTable.create(
          db,
          "my_table",
          schema=MyTable,
      )
  
      v1 = np.random.randn(10)
      v2 = np.random.randn(10)
      data = [
          {"vector1": v1, "vector2": v2, "text": "foo"},
          {"vector1": v2, "vector2": v1, "text": "bar"},
      ]
      df = pd.DataFrame(data)
      table.add(df)
  
      q = np.random.randn(10)
      result1 = table.search(q, vector_column_name="vector1").limit(1).to_pandas()
      result2 = table.search(q, vector_column_name="vector2").limit(1).to_pandas()
  
      assert result1["text"].iloc[0] != result2["text"].iloc[0]
  
  
  def test_create_scalar_index(db):
      vec_array = pa.array(
          [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], pa.list_(pa.float32(), 2)
      )
      test_data = pa.Table.from_pydict(
          {"x": ["c", "b", "a", "e", "b"], "y": [1, 2, 3, 4, 5], "vector": vec_array}
      )
      table = LanceTable.create(
          db,
          "my_table",
          data=test_data,
      )
      table.create_scalar_index("x")
      indices = table.to_lance().list_indices()
      assert len(indices) == 1
      scalar_index = indices[0]
      assert scalar_index["type"] == "Scalar"
  
      # Confirm that prefiltering still works with the scalar index column
      results = table.search().where("x = 'c'").to_arrow()
      assert results == test_data.slice(0, 1)
      results = table.search([5, 5]).to_arrow()
      assert results["_distance"][0].as_py() == 0
      results = table.search([5, 5]).where("x != 'b'").to_arrow()
      assert results["_distance"][0].as_py() > 0
  
  
  def test_empty_query(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"text": "foo", "id": 0}, {"text": "bar", "id": 1}],
      )
      df = table.search().select(["id"]).where("text='bar'").limit(1).to_pandas()
      val = df.id.iloc[0]
      assert val == 1
  
      table = LanceTable.create(db, "my_table2", data=[{"id": i} for i in range(100)])
      df = table.search().select(["id"]).to_pandas()
      assert len(df) == 10
      df = table.search().select(["id"]).limit(None).to_pandas()
      assert len(df) == 100
      df = table.search().select(["id"]).limit(-1).to_pandas()
      assert len(df) == 100
  
  
  def test_search_with_schema_inf_single_vector(db):
      class MyTable(LanceModel):
          text: str
          vector_col: Vector(10)
  
      table = LanceTable.create(
          db,
          "my_table",
          schema=MyTable,
      )
  
      v1 = np.random.randn(10)
      v2 = np.random.randn(10)
      data = [
          {"vector_col": v1, "text": "foo"},
          {"vector_col": v2, "text": "bar"},
      ]
      df = pd.DataFrame(data)
      table.add(df)
  
      q = np.random.randn(10)
      result1 = table.search(q, vector_column_name="vector_col").limit(1).to_pandas()
      result2 = table.search(q).limit(1).to_pandas()
  
      assert result1["text"].iloc[0] == result2["text"].iloc[0]
  
  
  def test_search_with_schema_inf_multiple_vector(db):
      class MyTable(LanceModel):
          text: str
          vector1: Vector(10)
          vector2: Vector(10)
  
      table = LanceTable.create(
          db,
          "my_table",
          schema=MyTable,
      )
  
      v1 = np.random.randn(10)
      v2 = np.random.randn(10)
      data = [
          {"vector1": v1, "vector2": v2, "text": "foo"},
          {"vector1": v2, "vector2": v1, "text": "bar"},
      ]
      df = pd.DataFrame(data)
      table.add(df)
  
      q = np.random.randn(10)
      with pytest.raises(ValueError):
          table.search(q).limit(1).to_pandas()
  
  
  def test_compact_cleanup(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"text": "foo", "id": 0}, {"text": "bar", "id": 1}],
      )
  
      table.add([{"text": "baz", "id": 2}])
      assert len(table) == 3
      assert table.version == 3
  
      stats = table.compact_files()
      assert len(table) == 3
      # Compact_files bump 2 versions.
      assert table.version == 5
      assert stats.fragments_removed > 0
      assert stats.fragments_added == 1
  
      stats = table.cleanup_old_versions()
      assert stats.bytes_removed == 0
  
      stats = table.cleanup_old_versions(older_than=timedelta(0), delete_unverified=True)
      assert stats.bytes_removed > 0
      assert table.version == 5
  
      with pytest.raises(Exception, match="Version 3 no longer exists"):
          table.checkout(3)
  
  
  def test_count_rows(db):
      table = LanceTable.create(
          db,
          "my_table",
          data=[{"text": "foo", "id": 0}, {"text": "bar", "id": 1}],
      )
      assert len(table) == 2
      assert table.count_rows() == 2
      assert table.count_rows(filter="text='bar'") == 1
  
  
  def test_hybrid_search(db, tmp_path):
      # This test uses an FTS index
      pytest.importorskip("lancedb.fts")
  
      db = MockDB(str(tmp_path))
      # Create a LanceDB table schema with a vector and a text column
      emb = EmbeddingFunctionRegistry.get_instance().get("test")()
  
      class MyTable(LanceModel):
          text: str = emb.SourceField()
          vector: Vector(emb.ndims()) = emb.VectorField()
  
      # Initialize the table using the schema
      table = LanceTable.create(
          db,
          "my_table",
          schema=MyTable,
      )
  
      # Create a list of 10 unique english phrases
      phrases = [
          "great kid don't get cocky",
          "now that's a name I haven't heard in a long time",
          "if you strike me down I shall become more powerful than you imagine",
          "I find your lack of faith disturbing",
          "I've got a bad feeling about this",
          "never tell me the odds",
          "I am your father",
          "somebody has to save our skins",
          "New strategy R2 let the wookiee win",
          "Arrrrggghhhhhhh",
      ]
  
      # Add the phrases and vectors to the table
      table.add([{"text": p} for p in phrases])
  
      # Create a fts index
      table.create_fts_index("text")
  
      result1 = (
          table.search("Our father who art in heaven", query_type="hybrid")
          .rerank(normalize="score")
          .to_pydantic(MyTable)
      )
      result2 = (  # noqa
          table.search("Our father who art in heaven", query_type="hybrid")
          .rerank(normalize="rank")
          .to_pydantic(MyTable)
      )
      result3 = table.search(
          "Our father who art in heaven", query_type="hybrid"
      ).to_pydantic(MyTable)
  
      assert result1 == result3
  
      # with post filters
      result = (
          table.search("Arrrrggghhhhhhh", query_type="hybrid")
          .where("text='Arrrrggghhhhhhh'")
          .to_list()
      )
      len(result) == 1
  
  
  @pytest.mark.parametrize(
      "consistency_interval", [None, timedelta(seconds=0), timedelta(seconds=0.1)]
  )
  def test_consistency(tmp_path, consistency_interval):
      db = lancedb.connect(tmp_path)
      table = LanceTable.create(db, "my_table", data=[{"id": 0}])
  
      db2 = lancedb.connect(tmp_path, read_consistency_interval=consistency_interval)
      table2 = db2.open_table("my_table")
      assert table2.version == table.version
  
      table.add([{"id": 1}])
  
      if consistency_interval is None:
          assert table2.version == table.version - 1
          table2.checkout_latest()
          assert table2.version == table.version
      elif consistency_interval == timedelta(seconds=0):
          assert table2.version == table.version
      else:
          # (consistency_interval == timedelta(seconds=0.1)
          assert table2.version == table.version - 1
          sleep(0.1)
          assert table2.version == table.version
  
  
  def test_restore_consistency(tmp_path):
      db = lancedb.connect(tmp_path)
      table = LanceTable.create(db, "my_table", data=[{"id": 0}])
  
      db2 = lancedb.connect(tmp_path, read_consistency_interval=timedelta(seconds=0))
      table2 = db2.open_table("my_table")
      assert table2.version == table.version
  
      # If we call checkout, it should lose consistency
      table_fixed = copy(table2)
      table_fixed.checkout(table.version)
      # But if we call checkout_latest, it should be consistent again
      table_ref_latest = copy(table_fixed)
      table_ref_latest.checkout_latest()
      table.add([{"id": 2}])
      assert table_fixed.version == table.version - 1
      assert table_ref_latest.version == table.version
  
  
  # Schema evolution
  def test_add_columns(tmp_path):
      db = lancedb.connect(tmp_path)
      data = pa.table({"id": [0, 1]})
      table = LanceTable.create(db, "my_table", data=data)
      table.add_columns({"new_col": "id + 2"})
      assert table.to_arrow().column_names == ["id", "new_col"]
      assert table.to_arrow()["new_col"].to_pylist() == [2, 3]
  
  
  def test_alter_columns(tmp_path):
      db = lancedb.connect(tmp_path)
      data = pa.table({"id": [0, 1]})
      table = LanceTable.create(db, "my_table", data=data)
      table.alter_columns({"path": "id", "rename": "new_id"})
      assert table.to_arrow().column_names == ["new_id"]
  
  
  def test_drop_columns(tmp_path):
      db = lancedb.connect(tmp_path)
      data = pa.table({"id": [0, 1], "category": ["a", "b"]})
      table = LanceTable.create(db, "my_table", data=data)
      table.drop_columns(["category"])
      assert table.to_arrow().column_names == ["id"]
  
  
  @pytest.mark.asyncio
  async def test_time_travel(db_async: AsyncConnection):
      # Setup
      table = await db_async.create_table("some_table", data=[{"id": 0}])
      version = await table.version()
      await table.add([{"id": 1}])
      assert await table.count_rows() == 2
      # Make sure we can rewind
      await table.checkout(version)
      assert await table.count_rows() == 1
      # Can't add data in time travel mode
      with pytest.raises(
          ValueError,
          match="table cannot be modified when a specific version is checked out",
      ):
          await table.add([{"id": 2}])
      # Can go back to normal mode
      await table.checkout_latest()
      assert await table.count_rows() == 2
      # Should be able to add data again
      await table.add([{"id": 3}])
      assert await table.count_rows() == 3
      # Now checkout and restore
      await table.checkout(version)
      await table.restore()
      assert await table.count_rows() == 1
      # Should be able to add data
      await table.add([{"id": 4}])
      assert await table.count_rows() == 2
      # Can't use restore if not checked out
      with pytest.raises(ValueError, match="checkout before running restore"):
          await table.restore()
  
apis: lancedb.embeddings.EmbeddingFunctionRegistry.get_instance lancedb.conftest.MockTextEmbeddingFunction lancedb.db.LanceDBConnection lancedb.embeddings.EmbeddingFunctionConfig lancedb.table.LanceTable lancedb.pydantic.Vector lancedb.connect lancedb.table.LanceTable.create
extract_api: [((1992, 2014), 'lancedb.table.LanceTable', 'LanceTable', (['db', '"""test"""'], {}), "(db, 'test')\n", (2002, 2014), False, 'from lancedb.table import LanceTable\n'), ((3907, 3928), 'pandas.DataFrame', 'pd.DataFrame', (['data[0]'], {}), '(data[0])\n', (3919, 3928), True, 'import pandas as pd\n'), ((4450, 4494), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""test"""'], {'schema': 'schema'}), "(db, 'test', schema=schema)\n", (4467, 4494), False, 'from lancedb.table import LanceTable\n'), ((4892, 5041), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, {'vector': [5.9, \n    26.5], 'item': 'bar', 'price': 20.0}]"}), "(db, 'test', data=[{'vector': [3.1, 4.1], 'item': 'foo',\n    'price': 10.0}, {'vector': [5.9, 26.5], 'item': 'bar', 'price': 20.0}])\n", (4909, 5041), False, 'from lancedb.table import LanceTable\n'), ((5141, 5186), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""test2"""'], {'schema': 'schema'}), "(db, 'test2', schema=schema)\n", (5158, 5186), False, 'from lancedb.table import LanceTable\n'), ((5771, 5841), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""mytable"""'], {'schema': 'LanceSchema', 'mode': '"""overwrite"""'}), "(db, 'mytable', schema=LanceSchema, mode='overwrite')\n", (5788, 5841), False, 'from lancedb.table import LanceTable\n'), ((7925, 7960), 'numpy.allclose', 'np.allclose', (["result['vector'][0]", 'q'], {}), "(result['vector'][0], q)\n", (7936, 7960), True, 'import numpy as np\n'), ((8010, 8047), 'numpy.allclose', 'np.allclose', (["result['price'][0]", '(10.0)'], {}), "(result['price'][0], 10.0)\n", (8021, 8047), True, 'import numpy as np\n'), ((8926, 9075), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, {'vector': [5.9, \n    26.5], 'item': 'bar', 'price': 20.0}]"}), "(db, 'test', data=[{'vector': [3.1, 4.1], 'item': 'foo',\n    'price': 10.0}, {'vector': [5.9, 26.5], 'item': 'bar', 'price': 20.0}])\n", (8943, 9075), False, 'from lancedb.table import LanceTable\n'), ((11156, 11447), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""drop_test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, {'vector': [np.nan],\n    'item': 'bar', 'price': 20.0}, {'vector': [5], 'item': 'bar', 'price': \n    20.0}, {'vector': [np.nan, np.nan], 'item': 'bar', 'price': 20.0}]", 'on_bad_vectors': '"""drop"""'}), "(db, 'drop_test', data=[{'vector': [3.1, 4.1], 'item':\n    'foo', 'price': 10.0}, {'vector': [np.nan], 'item': 'bar', 'price': \n    20.0}, {'vector': [5], 'item': 'bar', 'price': 20.0}, {'vector': [np.\n    nan, np.nan], 'item': 'bar', 'price': 20.0}], on_bad_vectors='drop')\n", (11173, 11447), False, 'from lancedb.table import LanceTable\n'), ((11616, 11875), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""fill_test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, {'vector': [np.nan],\n    'item': 'bar', 'price': 20.0}, {'vector': [np.nan, np.nan], 'item':\n    'bar', 'price': 20.0}]", 'on_bad_vectors': '"""fill"""', 'fill_value': '(0.0)'}), "(db, 'fill_test', data=[{'vector': [3.1, 4.1], 'item':\n    'foo', 'price': 10.0}, {'vector': [np.nan], 'item': 'bar', 'price': \n    20.0}, {'vector': [np.nan, np.nan], 'item': 'bar', 'price': 20.0}],\n    on_bad_vectors='fill', fill_value=0.0)\n", (11633, 11875), False, 'from lancedb.table import LanceTable\n'), ((12177, 12263), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'vector': [1.1, 0.9], 'type': 'vector'}]"}), "(db, 'my_table', data=[{'vector': [1.1, 0.9], 'type':\n    'vector'}])\n", (12194, 12263), False, 'from lancedb.table import LanceTable\n'), ((12865, 12976), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'vector': [1.1, 0.9], 'id': 0}, {'vector': [1.2, 1.9], 'id': 1}]"}), "(db, 'my_table', data=[{'vector': [1.1, 0.9], 'id': 0}, {\n    'vector': [1.2, 1.9], 'id': 1}])\n", (12882, 12976), False, 'from lancedb.table import LanceTable\n'), ((13021, 13073), 'pyarrow.table', 'pa.table', (["{'document': ['foo', 'bar'], 'id': [0, 1]}"], {}), "({'document': ['foo', 'bar'], 'id': [0, 1]})\n", (13029, 13073), True, 'import pyarrow as pa\n'), ((13175, 13289), 'pyarrow.table', 'pa.table', (["{'vector': [[1.1, 0.9], [1.2, 1.9]], 'id': [0, 1], 'document': ['foo', 'bar']}"], {'schema': 'table.schema'}), "({'vector': [[1.1, 0.9], [1.2, 1.9]], 'id': [0, 1], 'document': [\n    'foo', 'bar']}, schema=table.schema)\n", (13183, 13289), True, 'import pyarrow as pa\n'), ((13369, 13433), 'lance.write_dataset', 'lance.write_dataset', (['other_table', "(tmp_path / 'other_table.lance')"], {}), "(other_table, tmp_path / 'other_table.lance')\n", (13388, 13433), False, 'import lance\n'), ((13535, 13646), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'vector': [1.1, 0.9], 'id': 0}, {'vector': [1.2, 1.9], 'id': 1}]"}), "(db, 'my_table', data=[{'vector': [1.1, 0.9], 'id': 0}, {\n    'vector': [1.2, 1.9], 'id': 1}])\n", (13552, 13646), False, 'from lancedb.table import LanceTable\n'), ((13954, 14065), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'vector': [1.1, 0.9], 'id': 0}, {'vector': [1.2, 1.9], 'id': 1}]"}), "(db, 'my_table', data=[{'vector': [1.1, 0.9], 'id': 0}, {\n    'vector': [1.2, 1.9], 'id': 1}])\n", (13971, 14065), False, 'from lancedb.table import LanceTable\n'), ((16294, 16342), 'pyarrow.table', 'pa.table', (["{'a': [2, 3, 4], 'b': ['x', 'y', 'z']}"], {}), "({'a': [2, 3, 4], 'b': ['x', 'y', 'z']})\n", (16302, 16342), True, 'import pyarrow as pa\n'), ((16489, 16545), 'pyarrow.table', 'pa.table', (["{'a': [1, 2, 3, 4], 'b': ['a', 'x', 'y', 'z']}"], {}), "({'a': [1, 2, 3, 4], 'b': ['a', 'x', 'y', 'z']})\n", (16497, 16545), True, 'import pyarrow as pa\n'), ((16776, 16824), 'pyarrow.table', 'pa.table', (["{'a': [1, 2, 3], 'b': ['a', 'x', 'c']}"], {}), "({'a': [1, 2, 3], 'b': ['a', 'x', 'c']})\n", (16784, 16824), True, 'import pyarrow as pa\n'), ((17026, 17082), 'pyarrow.table', 'pa.table', (["{'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'z']}"], {}), "({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'z']})\n", (17034, 17082), True, 'import pyarrow as pa\n'), ((17180, 17220), 'pyarrow.table', 'pa.table', (["{'a': [2, 4], 'b': ['x', 'z']}"], {}), "({'a': [2, 4], 'b': ['x', 'z']})\n", (17188, 17220), True, 'import pyarrow as pa\n'), ((17431, 17479), 'pyarrow.table', 'pa.table', (["{'a': [1, 2, 4], 'b': ['a', 'x', 'z']}"], {}), "({'a': [1, 2, 4], 'b': ['a', 'x', 'z']})\n", (17439, 17479), True, 'import pyarrow as pa\n'), ((17777, 17817), 'pyarrow.table', 'pa.table', (["{'a': [2, 4], 'b': ['x', 'z']}"], {}), "({'a': [2, 4], 'b': ['x', 'z']})\n", (17785, 17817), True, 'import pyarrow as pa\n'), ((18006, 18033), 'lancedb.conftest.MockTextEmbeddingFunction', 'MockTextEmbeddingFunction', ([], {}), '()\n', (18031, 18033), False, 'from lancedb.conftest import MockTextEmbeddingFunction\n'), ((18204, 18292), 'lancedb.embeddings.EmbeddingFunctionConfig', 'EmbeddingFunctionConfig', ([], {'source_column': '"""text"""', 'vector_column': '"""vector"""', 'function': 'func'}), "(source_column='text', vector_column='vector',\n    function=func)\n", (18227, 18292), False, 'from lancedb.embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry\n'), ((18315, 18392), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable', 'embedding_functions': '[conf]'}), "(db, 'my_table', schema=MyTable, embedding_functions=[conf])\n", (18332, 18392), False, 'from lancedb.table import LanceTable\n'), ((19036, 19084), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""f16_tbl"""'], {'schema': 'MyTable'}), "(db, 'f16_tbl', schema=MyTable)\n", (19053, 19084), False, 'from lancedb.table import LanceTable\n'), ((19578, 19627), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable'}), "(db, 'my_table', schema=MyTable)\n", (19595, 19627), False, 'from lancedb.table import LanceTable\n'), ((19708, 19737), 'pandas.DataFrame', 'pd.DataFrame', (["{'text': texts}"], {}), "({'text': texts})\n", (19720, 19737), True, 'import pandas as pd\n'), ((20270, 20319), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable'}), "(db, 'my_table', schema=MyTable)\n", (20287, 20319), False, 'from lancedb.table import LanceTable\n'), ((20361, 20380), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (20376, 20380), True, 'import numpy as np\n'), ((20390, 20409), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (20405, 20409), True, 'import numpy as np\n'), ((20548, 20566), 'pandas.DataFrame', 'pd.DataFrame', (['data'], {}), '(data)\n', (20560, 20566), True, 'import pandas as pd\n'), ((20594, 20613), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (20609, 20613), True, 'import numpy as np\n'), ((20999, 21100), 'pyarrow.Table.from_pydict', 'pa.Table.from_pydict', (["{'x': ['c', 'b', 'a', 'e', 'b'], 'y': [1, 2, 3, 4, 5], 'vector': vec_array}"], {}), "({'x': ['c', 'b', 'a', 'e', 'b'], 'y': [1, 2, 3, 4, 5],\n    'vector': vec_array})\n", (21019, 21100), True, 'import pyarrow as pa\n'), ((21123, 21172), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': 'test_data'}), "(db, 'my_table', data=test_data)\n", (21140, 21172), False, 'from lancedb.table import LanceTable\n'), ((21808, 21904), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'text': 'foo', 'id': 0}, {'text': 'bar', 'id': 1}]"}), "(db, 'my_table', data=[{'text': 'foo', 'id': 0}, {'text':\n    'bar', 'id': 1}])\n", (21825, 21904), False, 'from lancedb.table import LanceTable\n'), ((22540, 22589), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable'}), "(db, 'my_table', schema=MyTable)\n", (22557, 22589), False, 'from lancedb.table import LanceTable\n'), ((22631, 22650), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (22646, 22650), True, 'import numpy as np\n'), ((22660, 22679), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (22675, 22679), True, 'import numpy as np\n'), ((22794, 22812), 'pandas.DataFrame', 'pd.DataFrame', (['data'], {}), '(data)\n', (22806, 22812), True, 'import pandas as pd\n'), ((22840, 22859), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (22855, 22859), True, 'import numpy as np\n'), ((23231, 23280), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable'}), "(db, 'my_table', schema=MyTable)\n", (23248, 23280), False, 'from lancedb.table import LanceTable\n'), ((23322, 23341), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (23337, 23341), True, 'import numpy as np\n'), ((23351, 23370), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (23366, 23370), True, 'import numpy as np\n'), ((23509, 23527), 'pandas.DataFrame', 'pd.DataFrame', (['data'], {}), '(data)\n', (23521, 23527), True, 'import pandas as pd\n'), ((23555, 23574), 'numpy.random.randn', 'np.random.randn', (['(10)'], {}), '(10)\n', (23570, 23574), True, 'import numpy as np\n'), ((23700, 23796), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'text': 'foo', 'id': 0}, {'text': 'bar', 'id': 1}]"}), "(db, 'my_table', data=[{'text': 'foo', 'id': 0}, {'text':\n    'bar', 'id': 1}])\n", (23717, 23796), False, 'from lancedb.table import LanceTable\n'), ((24499, 24595), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'text': 'foo', 'id': 0}, {'text': 'bar', 'id': 1}]"}), "(db, 'my_table', data=[{'text': 'foo', 'id': 0}, {'text':\n    'bar', 'id': 1}])\n", (24516, 24595), False, 'from lancedb.table import LanceTable\n'), ((24817, 24851), 'pytest.importorskip', 'pytest.importorskip', (['"""lancedb.fts"""'], {}), "('lancedb.fts')\n", (24836, 24851), False, 'import pytest\n'), ((25200, 25249), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'schema': 'MyTable'}), "(db, 'my_table', schema=MyTable)\n", (25217, 25249), False, 'from lancedb.table import LanceTable\n'), ((26793, 26818), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {}), '(tmp_path)\n', (26808, 26818), False, 'import lancedb\n'), ((26831, 26882), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'id': 0}]"}), "(db, 'my_table', data=[{'id': 0}])\n", (26848, 26882), False, 'from lancedb.table import LanceTable\n'), ((26894, 26967), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {'read_consistency_interval': 'consistency_interval'}), '(tmp_path, read_consistency_interval=consistency_interval)\n', (26909, 26967), False, 'import lancedb\n'), ((27586, 27611), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {}), '(tmp_path)\n', (27601, 27611), False, 'import lancedb\n'), ((27624, 27675), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': "[{'id': 0}]"}), "(db, 'my_table', data=[{'id': 0}])\n", (27641, 27675), False, 'from lancedb.table import LanceTable\n'), ((27917, 27929), 'copy.copy', 'copy', (['table2'], {}), '(table2)\n', (27921, 27929), False, 'from copy import copy\n'), ((28061, 28078), 'copy.copy', 'copy', (['table_fixed'], {}), '(table_fixed)\n', (28065, 28078), False, 'from copy import copy\n'), ((28312, 28337), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {}), '(tmp_path)\n', (28327, 28337), False, 'import lancedb\n'), ((28349, 28373), 'pyarrow.table', 'pa.table', (["{'id': [0, 1]}"], {}), "({'id': [0, 1]})\n", (28357, 28373), True, 'import pyarrow as pa\n'), ((28386, 28430), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': 'data'}), "(db, 'my_table', data=data)\n", (28403, 28430), False, 'from lancedb.table import LanceTable\n'), ((28644, 28669), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {}), '(tmp_path)\n', (28659, 28669), False, 'import lancedb\n'), ((28681, 28705), 'pyarrow.table', 'pa.table', (["{'id': [0, 1]}"], {}), "({'id': [0, 1]})\n", (28689, 28705), True, 'import pyarrow as pa\n'), ((28718, 28762), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': 'data'}), "(db, 'my_table', data=data)\n", (28735, 28762), False, 'from lancedb.table import LanceTable\n'), ((28922, 28947), 'lancedb.connect', 'lancedb.connect', (['tmp_path'], {}), '(tmp_path)\n', (28937, 28947), False, 'import lancedb\n'), ((28959, 29007), 'pyarrow.table', 'pa.table', (["{'id': [0, 1], 'category': ['a', 'b']}"], {}), "({'id': [0, 1], 'category': ['a', 'b']})\n", (28967, 29007), True, 'import pyarrow as pa\n'), ((29020, 29064), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""my_table"""'], {'data': 'data'}), "(db, 'my_table', data=data)\n", (29037, 29064), False, 'from lancedb.table import LanceTable\n'), ((2370, 2430), 'pytest.raises', 'pytest.raises', (['Exception'], {'match': '"""Table some_table is closed"""'}), "(Exception, match='Table some_table is closed')\n", (2383, 2430), False, 'import pytest\n'), ((3965, 4004), 'pyarrow.Table.from_pandas', 'pa.Table.from_pandas', (['df'], {'schema': 'schema'}), '(df, schema=schema)\n', (3985, 4004), True, 'import pyarrow as pa\n'), ((5702, 5711), 'lancedb.pydantic.Vector', 'Vector', (['(2)'], {}), '(2)\n', (5708, 5711), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((9508, 9575), 'unittest.mock.patch.object', 'patch.object', (['LanceTable', '"""_dataset_mut"""'], {'new_callable': 'PropertyMock'}), "(LanceTable, '_dataset_mut', new_callable=PropertyMock)\n", (9520, 9575), False, 'from unittest.mock import PropertyMock, patch\n'), ((9764, 9797), 'lancedb.db.LanceDBConnection', 'LanceDBConnection', ([], {'uri': '"""mock.uri"""'}), "(uri='mock.uri')\n", (9781, 9797), False, 'from lancedb.db import AsyncConnection, LanceDBConnection\n'), ((9814, 9850), 'lancedb.table.LanceTable', 'LanceTable', (['connection', '"""test_table"""'], {}), "(connection, 'test_table')\n", (9824, 9850), False, 'from lancedb.table import LanceTable\n'), ((12119, 12139), 'numpy.array', 'np.array', (['[0.0, 0.0]'], {}), '([0.0, 0.0])\n', (12127, 12139), True, 'import numpy as np\n'), ((12707, 12732), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (12720, 12732), False, 'import pytest\n'), ((12769, 12794), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (12782, 12794), False, 'import pytest\n'), ((14444, 14478), 'numpy.array', 'np.array', (['[[1.2, 1.9], [1.1, 1.1]]'], {}), '([[1.2, 1.9], [1.1, 1.1]])\n', (14452, 14478), True, 'import numpy as np\n'), ((17983, 17993), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (17989, 17993), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((20218, 20228), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (20224, 20228), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((20246, 20256), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (20252, 20256), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((22516, 22526), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (22522, 22526), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((23179, 23189), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (23185, 23189), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((23207, 23217), 'lancedb.pydantic.Vector', 'Vector', (['(10)'], {}), '(10)\n', (23213, 23217), False, 'from lancedb.pydantic import LanceModel, Vector\n'), ((23584, 23609), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (23597, 23609), False, 'import pytest\n'), ((24372, 24432), 'pytest.raises', 'pytest.raises', (['Exception'], {'match': '"""Version 3 no longer exists"""'}), "(Exception, match='Version 3 no longer exists')\n", (24385, 24432), False, 'import pytest\n'), ((26682, 26702), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (26691, 26702), False, 'from datetime import date, datetime, timedelta\n'), ((26704, 26726), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0.1)'}), '(seconds=0.1)\n', (26713, 26726), False, 'from datetime import date, datetime, timedelta\n'), ((29580, 29683), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""table cannot be modified when a specific version is checked out"""'}), "(ValueError, match=\n    'table cannot be modified when a specific version is checked out')\n", (29593, 29683), False, 'import pytest\n'), ((30252, 30318), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""checkout before running restore"""'}), "(ValueError, match='checkout before running restore')\n", (30265, 30318), False, 'import pytest\n'), ((1756, 1905), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, {'vector': [5.9, \n    26.5], 'item': 'bar', 'price': 20.0}]"}), "(db, 'test', data=[{'vector': [3.1, 4.1], 'item': 'foo',\n    'price': 10.0}, {'vector': [5.9, 26.5], 'item': 'bar', 'price': 20.0}])\n", (1773, 1905), False, 'from lancedb.table import LanceTable\n'), ((3624, 3648), 'pyarrow.array', 'pa.array', (["['foo', 'bar']"], {}), "(['foo', 'bar'])\n", (3632, 3648), True, 'import pyarrow as pa\n'), ((3662, 3684), 'pyarrow.array', 'pa.array', (['[10.0, 20.0]'], {}), '([10.0, 20.0])\n', (3670, 3684), True, 'import pyarrow as pa\n'), ((7305, 7323), 'polars.DataFrame', 'pl.DataFrame', (['data'], {}), '(data)\n', (7317, 7323), True, 'import polars as pl\n'), ((8732, 8763), 'pyarrow.array', 'pa.array', (["['foo', 'bar', 'new']"], {}), "(['foo', 'bar', 'new'])\n", (8740, 8763), True, 'import pyarrow as pa\n'), ((8777, 8805), 'pyarrow.array', 'pa.array', (['[10.0, 20.0, 30.0]'], {}), '([10.0, 20.0, 30.0])\n', (8785, 8805), True, 'import pyarrow as pa\n'), ((10939, 10964), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (10952, 10964), False, 'import pytest\n'), ((10978, 11083), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', '"""error_test"""'], {'data': "[{'vector': [3.1, 4.1], 'item': 'foo', 'price': 10.0}, row]"}), "(db, 'error_test', data=[{'vector': [3.1, 4.1], 'item':\n    'foo', 'price': 10.0}, row])\n", (10995, 11083), False, 'from lancedb.table import LanceTable\n'), ((15338, 15358), 'datetime.datetime', 'datetime', (['(2021)', '(1)', '(2)'], {}), '(2021, 1, 2)\n', (15346, 15358), False, 'from datetime import date, datetime, timedelta\n'), ((15373, 15389), 'datetime.date', 'date', (['(2021)', '(1)', '(2)'], {}), '(2021, 1, 2)\n', (15377, 15389), False, 'from datetime import date, datetime, timedelta\n'), ((15917, 15937), 'datetime.datetime', 'datetime', (['(2021)', '(1)', '(3)'], {}), '(2021, 1, 3)\n', (15925, 15937), False, 'from datetime import date, datetime, timedelta\n'), ((15952, 15968), 'datetime.date', 'date', (['(2021)', '(1)', '(3)'], {}), '(2021, 1, 3)\n', (15956, 15968), False, 'from datetime import date, datetime, timedelta\n'), ((16167, 16215), 'pyarrow.table', 'pa.table', (["{'a': [1, 2, 3], 'b': ['a', 'b', 'c']}"], {}), "({'a': [1, 2, 3], 'b': ['a', 'b', 'c']})\n", (16175, 16215), True, 'import pyarrow as pa\n'), ((20960, 20972), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (20970, 20972), True, 'import pyarrow as pa\n'), ((24259, 24271), 'datetime.timedelta', 'timedelta', (['(0)'], {}), '(0)\n', (24268, 24271), False, 'from datetime import date, datetime, timedelta\n'), ((27281, 27301), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (27290, 27301), False, 'from datetime import date, datetime, timedelta\n'), ((27477, 27487), 'time.sleep', 'sleep', (['(0.1)'], {}), '(0.1)\n', (27482, 27487), False, 'from time import sleep\n'), ((27739, 27759), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (27748, 27759), False, 'from datetime import date, datetime, timedelta\n'), ((1698, 1718), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(0)'}), '(seconds=0)\n', (1707, 1718), False, 'from datetime import date, datetime, timedelta\n'), ((3407, 3418), 'pyarrow.string', 'pa.string', ([], {}), '()\n', (3416, 3418), True, 'import pyarrow as pa\n'), ((3451, 3463), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (3461, 3463), True, 'import pyarrow as pa\n'), ((3575, 3606), 'pyarrow.array', 'pa.array', (['[3.1, 4.1, 5.9, 26.5]'], {}), '([3.1, 4.1, 5.9, 26.5])\n', (3583, 3606), True, 'import pyarrow as pa\n'), ((4365, 4376), 'pyarrow.string', 'pa.string', ([], {}), '()\n', (4374, 4376), True, 'import pyarrow as pa\n'), ((4409, 4421), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (4419, 4421), True, 'import pyarrow as pa\n'), ((4804, 4815), 'pyarrow.string', 'pa.string', ([], {}), '()\n', (4813, 4815), True, 'import pyarrow as pa\n'), ((4848, 4860), 'pyarrow.float64', 'pa.float64', ([], {}), '()\n', (4858, 4860), True, 'import pyarrow as pa\n'), ((7688, 7705), 'pyarrow.large_string', 'pa.large_string', ([], {}), '()\n', (7703, 7705), True, 'import pyarrow as pa\n'), ((7738, 7750), 'pyarrow.float64', 'pa.float64', ([], {}), '()\n', (7748, 7750), True, 'import pyarrow as pa\n'), ((8658, 8701), 'pyarrow.array', 'pa.array', (['[3.1, 4.1, 5.9, 26.5, 6.3, 100.5]'], {}), '([3.1, 4.1, 5.9, 26.5, 6.3, 100.5])\n', (8666, 8701), True, 'import pyarrow as pa\n'), ((18819, 18831), 'pyarrow.float16', 'pa.float16', ([], {}), '()\n', (18829, 18831), True, 'import pyarrow as pa\n'), ((19384, 19424), 'lancedb.embeddings.EmbeddingFunctionRegistry.get_instance', 'EmbeddingFunctionRegistry.get_instance', ([], {}), '()\n', (19422, 19424), False, 'from lancedb.embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry\n'), ((24962, 25002), 'lancedb.embeddings.EmbeddingFunctionRegistry.get_instance', 'EmbeddingFunctionRegistry.get_instance', ([], {}), '()\n', (25000, 25002), False, 'from lancedb.embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry\n'), ((3359, 3371), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (3369, 3371), True, 'import pyarrow as pa\n'), ((4317, 4329), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (4327, 4329), True, 'import pyarrow as pa\n'), ((4756, 4768), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (4766, 4768), True, 'import pyarrow as pa\n'), ((7640, 7652), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (7650, 7652), True, 'import pyarrow as pa\n'), ((14715, 14735), 'datetime.datetime', 'datetime', (['(2021)', '(1)', '(1)'], {}), '(2021, 1, 1)\n', (14723, 14735), False, 'from datetime import date, datetime, timedelta\n'), ((14761, 14777), 'datetime.date', 'date', (['(2021)', '(1)', '(1)'], {}), '(2021, 1, 1)\n', (14765, 14777), False, 'from datetime import date, datetime, timedelta\n'), ((15634, 15654), 'datetime.datetime', 'datetime', (['(2021)', '(1)', '(3)'], {}), '(2021, 1, 3)\n', (15642, 15654), False, 'from datetime import date, datetime, timedelta\n'), ((15673, 15689), 'datetime.date', 'date', (['(2021)', '(1)', '(3)'], {}), '(2021, 1, 3)\n', (15677, 15689), False, 'from datetime import date, datetime, timedelta\n'), ((15743, 15763), 'numpy.array', 'np.array', (['[4.0, 4.0]'], {}), '([4.0, 4.0])\n', (15751, 15763), True, 'import numpy as np\n'), ((4068, 4125), 'lancedb.table.LanceTable.create', 'LanceTable.create', (['db', 'f"""test_{i}"""'], {'data': 'd', 'schema': 'schema'}), "(db, f'test_{i}', data=d, schema=schema)\n", (4085, 4125), False, 'from lancedb.table import LanceTable\n'), ((8271, 8285), 'polars.col', 'pl.col', (['"""item"""'], {}), "('item')\n", (8277, 8285), True, 'import polars as pl\n'), ((18944, 18964), 'numpy.random.randn', 'np.random.randn', (['(128)'], {}), '(128)\n', (18959, 18964), True, 'import numpy as np\n'), ((6099, 6113), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (6111, 6113), False, 'from datetime import date, datetime, timedelta\n')]