Files
lancedb/python/python/lancedb/table.py
T
Alex Castillo GonzálezandClaude Sonnet 5.5 d31c1568e0 fix(python)!: compute JinaEmbeddings.ndims() per model (#4840)
BREAKING CHANGE: `JinaEmbeddings.ndims()` previously returned 768 for
unlisted model names. It now raises `ValueError`. When creating a schema
with an unlisted model, pass its documented output width to
`Vector(width)`.

Fixes the `# TODO: fix hardcoding` in `JinaEmbeddings.ndims()` (same
kind of fix as #4737, which made the OpenAI-compatible models use their
real dimension).

**Root cause:** `ndims()` returned 768 for every model. Any model whose
default output is not 768 (jina-clip-v2 and jina-embeddings-v3: 1024,
jina-embeddings-v4: 2048, jina-embeddings-v2-small-en: 512) produced a
vector column of the wrong size.

**Fix:** per-model lookup table of default output dimensions, only for
models whose dimension is documented (model configs on Hugging Face
`hidden_size`/`projection_dim`, and https://jina.ai/models/ pages).
Unknown models raise a `ValueError` listing the supported names instead
of silently guessing 768. The default `jina-clip-v1` still returns 768.
When appending to a table, generated vectors are validated against the
width of the stored fixed-size column, so existing tables that use an
unlisted model keep working.

**Test:** `python/tests/test_jina_embeddings.py` (no network, no API
key), including reopening a table with an unlisted model, appending a
matching vector and rejecting a mismatched width.

This change was prepared with an AI coding assistant (Claude).

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-30 20:23:42 +08:00

11246 lines
437 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
from __future__ import annotations
import asyncio
import inspect
import json
import deprecation
import warnings
from abc import ABC, abstractmethod
from collections.abc import Mapping
from dataclasses import dataclass
from datetime import datetime, timedelta
from functools import cached_property
from typing import (
TYPE_CHECKING,
Any,
AsyncIterator,
Callable,
Dict,
Iterable,
Iterator,
List,
Literal,
Optional,
Sequence,
Tuple,
TypedDict,
Union,
overload,
)
from urllib.parse import urlparse
from lancedb.scannable import _hf_batches, _register_optional_converters, to_scannable
from . import __version__
from ._lancedb import FunctionColumnStatus, _parse_field_path
from ._blob import (
BlobFile,
_normalize_blob_descriptor,
_normalize_blob_row_ids,
_validate_blob_buffer_size,
_wrap_blob_files,
strip_auto_row_ids,
validate_blob_mode,
)
from .types import BlobMode
from lancedb.arrow import AsyncRecordBatchReader, peek_reader
from lancedb.sql import QueryParameters, to_parameter_batch
from lancedb.background_loop import LOOP, embedding_executor
from lancedb.prewarm import PrewarmDataPlan, PrewarmDataResult
from lancedb.job import AsyncJob, Job, _typed_job
from lancedb.compaction import CompactionResult
from lancedb.cleanup import CleanupPlan, CleanupResult, _validate_min_age_seconds
from .dependencies import (
_check_for_hugging_face,
_check_for_lance,
_check_for_pandas,
lance,
pandas as pd,
polars as pl,
)
import pyarrow as pa
import pyarrow.dataset
import pyarrow.compute as pc
import pyarrow.fs as pa_fs
import numpy as np
from .common import DATA, VEC, VECTOR_COLUMN_NAME
from .embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry
from .embeddings.registry import _embedding_column_pairs
from .index import (
BTree,
IvfFlat,
IvfPq,
IvfSq,
Bitmap,
IvfRq,
LabelList,
Fm,
NGram,
ZoneMap,
RTree,
ScalarPlugin,
HnswPq,
HnswSq,
HnswFlat,
FTS,
)
from .expr import Expr
from .functions import (
FunctionErrors,
AsyncFunctionErrors,
FunctionProvenance,
FunctionRetryPolicy,
FunctionApplication,
RefreshColumnResult as RefreshColumnJobResult,
)
from .merge import LanceMergeInsertBuilder
from .pydantic import LanceModel, model_to_dict
from .query import (
FullTextOperator,
AnalyzePlanDistributedMetrics,
AsyncFTSQuery,
AsyncHybridQuery,
AsyncQuery,
AsyncTakeQuery,
AsyncVectorQuery,
DocumentGranularity,
FullTextQuery,
LanceEmptyQueryBuilder,
LanceFtsQueryBuilder,
LanceHybridQueryBuilder,
LanceQueryBuilder,
LanceVectorQueryBuilder,
LanceTakeQueryBuilder,
Query,
)
from .util import (
add_note,
fs_from_uri,
get_uri_scheme,
infer_vector_column_name,
join_uri,
value_to_sql,
)
from .index import lang_mapping
from .schema import (
_restore_blob_extensions,
blob_v2_column_paths,
is_blob_v2_field,
row_addressable_blob_v2_paths,
schema_has_blob_field,
)
from .resources import _resource_options
def _should_push_down_query_table(
namespace_client: Optional[Any], pushdown_operations: set
) -> bool:
return namespace_client is not None and "QueryTable" in pushdown_operations
def _requires_local_namespace_execution(query: Query) -> bool:
# The namespace QueryTable request does not support these query options.
return (
query.take_offsets is not None
or query.take_row_ids is not None
or query.search_effort is not None
or query.approx_mode is not None
# The namespace QueryTable request has no order_by field. Pushing a
# nonempty ordering down would drop it and return the wrong top-K rows.
or bool(query.order_by)
# An ordered scan is a local manifest-order guarantee. The request has
# no scan-order field, so run it locally where storage is available.
or query.scan_in_order is True
or (
query.full_text_query is not None
and query.fast_search is True
and query.vector is None
)
)
_CUDA_UNAVAILABLE_MESSAGE = (
"CUDA acceleration is unavailable in this Python environment. Install a "
"CUDA-enabled PyTorch build and verify that the CUDA device is accessible, or "
"omit `accelerator` (or set `accelerator=None`) to create the index on CPU."
)
def _validate_index_accelerator(accelerator: Any) -> None:
if not isinstance(accelerator, str) or accelerator.split(":", 1)[0] != "cuda":
return
try:
import torch
cuda_available = torch.cuda.is_available()
except (ImportError, RuntimeError, AssertionError) as err:
raise RuntimeError(_CUDA_UNAVAILABLE_MESSAGE) from err
if not cuda_available:
raise RuntimeError(_CUDA_UNAVAILABLE_MESSAGE)
try:
torch.cuda.init()
except (RuntimeError, AssertionError) as err:
raise RuntimeError(_CUDA_UNAVAILABLE_MESSAGE) from err
def _create_accelerated_index(dataset, *, exist_ok=False, name=None, **options):
if exist_ok:
parts = _parse_field_path(options["column"])
default_name = ".".join(parts)
if any("." in part for part in parts):
default_name = ".".join(
"`" + part.replace("`", "``") + "`"
if any(
not character.isalnum() and character != "_" for character in part
)
else part
for part in parts
)
name = name or f"{default_name}_idx"
dataset.checkout_latest()
if any(index["name"] == name for index in dataset.list_indices()):
return
options["replace"] = False
options["name"] = name
_validate_index_accelerator(options.get("accelerator"))
try:
dataset.create_index(**options)
except Exception:
if exist_ok:
dataset.checkout_latest()
if any(index["name"] == name for index in dataset.list_indices()):
return
raise
def _polars_predicate_pushdown_barrier(frame: Any) -> Any:
"""Return a Polars frame unchanged while blocking predicate pushdown."""
return frame
_MODEL_BACKED_TOKENIZER_PREFIXES = ("jieba", "lindera")
_MODEL_BACKED_TOKENIZER_ERRORS = (
"unknown base tokenizer",
"Invalid directory path:",
"Failed to load Jieba",
"Failed to load tokenizer config",
"Failed to initialize default tokenizer",
)
def _optimize_cleanup_since_ms(
cleanup_older_than: Optional[timedelta], retrain: bool
) -> Optional[int]:
# Called directly by both the sync and async optimize so stacklevel=3
# names the user's call site rather than the background event loop.
cleanup_since_ms: Optional[int] = None
if cleanup_older_than is not None:
cleanup_since_ms = round(cleanup_older_than.total_seconds() * 1000)
if cleanup_since_ms <= 0:
warnings.warn(
"optimize(cleanup_older_than=0) removes every version except "
"the latest. Any concurrent reader or writer still using an "
"older version will fail. Use a longer cleanup_older_than "
"unless no other process is working on this table.",
UserWarning,
stacklevel=3,
)
if retrain:
warnings.warn(
"The 'retrain' parameter is deprecated and will be removed in a "
"future version.",
DeprecationWarning,
stacklevel=3,
)
return cleanup_since_ms
def _add_unique_note(exception: BaseException, note: str) -> None:
existing_notes = getattr(exception, "__notes__", ()) or ()
message = (
exception.args[0]
if exception.args and isinstance(exception.args[0], str)
else ""
)
if note not in existing_notes and note not in message:
add_note(exception, note)
def _is_model_backed_tokenizer(base_tokenizer: str) -> bool:
return any(
base_tokenizer == prefix or base_tokenizer.startswith(f"{prefix}/")
for prefix in _MODEL_BACKED_TOKENIZER_PREFIXES
)
def _maybe_add_fts_error_note(
exception: BaseException, *, base_tokenizer: str, language: Optional[str] = None
) -> None:
message = str(exception)
if language is not None and "not support the requested language" in message:
supported_langs = ", ".join(lang_mapping.values())
_add_unique_note(exception, f"Supported languages: {supported_langs}")
return
if not _is_model_backed_tokenizer(base_tokenizer):
return
if not any(marker in message for marker in _MODEL_BACKED_TOKENIZER_ERRORS):
return
_add_unique_note(
exception,
"Model-backed tokenizers such as 'jieba/default' and 'lindera/ipadic' "
"require tokenizer models in Lance's language model home. Set "
"LANCE_LANGUAGE_MODEL_HOME to override the default platform data "
"directory under 'lance/language_models'. Expected layouts include "
"'<model-home>/jieba/default/...' and "
"'<model-home>/lindera/ipadic/...'.",
)
if TYPE_CHECKING:
from .db import DBConnection, LanceDBConnection
from ._lancedb import (
Table as LanceDBTable,
OptimizeStats,
CleanupStats,
CompactionStats,
Tag,
AddColumnsResult,
RefreshColumnResult,
AddResult,
AlterColumnsResult,
UpdateFieldMetadataResult,
DeleteResult,
DropColumnsResult,
FtsToken,
RTEWriteSpec,
MergeResult,
UpdateResult,
)
from .index import IndexConfig
import pandas
import PIL
from .types import (
QueryType,
OnBadVectorsType,
AddMode,
CreateMode,
ScalarIndexType,
BaseTokenizerType,
)
# Type alias for index configuration objects
IndexConfigType = Union[
IvfFlat,
IvfPq,
IvfSq,
IvfRq,
HnswFlat,
HnswPq,
HnswSq,
BTree,
Bitmap,
LabelList,
Fm,
NGram,
ZoneMap,
RTree,
ScalarPlugin,
FTS,
]
class CompactionOptions(TypedDict, total=False):
"""Options that control file compaction during table optimization.
Unspecified options use Lance's defaults for local tables. Remote tables
forward ``target_rows_per_fragment``, ``max_source_rows``, ``io_buffer_size``,
and ``batch_size`` as per-job SQL options; other non-default settings are
unsupported. Omitted remote options inherit table tuning, then deployment/built-in
defaults. The server must support these SQL options. These settings do not
change table policy.
Compaction planning is row based. Lowering ``target_rows_per_fragment``
based on the expected row size can bound later compaction passes once
oversized fragments have been rewritten. It does not split an existing
fragment, so the first pass over an oversized fragment is not subject to
that bound. ``max_bytes_per_file`` limits output file size, not compaction
memory. Source budgets keep whole planned tasks; if the first task exceeds
a budget, that run performs no compaction work.
Examples
--------
Derive a steady-state row target from the expected row size:
>>> desired_fragment_bytes = 750 * 1024 * 1024
>>> average_row_bytes = 1_500_000
>>> options: CompactionOptions = {
... "target_rows_per_fragment": max(
... 1, desired_fragment_bytes // average_row_bytes
... ),
... }
>>> await table.optimize(compaction_options=options) # doctest: +SKIP
"""
target_rows_per_fragment: int
"""Target rows per fragment; existing oversized fragments are not split."""
max_rows_per_group: int
"""Maximum number of rows per row group (default: 1,024)."""
max_bytes_per_file: Optional[int]
"""Maximum output data-file size; this does not bound compaction memory."""
materialize_deletions: bool
"""Whether to rewrite fragments containing deleted rows (default: True)."""
materialize_deletions_threshold: float
"""Minimum deleted-row fraction that makes a fragment eligible (default: 0.1)."""
num_threads: Optional[int]
"""Number of compaction tasks to run in parallel."""
batch_size: Optional[int]
"""Number of rows per input scan batch."""
io_buffer_size: Optional[int]
"""Input scan I/O read-ahead budget in bytes.
Zero disables byte-based I/O backpressure.
"""
defer_index_remap: bool
"""Whether to defer index remapping during compaction (default: False)."""
index_remap_mode: Literal["direct", "compact"]
"""How to construct the old-to-new row-address mapping."""
compaction_mode: Optional[
Literal["reencode", "try_binary_copy", "force_binary_copy"]
]
"""Whether compaction re-encodes data or uses binary copying."""
binary_copy_read_batch_bytes: Optional[int]
"""Number of bytes read per batch during binary-copy compaction."""
max_source_fragments: Optional[int]
"""Maximum number of source fragments compacted in one run."""
max_source_rows: Optional[int]
"""Maximum live source rows per run, applied to whole planned tasks."""
max_source_bytes: Optional[int]
"""Maximum source bytes per run, applied to whole planned tasks."""
excluded_fragment_ids: List[int]
"""Fragment IDs to leave unchanged and use as planning boundaries."""
max_overlays_per_fragment: Optional[int]
"""Maximum overlays before a fragment is fully compacted."""
data_storage_version: str
"""Data-file version or release selector for compacted output."""
# Known distance metrics for legacy API detection
KNOWN_METRICS = {"l2", "cosine", "dot", "hamming"}
def _blob_value_to_storage(value: Any) -> Optional[dict]:
"""Keep a Python blob's inline data or external URI before Arrow infers it."""
if value is None:
return None
if isinstance(value, (bytes, bytearray, memoryview)):
return {"data": bytes(value)}
if isinstance(value, str):
if not value:
raise ValueError("Blob uri cannot be empty")
return {"uri": value}
if isinstance(value, dict):
unknown = value.keys() - {"data", "uri", "position", "size"}
if unknown:
raise ValueError(f"Unknown blob fields: {sorted(unknown)}")
return value
try:
from lance.blob import Blob
except ModuleNotFoundError as err:
if err.name not in ("lance", "lance.blob"):
raise
else:
if isinstance(value, Blob):
return {
"data": value.data,
"uri": value.uri,
"position": value.position,
"size": value.size,
}
raise TypeError(f"Unsupported blob value: {type(value).__name__}")
def _blob_input_to_arrow(data: Any, schema: Optional[pa.Schema]) -> Optional[pa.Table]:
"""Convert Python rows with blob fields before Arrow loses their value types."""
if schema is None:
return None
if isinstance(data, list) and data and isinstance(data[0], dict):
names = {
field.name
for field in schema
if is_blob_v2_field(field)
and any(isinstance(row, dict) and field.name in row for row in data)
}
if not names:
return None
values = {name: [row.get(name) for row in data] for name in names}
rows = [{**row, **{name: None for name in names}} for row in data]
table = pa.Table.from_pylist(rows)
elif _check_for_pandas(data) and isinstance(data, pd.DataFrame):
names = {
field.name
for field in schema
if is_blob_v2_field(field) and field.name in data.columns
}
if not names:
return None
values = {
name: [
None
if value is pd.NA or isinstance(value, float) and np.isnan(value)
else value
for value in data[name]
]
for name in names
}
table = pa.Table.from_pandas(
data.assign(**{name: None for name in names}), preserve_index=False
).replace_schema_metadata(None)
else:
return None
for field in schema:
if field.name not in names:
continue
storage_type = (
field.type.storage_type
if isinstance(field.type, pa.ExtensionType)
else field.type
)
storage_values = [_blob_value_to_storage(value) for value in values[field.name]]
missing_range_fields = {"position", "size"} - set(storage_type.names)
# Arrow silently ignores dictionary keys absent from the storage type.
if missing_range_fields and any(
value is not None
and any(value.get(name) is not None for name in missing_range_fields)
for value in storage_values
):
raise ValueError(
f"Blob column '{field.name}' storage schema cannot store byte ranges. "
"Create the column with lancedb.blob() to use position and size."
)
storage = pa.array(storage_values, type=storage_type)
column = (
pa.ExtensionArray.from_storage(field.type, storage)
if isinstance(field.type, pa.ExtensionType)
else storage
)
table = table.set_column(
table.schema.get_field_index(field.name), field, column
)
return table
def _into_pyarrow_reader(
data, schema: Optional[pa.Schema] = None
) -> pa.RecordBatchReader:
from lancedb.dependencies import datasets
if _check_for_hugging_face(data):
if isinstance(data, datasets.Dataset):
schema = data.features.arrow_schema
return pa.RecordBatchReader.from_batches(schema, _hf_batches(data))
elif isinstance(data, datasets.dataset_dict.DatasetDict):
schema = _schema_from_hf(data, None)
if "split" not in schema.names:
schema = schema.append(pa.field("split", pa.string()))
return pa.RecordBatchReader.from_batches(
schema, _to_batches_with_split(data)
)
if isinstance(data, LanceModel):
raise ValueError("Cannot add a single LanceModel to a table. Use a list.")
if isinstance(data, dict):
raise ValueError(
"Cannot create or add rows from a single dictionary. "
"Use a list of dictionaries instead."
)
if isinstance(data, list):
# Handle empty list case
if not data:
if schema is None:
raise ValueError("Cannot create table from empty list without a schema")
return pa.Table.from_pylist(data, schema=schema).to_reader()
# convert to list of dict if data is a bunch of LanceModels
if isinstance(data[0], LanceModel):
model_schema = data[0].__class__.to_arrow_schema()
data = [model_to_dict(d) for d in data]
data = _serialize_json_values(data, schema or model_schema)
return pa.Table.from_pylist(data, schema=model_schema).to_reader()
elif isinstance(data[0], pa.RecordBatch):
return pa.Table.from_batches(data).to_reader()
else:
data = _serialize_json_values(data, schema)
table = _blob_input_to_arrow(data, schema)
return (
table if table is not None else pa.Table.from_pylist(data)
).to_reader()
elif _check_for_pandas(data) and isinstance(data, pd.DataFrame):
table = _blob_input_to_arrow(data, schema)
if table is None:
table = pa.Table.from_pandas(data, preserve_index=False)
# Do not serialize Pandas metadata
meta = table.schema.metadata if table.schema.metadata is not None else {}
meta = {k: v for k, v in meta.items() if k != b"pandas"}
return table.replace_schema_metadata(meta).to_reader()
elif isinstance(data, pa.Table):
return data.to_reader()
elif isinstance(data, pa.RecordBatch):
return pa.RecordBatchReader.from_batches(data.schema, [data])
elif _check_for_lance(data) and isinstance(data, lance.LanceDataset):
return data.scanner().to_reader()
elif isinstance(data, pa.dataset.Dataset):
return data.scanner().to_reader()
elif isinstance(data, pa.dataset.Scanner):
return data.to_reader()
elif isinstance(data, pa.RecordBatchReader):
return data
elif (
type(data).__module__.startswith("polars")
and data.__class__.__name__ == "DataFrame"
):
return data.to_arrow().to_reader()
elif (
type(data).__module__.startswith("polars")
and data.__class__.__name__ == "LazyFrame"
):
return data.collect().to_arrow().to_reader()
elif isinstance(data, Iterable) and not isinstance(data, str):
return _iterator_to_reader(data, schema)
else:
raise TypeError(
f"Unknown data type {type(data)}. "
"Supported types: list of dicts, pandas DataFrame, polars DataFrame, "
"pyarrow Table/RecordBatch, or Pydantic models. "
"See https://docs.lancedb.com/tables/ for examples."
)
def _iterator_to_reader(
data: Iterable, schema: Optional[pa.Schema] = None
) -> pa.RecordBatchReader:
# Each batch is treated as it's own reader, mainly so we can
# re-use the _into_pyarrow_reader logic.
data = iter(data)
try:
first_batch = next(data)
except StopIteration:
if schema is None:
raise ValueError("Cannot create table from empty iterable without a schema")
return pa.RecordBatchReader.from_batches(schema, [])
first = _into_pyarrow_reader(first_batch)
schema = first.schema
def gen():
yield from first
for batch in data:
table: pa.Table = _into_pyarrow_reader(batch).read_all()
if table.schema != schema:
try:
table = table.cast(schema)
except (ValueError, pa.ArrowNotImplementedError, pa.ArrowTypeError):
raise ValueError(
f"Input iterator yielded a batch with schema that "
f"does not match the schema of other batches.\n"
f"Expected:\n{schema}\nGot:\n{table.schema}"
)
yield from table.to_batches()
return pa.RecordBatchReader.from_batches(schema, gen())
def _sanitize_data(
data: "DATA",
target_schema: Optional[pa.Schema] = None,
metadata: Optional[dict] = None, # embedding metadata
on_bad_vectors: OnBadVectorsType = "error",
fill_value: float = 0.0,
*,
allow_subschema: bool = False,
) -> pa.RecordBatchReader:
"""
Handle input data, applying all standard transformations.
This includes:
* Converting the data to a PyArrow Table
* Adding vector columns defined in the metadata
* Adding embedding metadata into the schema
* Casting the table to the target schema
* Handling bad vectors
Parameters
----------
target_schema : Optional[pa.Schema], default None
The schema to cast the table to. This is typically the schema of the table
if it already exists. Otherwise it might be a user-requested schema.
allow_subschema : bool, default False
If True, the input table is allowed to omit columns from the target schema.
The target schema will be filtered to only include columns that are present
in the input table before casting.
metadata : Optional[dict], default None
The embedding metadata to add to the schema.
on_bad_vectors : Literal["error", "drop", "fill", "null"], default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
fill_value : float, default 0.0
The value to use when filling vectors. Only used if on_bad_vectors="fill".
NaN or null elements and missing trailing elements are replaced with this
value. Valid elements are preserved.
"""
# At this point, the table might not match the schema we are targeting:
# 1. There might be embedding columns missing that will be added
# in the add_embeddings step.
# 2. If `allow_subschemas` is True, there might be columns missing.
reader = _into_pyarrow_reader(data, target_schema)
reader = _append_vector_columns(reader, target_schema, metadata=metadata)
# This happens before the cast so we can fix vector columns with
# incorrect lengths before they are cast to FSL.
reader = _handle_bad_vectors(
reader,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
target_schema=target_schema,
metadata=metadata,
)
if target_schema is None:
target_schema, reader = _infer_target_schema(reader)
if metadata:
target_schema = target_schema.with_metadata(
_merge_metadata(target_schema.metadata, metadata)
)
_validate_schema(target_schema)
reader = _cast_to_target_schema(reader, target_schema, allow_subschema)
return reader
def _cast_to_target_schema(
reader: pa.RecordBatchReader,
target_schema: pa.Schema,
allow_subschema: bool = False,
) -> pa.RecordBatchReader:
# pa.Table.cast expects field order not to be changed.
# Lance doesn't care about field order, so we don't need to rearrange fields
# to match the target schema. We just need to correctly cast the fields.
if reader.schema.equals(target_schema, check_metadata=True):
# Fast path when the schemas are already the same
return reader
fields = _align_field_types(list(iter(reader.schema)), list(iter(target_schema)))
reordered_schema = pa.schema(fields, metadata=target_schema.metadata)
if not allow_subschema and len(reordered_schema) != len(target_schema):
raise ValueError(
"Input table has different number of columns than target schema"
)
if allow_subschema and len(reordered_schema) != len(target_schema):
fields = _infer_subschema(
list(iter(reader.schema)), list(iter(reordered_schema))
)
reordered_schema = pa.schema(fields, metadata=target_schema.metadata)
def gen():
for batch in reader:
batch = _coerce_blob_write_columns(batch, reordered_schema)
# Table but not RecordBatch has cast.
cast_batches = (
pa.Table.from_batches([batch]).cast(reordered_schema).to_batches()
)
if cast_batches:
yield pa.RecordBatch.from_arrays(
cast_batches[0].columns, schema=reordered_schema
)
return pa.RecordBatchReader.from_batches(reordered_schema, gen())
_PRIMARY_KEY_METADATA_KEYS = (
b"lance-schema:unenforced-primary-key",
b"lance-schema:unenforced-primary-key:position",
)
def _drop_nullable_primary_key(reader: pa.RecordBatchReader) -> pa.RecordBatchReader:
"""Remove the table's primary key markers from source fields that allow nulls.
Casting to the table schema copies the table's field metadata but keeps the
source's nullability, and Lance rejects a schema with a nullable primary key.
merge_insert reads the key from the table, and the write still rejects null
keys.
"""
schema = pa.schema(
[_drop_primary_key(field, False) for field in reader.schema],
metadata=reader.schema.metadata,
)
if schema.equals(reader.schema, check_metadata=True):
return reader
return pa.RecordBatchReader.from_batches(
schema,
(pa.RecordBatch.from_arrays(batch.columns, schema=schema) for batch in reader),
)
def _drop_primary_key(field: pa.Field, nullable_ancestor: bool) -> pa.Field:
nullable = nullable_ancestor or field.nullable
if pa.types.is_struct(field.type):
field = field.with_type(
pa.struct([_drop_primary_key(child, nullable) for child in field.type])
)
metadata = field.metadata or {}
if nullable and any(key in metadata for key in _PRIMARY_KEY_METADATA_KEYS):
kept = {
k: v for k, v in metadata.items() if k not in _PRIMARY_KEY_METADATA_KEYS
}
field = field.with_metadata(kept) if kept else field.remove_metadata()
return field
def _preserve_blob_v2_fields(
inferred_schema: pa.Schema, existing_schema: pa.Schema
) -> pa.Schema:
"""Keep inferred blob semantics without overriding explicit replacement types."""
existing_fields = {field.name: field for field in existing_schema}
fields = [
_preserve_blob_v2_field(field, existing_fields[field.name])
if field.name in existing_fields
else field
for field in inferred_schema
]
return pa.schema(fields, metadata=inferred_schema.metadata)
def _preserve_blob_v2_field(inferred: pa.Field, existing: pa.Field) -> pa.Field:
if is_blob_v2_field(inferred):
# An explicitly declared blob layout is part of the replacement schema.
return inferred
if is_blob_v2_field(existing):
return existing
inferred_type, existing_type = inferred.type, existing.type
if pa.types.is_struct(inferred_type) and pa.types.is_struct(existing_type):
existing_children = {field.name: field for field in existing_type}
children = [
_preserve_blob_v2_field(field, existing_children[field.name])
if field.name in existing_children
else field
for field in inferred_type
]
return inferred.with_type(pa.struct(children))
if _is_list_like(inferred_type) and _is_list_like(existing_type):
value_field = _preserve_blob_v2_field(
inferred_type.value_field, existing_type.value_field
)
if value_field == inferred_type.value_field:
return inferred
value_field = value_field.with_name(existing_type.value_field.name)
if pa.types.is_fixed_size_list(inferred_type):
value_type = pa.list_(value_field, inferred_type.list_size)
elif pa.types.is_large_list(inferred_type):
value_type = pa.large_list(value_field)
else:
value_type = pa.list_(value_field)
return inferred.with_type(value_type)
if blob_v2_column_paths(pa.schema([existing])):
return existing
return inferred
def _coerce_blob_write_columns(
batch: pa.RecordBatch, target_schema: pa.Schema
) -> pa.RecordBatch:
"""Materialize Python bytes and URIs as blob structs before Arrow casting.
Existing blob structs retain their input layout. Rust normalizes them to
the stored schema and rejects populated ranges on two-field targets.
"""
columns = []
fields = []
changed = False
for field, column in zip(batch.schema, batch.columns):
target_field = target_schema.field(field.name)
coerced = _coerce_blob_value(column, target_field)
if coerced is not column:
column = coerced
field = pa.field(
field.name,
coerced.type,
field.nullable,
target_field.metadata,
)
changed = True
columns.append(column)
fields.append(field)
if not changed:
return batch
return pa.RecordBatch.from_arrays(
columns, schema=pa.schema(fields, metadata=batch.schema.metadata)
)
def _coerce_blob_value(column: pa.Array, target_field: pa.Field) -> pa.Array:
if is_blob_v2_field(target_field) and _can_coerce_to_blob(column.type):
return _coerce_value_to_blob(column, target_field)
target_type = target_field.type
if pa.types.is_struct(target_type) and pa.types.is_struct(column.type):
children = []
fields = []
changed = False
for source_field in column.type:
source_column = column.field(source_field.name)
nested_target = next(
(field for field in target_type if field.name == source_field.name),
None,
)
if nested_target is None:
children.append(source_column)
fields.append(source_field)
continue
coerced = _coerce_blob_value(source_column, nested_target)
if coerced is not source_column:
changed = True
child_array, child_type = _physical_array_and_type(coerced)
children.append(child_array)
fields.append(
pa.field(
source_field.name,
child_type,
source_field.nullable,
nested_target.metadata,
)
)
if not changed:
return column
return pa.StructArray.from_arrays(
children,
fields=fields,
mask=column.is_null() if column.null_count else None,
)
if _is_list_like(target_type) and _is_list_like(column.type):
return _coerce_blob_list_values(column, target_type.value_field)
return column
def _coerce_blob_list_values(
column: pa.Array, target_value_field: pa.Field
) -> pa.Array:
"""Coerce blob values inside a list column, preserving offsets and nulls.
Works on the raw child values window instead of ``pc.list_flatten`` because
flatten drops values spanned by null slots, which would misalign offsets.
"""
mask = column.is_null() if column.null_count else None
if pa.types.is_fixed_size_list(column.type):
list_size = column.type.list_size
values = column.values.slice(column.offset * list_size, len(column) * list_size)
coerced = _coerce_blob_value(values, target_value_field)
if coerced is values:
return column
physical_values, _ = _physical_array_and_type(coerced)
return pa.FixedSizeListArray.from_arrays(physical_values, list_size, mask=mask)
offsets = column.offsets
first_offset = offsets[0].as_py()
values = column.values.slice(
first_offset,
offsets[-1].as_py() - first_offset,
)
coerced = _coerce_blob_value(values, target_value_field)
if coerced is values:
return column
physical_values, _ = _physical_array_and_type(coerced)
if first_offset:
offsets = pc.subtract(offsets, pa.scalar(first_offset, offsets.type))
if pa.types.is_large_list(column.type):
return pa.LargeListArray.from_arrays(offsets, physical_values, mask=mask)
return pa.ListArray.from_arrays(offsets, physical_values, mask=mask)
def _coerce_value_to_blob(values: pa.Array, target_field: pa.Field) -> pa.Array:
value_field = "uri" if _is_string_like(values.type) else "data"
length = len(values)
storage_type = target_field.type
if isinstance(storage_type, pa.ExtensionType):
storage_type = storage_type.storage_type
storage_fields = list(storage_type)
children = []
for storage_field in storage_fields:
if storage_field.name == value_field and not pa.types.is_null(values.type):
if values.type == storage_field.type:
children.append(values)
else:
children.append(values.cast(storage_field.type))
else:
children.append(pa.nulls(length, type=storage_field.type))
storage = pa.StructArray.from_arrays(
children,
fields=storage_fields,
mask=values.is_null() if values.null_count else None,
)
if isinstance(target_field.type, pa.ExtensionType):
return pa.ExtensionArray.from_storage(target_field.type, storage)
return storage
def _physical_array_and_type(array: pa.Array) -> tuple[pa.Array, pa.DataType]:
if isinstance(array.type, pa.ExtensionType):
return array.storage, array.type.storage_type
return array, array.type
def _can_coerce_to_blob(data_type: pa.DataType) -> bool:
return (
_is_binary_like(data_type)
or _is_string_like(data_type)
or pa.types.is_null(data_type)
)
def _is_string_like(data_type: pa.DataType) -> bool:
return (
pa.types.is_string(data_type)
or pa.types.is_large_string(data_type)
or pa.types.is_string_view(data_type)
)
def _is_binary_like(data_type: pa.DataType) -> bool:
return (
pa.types.is_binary(data_type)
or pa.types.is_large_binary(data_type)
or pa.types.is_binary_view(data_type)
)
def _field_extension_name(field: pa.Field) -> Optional[str]:
extension_name = getattr(field.type, "extension_name", None)
if extension_name is not None:
return extension_name
metadata = field.metadata or {}
extension_name = metadata.get(b"ARROW:extension:name") or metadata.get(
"ARROW:extension:name"
)
if isinstance(extension_name, bytes):
return extension_name.decode()
return extension_name
def _is_json_field(field: pa.Field) -> bool:
return _field_extension_name(field) in ("arrow.json", "lance.json")
@dataclass(frozen=True)
class _JsonSerializationPlan:
arrow_field: pa.Field
children: Optional[Dict[str, "_JsonSerializationPlan"]] = None
item: Optional["_JsonSerializationPlan"] = None
def _json_serialization_plan(field: pa.Field) -> Optional[_JsonSerializationPlan]:
if _is_json_field(field):
return _JsonSerializationPlan(field)
if pa.types.is_struct(field.type):
children: Dict[str, _JsonSerializationPlan] = {}
for child_field in field.type:
child_plan = _json_serialization_plan(child_field)
if child_plan is not None:
children[child_field.name] = child_plan
if children:
return _JsonSerializationPlan(field, children=children)
if _is_list_like(field.type):
item_plan = _json_serialization_plan(field.type.value_field)
if item_plan is not None:
return _JsonSerializationPlan(field, item=item_plan)
return None
def _json_serialization_plans(
schema: pa.Schema,
) -> Dict[str, _JsonSerializationPlan]:
plans: Dict[str, _JsonSerializationPlan] = {}
for field in schema:
plan = _json_serialization_plan(field)
if plan is not None:
plans[field.name] = plan
return plans
def _serialize_json_value(value: Any, plan: _JsonSerializationPlan) -> Any:
if value is None or isinstance(value, str):
return value
if _is_json_field(plan.arrow_field):
if isinstance(value, (dict, list)):
return json.dumps(value)
return value
if plan.children is not None and isinstance(value, dict):
serialized = None
for child_name, child_plan in plan.children.items():
if child_name not in value:
continue
child_value = _serialize_json_value(value[child_name], child_plan)
if child_value is not value[child_name]:
if serialized is None:
serialized = dict(value)
serialized[child_name] = child_value
return serialized if serialized is not None else value
if plan.item is not None and isinstance(value, list):
serialized = None
for index, item in enumerate(value):
serialized_item = _serialize_json_value(item, plan.item)
if serialized_item is not item:
if serialized is None:
serialized = list(value)
serialized[index] = serialized_item
return serialized if serialized is not None else value
return value
def _serialize_json_values(data: Any, target_schema: Optional[pa.Schema]) -> Any:
if target_schema is None or not isinstance(data, list):
return data
plans = _json_serialization_plans(target_schema)
if not plans:
return data
serialized_rows = []
for row in data:
if not isinstance(row, dict):
serialized_rows.append(row)
continue
serialized_row = None
for field_name, plan in plans.items():
if field_name not in row:
continue
value = _serialize_json_value(row[field_name], plan)
if value is not row[field_name]:
if serialized_row is None:
serialized_row = dict(row)
serialized_row[field_name] = value
serialized_rows.append(serialized_row if serialized_row is not None else row)
return serialized_rows
def _align_field_types(
fields: List[pa.Field],
target_fields: List[pa.Field],
) -> List[pa.Field]:
"""
Apply the data types from the target_fields to the fields.
"""
new_fields = []
for field in fields:
target_field = next((f for f in target_fields if f.name == field.name), None)
if target_field is None:
raise ValueError(f"Field '{field.name}' not found in target schema")
new_fields.append(_align_field(field, target_field))
return new_fields
def _align_container_child(child: pa.Field, target_child: pa.Field) -> pa.Field:
# A list has one child, a map one key and one item, so an inferred child name
# ("item") aligns positionally and adopts the table's; pa.Table.cast renames it.
return _align_field(child, target_child).with_name(target_child.name)
def _arrow_json_storage_type(input_type: pa.DataType) -> Optional[pa.DataType]:
"""The storage type arrow.json would use for ``input_type``.
Returns None if the type cannot hold JSON text.
"""
if pa.types.is_string(input_type) or pa.types.is_string_view(input_type):
return pa.string()
if pa.types.is_large_string(input_type):
return pa.large_string()
return None
def _align_field(field: pa.Field, target_field: pa.Field) -> pa.Field:
if is_blob_v2_field(target_field):
source_type = field.type
if pa.types.is_struct(source_type):
if not any(child.name in ("data", "uri") for child in source_type):
raise ValueError(
f"blob struct input for column '{field.name}' must contain "
"a 'data' or 'uri' child. A blob query returns an SDK "
"descriptor in this shape, and a descriptor is not payload "
"bytes. Use blob_mode='bytes' to materialize payloads, or "
"inspect descriptors with blob_mode='descriptions'. To copy "
"payloads, select row IDs with with_row_id(True), then call "
"table.fetch_blobs(column, row_ids) or "
"table.fetch_blob_files(column, row_ids). Keep the source "
"unchanged between selection and fetching."
)
elif not _can_coerce_to_blob(source_type) and not isinstance(
source_type, pa.ExtensionType
):
raise ValueError(
f"cannot coerce column '{field.name}' with type {source_type} "
"into a blob v2 struct. expected binary bytes "
"(Binary, LargeBinary, BinaryView), strings "
"(Utf8, LargeUtf8, Utf8View), or a Struct with a "
"'data' or 'uri' child"
)
if pa.types.is_struct(source_type) or (
isinstance(source_type, pa.ExtensionType)
and (
not isinstance(target_field.type, pa.ExtensionType)
or type(source_type) is type(target_field.type)
)
):
# Keep the source layout until Rust can fill missing range fields or
# validate every batch before dropping all-null range fields. Arrow
# casting here would reject differing layouts before that validation.
return field.with_metadata(target_field.metadata)
# LanceDB exposes stored JSON columns as lance.json (JSONB-backed LargeBinary), but
# casting the input to that storage type here merely relabels the raw JSON bytes as
# JSONB. Lance must see arrow.json so it can perform the JSONB encoding.
if _field_extension_name(target_field) == "lance.json":
if _field_extension_name(field) == "arrow.json":
return field
# Plain JSON text, which is what pyarrow infers for a column of `str`, only
# needs the arrow.json label.
json_storage = _arrow_json_storage_type(field.type)
if json_storage is not None:
# Labelled through metadata rather than pa.json_(), which only exists on
# newer PyArrow; Lance reads the extension name off the field either way.
# The other metadata keys mirror the table field's: Lance swaps the name
# back to lance.json on write and then requires the field to match the
# stored one exactly, including the empty ``ARROW:extension:metadata``
# that pyarrow records for a ``pa.json_()`` column.
metadata = dict(target_field.metadata or {})
metadata[b"ARROW:extension:name"] = b"arrow.json"
return pa.field(field.name, json_storage, field.nullable, metadata)
if pa.types.is_struct(target_field.type):
if pa.types.is_struct(field.type):
new_type = pa.struct(
_align_field_types(
field.type.fields,
target_field.type.fields,
)
)
else:
new_type = target_field.type
elif pa.types.is_list(target_field.type):
if _is_list_like(field.type):
new_type = pa.list_(
_align_container_child(
field.type.value_field, target_field.type.value_field
)
)
else:
new_type = target_field.type
elif pa.types.is_large_list(target_field.type):
if _is_list_like(field.type):
new_type = pa.large_list(
_align_container_child(
field.type.value_field, target_field.type.value_field
)
)
else:
new_type = target_field.type
elif pa.types.is_fixed_size_list(target_field.type):
if _is_list_like(field.type):
new_type = pa.list_(
_align_container_child(
field.type.value_field, target_field.type.value_field
),
target_field.type.list_size,
)
else:
new_type = target_field.type
elif pa.types.is_map(target_field.type):
if pa.types.is_map(field.type):
# A map has exactly one key and one item field, so like a list's child they
# align positionally and adopt the table's names.
new_type = pa.map_(
_align_container_child(
field.type.key_field, target_field.type.key_field
),
_align_container_child(
field.type.item_field, target_field.type.item_field
),
keys_sorted=target_field.type.keys_sorted,
)
else:
new_type = target_field.type
else:
new_type = target_field.type
return pa.field(field.name, new_type, field.nullable, target_field.metadata)
def _infer_subschema(
schema: List[pa.Field],
reference_fields: List[pa.Field],
) -> List[pa.Field]:
"""
Transform the list of fields so the types match the reference_fields.
The order of the fields is preserved.
``schema`` may have fewer fields than `reference_fields`, but it may not have
more fields.
"""
fields = []
lookup = {f.name: f for f in reference_fields}
for field in schema:
reference = lookup.get(field.name)
if reference is None:
raise ValueError("Unexpected field in schema: {}".format(field))
if pa.types.is_struct(reference.type):
new_type = pa.struct(
_infer_subschema(
field.type.fields,
reference.type.fields,
)
)
new_field = pa.field(
field.name,
new_type,
reference.nullable,
)
else:
new_field = reference
fields.append(new_field)
return fields
def sanitize_create_table(
data,
schema: Union[pa.Schema, LanceModel],
metadata=None,
on_bad_vectors: OnBadVectorsType = "error",
fill_value: float = 0.0,
):
if inspect.isclass(schema) and issubclass(schema, LanceModel):
# convert LanceModel to pyarrow schema
# note that it's possible this contains
# embedding function metadata already
schema: pa.Schema = schema.to_arrow_schema()
if data is not None:
if metadata is None and schema is not None:
metadata = schema.metadata
data = _sanitize_data(
data,
schema,
metadata=metadata,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
)
schema = data.schema
else:
if schema is not None:
data = pa.Table.from_batches([], schema=schema)
if schema is None:
if data is None:
raise ValueError("Either data or schema must be provided")
elif hasattr(data, "schema"):
schema = data.schema
if metadata:
metadata = _merge_metadata(schema.metadata, metadata)
schema = schema.with_metadata(metadata)
# Need to apply metadata to the data as well
if isinstance(data, pa.Table):
data = data.replace_schema_metadata(metadata)
elif isinstance(data, pa.RecordBatchReader):
data = pa.RecordBatchReader.from_batches(schema, data)
return data, schema
def _schema_from_hf(data, schema) -> pa.Schema:
"""
Extract pyarrow schema from HuggingFace DatasetDict
and validate that they're all the same schema between
splits
"""
for dataset in data.values():
if schema is None:
schema = dataset.features.arrow_schema
elif schema != dataset.features.arrow_schema:
msg = "All datasets in a HuggingFace DatasetDict must have the same schema"
raise TypeError(msg)
return schema
def _to_batches_with_split(data):
"""
Return a generator of RecordBatches from a HuggingFace DatasetDict
with an extra `split` column
"""
for key, dataset in data.items():
for batch in _hf_batches(dataset):
table = pa.Table.from_batches([batch])
if "split" not in table.column_names:
table = table.append_column(
"split", pa.array([key] * batch.num_rows, pa.string())
)
for b in table.to_batches():
yield b
def _append_vector_columns(
reader: pa.RecordBatchReader,
schema: Optional[pa.Schema] = None,
*,
metadata: Optional[dict] = None,
) -> pa.RecordBatchReader:
"""
Use the embedding function to automatically embed the source columns and add the
vector columns to the table.
"""
if schema is None:
metadata = _merge_metadata(metadata)
else:
metadata = _merge_metadata(schema.metadata, metadata)
functions = EmbeddingFunctionRegistry.get_instance().parse_functions(metadata)
if not functions:
return reader
target_schema = schema
fields = list(reader.schema)
for vector_column, conf in functions.items():
if vector_column not in reader.schema.names:
if schema is not None:
field = schema.field(vector_column)
else:
dtype = pa.list_(pa.float32(), conf.function.ndims())
field = pa.field(vector_column, type=dtype, nullable=True)
fields.append(field)
schema = pa.schema(fields, metadata=reader.schema.metadata)
def gen():
for batch in reader:
for vector_column, conf in functions.items():
func = conf.function
no_vector_column = vector_column not in batch.column_names
if no_vector_column or pc.all(pc.is_null(batch[vector_column])).as_py():
col_data = func.compute_source_embeddings_with_retry(
batch[conf.source_column]
)
# Replace vectors with wrong length (including empty lists
# returned for inputs like empty strings) with None so that
# _handle_bad_vectors can process them according to the
# on_bad_vectors policy instead of crashing when PyArrow
# tries to cast them into a fixed-size list array.
# A fixed-size column already records the width, so only ask
# the function when it doesn't. Tables can use models whose
# dimension the function doesn't know.
field = schema.field(vector_column)
if (
target_schema is not None
and vector_column in target_schema.names
):
field = target_schema.field(vector_column)
if pa.types.is_fixed_size_list(field.type):
expected_ndims = field.type.list_size
else:
expected_ndims = conf.function.ndims()
col_data = [
v if v is not None and len(v) == expected_ndims else None
for v in col_data
]
if no_vector_column:
batch = batch.append_column(
schema.field(vector_column),
pa.array(col_data, type=schema.field(vector_column).type),
)
else:
batch = batch.set_column(
batch.column_names.index(vector_column),
schema.field(vector_column),
pa.array(col_data, type=schema.field(vector_column).type),
)
yield batch
return pa.RecordBatchReader.from_batches(schema, gen())
def _table_path(base: str, table_name: str) -> str:
"""
Get a table path that can be used in PyArrow FS.
Removes any weird schemes (such as "s3+ddb") and drops any query params.
"""
uri = _table_uri(base, table_name)
# Parse as URL
parsed = urlparse(uri)
# If scheme is s3+ddb, convert to s3
if parsed.scheme == "s3+ddb":
parsed = parsed._replace(scheme="s3")
# Remove query parameters
return parsed._replace(query=None).geturl()
def _table_uri(base: str, table_name: str) -> str:
return join_uri(base, f"{table_name}.lance")
def _normalize_progress(progress):
"""Normalize a ``progress`` parameter for :meth:`Table.add`.
Returns ``(progress_obj, owns)`` where *owns* is True when we created a
tqdm bar that the caller must close.
"""
if progress is True:
from tqdm.auto import tqdm
return tqdm(unit=" rows"), True
if progress is False or progress is None:
return None, False
return progress, False
class Table(ABC):
"""
A Table is a collection of Records in a LanceDB Database.
Examples
--------
Create using [DBConnection.create_table][lancedb.DBConnection.create_table]
(more examples in that method's documentation).
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data=[{"vector": [1.1, 1.2], "b": 2}])
>>> table.head()
pyarrow.Table
vector: fixed_size_list<item: float>[2]
child 0, item: float
b: int64
----
vector: [[[1.1,1.2]]]
b: [[2]]
Can append new data with [Table.add()][lancedb.table.Table.add].
>>> table.add([{"vector": [0.5, 1.3], "b": 4}])
AddResult(version=2)
Can query the table with [Table.search][lancedb.table.Table.search].
>>> table.search([0.4, 0.4]).select(["b", "vector"]).to_pandas()
b vector _distance
0 4 [0.5, 1.3] 0.82
1 2 [1.1, 1.2] 1.13
Search queries are much faster when an index is created. See
[Table.create_index][lancedb.table.Table.create_index].
"""
@property
@abstractmethod
def name(self) -> str:
"""The name of this Table"""
raise NotImplementedError("name is not supported for this table type")
@property
def namespace(self) -> List[str]:
"""Return the namespace path of the table (empty for the root namespace)."""
return []
@property
def id(self) -> str:
"""Return the full identifier of the table (namespace$name)."""
return "$".join(self.namespace + [self.name])
@property
@abstractmethod
def version(self) -> int:
"""The version of this Table"""
raise NotImplementedError("version is not supported for this table type")
@property
@abstractmethod
def schema(self) -> pa.Schema:
"""The [Arrow Schema](https://arrow.apache.org/docs/python/api/datatypes.html#)
of this Table
"""
raise NotImplementedError("schema is not supported for this table type")
@property
@abstractmethod
def tags(self) -> Tags:
"""Tag management for the table.
Similar to Git, tags are a way to add metadata to a specific version of the
table.
.. warning::
Tagged versions are exempted from the :py:meth:`cleanup_old_versions()`
process.
To remove a version that has been tagged, you must first
:py:meth:`~Tags.delete` the associated tag.
Examples
--------
.. code-block:: python
table = db.open_table("my_table")
table.tags.create("v2-prod-20250203", 10)
tags = table.tags.list()
"""
raise NotImplementedError("tags is not supported for this table type")
@property
def branches(self) -> "Branches":
"""Branch management for the table.
Branches are isolated, writable lines of history forked from another
branch (or version). Writes on a branch do not affect ``main``.
"""
raise NotImplementedError("branches is not supported for this table type")
def current_branch(self) -> Optional[str]:
"""The branch of the visible snapshot, or ``None`` for ``main``.
During Embedded tag time travel, this can differ from the original
branch that ``checkout_latest()`` returns to.
"""
raise NotImplementedError("current_branch is not supported for this table type")
def delta(
self,
from_version: int | str,
to_version: Optional[int | str] = None,
) -> "TableDelta":
"""Compare two persisted versions of this table on its current branch.
``from_version`` is exclusive and ``to_version`` is inclusive. Both
accept a version number (starting at 1) or a tag on the current branch.
Omitting ``to_version`` uses the version this handle currently sees,
including a time-travel checkout. Endpoints are resolved once when this
method is called, so moving a tag or advancing the table later does not
change the delta, and this table's checkout state is left untouched.
The returned [TableDelta][lancedb.table.TableDelta] streams inserted,
updated, upserted, and deleted rows. Those row-level readers require
stable row ids at both endpoints; create the table with
``storage_options={"new_table_enable_stable_row_ids": "true"}`` to
enable them. ``list_transactions()`` works without stable row ids.
Equal valid versions produce empty readers with the same schemas.
Reversed ranges, version 0, missing versions or tags, and tags from a
different branch are rejected. A row-level range that contains a
``Restore``, or whose transaction history is incomplete, is rejected
because the restored snapshot can retain older row-version metadata that
the delta readers would omit; recompute the checkpoint with a full
rescan and reinitialize it at the end version. Remote tables raise
``NotImplementedError``.
Parameters
----------
from_version: int | str
The version or tag to compare from (exclusive).
to_version: int | str, optional
The version or tag to compare to (inclusive). Defaults to the
currently visible version.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("memory://")
>>> table = db.create_table(
... "delta_example",
... [{"id": 1}, {"id": 2}],
... storage_options={"new_table_enable_stable_row_ids": "true"},
... )
>>> table.tags.create("baseline", table.version)
>>> _ = table.add([{"id": 3}])
>>> delta = table.delta("baseline")
>>> inserted = delta.inserted_rows().read_all()
>>> inserted["id"].to_pylist()
[3]
>>> [transaction["operation"] for transaction in delta.list_transactions()]
['Append']
"""
raise NotImplementedError("delta is not supported for this table type")
def __len__(self) -> int:
"""The number of rows in this Table"""
return self.count_rows(None)
@property
@abstractmethod
def embedding_functions(self) -> Dict[str, EmbeddingFunctionConfig]:
"""
Get a mapping from vector column name to it's configured embedding function.
"""
@abstractmethod
def count_rows(self, filter: Optional[str] = None) -> int:
"""
Count the number of rows in the table.
Parameters
----------
filter: str, optional
A SQL where clause to filter the rows to count.
"""
raise NotImplementedError("count_rows is not supported for this table type")
def to_pandas(self, blob_mode: BlobMode = "lazy", **kwargs) -> "pandas.DataFrame":
"""Return the table as a pandas DataFrame.
Parameters
----------
blob_mode: str, default "lazy"
Controls how blob columns are returned for backends that support
Lance blob-aware pandas conversion.
**kwargs
Forwarded to PyArrow / Lance pandas conversion.
Returns
-------
pd.DataFrame
"""
return self.to_arrow().to_pandas(**kwargs)
@abstractmethod
def to_arrow(self, *, blob_mode: BlobMode = "lazy") -> pa.Table:
"""Return the table as a pyarrow Table.
Parameters
----------
blob_mode: str, default "lazy"
``"lazy"`` returns blob v2 descriptors that carry the row id
``fetch_blobs`` reads. ``"descriptions"`` returns them without it.
``"bytes"`` returns payload bytes for supported row-addressable blob
columns and raises for blobs inside lists.
Returns
-------
pa.Table
"""
raise NotImplementedError("to_arrow is not supported for this table type")
def to_lance(self, **kwargs) -> lance.LanceDataset:
"""Return a LanceDB Embedded table as a lance.LanceDataset.
LanceDB Enterprise tables cannot be opened as Lance datasets by the
client. Use
``table.search().to_arrow()`` to read them through the server.
Returns
-------
lance.LanceDataset
"""
raise NotImplementedError("to_lance is not supported for this table type")
def to_polars(self, batch_size=None) -> "pl.LazyFrame":
"""Return the table as a Polars LazyFrame.
In LanceDB Enterprise, use ``table.search().to_polars()`` to convert query
results to a Polars DataFrame.
Parameters
----------
batch_size: int, optional
Passed to Polars as the maximum row count for scanned PyArrow
record batches.
Note
----
The Polars streaming engine is not supported because it does not currently
implement Python PyArrow dataset scans. Use the default engine when collecting
this LazyFrame.
Returns
-------
polars.LazyFrame
"""
raise NotImplementedError("to_polars is not supported for this table type")
def _is_legacy_create_index_call(
self,
first_arg: str,
config: Optional[IndexConfigType],
num_partitions: Optional[int],
num_sub_vectors: Optional[int],
vector_column_name: str,
accelerator: Optional[str],
index_cache_size: Optional[int],
) -> bool:
"""Detect if this is a legacy create_index call."""
# If config is provided, it's definitely the new API
if config is not None:
return False
# If old-style parameters were explicitly set, it's legacy
if any(
x is not None
for x in (num_partitions, num_sub_vectors, accelerator, index_cache_size)
):
return True
# If vector_column_name differs from default, it's legacy
if vector_column_name != VECTOR_COLUMN_NAME:
return True
# If first arg is a known metric, assume legacy
if first_arg.lower() in KNOWN_METRICS:
return True
# Otherwise assume new API
return False
def _build_vector_config_from_legacy_params(
self,
metric: str,
index_type: str,
num_partitions: Optional[int],
num_sub_vectors: Optional[int],
num_bits: int,
max_iterations: int,
sample_rate: int,
m: int,
ef_construction: int,
target_partition_size: Optional[int],
accelerator: Optional[str],
) -> IndexConfigType:
"""Build an index config object from legacy parameters."""
if index_type == "IVF_FLAT":
return IvfFlat(
distance_type=metric,
num_partitions=num_partitions,
max_iterations=max_iterations,
sample_rate=sample_rate,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_SQ":
return IvfSq(
distance_type=metric,
num_partitions=num_partitions,
max_iterations=max_iterations,
sample_rate=sample_rate,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_PQ":
return IvfPq(
distance_type=metric,
num_partitions=num_partitions,
num_sub_vectors=num_sub_vectors,
num_bits=num_bits,
max_iterations=max_iterations,
sample_rate=sample_rate,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_RQ":
return IvfRq(
distance_type=metric,
num_partitions=num_partitions,
num_bits=num_bits,
max_iterations=max_iterations,
sample_rate=sample_rate,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_HNSW_PQ":
return HnswPq(
distance_type=metric,
num_partitions=num_partitions,
num_sub_vectors=num_sub_vectors,
num_bits=num_bits,
max_iterations=max_iterations,
sample_rate=sample_rate,
m=m,
ef_construction=ef_construction,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_HNSW_SQ":
return HnswSq(
distance_type=metric,
num_partitions=num_partitions,
max_iterations=max_iterations,
sample_rate=sample_rate,
m=m,
ef_construction=ef_construction,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
elif index_type == "IVF_HNSW_FLAT":
return HnswFlat(
distance_type=metric,
num_partitions=num_partitions,
max_iterations=max_iterations,
sample_rate=sample_rate,
m=m,
ef_construction=ef_construction,
target_partition_size=target_partition_size,
)
else:
raise ValueError(f"Unknown index type {index_type}")
@staticmethod
def infer_tokenizer_configs(tokenizer_name: str) -> dict:
if tokenizer_name == "default":
return {
"base_tokenizer": "simple",
"language": "English",
"max_token_length": 40,
"lower_case": True,
"stem": False,
"remove_stop_words": False,
"ascii_folding": False,
"ngram_min_length": 3,
"ngram_max_length": 3,
"prefix_only": False,
}
elif tokenizer_name == "raw":
return {
"base_tokenizer": "raw",
"language": "English",
"max_token_length": None,
"lower_case": False,
"stem": False,
"remove_stop_words": False,
"ascii_folding": False,
"ngram_min_length": 3,
"ngram_max_length": 3,
"prefix_only": False,
}
elif tokenizer_name == "whitespace":
return {
"base_tokenizer": "whitespace",
"language": "English",
"max_token_length": None,
"lower_case": False,
"stem": False,
"remove_stop_words": False,
"ascii_folding": False,
"ngram_min_length": 3,
"ngram_max_length": 3,
"prefix_only": False,
}
# or it's with language stemming with pattern like "en_stem"
if len(tokenizer_name) != 7:
raise ValueError(f"Invalid tokenizer name {tokenizer_name}")
lang = tokenizer_name[:2]
if tokenizer_name[-5:] != "_stem":
raise ValueError(f"Invalid tokenizer name {tokenizer_name}")
if lang not in lang_mapping:
raise ValueError(f"Invalid language code {lang}")
return {
"base_tokenizer": "simple",
"language": lang_mapping[lang],
"max_token_length": 40,
"lower_case": True,
"stem": True,
"remove_stop_words": False,
"ascii_folding": False,
"ngram_min_length": 3,
"ngram_max_length": 3,
"prefix_only": False,
}
# New unified API overload
@overload
def create_index(
self,
column: str,
/,
*,
config: IndexConfigType,
replace: bool = ...,
wait_timeout: Optional[timedelta] = ...,
name: Optional[str] = ...,
exist_ok: bool = ...,
train: bool = ...,
) -> None: ...
# Legacy API overload (deprecated)
@overload
def create_index(
self,
metric: Literal["l2", "cosine", "dot", "hamming"] = ...,
num_partitions: Optional[int] = ...,
num_sub_vectors: Optional[int] = ...,
vector_column_name: str = ...,
replace: bool = ...,
accelerator: Optional[str] = ...,
index_cache_size: Optional[int] = ...,
num_bits: int = ...,
index_type: Literal[
"IVF_FLAT", "IVF_SQ", "IVF_PQ", "IVF_RQ", "IVF_HNSW_SQ", "IVF_HNSW_PQ"
] = ...,
max_iterations: int = ...,
sample_rate: int = ...,
m: int = ...,
ef_construction: int = ...,
*,
wait_timeout: Optional[timedelta] = ...,
name: Optional[str] = ...,
exist_ok: bool = ...,
train: bool = ...,
target_partition_size: Optional[int] = ...,
) -> None: ...
def create_index(
self,
metric: str = "l2",
num_partitions: Optional[int] = None,
num_sub_vectors: Optional[int] = None,
vector_column_name: str = VECTOR_COLUMN_NAME,
replace: bool = True,
accelerator: Optional[str] = None,
index_cache_size: Optional[int] = None,
num_bits: int = 8,
index_type: Literal[
"IVF_FLAT",
"IVF_SQ",
"IVF_PQ",
"IVF_RQ",
"IVF_HNSW_SQ",
"IVF_HNSW_PQ",
"IVF_HNSW_FLAT",
] = "IVF_PQ",
max_iterations: int = 50,
sample_rate: int = 256,
m: int = 20,
ef_construction: int = 300,
*,
config: Optional[IndexConfigType] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
target_partition_size: Optional[int] = None,
):
"""Create an index on a column.
This method supports both the new unified API and the legacy API
for backwards compatibility. The new API takes the column name as the
first positional argument and an index configuration object via
``config``; the legacy API takes the distance metric as the first
argument plus separate ``vector_column_name`` / ``num_partitions`` /
etc. parameters, and emits a ``DeprecationWarning``.
Parameters
----------
metric : str
For new API: the column name to index.
For legacy API: the distance metric ("l2", "cosine", "dot", "hamming").
config : IndexConfigType, optional
The index configuration object. If provided, uses the new unified API.
Can be one of: IvfFlat, IvfPq, IvfSq, IvfRq, HnswPq, HnswSq,
BTree, Bitmap, LabelList, Fm, NGram, ZoneMap, RTree, ScalarPlugin, FTS.
replace : bool, default True
Whether to replace an existing index on this column.
exist_ok : bool, default False
Preserve the existing index with the requested or derived name.
Takes precedence over replace and keeps its definition and build.
wait_timeout : timedelta, optional
LanceDB Enterprise only: how long to wait for the index build to finish.
If not set, the call returns once the build is scheduled.
name : str, optional
Custom name for the index.
train : bool, default True
Whether to train the index with existing data.
Notes
-----
On LanceDB Embedded tables the index is built before this method
returns. On LanceDB Enterprise tables it returns once the build is
scheduled. Until the build finishes, a vector index may be missing
from [list_indices][lancedb.table.Table.list_indices], a scalar or FTS
index may be listed with 0 indexed rows, and queries won't use the
index. To block until the index is ready, pass ``wait_timeout``, call
[wait_for_index][lancedb.table.Table.wait_for_index], or use
[create_index_async][lancedb.table.Table.create_index_async] and wait
on its job when the server returned a job ID.
``wait_timeout`` waits for the submitted build when the server returns
a job ID. On older servers that don't return one, it falls back to
polling the index by name. ``wait_for_index`` always waits by name.
When replacing an existing index, those name-based waits may return
before the new build finishes because the old index can still match.
``create_index_async`` can track a specific build only when the server
returned a job ID; otherwise it returns an already-complete job and
callers must use ``wait_for_index`` for that build.
Examples
--------
New API (recommended):
>>> table.create_index( # doctest: +SKIP
... "vector", config=IvfPq(distance_type="l2")
... )
>>> table.create_index("category", config=BTree()) # doctest: +SKIP
>>> table.create_index("content", config=FTS()) # doctest: +SKIP
Legacy API (deprecated):
>>> table.create_index( # doctest: +SKIP
... "l2", vector_column_name="vector"
... )
"""
raise NotImplementedError("create_index is not supported for this table type")
def prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> None:
"""Submit remote data prewarm and return after acceptance.
Omitted or empty ``columns`` warm all fields. ``filter`` is a single
SQL predicate for conservative metadata pruning; ``concurrency`` is
a positive per-executor integer, or omitted for the server default.
The table handle supplies the namespace and branch. Requires a
compatible SQL PREWARM server and Flight SQL connectivity.
The operation continues in the background. Use
[prewarm_data_async][lancedb.table.Table.prewarm_data_async] to retain
the job handle and wait for completion.
Examples
--------
>>> table.prewarm_data( # doctest: +SKIP
... ["payload"], filter="quality > 0.7", concurrency=4)
"""
self.prewarm_data_async(
columns, filter=filter, concurrency=concurrency, parameters=parameters
)
def explain_prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> PrewarmDataPlan:
"""Preview data prewarm without creating a warming job.
Accepts the same columns, filter, parameters, and concurrency as
``prewarm_data_async`` and uses the table handle's namespace and branch.
Requires LanceDB Enterprise with structured SQL previews, table Select,
and database/namespace Usage. Historical-version and Embedded handles
are unsupported.
Returns the source version, readable plan, and conservative pruning summary.
Unknown metrics remain None; selected ranges are not exact matching rows.
The preview reads metadata. A later submission plans its own snapshot.
Examples
--------
>>> plan = table.explain_prewarm_data( # doctest: +SKIP
... ["video"], filter="quality > $minimum", parameters={"minimum": 0.7})
>>> print(plan.pruning.selected_fragments) # doctest: +SKIP
"""
raise NotImplementedError(
"data prewarm previews are only supported on remote tables"
)
def prewarm_data_async(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> Job[PrewarmDataResult]:
"""Submit a remote data-cache prewarm and return its durable job.
Requires a compatible SQL PREWARM server and Flight SQL connectivity.
The table handle supplies the branch and namespace. ``columns`` uses
Lance field paths; an omitted/empty list warms all columns. ``filter``
is one SQL predicate for conservative metadata pruning, not a row scan.
``parameters`` binds ``$1``/``$name`` values using a sequence, mapping,
or one-row Arrow batch/table. Values are sent separately from SQL.
``concurrency`` is a positive per-executor integer. Historical-version
handles are unsupported; call ``checkout_latest()`` first.
Submission requires table Select and database/namespace Usage under
Standard authorization. Status/wait use the generic HTTP job API;
events and cancellation have their own privileges. Unsupported servers
are not retried through REST or with settings removed.
Returns once accepted. ``job.wait()`` waits for warming to finish and
returns [PrewarmDataResult][lancedb.prewarm.PrewarmDataResult].
Save ``job.id()`` to resume with ``db.open_job(id)``. A wait timeout does
not cancel the operation. After ``job.checkout_latest()``,
``job.spec()["table_version"]`` identifies the pinned source version.
Examples
--------
>>> job = table.prewarm_data_async( # doctest: +SKIP
... ["video"], filter="quality > $min", parameters={"min": 0.7},
... concurrency=4)
>>> job.wait() # doctest: +SKIP
"""
raise NotImplementedError(
"data prewarm jobs are only supported on remote tables"
)
def create_index_async(
self,
column: str,
*,
config: IndexConfigType,
replace: Optional[bool] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
) -> Job:
"""Create an index, returning a handle to the indexing job.
Takes the same arguments as :meth:`create_index`. The job may already
be complete when returned; callers must not assume the index exists
until :meth:`Job.wait` returns.
In LanceDB Enterprise, the returned job tracks this submission only if
the server returns a job ID. If it does not, the job is already
complete and does not wait for the background build; use
``wait_for_index`` and account for the replacement caveat described in
[create_index][lancedb.table.Table.create_index].
"""
raise NotImplementedError(
"create_index_async is not supported for this table type"
)
def get_table_overrides(self) -> dict[str, Any]:
"""Read Enterprise table overrides for this table.
Table overrides configure Enterprise-managed background behavior such as
automatic compaction, cleanup, and reindex execution. This method is
supported only on remote Enterprise tables.
"""
raise NotImplementedError
def update_table_overrides(self, **overrides: Any) -> dict[str, Any]:
"""Merge Enterprise table overrides into the current table overrides.
Fetches the current overrides, recursively merges nested objects locally,
then sends the complete replacement using REST PUT. Other values replace
existing values; unrelated fields from the read snapshot are preserved.
This read-modify-write is not atomic: concurrent edits can be overwritten,
even to unrelated fields. Coordinate writers when concurrent edits must
be preserved. An empty update only reads and returns the current overrides.
Unlike this SDK method, CLI ``set`` and REST PUT replace the overrides
object without merging. Use ``reset_table_overrides()`` to clear all
overrides. Supported only on remote Enterprise tables.
"""
raise NotImplementedError
def reset_table_overrides(self) -> dict[str, Any]:
"""Clear Enterprise table overrides for this table."""
raise NotImplementedError
def drop_index(self, name: str) -> None:
"""
Drop an index from the table.
Parameters
----------
name: str
The name of the index to drop.
Notes
-----
This does not delete the index from disk, it just removes it from the table.
To delete the index, run [optimize][lancedb.table.Table.optimize]
after dropping the index.
Use [list_indices][lancedb.table.Table.list_indices] to find the names of
the indices.
"""
raise NotImplementedError("drop_index is not supported for this table type")
def wait_for_index(
self, index_names: Iterable[str], timeout: timedelta = timedelta(seconds=300)
) -> None:
"""
Wait for indexing to complete for the given index names.
This will poll the table until all the indices are fully indexed,
or raise a timeout exception if the timeout is reached.
Parameters
----------
index_names: str
The name of the indices to poll
timeout: timedelta
Timeout to wait for asynchronous indexing. The default is 5 minutes.
"""
raise NotImplementedError("wait_for_index is not supported for this table type")
@abstractmethod
def stats(self) -> TableStatistics:
"""
Retrieve table and fragment statistics.
"""
raise NotImplementedError("stats is not supported for this table type")
@abstractmethod
def create_scalar_index(
self,
column: str,
*,
replace: bool = True,
index_type: ScalarIndexType = "BTREE",
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
):
"""Create a scalar index on a column.
Parameters
----------
column : str
The column to be indexed. Must be a boolean, integer, float,
or string column.
replace : bool, default True
Replace the existing index if it exists.
index_type: Literal["BTREE", "BITMAP", "LABEL_LIST"], default "BTREE"
The type of index to create.
wait_timeout: timedelta, optional
The timeout to wait if indexing is asynchronous.
name: str, optional
The name of the index. If not provided, a default name will be generated.
Examples
--------
Scalar indices, like vector indices, can be used to speed up scans. A scalar
index can speed up scans that contain filter expressions on the indexed column.
For example, the following scan will be faster if the column ``my_col`` has
a scalar index:
>>> import lancedb # doctest: +SKIP
>>> db = lancedb.connect("/data/lance") # doctest: +SKIP
>>> img_table = db.open_table("images") # doctest: +SKIP
>>> my_df = img_table.search().where("my_col = 7", # doctest: +SKIP
... prefilter=True).to_pandas()
Scalar indices can also speed up scans containing a vector search and a
prefilter:
>>> import lancedb # doctest: +SKIP
>>> db = lancedb.connect("/data/lance") # doctest: +SKIP
>>> img_table = db.open_table("images") # doctest: +SKIP
>>> img_table.search([1, 2, 3, 4], vector_column_name="vector") # doctest: +SKIP
... .where("my_col != 7", prefilter=True)
... .to_pandas()
Scalar indices can only speed up scans for basic filters using
equality, comparison, range (e.g. ``my_col BETWEEN 0 AND 100``), and set
membership (e.g. `my_col IN (0, 1, 2)`)
Scalar indices can be used if the filter contains multiple indexed columns and
the filter criteria are AND'd or OR'd together
(e.g. ``my_col < 0 AND other_col> 100``)
Scalar indices may be used if the filter contains non-indexed columns but,
depending on the structure of the filter, they may not be usable. For example,
if the column ``not_indexed`` does not have a scalar index then the filter
``my_col = 0 OR not_indexed = 1`` will not be able to use any scalar index on
``my_col``.
"""
raise NotImplementedError(
"create_scalar_index is not supported for this table type"
)
def create_fts_index(
self,
field_names: Union[str, List[str]],
*,
ordering_field_names: Optional[Union[str, List[str]]] = None,
replace: bool = False,
writer_heap_size: Optional[int] = 1024 * 1024 * 1024,
use_tantivy: bool = False,
tokenizer_name: Optional[str] = None,
with_position: bool = False,
# tokenizer configs:
base_tokenizer: BaseTokenizerType = "simple",
language: str = "English",
max_token_length: Optional[int] = 40,
lower_case: bool = True,
stem: Optional[bool] = None,
remove_stop_words: Optional[bool] = None,
custom_stop_words: Optional[List[str]] = None,
ascii_folding: bool = True,
ngram_min_length: int = 3,
ngram_max_length: int = 3,
prefix_only: bool = False,
block_size: int = 128,
document_granularity: DocumentGranularity = DocumentGranularity.ROW,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
):
"""Create a full-text search index on the table.
Warning - this API is highly experimental and is highly likely to change
in the future.
Parameters
----------
field_names: str or list of str
The name of the field to index. Native FTS indexes can only be
created on a single field at a time. To search over multiple text
fields, create a separate FTS index for each field.
replace: bool, default False
If True, replace the existing index if it exists. Note that this is
not yet an atomic operation; the index will be temporarily
unavailable while the new index is being created.
writer_heap_size: int, default 1GB
Deprecated legacy Tantivy parameter. Any value other than the
default raises an error.
ordering_field_names:
Deprecated legacy Tantivy parameter. Setting this raises an error.
tokenizer_name: str, default "default"
A compatibility alias for native tokenizer configs. Can be "raw",
"default" or the 2 letter language code followed by "_stem". So
for english it would be "en_stem". For new native FTS indexes, use
``base_tokenizer`` directly; ``tokenizer_name`` is a legacy
compatibility alias and does not expose model-backed tokenizer names
such as ``jieba/default`` or ``lindera/ipadic``.
use_tantivy: bool, default False
Deprecated legacy Tantivy parameter. Setting this to True raises an
error.
with_position: bool, default False
If False, do not store the positions of the terms in the text.
This can reduce the size of the index and improve indexing speed.
But it will raise an exception for phrase queries.
base_tokenizer : str, default "simple"
The base tokenizer to use for tokenization. Options are:
- "simple": Splits text by whitespace and punctuation.
- "whitespace": Split text by whitespace, but not punctuation.
- "raw": No tokenization. The entire text is treated as a single token.
- "code": Tokenizes source code and identifiers.
- "ngram": N-Gram tokenizer.
- "icu": ICU dictionary-based word segmentation.
- "icu/split": ICU segmentation with simple-style delimiter splitting.
- "jieba/*": Jieba tokenizer loaded from Lance's language model home.
- "lindera/*": Lindera tokenizer loaded from Lance's language model home.
language : str, default "English"
The language to use for stemming and stop-word removal. This is not
the primary way to enable CJK tokenization.
max_token_length : int, default 40
The maximum token length to index. Tokens longer than this length will be
ignored.
lower_case : bool, default True
Whether to convert the token to lower case. This makes queries
case-insensitive.
stem : bool, optional
Whether to stem the token. Stemming reduces words to their root form.
For example, in English "running" and "runs" would both be reduced to "run".
``None`` uses the base tokenizer's default: False for ``code`` and
``ngram``, True otherwise.
remove_stop_words : bool, optional
Whether to remove stop words. Stop words are common words that are often
removed from text before indexing. For example, in English "the" and "and".
``None`` uses the base tokenizer's default: False for ``code`` and
``ngram``, True otherwise.
custom_stop_words : list of str, optional
Custom words that replace the built-in language stop words. ``None``
uses the built-in list; an empty list explicitly uses no stop words.
ascii_folding : bool, default True
Whether to fold ASCII characters. This converts accented characters to
their ASCII equivalent. For example, "café" would be converted to "cafe".
ngram_min_length: int, default 3
The minimum length of an n-gram.
ngram_max_length: int, default 3
The maximum length of an n-gram.
prefix_only: bool, default False
Whether to only index the prefix of the token for ngram tokenizer.
block_size: int, default 128
The number of documents per compressed posting block. Must be 128
or 256. A value of 256 uses the experimental FTS V3 format and
may introduce breaking changes.
document_granularity: DocumentGranularity, default ROW
``ROW`` treats the selected text in one table row as one document.
``LIST_ELEMENT`` treats each element of the deepest list on the field
path as one document and returns its physical coordinates in
``_doc_index`` for matching queries.
wait_timeout: timedelta, optional
The timeout to wait if indexing is asynchronous.
name: str, optional
The name of the index. If not provided, a default name will be generated.
Notes
-----
Model-backed tokenizers such as ``jieba/default`` and ``lindera/ipadic``
require tokenizer models in Lance's language model home. Set
``LANCE_LANGUAGE_MODEL_HOME`` to override the default platform data
directory under ``lance/language_models``.
"""
raise NotImplementedError(
"create_fts_index is not supported for this table type"
)
@abstractmethod
def add(
self,
data: DATA,
mode: AddMode = "append",
on_bad_vectors: OnBadVectorsType = "error",
fill_value: float = 0.0,
progress: Optional[Union[bool, Callable, Any]] = None,
write_parallelism: Optional[int] = None,
allow_external_blob_outside_bases: bool = False,
retries: Optional[int] = None,
) -> AddResult:
"""Add more data to the [Table][lancedb.table.Table].
Plain binary values are cast into blob columns using the table's schema.
To construct Arrow record batches explicitly, use
[blob_array][lancedb.schema.blob_array].
Parameters
----------
data: DATA
The data to insert into the table. Acceptable types are:
- list-of-dict
- pandas.DataFrame
- pyarrow.Table or pyarrow.RecordBatch
mode: str
The mode to use when writing the data. Valid values are
"append" and "overwrite". Overwrite adopts explicitly declared blob
layouts, while inferred bytes retain the existing blob v2 fields.
on_bad_vectors: str, default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
One of "error", "drop", "fill", "null".
fill_value: float, default 0.
The value to use when filling vectors. Only used if on_bad_vectors="fill".
progress: bool, callable, or tqdm-like, optional
Progress reporting during the add operation. Can be:
- ``True`` to automatically create and display a tqdm progress
bar (requires ``tqdm`` to be installed)::
table.add(data, progress=True)
- A **callable** that receives a dict with keys ``output_rows``,
``output_bytes``, ``total_rows``, ``elapsed_seconds``,
``active_tasks``, ``total_tasks``, and ``done``::
def on_progress(p):
print(f"{p['output_rows']}/{p['total_rows']} rows, "
f"{p['active_tasks']}/{p['total_tasks']} workers")
table.add(data, progress=on_progress)
- A **tqdm-compatible** progress bar whose ``total`` and
``update()`` will be called automatically. The postfix shows
write throughput (MB/s) and active worker count::
with tqdm() as pbar:
table.add(data, progress=pbar)
write_parallelism: int, optional
Number of partitions to write in parallel. Higher values increase
throughput but also peak memory use, since each partition buffers
data in flight. Defaults to an estimate based on the data size,
capped at the number of CPU cores. Lower this if bulk ingestion is
using too much memory.
allow_external_blob_outside_bases: bool, default False
Store blob URIs that sit outside registered blob bases. The row
keeps a reference, so the object has to stay readable. Local
tables only.
retries: int, optional
Override the remote retry budget for this call (0 to 255). Zero
disables retries, including multipart parts. Ambiguous commits are
never replayed. Ignored by LanceDB Embedded.
Returns
-------
AddResult
An object containing the new version number of the table after adding data.
"""
raise NotImplementedError("add is not supported for this table type")
def merge_insert(self, on: Union[str, Iterable[str]]) -> LanceMergeInsertBuilder:
"""
Returns a [`LanceMergeInsertBuilder`][lancedb.merge.LanceMergeInsertBuilder]
that can be used to create a "merge insert" operation
This operation can add rows, update rows, and remove rows all in a single
transaction. It is a very generic tool that can be used to create
behaviors like "insert if not exists", "update or insert (i.e. upsert)",
or even replace a portion of existing data with new data (e.g. replace
all data where month="january")
The merge insert operation works by combining new data from a
**source table** with existing data in a **target table** by using a
join. There are three categories of records.
"Matched" records are records that exist in both the source table and
the target table. "Not matched" records exist only in the source table
(e.g. these are new data) "Not matched by source" records exist only
in the target table (this is old data)
The builder returned by this method can be used to customize what
should happen for each category of data.
Please note that the data may appear to be reordered as part of this
operation. This is because updated rows will be deleted from the
dataset and then reinserted at the end with the new values.
Parameters
----------
on: Union[str, Iterable[str]]
A column (or columns) to join on. This is how records from the
source table and target table are matched. Typically this is some
kind of key or id column. Passing several columns matches on the
composite key: a source row updates a target row only when it
agrees on every one of them.
Examples
--------
>>> import lancedb
>>> data = pa.table({"a": [2, 1, 3], "b": ["a", "b", "c"]})
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> new_data = pa.table({"a": [2, 3, 4], "b": ["x", "y", "z"]})
>>> # Perform a "upsert" operation
>>> res = table.merge_insert("a") \\
... .when_matched_update_all() \\
... .when_not_matched_insert_all() \\
... .execute(new_data)
>>> res
MergeResult(version=2, num_updated_rows=2, num_inserted_rows=1, num_deleted_rows=0, num_attempts=1, num_rows=3)
>>> # The order of new rows is non-deterministic since we use
>>> # a hash-join as part of this operation and so we sort here
>>> table.to_arrow().sort_by("a").to_pandas()
a b
0 1 b
1 2 x
2 3 y
3 4 z
""" # noqa: E501
on = [on] if isinstance(on, str) else list(iter(on))
return LanceMergeInsertBuilder(self, on)
@abstractmethod
def search(
self,
query: Optional[
Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery]
] = None,
vector_column_name: Optional[str] = None,
query_type: QueryType = "auto",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceQueryBuilder:
"""Create a search query to find the nearest neighbors
of the given query vector. We currently support [vector search](https://lancedb.com/docs/search/vector-search/)
and [full-text search](https://lancedb.com/docs/search/full-text-search/).
All query options are defined in
[LanceQueryBuilder][lancedb.query.LanceQueryBuilder].
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> data = [
... {"original_width": 100, "caption": "bar", "vector": [0.1, 2.3, 4.5]},
... {"original_width": 2000, "caption": "foo", "vector": [0.5, 3.4, 1.3]},
... {"original_width": 3000, "caption": "test", "vector": [0.3, 6.2, 2.6]}
... ]
>>> table = db.create_table("my_table", data)
>>> query = [0.4, 1.4, 2.4]
>>> (table.search(query)
... .where("original_width > 1000", prefilter=True)
... .select(["caption", "original_width", "vector"])
... .limit(2)
... .to_pandas())
caption original_width vector _distance
0 foo 2000 [0.5, 3.4, 1.3] 5.220000
1 test 3000 [0.3, 6.2, 2.6] 23.089996
Parameters
----------
query: list/np.ndarray/str/PIL.Image.Image, default None
The targeted vector to search for.
- *default None*.
Acceptable types are: list, np.ndarray, PIL.Image.Image
- If None then the select/where/limit clauses are applied to filter
the table
vector_column_name: str, optional
The name of the vector column to search.
The vector column needs to be a pyarrow fixed size list type
- If not specified then the vector column is inferred from
the table schema
- If the table has multiple vector columns then the *vector_column_name*
needs to be specified. Otherwise, an error is raised.
query_type: str
*default "auto"*.
Acceptable types are: "vector", "fts", "hybrid", or "auto"
- If "auto" then the query type is inferred from the query;
- If `query` is a list/np.ndarray then the query type is
"vector";
- If `query` is a PIL.Image.Image then either do vector search,
or raise an error if no corresponding embedding function is found.
- If `query` is a string, then the query type is "vector" if the
table has embedding functions else the query type is "fts"
operator: "AND" or "OR", optional
Combine terms in a string full-text query; the default is OR.
Use ``query_type="fts"`` when the table also has embeddings.
Returns
-------
LanceQueryBuilder
A query builder object representing the query.
Once executed, the query returns
- selected columns
- the vector
- and also the "_distance" column which is the distance between the query
vector and the returned vector.
"""
raise NotImplementedError("search is not supported for this table type")
@abstractmethod
def take_offsets(
self,
offsets: list[int],
*,
with_row_id: bool = False,
preserve_order: bool = False,
) -> LanceTakeQueryBuilder:
"""
Take a list of offsets from the table.
Offsets are 0-indexed and relative to the current version of the table. Offsets
are not stable. A row with an offset of N may have a different offset in a
different version of the table (e.g. if an earlier row is deleted).
Offsets are mostly useful for sampling as the set of all valid offsets is easily
known in advance to be [0, len(table)).
By default, no guarantees are made regarding the order in which results
are returned. Repeated offsets produce repeated rows, which makes this
method suitable for sampling with replacement. Set ``preserve_order=True``
to return found rows in input order, including repeated offsets. Missing
non-negative offsets are omitted. Negative offsets are rejected and are
not Python-style relative indices. ``preserve_order`` cannot be combined
with an explicit ``order_by``.
Parameters
----------
offsets: list[int]
The offsets to take.
with_row_id: bool, default False
If True, include the "_rowid" column in the results.
preserve_order: bool, default False
If True, return found rows in input order. Rust take queries apply
limit/offset after restoration; Python take builders and Query
conversion do not expose pagination.
Returns
-------
LanceTakeQueryBuilder
A query builder that can be executed to get the rows at the given
offsets.
"""
def __getitems__(self, offsets: list[int]) -> pa.RecordBatch:
"""
Take a list of offsets from the table and return as a record batch.
This method uses the `take_offsets` method to take the rows. However, it
aligns the offsets to the passed in offsets. This means the return type
is a record batch (and so users should take care not to pass in too many
offsets)
Note: this method is primarily intended to fulfill the Dataset contract
for pytorch.
Parameters
----------
offsets: list[int]
The offsets to take.
Returns
-------
pa.RecordBatch
A record batch containing the rows at the given offsets.
"""
# We don't know the order of the results at all. So we calculate a permutation
# for ordering the given offsets. Then we load the data with the _rowoffset
# column. Then we sort by _rowoffset and apply the inverse of the permutation
# that we calculated.
#
# Note: this is potentially a lot of memory copy if we're operating on large
# batches :(
num_offsets = len(offsets)
indices = list(range(num_offsets))
permutation = sorted(indices, key=lambda idx: offsets[idx])
permutation_inv = [0] * num_offsets
for i in range(num_offsets):
permutation_inv[permutation[i]] = i
columns = self.schema.names
columns.append("_rowoffset")
tbl = (
self.take_offsets(offsets)
.select(columns)
.to_arrow()
.sort_by("_rowoffset")
.take(permutation_inv)
.combine_chunks()
.drop_columns(["_rowoffset"])
)
return tbl
@abstractmethod
def take_row_ids(
self, row_ids: list[int], *, with_row_id: bool = False
) -> LanceTakeQueryBuilder:
"""
Take a list of row ids from the table.
Row ids are not stable and are relative to the current version of the table.
They can change due to compaction and updates.
Results follow the requested row-id order. Repeated IDs produce repeated
rows; missing or deleted IDs are omitted. This also applies to remote tables.
Unlike offsets, row ids are not 0-indexed and no assumptions should be made
about the possible range of row ids. In order to use this method you must
first obtain the row ids by scanning or searching the table.
Even so, row ids are more stable than offsets and can be useful in some
situations.
There is an ongoing effort to make row ids stable which is tracked at
https://github.com/lancedb/lancedb/issues/1120
Parameters
----------
row_ids: list[int]
The row ids to take.
with_row_id: bool, default False
If True, include the "_rowid" column in the results.
Returns
-------
LanceTakeQueryBuilder
A query builder that can be executed to get the rows.
"""
@abstractmethod
def blob_columns(self) -> list[str]:
"""Names of blob v1 and v2 columns that can be fetched by row id.
Struct-nested blobs use dotted paths. Blobs inside lists are omitted
because a row id does not identify an individual list element.
"""
@abstractmethod
def fetch_blobs(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
) -> pa.LargeBinaryArray:
"""Materialize full blob bytes for ``column`` at the given rows.
LanceDB Embedded supports both legacy blob v1 and blob v2 columns.
The result has the same length and order as ``row_ids``. Null blobs
produce null slots; valid empty blobs produce ``b""``.
``row_ids`` may be a list, an integer Arrow array or chunked array,
or a query table with row ids. ``_rowid`` values stay valid after
compaction when the table has stable row ids.
One missing or deleted row ID rejects the entire batch; no partial
results are returned.
A query result table that carries snapshot metadata keeps the snapshot
that produced it on LanceDB Enterprise. A query result without that
metadata, including a legacy query response, and a plain list are read
from the table's current snapshot; keep the source unchanged between
selection and fetching. Partial snapshot metadata is rejected. Row
positions are not row IDs: to read by position, call
``table.take_offsets(offsets, with_row_id=True)`` first and pass the
resulting ``_rowid`` values.
On Enterprise clusters that enable direct Blob reads, eligible Blob V2
values of at least 32 MiB may use short-lived object-store URLs.
Smaller values go through LanceDB. Direct reads also require
network access to the object store, including endpoint routing, DNS,
TLS trust, and egress rules.
Blobs inside lists cannot be fetched by row id yet.
LanceDB Enterprise limits each request to 1024 row IDs and 64 MiB of blob
bytes. The client splits requests automatically and reads an individual
larger blob through the Range route. This method still materializes all
bytes in memory; for large values use :meth:`fetch_blob_files`.
The returned Arrow ``large_binary`` array contains payload bytes, not
SQL/query descriptor dictionaries. Use ``.to_pylist()`` to obtain
Python ``bytes`` or ``None`` values for copying into a blob column.
Select row IDs with ``search().with_row_id(True)`` first. Selection and
fetching are separate requests: keep the source unchanged between them.
"""
@abstractmethod
def fetch_blob_ranges(
self,
column: str,
requests: Sequence[Sequence[int]],
) -> pa.LargeBinaryArray:
"""Materialize row-specific byte ranges from a blob column.
LanceDB Embedded supports both legacy blob v1 and blob v2 columns.
Each request is a three-item ``(row_id, offset, length)`` sequence.
Requests may be repeated or reordered, including multiple ranges for
the same blob.
The result has the same length and order as ``requests``; null blobs
produce null slots and empty ranges on non-null blobs produce ``b""``.
``_rowid`` values stay valid after compaction when the table has stable
row ids.
One missing or deleted row ID rejects the entire batch; no partial
results are returned.
On Enterprise clusters that enable direct Blob reads, eligible Blob V2
ranges of at least 32 MiB may use short-lived object-store URLs.
Smaller ranges go through LanceDB. Direct reads also require
network access to the object store, including endpoint routing, DNS,
TLS trust, and egress rules.
Row IDs can be obtained from a query with ``with_row_id(True)``.
LanceDB Enterprise uses the batched endpoint when the server advertises
support, partitioning at 1024 ranges and 64 MiB, including individual
larger ranges. Older servers use descriptor queries and BlobFile range
reads. Both protocols read one exact branch and version without changing
the table's checkout. Errors from an advertised endpoint propagate without
switching protocols. The final output occupies memory.
"""
@abstractmethod
def fetch_blob_files(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
*,
buffer_size: Optional[int] = None,
) -> "list[Optional[BlobFile]]":
"""Open lazy, seekable :class:`~lancedb._blob.BlobFile` handles.
LanceDB Embedded supports both legacy blob v1 and blob v2 columns.
Prefer this over :meth:`fetch_blobs` for large payloads. ``row_ids`` is
a ``list[int]``, an integer Arrow array or chunked array, or a query
``pyarrow.Table`` carrying row identity via ``_rowid`` or a
``_lance_row_id`` field on the blob descriptor. Null blob values return
``None``. LanceDB Enterprise requires server version 0.5.0 or newer.
``buffer_size`` controls sequential read-ahead in bytes (default 512 KiB).
Set it to 0 for sparse reads. Explicit range reads bypass this buffer.
On Enterprise clusters that enable direct Blob reads, eligible Blob V2
reads of at least 32 MiB may use short-lived object-store URLs.
Smaller reads go through LanceDB. Direct reads also require
network access to the object store, including endpoint routing, DNS,
TLS trust, and egress rules.
``_rowid`` values stay valid after compaction when the table has stable
row ids.
One missing or deleted row ID rejects the entire batch; no partial
handles are returned.
A query result table that carries snapshot metadata keeps the snapshot
that produced it on LanceDB Enterprise, so every returned handle reads
that snapshot. Metadata-free query results, including legacy server
responses, and plain lists are read from the table's current snapshot.
Partial snapshot metadata is rejected. A snapshot-bound handle fails
explicitly when the serving node cannot echo the query snapshot
identity; the 0.5.0 floor covers the basic remote Blob API but not that
echo. Row positions are not row IDs: call
``table.take_offsets(offsets, with_row_id=True)`` first and pass the
resulting ``_rowid`` values.
"""
@abstractmethod
def open_blob_file(
self,
descriptor: Union[dict, pa.StructScalar, None],
*,
buffer_size: Optional[int] = None,
) -> Optional[BlobFile]:
"""Open a seekable file from a SQL ``blob_descriptor`` result.
Accepts the descriptor dictionary or an Arrow struct scalar. Its source
pins the table version, branch, column and row identity, so opening does
not change this table's checkout. Null descriptors return ``None``.
Payload bytes load only when the returned handle is read. An external
blob with an unspecified size may require a metadata request on opening.
``buffer_size`` controls sequential read-ahead (default 512 KiB; 0 disables).
```python
rows = db.execute_query(
"SELECT blob_descriptor(video) AS video FROM videos"
).read_all()
with videos.open_blob_file(rows["video"][0]) as video:
header = video.read(4096)
```
"""
def open_blob_files(
self, descriptors: Iterable, *, buffer_size: Optional[int] = None
) -> Iterator[Optional[BlobFile]]:
"""Iterate over lazy files from descriptor dictionaries or Arrow scalars.
Accepts an Arrow array/chunked array or any iterable. Opens each handle
as iteration advances, preserving input order and nulls as ``None``.
Close each non-null handle after use, normally with a ``with`` block.
```python
for video in videos.open_blob_files(rows["video"]):
if video is not None:
with video:
header = video.read(4096)
```
"""
for descriptor in descriptors:
if buffer_size is None:
yield self.open_blob_file(descriptor)
else:
yield self.open_blob_file(descriptor, buffer_size=buffer_size)
@abstractmethod
def _execute_query(
self,
query: Query,
*,
batch_size: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> pa.RecordBatchReader: ...
def _execute_pull_query(
self,
query: Query,
*,
batch_size: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> pa.RecordBatchReader:
"""Execute a pull-driven batch query, falling back to the normal path."""
return self._execute_query(query, batch_size=batch_size, timeout=timeout)
def _execute_arrow(
self,
query: Query,
*,
timeout: Optional[timedelta] = None,
) -> pa.Table:
"""Execute a query and collect the whole result eagerly."""
return self._execute_query(query, timeout=timeout).read_all()
@abstractmethod
def _explain_plan(self, query: Query, verbose: Optional[bool] = False) -> str: ...
@abstractmethod
def _analyze_plan(
self,
query: Query,
*,
distributed_metrics: AnalyzePlanDistributedMetrics = "aggregate",
) -> str: ...
@abstractmethod
def _output_schema(self, query: Query) -> pa.Schema: ...
@abstractmethod
def _do_merge(
self,
merge: LanceMergeInsertBuilder,
new_data: DATA,
on_bad_vectors: OnBadVectorsType,
fill_value: float,
retries: Optional[int] = None,
) -> MergeResult: ...
@abstractmethod
def delete(self, where: Union[str, Expr]) -> DeleteResult:
"""Delete rows from the table.
This can be used to delete a single row, many rows, all rows, or
sometimes no rows (if your predicate matches nothing).
Parameters
----------
where: str or :class:`~lancedb.expr.Expr`
The filter condition. Can be a SQL string or a type-safe
:class:`~lancedb.expr.Expr` built with :func:`~lancedb.expr.col`
and :func:`~lancedb.expr.lit`.
The filter must not be empty, or it will error.
Returns
-------
DeleteResult
An object containing the new version number of the table after deletion.
Examples
--------
>>> import lancedb
>>> data = [
... {"x": 1, "vector": [1.0, 2]},
... {"x": 2, "vector": [3.0, 4]},
... {"x": 3, "vector": [5.0, 6]}
... ]
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
>>> table.delete("x = 2")
DeleteResult(num_deleted_rows=1, version=2)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 3 [5.0, 6.0]
If you have a list of values to delete, you can combine them into a
stringified list and use the `IN` operator:
>>> to_remove = [1, 5]
>>> to_remove = ", ".join([str(v) for v in to_remove])
>>> to_remove
'1, 5'
>>> table.delete(f"x IN ({to_remove})")
DeleteResult(num_deleted_rows=1, version=3)
>>> table.to_pandas()
x vector
0 3 [5.0, 6.0]
"""
raise NotImplementedError("delete is not supported for this table type")
@abstractmethod
def update(
self,
where: Optional[Union[str, Expr]] = None,
values: Optional[dict] = None,
*,
values_sql: Optional[Dict[str, str]] = None,
) -> UpdateResult:
"""
This can be used to update zero to all rows depending on how many
rows match the where clause. If no where clause is provided, then
all rows will be updated.
Either `values` or `values_sql` must be provided. You cannot provide
both.
Embeddings are not computed by this method. Updating an embedding source
column requires providing all associated vector columns in the same call;
otherwise a ValueError is raised. Use `merge_insert` to recompute embeddings
automatically.
Parameters
----------
where: str or [Expr][lancedb.expr.Expr], optional
The filter condition. Can be a SQL string or a type-safe
[Expr][lancedb.expr.Expr] built with [col][lancedb.expr.col] and
[lit][lancedb.expr.lit]. The filter must not be empty, or it will
error.
values: dict, optional
The values to update. The keys are the column names and the values
are the values to set.
values_sql: dict, optional
The values to update, expressed as SQL expression strings. These can
reference existing columns. For example, {"x": "x + 1"} will increment
the x column by 1.
Returns
-------
UpdateResult
- rows_updated: The number of rows that were updated
- version: The new version number of the table after the update
Examples
--------
>>> import lancedb
>>> from lancedb.expr import col
>>> import pandas as pd
>>> data = pd.DataFrame({"x": [1, 2, 3], "vector": [[1.0, 2], [3, 4], [5, 6]]})
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
>>> table.update(where=col("x") == 2, values={"vector": [10.0, 10]})
UpdateResult(rows_updated=1, version=2)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 3 [5.0, 6.0]
2 2 [10.0, 10.0]
>>> table.update(values_sql={"x": "x + 1"})
UpdateResult(rows_updated=3, version=3)
>>> table.to_pandas()
x vector
0 2 [1.0, 2.0]
1 4 [5.0, 6.0]
2 3 [10.0, 10.0]
"""
raise NotImplementedError("update is not supported for this table type")
@abstractmethod
def cleanup_old_versions(
self,
older_than: Optional[timedelta] = None,
*,
delete_unverified: bool = False,
) -> "CleanupStats":
"""
Clean up old versions of the table, freeing disk space.
Parameters
----------
older_than: timedelta, default None
The minimum age of the version to delete. If None, then this defaults
to two weeks.
delete_unverified: bool, default False
Because they may be part of an in-progress transaction, files newer
than 7 days old are not deleted by default. If you are sure that
there are no in-progress transactions, then you can set this to True
to delete all files older than `older_than`.
Returns
-------
CleanupStats
The stats of the cleanup operation, including how many bytes were
freed.
See Also
--------
[Table.optimize][lancedb.table.Table.optimize]: A more comprehensive
optimization operation that includes cleanup as well as other operations.
Notes
-----
This function is not available in LanceDB Enterprise, which manages
cleanup for you automatically.
"""
@abstractmethod
def compact_files(self, *args, **kwargs):
"""
Run the compaction process on the table.
This can be run after making several small appends to optimize the table
for faster reads.
Arguments are passed onto Lance's
`lance.dataset.DatasetOptimizer.compact_files`.
For most cases, the default should be fine.
See Also
--------
[Table.optimize][lancedb.table.Table.optimize]: A more comprehensive
optimization operation that includes cleanup as well as other operations.
Notes
-----
This function is not available in LanceDB Enterprise, which manages
compaction for you automatically.
"""
def explain_cleanup(self, *, min_age_seconds: Optional[int] = None) -> CleanupPlan:
"""Preview version deletion without submitting a job or deleting files.
Uses the same retention, branch and Ownership boundary as cleanup_async.
Estimates exclude auxiliary sweeps and reserve no snapshot for execution.
"""
raise NotImplementedError("cleanup previews require LanceDB Enterprise")
def cleanup_async(
self, *, min_age_seconds: Optional[int] = None
) -> Job[CleanupResult]:
"""Submit durable remote version cleanup through SQL VACUUM.
Omitted retention inherits selected table/branch then deployment policy.
Requires Ownership and parent Usage. Local/historical handles are unsupported.
Disable/drain automatic cleanup and prevent other cleanup, tag/branch
publication and restore until completion, including direct storage clients.
The server does not enforce coordination. Cancellation cannot undo deletion
or prove storage I/O stopped. Preview is available without deletion.
Save ``job.id()`` to reopen with ``db.open_job(id)``. Waiting returns
[CleanupResult][lancedb.cleanup.CleanupResult]; timeout leaves work running.
Unknown totals after retry/recovery remain None. Auxiliary sweeps are
best effort and excluded from the version-deletion counts.
>>> job = table.cleanup_async(min_age_seconds=604800) # doctest: +SKIP
>>> result = job.wait() # doctest: +SKIP
"""
raise NotImplementedError(
"durable cleanup jobs are only supported on remote tables"
)
def compact_async(
self, *, compaction_options: Optional[CompactionOptions] = None
) -> Job[CompactionResult]:
"""Submit remote compaction and return a durable job after acceptance.
Uses SQL OPTIMIZE with the table handle's branch and namespace. Supports
the four per-job settings in
[CompactionOptions][lancedb.table.CompactionOptions].
Requires a compatible remote server, table Ownership and parent Usage.
Local and historical-version handles are unsupported. Existing
``optimize()`` retains its return-on-submission behavior remotely.
``job.wait()`` waits for the actual rewrite and returns
[CompactionResult][lancedb.compaction.CompactionResult]. Partial/bounded
runs report uncommitted rows and ``more_work``; unknown file statistics
are None. Servers without a result payload raise an error rather than
returning fabricated zero statistics. Wait timeouts do not cancel work.
Save ``job.id()`` and reopen with ``db.open_job(id)``. Monitoring and
cancellation use generic HTTP job APIs and their existing permissions.
>>> job = table.compact_async( # doctest: +SKIP
... compaction_options={"target_rows_per_fragment": 1000})
>>> result = job.wait() # doctest: +SKIP
>>> print(result.rows_succeeded) # doctest: +SKIP
"""
raise NotImplementedError("compaction jobs are only supported on remote tables")
@abstractmethod
def optimize(
self,
*,
cleanup_older_than: Optional[timedelta] = None,
delete_unverified: bool = False,
retrain: bool = False,
compaction_options: Optional[CompactionOptions] = None,
):
"""
Optimize the on-disk data and indices for better performance.
.. note::
On a LanceDB Enterprise (remote) table, only compaction is
currently supported. Pruning old versions and index optimization
are silently skipped there instead of being attempted, so this
call returns successfully, with zeroed pruning statistics, once
the compaction job has been submitted; it does not wait for that
job to finish. Remote compaction supports per-job
``target_rows_per_fragment``, ``max_source_rows``, ``io_buffer_size``,
and ``batch_size``. Omitted fields inherit table tuning, then
deployment/built-in defaults. Other non-default compaction settings raise
``NotImplementedError`` before submission. The server must support
these SQL options; older-server errors are propagated without
dropping the requested settings.
Modeled after ``VACUUM`` in PostgreSQL.
Optimization covers three operations:
* Compaction: Merges small files into larger ones
* Prune: Removes old versions of the dataset
* Index: Optimizes the indices, adding new data to existing indices
Parameters
----------
cleanup_older_than: timedelta, optional default 7 days
All files belonging to versions older than this will be removed. Set
to 0 days to remove all versions except the latest. The latest version
is never removed.
.. warning::
Setting this to 0 deletes the data files of every older
version, so any other reader or writer still using an older
version of the table will fail. Only set it to 0 if no other
process is working on this dataset.
delete_unverified: bool, default False
Files leftover from a failed transaction may appear to be part of an
in-progress operation (e.g. appending new data) and these files will not
be deleted unless they are at least 7 days old. If delete_unverified is True
then these files will be deleted regardless of their age.
.. warning::
This should only be set to True if you can guarantee that no other
process is currently working on this dataset. Otherwise the dataset
could be put into a corrupted state.
retrain: bool, default False
This parameter is no longer used and is deprecated.
compaction_options: CompactionOptions, optional
Options that control file compaction. For large rows, derive a lower
``target_rows_per_fragment`` from the expected row size to bound later
passes. This does not cap the first pass over an existing oversized
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
Notes
-----
The frequency an application should call optimize is based on the frequency of
data modifications. If data is frequently added, deleted, or updated then
optimize should be run frequently. A good rule of thumb is to run optimize if
you have added or modified 100,000 or more records or run more than 20 data
modification operations.
"""
@abstractmethod
def list_indices(self) -> Iterable[IndexConfig]:
"""
List all indices that have been created with
[Table.create_index][lancedb.table.Table.create_index]
On LanceDB Enterprise tables, an index whose build hasn't finished may
be missing (vector indices) or listed with 0 indexed rows (scalar and
FTS indices), and ``num_unindexed_rows`` may be ``None``. Use
[wait_for_index][lancedb.table.Table.wait_for_index] to wait until an
index is ready.
"""
@abstractmethod
def tokenize(
self,
query: str,
*,
column: Optional[str] = None,
index_name: Optional[str] = None,
) -> Iterable[FtsToken]:
"""
Tokenize a query using the tokenizer configured on an FTS index.
Specify exactly one of ``column`` or ``index_name``.
Model-backed tokenizers such as ``jieba/*`` and ``lindera/*`` are
rebuilt in the client process from index metadata. In LanceDB Enterprise,
this means the same tokenizer model files must also exist locally
(under ``LANCE_LANGUAGE_MODEL_HOME``) and must match the files the
server uses. A mismatch cannot be detected: the returned tokens may
differ from what the server's index produces. If the model files are
missing, an error naming the tokenizer is raised.
"""
@abstractmethod
def index_stats(self, index_name: str) -> Optional[IndexStatistics]:
"""
Retrieve statistics about an index
Parameters
----------
index_name: str
The name of the index to retrieve statistics for
Returns
-------
IndexStatistics or None
The statistics about the index. Returns None if the index does not exist.
"""
@abstractmethod
def add_columns(
self,
transforms: Mapping[str, str | FunctionApplication]
| FunctionApplication
| pa.Field
| List[pa.Field]
| pa.Schema
| DATA
| pa.RecordBatch
| pa.RecordBatchReader
| pl.DataFrame
| None = None,
*,
computed: Dict[str, str] | None = None,
auto_refresh_enabled: bool | None = None,
auto_refresh_interval_seconds: int | None = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
total_gpu_count: Optional[int] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
):
"""
Add new columns with defined values.
Parameters
----------
transforms: Mapping[str, str | FunctionApplication], FunctionApplication,
pa.Field, List[pa.Field], pa.Schema, pandas.DataFrame,
polars.DataFrame, pa.Table, pa.RecordBatch, pa.RecordBatchReader
A map of column name to a SQL expression to use to calculate the
value of the new column. These expressions will be evaluated for
each row in the table, and can reference existing columns.
Alternatively, a pyarrow Field or Schema can be provided to add
new columns with the specified data types. The new columns will
be initialized with null values.
A mapping of column names to ``FunctionApplication`` values declares
all bindings in one schema commit. Each column holds its scalar
or entire named-struct result. A bare named-struct application expands
its ordered result fields as one atomic binding. Set field aliases
with ``rename(columns=...)``.
Function columns are supported only in LanceDB
Enterprise.
On local tables, a pandas or Polars DataFrame, PyArrow Table,
RecordBatch, or RecordBatchReader supplies precomputed new-column
values. Supply exactly one row per current live table row, in storage
scan order; pandas indexes are ignored. The input contains only new
columns. Readers are consumed once, and their batch boundaries need
not match table fragments. Keep the table unchanged between computing
these values and calling add_columns. Data inputs cannot be combined
with computed columns. Remote tables do not support data inputs.
computed: Dict[str, str], optional
LanceDB Embedded declaration and refresh require a source build with the
``experimental-functions`` Cargo feature, which published packages
do not enable. LanceDB Enterprise operations remain available.
A mapping from output column names to SQL expressions derives each
output field from its expression. A direct projection of a Blob v2
field inherits Blob v2 semantics; other expressions derive their
ordinary Arrow type. Mapping order is declaration and dependency
order.
Unlike ``transforms``, the expression is stored rather than
evaluated now: the column is committed with no values, and rows get
them from [`refresh_column`][lancedb.table.Table.refresh_column].
Declaring one therefore costs the same on a large table as on an
empty one.
A refresh also recomputes the rows whose inputs changed since they
were computed, so a mutated input is reflected by the next refresh.
While a declaration reads a column, that column cannot be renamed,
retyped or dropped.
In LanceDB Enterprise the expression is planned by the
server, and the refresh runs as a server job -- see
[`refresh_column_async`][lancedb.table.Table.refresh_column_async].
Cannot be combined with ``transforms``.
auto_refresh_enabled: bool, optional
Enable automatic refresh for each declared computed output. Defaults
to True. LanceDB Embedded stores this option; scheduling requires a
server.
auto_refresh_interval_seconds: int, optional
Positive minimum interval after a successful refresh, in seconds.
When omitted, the server determines when to refresh. Applies to every
computed output in this call.
Scheduling options require computed or Function columns.
total_cpu_millis: int, optional
Default total CPU budget for manual and automatic Function refreshes.
total_memory_bytes: int, optional
Default total memory budget for these refreshes, in bytes.
total_gpu_count: int, optional
Default total GPU budget across the Function workers.
Returns
-------
AddColumnsResult
version: the new version number of the table after adding columns.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("computed_demo", [{"x": 1}, {"x": 2}])
>>> table.add_columns(computed={"doubled": "x * 2"}) # doctest: +SKIP
AddColumnsResult(version=2)
>>> table.refresh_column("doubled") # doctest: +SKIP
RefreshColumnResult(rows_filled=2, version=4)
>>> table.to_arrow().sort_by("x").to_pandas() # doctest: +SKIP
x doubled
0 1 2
1 2 4
>>> import pyarrow as pa
>>> result = table.add_columns(pa.table({"score": [0.25, 0.75]}).to_reader())
>>> table.to_arrow()["score"].to_pylist()
[0.25, 0.75]
"""
_resource_options(
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
@abstractmethod
def refresh_column(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> "RefreshColumnResult":
"""
Fill the rows of a computed column that hold no value yet.
Local refresh requires the ``experimental-functions`` Cargo feature
in a source build. Published packages leave it disabled.
Declared with ``add_columns(computed=...)``, a column starts empty and
gets its values here. Rows appended since the last refresh are filled
by the next one, and rows whose inputs changed since they were computed
are recomputed; everything else is left as it is.
Supported for local tables only. For remote tables, a refresh runs as
a server job, through
[`refresh_column_async`][lancedb.table.Table.refresh_column_async].
Parameters
----------
column: str
The name of the computed column to fill.
where: str, optional
SQL filter selecting the rows to refresh, as in ``REFRESH COLUMN
... WHERE``. Unfilled rows outside it stay unfilled. LanceDB
Enterprise only; local refreshes reject this option.
batch_size: int, optional
Maximum rows per Function call on the remote server. Must be
greater than zero. None uses the server default. Has no effect on
local expression-backed columns.
max_concurrency: int, optional
Override the registered Function's maximum concurrent user-code
calls for this logical execution. Must be between 1 and
4294967295. None uses the registration default. This is not an
account-wide quota. LanceDB Enterprise only; local refreshes
reject this option.
publication_interval_rows: int, optional
Opt-in publication threshold for completed Function outputs. Must
be greater than zero. None keeps whole-fragment publication. The
actual publication boundary can be larger than this threshold
because it also depends on completed work and Function batching.
LanceDB Enterprise only; local refreshes reject this option.
total_cpu_millis: int, optional
Override the total CPU budget in millicores for this refresh.
total_memory_bytes: int, optional
Override the total memory budget in bytes for this refresh.
supervisor_resources: dict[str, int], optional
CPU, memory, and ephemeral-storage limits for the Function refresh
supervisor pod. Keys are ``cpu_millis``, ``memory_bytes`` and
``ephemeral_storage_bytes``; all are required and positive.
LanceDB Enterprise only; local refreshes reject it.
planning_workers: dict[str, object], optional
Resources and sizing for the Function refresh planning fleet.
``resources`` has the same keys as ``supervisor_resources``; the
other keys are ``max_workers``, ``rows_per_worker`` and
``fragments_per_worker``. LanceDB Enterprise only; local refreshes
reject it.
gpu_class: str, optional
GPU class for Function workers. Must be a lowercase name of 1-63
letters, digits, or hyphens, without a leading or trailing hyphen.
None keeps the Function's registered class or the deployment
default. LanceDB Enterprise only.
total_gpu_count: int, optional
Nonnegative total GPU budget for Function workers without changing
the other resource limits. Unspecified resources inherit column defaults,
then Function defaults, then deployment defaults. LanceDB Enterprise only.
retry_policy: FunctionRetryPolicy, optional
Override retries of failed user-code calls. Only use when repeating
partial work is safe. Set max_attempts=1 to disable retries.
Enterprise Functions only; local refreshes reject this option.
error_policy: {"fail", "skip"}, optional
Action after failure or retry exhaustion. Skip records the error
and attempt count, subject to the server's failure limits.
Workflow version 1 servers default to skip with a 5% failure ceiling
and publish successful output before reporting a threshold failure.
Older deployments retain their existing defaults.
max_failed_fraction: float, optional
Under a skip policy, fail the job once more than this share of
rows (0 to 1) has failed. LanceDB Enterprise only.
max_failed_rows: int, optional
Under a skip policy, fail the job once more than this many rows
have failed. LanceDB Enterprise only.
row_timeout_seconds: float, optional
Deadline for one Function call. LanceDB Enterprise only.
init_timeout_seconds: float, optional
Deadline for Function initialization. LanceDB Enterprise only.
num_workers: int, optional
Worker count for an expression column; registered Function
columns reject it and set the corresponding ``total_*`` options. LanceDB
Enterprise only.
max_job_attempts: int, optional
Total automatic Function refresh attempts, including the initial job.
Set to 1 to disable automatic job recovery.
max_executor_processes_per_worker: int, optional
Cap on executor processes per Function worker. LanceDB
Enterprise only.
retry_errors: bool, default False
Select current failed rows on the server. Cannot be combined with
``where``. Requires Enterprise workflow version 1 or later.
retry_job_id: str, optional
Restrict a failed-row retry to errors recorded by this job.
retry_error_type: str, optional
Restrict a failed-row retry to this exception class.
include_quarantined: bool, default False
Explicitly release and include quarantined rows in a failed-row retry.
function_version: str, optional
Atomically rebind this column's Function to the specified immutable
version. Requires Enterprise workflow version 1 or later.
recompute: {"failed", "all", "none"}, optional
Which rows to recompute during an upgrade. Requires
``function_version``; the default is ``all``. ``failed`` selects
current failures, and ``none`` preserves existing values. Quarantined
rows stay excluded unless explicitly included in a failed-row retry.
Returns
-------
RefreshColumnResult
rows_filled: the number of rows given a value.
version: the new version number of the table.
"""
@abstractmethod
def refresh_column_async(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> Job[RefreshColumnJobResult]:
"""
Like :meth:`refresh_column`, but returns a handle to the refresh job
instead of blocking until it completes.
Local refresh requires a source build with ``experimental-functions``.
Remote jobs do not require this feature.
The job may already be complete when returned; callers must not assume
the column is filled until :meth:`Job.wait` returns. Invalid input --
an unknown column, or one that is not computed -- raises here rather
than failing the job. For local tables, the job runs in-process; for
remote tables, it is the server's backfill job.
Parameters
----------
column: str
The name of the computed column to fill.
where: str, optional
SQL filter selecting the rows to refresh, as in ``REFRESH COLUMN
... WHERE``. Unfilled rows outside it stay unfilled. LanceDB
Enterprise only; local refreshes reject this option.
batch_size: int, optional
Maximum rows per Function call on the remote server. Must be
greater than zero. None uses the server default. Lower this for
slow Functions to stay within the executor's per-call deadline.
Has no effect on local expression-backed columns.
max_concurrency: int, optional
Override the registered Function's maximum concurrent user-code
calls for this logical execution. Must be between 1 and
4294967295. None uses the registration default. This is not an
account-wide quota. LanceDB Enterprise only; local refreshes
reject this option.
publication_interval_rows: int, optional
Opt-in publication threshold for completed Function outputs. Must
be greater than zero. None keeps whole-fragment publication. The
actual publication boundary can be larger than this threshold
because it also depends on completed work and Function batching.
LanceDB Enterprise only; local refreshes reject this option.
total_cpu_millis: int, optional
Override the total CPU budget in millicores for this refresh.
total_memory_bytes: int, optional
Override the total memory budget in bytes for this refresh.
supervisor_resources: dict[str, int], optional
CPU, memory, and ephemeral-storage limits for the Function refresh
supervisor pod. Keys are ``cpu_millis``, ``memory_bytes`` and
``ephemeral_storage_bytes``; all are required and positive.
LanceDB Enterprise only; local refreshes reject it.
planning_workers: dict[str, object], optional
Resources and sizing for the Function refresh planning fleet.
``resources`` has the same keys as ``supervisor_resources``; the
other keys are ``max_workers``, ``rows_per_worker`` and
``fragments_per_worker``. LanceDB Enterprise only; local refreshes
reject it.
gpu_class: str, optional
GPU class for Function workers. Must be a lowercase name of 1-63
letters, digits, or hyphens, without a leading or trailing hyphen.
None keeps the Function's registered class or the deployment
default. LanceDB Enterprise only.
total_gpu_count: int, optional
Nonnegative total GPU budget for Function workers without changing
the other resource limits. Unspecified resources inherit column defaults,
then Function defaults, then deployment defaults. LanceDB Enterprise only.
retry_policy: FunctionRetryPolicy, optional
Override retries of failed user-code calls. Only use when repeating
partial work is safe. Set max_attempts=1 to disable retries.
Enterprise Functions only; local refreshes reject this option.
error_policy: {"fail", "skip"}, optional
Action after failure or retry exhaustion. Skip records the error
and attempt count, subject to the server's failure limits.
Workflow version 1 servers default to skip with a 5% failure ceiling
and publish successful output before reporting a threshold failure.
Older deployments retain their existing defaults.
max_failed_fraction: float, optional
Under a skip policy, fail the job once more than this share of
rows (0 to 1) has failed. LanceDB Enterprise only.
max_failed_rows: int, optional
Under a skip policy, fail the job once more than this many rows
have failed. LanceDB Enterprise only.
row_timeout_seconds: float, optional
Deadline for one Function call. LanceDB Enterprise only.
init_timeout_seconds: float, optional
Deadline for Function initialization. LanceDB Enterprise only.
num_workers: int, optional
Worker count for an expression column; registered Function
columns reject it and set the corresponding ``total_*`` options. LanceDB
Enterprise only.
max_job_attempts: int, optional
Total automatic Function refresh attempts, including the initial job.
Set to 1 to disable automatic job recovery.
max_executor_processes_per_worker: int, optional
Cap on executor processes per Function worker. LanceDB
Enterprise only.
retry_errors: bool, default False
Select current failed rows on the server. Cannot be combined with
``where``. Requires Enterprise workflow version 1 or later.
retry_job_id: str, optional
Restrict a failed-row retry to errors recorded by this job.
retry_error_type: str, optional
Restrict a failed-row retry to this exception class.
include_quarantined: bool, default False
Explicitly release and include quarantined rows in a failed-row retry.
function_version: str, optional
Atomically rebind this column's Function to the specified immutable
version. Requires Enterprise workflow version 1 or later.
recompute: {"failed", "all", "none"}, optional
Which rows to recompute during an upgrade. Requires
``function_version``; the default is ``all``. ``failed`` selects
current failures, and ``none`` preserves existing values. Quarantined
rows stay excluded unless explicitly included in a failed-row retry.
Returns
-------
Job[RefreshColumnResult]
A job whose successful ``wait`` returns row counts plus the source
and published table versions.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("computed_job_demo", [{"x": 1}, {"x": 2}])
>>> table.add_columns(computed={"doubled": "x * 2"}) # doctest: +SKIP
AddColumnsResult(version=2)
>>> job = table.refresh_column_async("doubled") # doctest: +SKIP
>>> result = job.wait() # doctest: +SKIP
>>> result.rows_assigned # doctest: +SKIP
2
>>> job.status() # doctest: +SKIP
'finished'
"""
def resume_refresh(self, job_id: str) -> Job[RefreshColumnJobResult]:
"""Resume a failed Function refresh on this table's current branch.
Returns the successor job. Call its ``wait()`` method for the refresh
result. Requires LanceDB Enterprise.
"""
raise NotImplementedError(
"resuming a Function refresh requires LanceDB Enterprise"
)
def retry_failed(
self,
column: str,
*,
error_type: Optional[str] = None,
job_id: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
**options: Any,
) -> Job[RefreshColumnJobResult]:
"""Start a job for exactly the current failed rows.
Stable row identities survive compaction. Set ``function_version`` to
upgrade and retry failures atomically. ``include_quarantined=True``
explicitly releases quarantined rows before retrying them.
>>> job = table.retry_failed("sharpness") # doctest: +SKIP
>>> result = job.wait() # doctest: +SKIP
"""
return self.refresh_column_async(
column,
retry_errors=True,
retry_error_type=error_type,
retry_job_id=job_id,
include_quarantined=include_quarantined,
function_version=function_version,
recompute="failed" if function_version is not None else None,
**options,
)
def upgrade_column(
self,
column: str,
*,
version: str,
recompute: Literal["failed", "all", "none"] = "all",
**options: Any,
) -> Job[RefreshColumnJobResult]:
"""Upgrade a Function binding while keeping its columns available.
``failed`` recomputes current failures, ``all`` recomputes all live rows,
and ``none`` preserves existing values. New and changed rows use the new
version; ``function_provenance`` identifies each value's producing version.
"""
return self.refresh_column_async(
column, function_version=version, recompute=recompute, **options
)
def quarantine_failed(
self,
column: str,
*,
error_type: Optional[str] = None,
job_id: Optional[str] = None,
after_attempts: Optional[int] = None,
quarantined: bool = True,
) -> int:
"""Quarantine current failures, or release them with ``quarantined=False``.
Normal and automatic refreshes skip quarantined rows. ``after_attempts``
restricts the selection to rows with at least that many failed calls.
Returns the number of selected rows. LanceDB Enterprise only.
"""
raise NotImplementedError("Function quarantine requires LanceDB Enterprise")
def function_provenance(
self,
column: str,
*,
limit: Optional[int] = None,
version: Optional[int] = None,
) -> FunctionProvenance:
"""Read each returned row's producing Function version and job.
``version`` selects a table snapshot. Check ``truncated`` before treating
the returned records as a complete snapshot. LanceDB Enterprise only.
"""
raise NotImplementedError("Function provenance requires LanceDB Enterprise")
@abstractmethod
def function_errors(
self,
job_id: Optional[str] = None,
column: Optional[str] = None,
limit: Optional[int] = None,
history: bool = False,
page_token: Optional[str] = None,
) -> "FunctionErrors":
"""
The per-row errors Function refreshes recorded on this table.
A refresh running under a skip policy records each row it skipped.
On servers advertising workflow version 1 or later, the default lists
current unresolved failures, deduplicated by row and Function column,
with attempt counts and first/last failure times. ``history=True``
returns retained observations, including repaired failures. Older servers
keep their legacy listing behavior and reject explicit history requests.
LanceDB Enterprise only; reading errors needs table read access because
messages can include the input that failed.
Parameters
----------
job_id: str, optional
Only errors recorded by this job.
column: str, optional
Only errors on this column.
limit: int, optional
At most this many records (server default 10000, cap 100000).
history: bool, default False
Include retained historical errors instead of current failures.
Requires Enterprise workflow version 1 or later. Historical records
cannot be used with the current-input ``rows()`` helper.
page_token: str, optional
Empty starts retained history, including repaired rows, for an exact
job_id. Continue with the response's
page_token until it is None, keeping all filters unchanged.
Returns
-------
FunctionErrors
``records``, ``fragments`` and ``truncated``, the last saying
whether the listing stopped at its limit.
Each record identifies its row with ``fragment_id``, ``row_offset``,
the physical ``row_address`` (``(fragment_id << 32) |
row_offset``), and, when the table uses stable row IDs, the stable
``row_id`` read from the refresh's pinned table version. Prefer
``row_id`` for retries: compaction and rewrite can change the
physical fields, while a stable row ID survives them.
To retry the recorded rows with stable row IDs:
.. code-block:: python
errors = table.function_errors(column="embedding")
row_ids = sorted(
record.row_id
for record in errors.records
if record.row_id is not None
)
if row_ids:
row_id_list = ", ".join(str(row_id) for row_id in row_ids)
database.execute_query(
f"REFRESH COLUMN embedding ON {table.name} "
f"WHERE _rowid IN ({row_id_list})"
)
For a table without stable row IDs, collect the non-null physical
addresses before the table is compacted or rewritten and run the
SQL refresh:
.. code-block:: python
errors = table.function_errors(column="embedding")
addresses = sorted(
record.row_address
for record in errors.records
if record.row_address is not None
)
if addresses:
address_list = ", ".join(str(address) for address in addresses)
database.execute_query(
f"REFRESH COLUMN embedding ON {table.name} "
f"WHERE _rowaddr IN ({address_list})"
)
Records whose per-row detail was capped have no address and appear
only in ``fragments``; they cannot be selected individually here.
Examples
--------
>>> errors = table.function_errors(column="embedding") # doctest: +SKIP
>>> for record in errors.records: # doctest: +SKIP
... print(record.job_id, record.row_offset, record.error_message)
>>> if errors.truncated: # doctest: +SKIP
... print("listing stopped at the limit")
"""
@abstractmethod
def column_statuses(
self, column: Optional[str] = None
) -> list[FunctionColumnStatus]:
"""Return exact status for Function-backed output columns.
All statuses describe one server table version. ``column=None`` returns
every Function output. This operation is available on LanceDB
Enterprise tables. A table handle pinned to an exact version requests
that version; a latest handle retains the normal read-freshness
behavior.
"""
@abstractmethod
def column_status(self, column: str) -> FunctionColumnStatus:
"""Return exact status for one Function-backed output column."""
@abstractmethod
def wait_fresh(
self, column: str, timeout: Optional[timedelta] = None
) -> FunctionColumnStatus:
"""Poll until the column has no stale or never-computed rows.
Rows already failed remain visible in the returned status and do not
keep this method waiting. The method never submits or cancels work.
Transport errors and unrecoverable status-evidence errors propagate.
A typed finalization-pending response is retried; arbitrary failure
responses are not.
"""
@abstractmethod
def alter_columns(self, *alterations: Iterable[Dict[str, str]]):
"""
Alter column names and nullability.
Parameters
----------
alterations : Iterable[Dict[str, Any]]
A sequence of dictionaries, each with the following keys:
- "path": str
The column path to alter. For a top-level column, this is the name.
For a nested column, this is the dot-separated path, e.g. "a.b.c".
- "rename": str, optional
The new name of the column. If not specified, the column name is
not changed.
- "data_type": pyarrow.DataType, optional
The new data type of the column. Existing values will be casted
to this type. If not specified, the column data type is not changed.
- "nullable": bool, optional
Whether the column should be nullable. If not specified, the column
nullability is not changed. Only non-nullable columns can be changed
to nullable. Currently, you cannot change a nullable column to
non-nullable.
Each alteration must specify at least one of "rename", "data_type",
or "nullable". The legacy key "name" is also accepted for renaming.
Unknown keys raise ValueError before any alterations are applied.
Local tables reject renaming source or vector columns used by an
embedding function with ValueError, preserving the embedding binding.
Returns
-------
AlterColumnsResult
version: the new version number of the table after the alteration.
"""
@abstractmethod
def update_field_metadata(
self, *updates: dict[str, Any]
) -> UpdateFieldMetadataResult:
"""
Update per-field (column) metadata.
Parameters
----------
updates : dict
One or more dicts, each with:
- "path": str — dot-path to the field (e.g. "embedding" or "a.b.c").
- "metadata": dict[str, str | None] — keys to set; a value of ``None``
deletes that key.
- "replace": bool, optional — replace the field's whole metadata map
instead of merging (default False).
The following keys are treated specially, by convention, and should
be used when appropriate:
- "lancedb:description": for a human-readable description of a field.
- ``"lancedb:tag:<name>"`` for a user-defined key-value tag, where the
suffix names the tag category; e.g. "lancedb:tag:model": "clip".
- "lancedb:status" for status options ("production", "candidate",
"deprecated", "archived") to designate the current life cycle
state of this column.
Returns
-------
UpdateFieldMetadataResult
version: the new table version after the update.
"""
@deprecation.deprecated(
deprecated_in="0.33.1",
current_version=__version__,
details="Use update_field_metadata() instead.",
)
def replace_field_metadata(self, field_name: str, new_metadata: Dict[str, str]):
"""Replace all metadata of a field in the schema.
.. deprecated:: 0.33.1
Use [update_field_metadata][lancedb.table.Table.update_field_metadata]
with ``replace=True`` instead.
Parameters
----------
field_name: str
The name of the field whose metadata is replaced.
new_metadata: dict
The new metadata, replacing all existing keys.
"""
self.update_field_metadata(
{"path": field_name, "metadata": new_metadata, "replace": True}
)
@abstractmethod
def drop_columns(self, columns: Iterable[str]) -> DropColumnsResult:
"""
Drop columns from the table.
Local tables reject dropping source or vector columns used by an
embedding function with ValueError, preserving the embedding binding.
Parameters
----------
columns : Iterable[str]
The names of the columns to drop.
Returns
-------
DropColumnsResult
version: the new version number of the table dropping the columns.
"""
@abstractmethod
def checkout(self, version: Union[int, str]):
"""
Checks out a specific version of the Table
Any read operation on the table will now access the data at the checked out
version. As a consequence, calling this method will disable any read consistency
interval that was previously set.
In LanceDB Embedded, a tag can select a read-only snapshot on another
branch. `checkout_latest()` returns to this handle's original branch.
Restoring a snapshot from another branch raises `ValueError` before writing.
Use [branches.checkout()][lancedb.table.Branches.checkout] to open a
handle on that branch before restoring its tag.
This is a read-only operation that turns the table into a sort of "view"
or "detached head". Other table instances will not be affected. To make the
change permanent you can use the `[Self::restore]` method.
Any operation that modifies the table will fail while the table is in a checked
out state. To return the table to a normal state use
`[Self::checkout_latest]`.
For Enterprise tables, a tag must belong to this handle's current branch.
To read a tag on another branch, first obtain that branch's handle with
`table.branches.checkout("branch_name")`, then call `checkout(tag)` on
that handle. A tag on another branch raises an error without changing
the current selection.
Parameters
----------
version: int | str,
The version to check out. A version number (`int`) or a tag
(`str`) can be provided.
"""
@abstractmethod
def checkout_latest(self):
"""
Ensures the table is pointing at the latest version
This can be used to manually update a table when the read_consistency_interval
is None
It can also be used to undo a `[Self::checkout]` operation
"""
@abstractmethod
def restore(self, version: Optional[Union[int, str]] = None):
"""Restore a version of the table. This is an in-place operation.
This creates a new version where the data is equivalent to the
specified previous version. Data is not copied (as of python-v0.2.1).
In LanceDB Embedded, a snapshot on another branch cannot be restored
through this handle. Open that branch with `branches.checkout()` first;
an ambiguous cross-branch restore raises `ValueError` before writing.
Parameters
----------
version : int or str, default None
The version number or version tag to restore.
If unspecified then restores the currently checked out version.
If the currently checked out version is the
latest version then this is a no-op.
"""
@abstractmethod
def list_versions(self) -> List[Dict[str, Any]]:
"""List all versions of the table"""
@cached_property
def _dataset_uri(self) -> str:
return _table_uri(self._conn.uri, self.name)
def _get_fts_index_path(self) -> Tuple[str, pa_fs.FileSystem, bool]:
from .remote.table import RemoteTable
if isinstance(self, RemoteTable) or get_uri_scheme(self._dataset_uri) != "file":
return ("", None, False)
path = join_uri(self._dataset_uri, "_indices", "fts")
fs, path = fs_from_uri(path)
index_exists = fs.get_file_info(path).type != pa_fs.FileType.NotFound
return (path, fs, index_exists)
def _ensure_no_legacy_fts_index(self):
path, _, exists = self._get_fts_index_path()
if exists:
raise ValueError(
"Legacy Tantivy FTS index detected at "
f"{path}. Tantivy-based FTS has been removed. "
"Delete the legacy index and recreate it with "
"table.create_fts_index(...)."
)
@abstractmethod
def uses_v2_manifest_paths(self) -> bool:
"""
Check if the table is using the new v2 manifest paths.
Returns
-------
bool
True if the table is using the new v2 manifest paths, False otherwise.
"""
@abstractmethod
def migrate_v2_manifest_paths(self):
"""
Migrate the manifest paths to the new format.
This will update the manifest to use the new v2 format for paths.
This function is idempotent, and can be run multiple times without
changing the state of the object store.
!!! danger
This should not be run while other concurrent operations are happening.
And it should also run until completion before resuming other operations.
You can use
[Table.uses_v2_manifest_paths][lancedb.table.Table.uses_v2_manifest_paths]
to check if the table is already using the new path style.
"""
# WAL-PK-FUSION: delete both hooks, here and on AsyncTable and RemoteTable.
def _hybrid_pk_fusion_learned(self) -> bool:
"""Whether a hybrid query here has already been refused ``_rowid``.
Learned from a refusal, never probed, so asking is free. ``False`` for
table types that never refuse.
"""
return False
def _note_hybrid_pk_fusion(self) -> None:
"""Remember a ``_rowid`` refusal, so later hybrid queries skip it."""
class LanceTable(Table):
"""
A table in a LanceDB database.
This can be opened in two modes: standard and time-travel.
Standard mode is the default. In this mode, the table is mutable and tracks
the latest version of the table. The level of read consistency is controlled
by the `read_consistency_interval` parameter on the connection.
Time-travel mode is activated by specifying a version number. In this mode,
the table is immutable and fixed to a specific version. This is useful for
querying historical versions of the table.
"""
def __init__(
self,
connection: "LanceDBConnection",
name: str,
*,
namespace_path: Optional[List[str]] = None,
storage_options: Optional[Dict[str, str]] = None,
index_cache_size: Optional[int] = None,
location: Optional[str] = None,
namespace_client: Optional[Any] = None,
managed_versioning: Optional[bool] = None,
pushdown_operations: Optional[set] = None,
route_pushdown_to_rust: bool = False,
_async: AsyncTable = None,
):
if namespace_path is None:
namespace_path = []
self._conn = connection
self._namespace_path = namespace_path
self._storage_options = storage_options
self._index_cache_size = index_cache_size
self._location = location # Store location for use in _dataset_path
self._managed_versioning = managed_versioning
self._namespace_client = namespace_client
self._pushdown_operations = pushdown_operations or set()
# When the connection built the namespace client natively (e.g. an
# enterprise "rest" connection), the underlying Rust table already
# executes QueryTable pushdown itself -- and, unlike this Python urllib3
# path, it routes through the read-freshness context provider that emits
# the ``x-lancedb-min-timestamp`` header. So we must defer pushdown to
# Rust instead of calling the Python ``namespace_client.query_table``
# directly, or reads silently bypass read-freshness (stale results).
self._route_pushdown_to_rust = route_pushdown_to_rust
if _async is not None:
self._table = _async
else:
self._table = LOOP.run(
connection._conn.open_table(
name,
namespace_path=namespace_path,
storage_options=storage_options,
index_cache_size=index_cache_size,
location=location,
namespace_client=namespace_client,
managed_versioning=managed_versioning,
)
)
# Take queries read the pushdown set from the inner table.
self._table._pushdown_operations = self._pushdown_operations
# These coordinates live on the Python wrapper: the native table only
# reports its current branch and version, not whether it is pinned.
self._branch = self._table.current_branch()
self._checkout_version: Optional[int] = None
def _pickle_connection_state(self) -> str:
"""Serialize the connection, rejecting state a worker cannot recreate."""
connection = self._conn
namespace_client = getattr(connection, "_namespace_client", None)
namespace_impl = getattr(connection, "_namespace_client_impl", None)
if namespace_client is not None and namespace_impl is None:
raise TypeError(
f"Cannot pickle LanceTable {self.name!r} backed by a namespace "
"connection whose custom namespace_client cannot be "
"reconstructed. Connect through connect_namespace(impl, "
"properties) or provide a picklable worker-side namespace "
"connection instead."
)
pickle_state = getattr(connection, "_pickle_state", None)
if pickle_state is not None:
return pickle_state()
session = getattr(connection, "session", None)
if session is not None:
raise TypeError(
"Cannot pickle a LanceTable whose connection was created with a "
"Session; Session objects are not serializable. Create the "
"Session in each worker process instead."
)
return connection.serialize()
def _table_namespace_client(self) -> Optional[Any]:
"""The namespace client set on this table rather than its connection."""
namespace_client = self._namespace_client
if namespace_client is getattr(self._conn, "_namespace_client", None):
# The connection rebuilds its own namespace client.
return None
if namespace_client is getattr(self._conn, "_cached_namespace_client", None):
# Local manifest-mode connections cache their namespace client here.
return None
return namespace_client
def _namespace_client_for_pickle(self) -> Optional[Any]:
"""Keep a table-level namespace client only when pickle can carry it."""
namespace_client = self._table_namespace_client()
if namespace_client is None:
return None
import pickle
try:
pickle.dumps(namespace_client)
except Exception as error:
raise TypeError(
f"Cannot pickle LanceTable {self.name!r}: its custom "
f"namespace_client ({type(namespace_client).__name__}) is not "
"serializable. Reopen the table in the worker through a "
"picklable namespace connection instead."
) from error
return namespace_client
@classmethod
def _open_from_pickle_state(
cls, connection: "DBConnection", state: dict[str, Any]
) -> "LanceTable":
"""Reopen a table from the state returned by ``__getstate__``."""
async_connection = getattr(connection, "_conn", None)
if async_connection is None:
async_connection = connection._inner
# Only a table-level explicit client travels in the state. A connection
# namespace client must reopen through its own provider path so caller
# storage options keep their layering; passing the reconstructed client
# here would turn it into an explicit table provider.
namespace_client = state.get("namespace_client")
async_table = LOOP.run(
async_connection.open_table(
state["name"],
namespace_path=state["namespace_path"],
storage_options=state["storage_options"],
index_cache_size=state["index_cache_size"],
location=state["location"],
namespace_client=namespace_client,
managed_versioning=state["managed_versioning"],
)
)
table = cls(
connection,
state["name"],
namespace_path=state["namespace_path"],
storage_options=state["storage_options"],
index_cache_size=state["index_cache_size"],
location=state["location"],
namespace_client=namespace_client,
managed_versioning=state["managed_versioning"],
pushdown_operations=state["pushdown_operations"],
route_pushdown_to_rust=state["route_pushdown_to_rust"],
_async=async_table,
)
version = state["checkout_version"]
# Older pickle states recorded only the visible snapshot branch.
tracking_branch = state.get("tracking_branch", state["branch"])
if tracking_branch is not None:
table = table.branches.checkout(tracking_branch)
if version is not None:
LOOP.run(table._table._checkout_snapshot(state["branch"], version))
table._branch = state["branch"]
table._checkout_version = version
# Apply after checkout so a foreign snapshot remains readable even if
# the original branch was replaced while the handle was serialized.
identifier = state.get("tracking_branch_identifier")
if identifier is not None:
LOOP.run(table._table._set_tracking_branch_identifier(identifier))
return table
def __getstate__(self) -> dict[str, Any]:
"""Return a process-independent state for pickle-based workers."""
if self._conn.uri.startswith("memory://"):
# In-memory databases cannot be reopened by name in another process.
# Keep the established behavior of exporting the visible table.
return {"kind": "memory", "name": self.name, "data": self.to_arrow()}
return {
"kind": "lance",
"connection_state": self._pickle_connection_state(),
"namespace_client": self._namespace_client_for_pickle(),
**self._reopen_state(),
}
def _reopen_state(self) -> dict[str, Any]:
"""Arguments for ``_open_from_pickle_state``, minus the connection."""
# Preserve the snapshot branch separately from the branch that
# checkout_latest() reattaches to after cross-branch time travel.
self._branch = self.current_branch()
return {
"name": self.name,
"namespace_path": list(self._namespace_path),
# Only caller-supplied overrides are safe to replay. Namespace-provided
# initial options may contain rotating credentials and must be fetched
# again by the reopened connection's storage-options provider.
"storage_options": self._storage_options,
"index_cache_size": self._index_cache_size,
"location": self._location,
"managed_versioning": self._managed_versioning,
"pushdown_operations": set(self._pushdown_operations),
"route_pushdown_to_rust": self._route_pushdown_to_rust,
"branch": self._branch,
"tracking_branch": self._table._inner._tracking_branch(),
"tracking_branch_identifier": LOOP.run(
self._table._tracking_branch_identifier()
),
"checkout_version": self._checkout_version,
}
def _private_copy(self) -> "LanceTable":
"""A new handle on the same table, branch and checkout, over this
handle's own connection.
``checkout()`` on the copy leaves this handle untouched. Unlike a
pickle round trip, the connection need not be serializable (for
example, one created with a ``Session``), and an in-memory table keeps
its version history.
"""
return type(self)._open_from_pickle_state(
self._conn,
{
**self._reopen_state(),
"namespace_client": self._table_namespace_client(),
},
)
def __setstate__(self, state: dict[str, Any]) -> None:
"""Rebuild a table from ``__getstate__`` in the current process."""
if state.get("kind") == "memory":
from . import connect
rebuilt = connect("memory://").create_table(state["name"], state["data"])
self.__dict__.update(rebuilt.__dict__)
return
from . import deserialize_conn
connection = deserialize_conn(state["connection_state"], for_worker=True)
reopened = type(self)._open_from_pickle_state(connection, state)
self.__dict__.update(reopened.__dict__)
@property
def name(self) -> str:
return self._table.name
@property
def namespace(self) -> List[str]:
"""Return the namespace path of the table."""
return self._namespace_path
@classmethod
async def from_inner(
cls,
tbl: LanceDBTable,
*,
connection: Optional["DBConnection"] = None,
name: Optional[str] = None,
namespace_path: Optional[List[str]] = None,
location: Optional[str] = None,
):
async_tbl = AsyncTable(tbl)
if connection is not None:
# A table created through a Python connection keeps that
# connection's explicit options. Reconstructing a bare connection
# from the native handle loses namespace, storage, manifest and
# Session state needed by pickle.
return cls(
connection,
name or async_tbl.name,
namespace_path=namespace_path,
storage_options=getattr(connection, "storage_options", None),
location=location,
namespace_client=getattr(connection, "_namespace_client", None),
managed_versioning=getattr(connection, "_managed_versioning", None),
pushdown_operations=getattr(
connection, "_namespace_client_pushdown_operations", None
),
route_pushdown_to_rust=getattr(
connection, "_route_pushdown_to_rust", False
),
_async=async_tbl,
)
from .db import AsyncConnection, LanceDBConnection
inner_conn = tbl.database()
read_consistency_interval = await AsyncConnection(
inner_conn
).get_read_consistency_interval()
conn = LanceDBConnection.from_inner(inner_conn, read_consistency_interval)
return cls(
conn,
async_tbl.name,
_async=async_tbl,
)
@classmethod
def open(
cls,
db,
name,
*,
namespace_path: Optional[List[str]] = None,
storage_options: Optional[Dict[str, str]] = None,
index_cache_size: Optional[int] = None,
location: Optional[str] = None,
namespace_client: Optional[Any] = None,
managed_versioning: Optional[bool] = None,
pushdown_operations: Optional[set] = None,
):
if namespace_path is None:
namespace_path = []
tbl = cls(
db,
name,
namespace_path=namespace_path,
storage_options=storage_options,
index_cache_size=index_cache_size,
location=location,
namespace_client=namespace_client,
managed_versioning=managed_versioning,
pushdown_operations=pushdown_operations,
)
# check the dataset exists
try:
tbl.version
except ValueError as e:
if "Not found:" in str(e):
raise FileNotFoundError(f"Table {name} does not exist")
raise e
return tbl
@cached_property
def _dataset_path(self) -> str:
# Cacheable since it's deterministic
# If table was opened with explicit location (e.g., from namespace),
# use that location directly instead of constructing from base URI
if self._location is not None:
return self._location
return _table_path(self._conn.uri, self.name)
def to_lance(self, **kwargs) -> lance.LanceDataset:
"""Return the LanceDataset backing this table."""
try:
import lance
except ImportError:
raise ImportError(
"The lance library is required to use this function. "
"Please install with `pip install pylance`."
)
branch = self.current_branch()
version = None if branch is not None else self.version
namespace_client = self._namespace_client
if namespace_client is None:
conn_uri = getattr(self._conn, "uri", "")
if get_uri_scheme(conn_uri) == "namespace" or self._namespace_path:
namespace_client = self._conn.namespace_client()
self._namespace_client = namespace_client
if namespace_client is not None:
table_id = self._namespace_path + [self.name]
ds = lance.dataset(
version=version,
storage_options=self._conn.storage_options,
namespace_client=namespace_client,
table_id=table_id,
**kwargs,
)
else:
ds = lance.dataset(
self._dataset_path,
version=version,
storage_options=self._conn.storage_options,
**kwargs,
)
if branch is not None:
ds = ds.checkout_version((branch, self.version))
return ds
def __datafusion_table_provider__(self, session: object) -> object:
return self._inner.__datafusion_table_provider__(session)
@property
def schema(self) -> pa.Schema:
"""Return the schema of the table.
Returns
-------
pa.Schema
A PyArrow schema object."""
schema_sync = getattr(self._table, "_schema_sync", None)
if schema_sync is None:
return LOOP.run(self._table.schema())
return schema_sync()
def list_versions(self) -> List[Dict[str, Any]]:
"""List all versions of the table"""
return LOOP.run(self._table.list_versions())
def delta(
self,
from_version: int | str,
to_version: Optional[int | str] = None,
) -> "TableDelta":
"""Compare two versions or tags.
See [Table.delta][lancedb.table.Table.delta].
"""
async_delta = LOOP.run(self._table.delta(from_version, to_version))
return TableDelta(async_delta._inner)
@property
def version(self) -> int:
"""Get the current version of the table"""
return LOOP.run(self._table.version())
def take_offsets(
self,
offsets: list[int],
*,
with_row_id: bool = False,
preserve_order: bool = False,
) -> LanceTakeQueryBuilder:
builder = LanceTakeQueryBuilder(
self._table.take_offsets(offsets, preserve_order=preserve_order)
)
if with_row_id:
builder.with_row_id()
return builder
def take_row_ids(
self, row_ids: list[int], *, with_row_id: bool = False
) -> LanceTakeQueryBuilder:
builder = LanceTakeQueryBuilder(self._table.take_row_ids(row_ids))
if with_row_id:
builder.with_row_id()
return builder
def blob_columns(self) -> list[str]:
return LOOP.run(self._table.blob_columns())
def fetch_blobs(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
) -> pa.LargeBinaryArray:
return LOOP.run(self._table.fetch_blobs(column, row_ids))
def fetch_blob_ranges(
self,
column: str,
requests: Sequence[Sequence[int]],
) -> pa.LargeBinaryArray:
return LOOP.run(self._table.fetch_blob_ranges(column, list(requests)))
def fetch_blob_files(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
*,
buffer_size: Optional[int] = None,
) -> "list[Optional[BlobFile]]":
return LOOP.run(
self._table.fetch_blob_files(column, row_ids, buffer_size=buffer_size)
)
def open_blob_file(
self,
descriptor: Union[dict, pa.StructScalar, None],
*,
buffer_size: Optional[int] = None,
) -> Optional[BlobFile]:
return LOOP.run(self._table.open_blob_file(descriptor, buffer_size=buffer_size))
@property
def tags(self) -> Tags:
"""Tag management for the table.
Similar to Git, tags are a way to add metadata to a specific version of the
table.
.. warning::
Tagged versions are exempted from the :py:meth:`cleanup_old_versions()`
process.
To remove a version that has been tagged, you must first
:py:meth:`~Tags.delete` the associated tag.
Returns
-------
Tags
The tag manager for managing tags for the table.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table",
... [{"vector": [1.1, 0.9], "type": "vector"}])
>>> table.tags.create("v1", table.version)
>>> table.add([{"vector": [0.5, 0.2], "type": "vector"}])
AddResult(version=2)
>>> tags = table.tags.list()
>>> print(tags["v1"]["version"])
1
>>> table.checkout("v1")
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
"""
return Tags(self._table)
@property
def branches(self) -> "Branches":
"""Branch management for the table.
``create``/``checkout`` return a new table handle scoped to the branch;
writes on it do not affect ``main``.
"""
return Branches(self)
def current_branch(self) -> Optional[str]:
"""The branch of the visible snapshot, or ``None`` for ``main``.
During Embedded tag time travel, this can differ from the original
branch that ``checkout_latest()`` returns to.
"""
return self._table.current_branch()
def _pin_current_snapshot(self) -> "LanceTable":
"""Return an independent handle pinned to this table's current snapshot."""
async def do_pin():
pinned = await self._table._checkout_current()
return pinned, await pinned.version()
pinned, version = LOOP.run(do_pin())
return self._wrap_branch_handle(pinned, version)
def _wrap_branch_handle(
self, async_table: "AsyncTable", version: Optional[int] = None
) -> "LanceTable":
table = LanceTable(
self._conn,
async_table.name,
namespace_path=self._namespace_path,
storage_options=self._storage_options,
index_cache_size=self._index_cache_size,
namespace_client=self._namespace_client,
managed_versioning=self._managed_versioning,
pushdown_operations=self._pushdown_operations,
route_pushdown_to_rust=self._route_pushdown_to_rust,
location=self._location,
_async=async_table,
)
# The native handle knows its branch but not whether this wrapper is a
# pinned branch version, so carry that coordinate explicitly.
table._checkout_version = version
return table
def checkout(self, version: Union[int, str]):
"""Checkout a version of the table. This is an in-place operation.
This allows viewing previous versions of the table. If you wish to
keep writing to the dataset starting from an old version, then use
the `restore` function.
Calling this method will set the table into time-travel mode. If you
wish to return to standard mode, call `checkout_latest`.
A tag can select a read-only snapshot on another branch.
`checkout_latest()` returns to this handle's original branch. Restoring
a snapshot from another branch raises `ValueError` before writing. Use
[branches.checkout()][lancedb.table.Branches.checkout] to open a handle
on that branch before restoring its tag.
Parameters
----------
version: int | str,
The version to check out. A version number (`int`) or a tag
(`str`) can be provided.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table",
... [{"vector": [1.1, 0.9], "type": "vector"}])
>>> table.version
1
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
>>> table.add([{"vector": [0.5, 0.2], "type": "vector"}])
AddResult(version=2)
>>> table.version
2
>>> table.checkout(1)
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
"""
LOOP.run(self._table.checkout(version))
self._checkout_version = self.version
self._branch = self.current_branch()
def checkout_latest(self):
"""Checkout the latest version of the table. This is an in-place operation.
The table will be set back into standard mode, and will track the latest
version of the table.
"""
LOOP.run(self._table.checkout_latest())
self._checkout_version = None
self._branch = self.current_branch()
def _ensure_mutable(self):
# Writes routed through pylance bypass the native pinned-version check,
# so reject them here with the same error the Rust write paths raise.
if self._checkout_version is not None:
raise ValueError(
"Invalid input, table cannot be modified when a specific version "
"is checked out"
)
def restore(self, version: Optional[Union[int, str]] = None):
"""Restore a version of the table. This is an in-place operation.
This creates a new version where the data is equivalent to the
specified previous version. Data is not copied (as of python-v0.2.1).
In LanceDB Embedded, a snapshot on another branch cannot be restored
through this handle. Open that branch with `branches.checkout()` first;
an ambiguous cross-branch restore raises `ValueError` before writing.
Parameters
----------
version : int or str, default None
The version number or version tag to restore.
If unspecified then restores the currently checked out version.
If the currently checked out version is the
latest version then this is a no-op.
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", [
... {"vector": [1.1, 0.9], "type": "vector"}])
>>> table.version
1
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
>>> table.add([{"vector": [0.5, 0.2], "type": "vector"}])
AddResult(version=2)
>>> table.version
2
>>> table.tags.create("v2", 2)
>>> table.restore(1)
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
>>> len(table.list_versions())
3
>>> table.restore("v2")
>>> table.to_pandas()
vector type
0 [1.1, 0.9] vector
1 [0.5, 0.2] vector
>>> len(table.list_versions())
4
"""
if version is not None:
self.checkout(version)
LOOP.run(self._table.restore())
self._checkout_version = None
self._branch = self.current_branch()
def count_rows(self, filter: Optional[str] = None) -> int:
return LOOP.run(self._table.count_rows(filter))
def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.name!r}, _conn={self._conn!r})"
def __str__(self) -> str:
return self.__repr__()
def head(self, n=5) -> pa.Table:
"""Return the first n rows of the table."""
return LOOP.run(self._table.head(n))
def to_pandas(self, blob_mode: BlobMode = "lazy", **kwargs) -> "pd.DataFrame":
"""Return the table as a pandas DataFrame.
Parameters
----------
blob_mode: str, default "lazy"
Controls how Lance blob columns are returned.
**kwargs
Forwarded to Lance pandas conversion.
Returns
-------
pd.DataFrame
"""
validate_blob_mode(blob_mode)
if blob_mode == "descriptions" or not schema_has_blob_field(self.schema):
arrow_tbl = self.to_arrow()
if blob_mode == "descriptions":
arrow_tbl = strip_auto_row_ids(
arrow_tbl, row_addressable_blob_v2_paths(self.schema)
)
return arrow_tbl.to_pandas(**kwargs)
if (
blob_mode == "lazy"
and self._namespace_client is None
and get_uri_scheme(self._dataset_path) == "memory"
):
return self.to_arrow().to_pandas(**kwargs)
if blob_mode == "bytes" and schema_has_blob_field(self.schema):
return self.search().to_pandas(blob_mode=blob_mode, **kwargs)
return self.to_lance().to_pandas(blob_mode=blob_mode, **kwargs)
def to_arrow(self, *, blob_mode: BlobMode = "lazy") -> pa.Table:
"""Return the table as a pyarrow Table.
Parameters
----------
blob_mode: str, default "lazy"
``"lazy"`` returns blob v2 descriptors that carry the row id
``fetch_blobs`` reads. ``"descriptions"`` returns them without it.
``"bytes"`` returns payload bytes for supported row-addressable blob
columns and raises for blobs inside lists.
Returns
-------
pa.Table"""
validate_blob_mode(blob_mode)
if blob_mode != "lazy" and schema_has_blob_field(self.schema):
return self.search().to_arrow(blob_mode=blob_mode)
if (
_should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
):
return self._execute_query(Query()).read_all()
return LOOP.run(self._table.to_arrow())
def to_polars(self, batch_size=None) -> "pl.LazyFrame":
"""Return the table as a polars LazyFrame.
Parameters
----------
batch_size: int, optional
Passed to polars. This is the maximum row count for
scanned pyarrow record batches
Note
----
1. This requires polars to be installed separately
2. Currently we've disabled push-down of the filters from polars
because polars pushdown into pyarrow uses pyarrow compute
expressions rather than SQl strings (which LanceDB supports)
3. The Polars streaming engine is not supported because it does not
currently implement Python PyArrow dataset scans. Use the default
engine when collecting this LazyFrame.
Returns
-------
pl.LazyFrame
"""
from lancedb.integrations.pyarrow import PyarrowDatasetAdapter
dataset = PyarrowDatasetAdapter(self)
# Polars 1.32's non-PyArrow callback path passes batch_size twice. Keep
# the compatible PyArrow path, but block predicates because this adapter
# cannot translate PyArrow expressions into LanceDB filters.
return pl.scan_pyarrow_dataset(dataset, batch_size=batch_size).map_batches(
_polars_predicate_pushdown_barrier,
predicate_pushdown=False,
)
# New unified API overload
@overload
def create_index(
self,
column: str,
/,
*,
config: IndexConfigType,
replace: bool = ...,
wait_timeout: Optional[timedelta] = ...,
name: Optional[str] = ...,
exist_ok: bool = ...,
train: bool = ...,
) -> None: ...
# Legacy API overload (deprecated)
@overload
def create_index(
self,
metric: Literal["l2", "cosine", "dot", "hamming"] = ...,
num_partitions: Optional[int] = ...,
num_sub_vectors: Optional[int] = ...,
vector_column_name: str = ...,
replace: bool = ...,
accelerator: Optional[str] = ...,
index_cache_size: Optional[int] = ...,
num_bits: int = ...,
index_type: Literal[
"IVF_FLAT", "IVF_SQ", "IVF_PQ", "IVF_RQ", "IVF_HNSW_SQ", "IVF_HNSW_PQ"
] = ...,
max_iterations: int = ...,
sample_rate: int = ...,
m: int = ...,
ef_construction: int = ...,
*,
wait_timeout: Optional[timedelta] = ...,
name: Optional[str] = ...,
exist_ok: bool = ...,
train: bool = ...,
target_partition_size: Optional[int] = ...,
) -> None: ...
def create_index(
self,
metric: str = "l2",
num_partitions: Optional[int] = None,
num_sub_vectors: Optional[int] = None,
vector_column_name: str = VECTOR_COLUMN_NAME,
replace: bool = True,
accelerator: Optional[str] = None,
index_cache_size: Optional[int] = None,
num_bits: int = 8,
index_type: Literal[
"IVF_FLAT",
"IVF_SQ",
"IVF_PQ",
"IVF_RQ",
"IVF_HNSW_SQ",
"IVF_HNSW_PQ",
"IVF_HNSW_FLAT",
] = "IVF_PQ",
max_iterations: int = 50,
sample_rate: int = 256,
m: int = 20,
ef_construction: int = 300,
*,
config: Optional[IndexConfigType] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
target_partition_size: Optional[int] = None,
):
"""Create an index on a column.
This method supports both the new unified API and the legacy API
for backwards compatibility. The new API takes the column name as the
first positional argument and an index configuration object via
``config``; the legacy API takes the distance metric as the first
argument plus separate ``vector_column_name`` / ``num_partitions`` /
etc. parameters, and emits a ``DeprecationWarning``.
Parameters
----------
metric : str
For new API: the column name to index.
For legacy API: the distance metric ("l2", "cosine", "dot", "hamming").
config : IndexConfigType, optional
The index configuration object. If provided, uses the new unified API.
Can be one of: IvfFlat, IvfPq, IvfSq, IvfRq, HnswPq, HnswSq,
BTree, Bitmap, LabelList, Fm, NGram, ZoneMap, RTree, ScalarPlugin, FTS.
replace : bool, default True
Whether to replace an existing index on this column.
exist_ok : bool, default False
Preserve the existing index with the requested or derived name.
Takes precedence over replace and keeps its definition and build.
wait_timeout : timedelta, optional
Timeout to wait for async indexing to complete.
name : str, optional
Custom name for the index.
train : bool, default True
Whether to train the index with existing data.
Examples
--------
New API (recommended):
>>> table.create_index( # doctest: +SKIP
... "vector", config=IvfPq(distance_type="l2")
... )
>>> table.create_index("category", config=BTree()) # doctest: +SKIP
>>> table.create_index("content", config=FTS()) # doctest: +SKIP
Legacy API (deprecated):
>>> table.create_index( # doctest: +SKIP
... "l2", vector_column_name="vector"
... )
"""
# Detect whether this is a legacy API call
is_legacy = self._is_legacy_create_index_call(
metric,
config,
num_partitions,
num_sub_vectors,
vector_column_name,
accelerator,
index_cache_size,
)
if is_legacy:
warnings.warn(
"The create_index() API with metric/num_partitions parameters is "
"deprecated and will be removed in a future version. "
"Please migrate to the new unified API:\n"
" # Old (deprecated):\n"
" table.create_index('l2', vector_column_name='my_vector')\n"
" # New (recommended):\n"
" table.create_index('my_vector', config=IvfPq(distance_type='l2'))",
DeprecationWarning,
stacklevel=2,
)
# Legacy API: first arg is the distance metric
column = vector_column_name
# Build config from legacy parameters
config = self._build_vector_config_from_legacy_params(
metric=metric,
index_type=index_type,
num_partitions=num_partitions,
num_sub_vectors=num_sub_vectors,
num_bits=num_bits,
max_iterations=max_iterations,
sample_rate=sample_rate,
m=m,
ef_construction=ef_construction,
target_partition_size=target_partition_size,
accelerator=accelerator,
)
# Handle accelerator through pylance
if accelerator is not None:
self._ensure_mutable()
dataset = self.to_lance()
_create_accelerated_index(
dataset,
exist_ok=exist_ok,
name=name,
column=column,
index_type=index_type,
metric=metric,
num_partitions=num_partitions,
num_sub_vectors=num_sub_vectors,
replace=replace,
accelerator=accelerator,
index_cache_size=index_cache_size,
num_bits=num_bits,
m=m,
ef_construction=ef_construction,
target_partition_size=target_partition_size,
)
self.checkout_latest()
return
else:
# New API: metric is the column name
column = metric
# Check if config has accelerator set and dispatch to pylance
if config is not None and hasattr(config, "accelerator"):
acc = getattr(config, "accelerator", None)
if acc is not None:
# Dispatch to pylance for GPU acceleration
self._ensure_mutable()
dataset = self.to_lance()
index_type_map = {
"IvfFlat": "IVF_FLAT",
"IvfSq": "IVF_SQ",
"IvfPq": "IVF_PQ",
"IvfRq": "IVF_RQ",
"HnswPq": "IVF_HNSW_PQ",
"HnswSq": "IVF_HNSW_SQ",
}
cfg_type = type(config).__name__
lance_index_type = index_type_map.get(cfg_type, "IVF_PQ")
quantization_kwargs = {}
num_bits = getattr(config, "num_bits", 8)
if num_bits is not None:
quantization_kwargs["num_bits"] = num_bits
_create_accelerated_index(
dataset,
exist_ok=exist_ok,
name=name,
column=column,
index_type=lance_index_type,
metric=getattr(config, "distance_type", "l2"),
num_partitions=getattr(config, "num_partitions", None),
num_sub_vectors=getattr(config, "num_sub_vectors", None),
replace=replace,
accelerator=acc,
m=getattr(config, "m", 20),
ef_construction=getattr(config, "ef_construction", 300),
target_partition_size=getattr(
config, "target_partition_size", None
),
**quantization_kwargs,
)
self.checkout_latest()
return
return LOOP.run(
self._table.create_index(
column,
replace=replace,
exist_ok=exist_ok,
config=config,
wait_timeout=wait_timeout,
name=name,
train=train,
)
)
def create_index_async(
self,
column: str,
*,
config: IndexConfigType,
replace: Optional[bool] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
) -> Job:
"""Create an index, returning a handle to the indexing job.
The job may already be complete when returned; callers must not assume
the index exists until :meth:`Job.wait` returns.
In LanceDB Enterprise, the returned job tracks this submission only if
the server returns a job ID. If it does not, the job is already
complete and does not wait for the background build; use
``wait_for_index`` and account for the replacement caveat described in
[create_index][lancedb.table.Table.create_index].
"""
return Job(
LOOP.run(
self._table.create_index_async(
column,
replace=replace,
exist_ok=exist_ok,
config=config,
wait_timeout=wait_timeout,
name=name,
train=train,
)
)
)
def drop_index(self, name: str) -> None:
"""
Drops an index from the table
Parameters
----------
name: str
The name of the index to drop
"""
return LOOP.run(self._table.drop_index(name))
def prewarm_index(self, name: str) -> None:
"""
Prewarm an index in the table.
This is a hint to the database that the index will be accessed in the
future and should be loaded into memory if possible. This can reduce
cold-start latency for subsequent queries.
This call initiates prewarming and returns once the request is accepted.
It is idempotent and safe to call from multiple clients concurrently.
It is generally wasteful to call this if the index does not fit into the
available cache. Not all index types support prewarming; unsupported
indices will silently ignore the request.
Parameters
----------
name: str
The name of the index to prewarm
"""
return LOOP.run(self._table.prewarm_index(name))
def prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> None:
"""
Prewarm data for the table.
This is a hint to the database that the given columns will be accessed
in the future and the database should prefetch the data if possible.
Currently only supported in LanceDB Enterprise.
This call initiates prewarming and returns once the request is accepted.
Uses SQL PREWARM and requires Flight SQL connectivity. To track the
durable warming job, use ``prewarm_data_async()``.
This operation has a large upfront cost but can speed up future queries
that need to fetch the given columns. Large columns such as embeddings
or binary data may not be practical to prewarm. This feature is intended
for workloads that issue many queries against the same columns.
Parameters
----------
columns: list of str, optional
The columns to prewarm. If None, all columns are prewarmed.
filter: str, optional
A single SQL predicate for conservative metadata pruning.
parameters: sequence, mapping, or one-row Arrow batch/table, optional
Scalar values for ``$1``/``$name`` placeholders in ``filter``.
Values are sent separately from SQL; requires server support.
concurrency: int, optional
Positive per-executor warming concurrency; omitted uses the server default.
Examples
--------
>>> table.prewarm_data( # doctest: +SKIP
... ["payload"], filter="quality > 0.7", concurrency=4)
"""
return LOOP.run(
self._table.prewarm_data(
columns, filter=filter, concurrency=concurrency, parameters=parameters
)
)
def explain_prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> PrewarmDataPlan:
"""Preview prewarm without submitting a warming job.
See [Table.explain_prewarm_data][lancedb.table.Table.explain_prewarm_data]
for options, permissions, snapshot semantics, and limitations.
"""
return LOOP.run(
self._table.explain_prewarm_data(
columns, filter=filter, concurrency=concurrency, parameters=parameters
)
)
def prewarm_data_async(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> Job[PrewarmDataResult]:
return Job(
LOOP.run(
self._table.prewarm_data_async(
columns,
filter=filter,
concurrency=concurrency,
parameters=parameters,
)
)
)
def wait_for_index(
self, index_names: Iterable[str], timeout: timedelta = timedelta(seconds=300)
) -> None:
return LOOP.run(self._table.wait_for_index(index_names, timeout))
def stats(self) -> TableStatistics:
return LOOP.run(self._table.stats())
@property
def uri(self) -> str:
return LOOP.run(self._table.uri())
def initial_storage_options(self) -> Optional[Dict[str, str]]:
"""Get the initial storage options that were passed in when opening this table.
For dynamically refreshed options (e.g., credential vending), use
:meth:`latest_storage_options`.
Warning: This is an internal API and the return value is subject to change.
Returns
-------
Optional[Dict[str, str]]
The storage options, or None if no storage options were configured.
"""
return LOOP.run(self._table.initial_storage_options())
def latest_storage_options(self) -> Optional[Dict[str, str]]:
"""Get the latest storage options, refreshing from provider if configured.
This method is useful for credential vending scenarios where storage options
may be refreshed dynamically. If no dynamic provider is configured, this
returns the initial static options.
Warning: This is an internal API and the return value is subject to change.
Returns
-------
Optional[Dict[str, str]]
The storage options, or None if no storage options were configured.
"""
return LOOP.run(self._table.latest_storage_options())
@deprecation.deprecated(
deprecated_in="0.25.0",
current_version=__version__,
details="Use create_index() with config=BTree()/Bitmap()/LabelList() instead.",
)
def create_scalar_index(
self,
column: str,
*,
replace: bool = True,
index_type: ScalarIndexType = "BTREE",
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
):
"""Create a scalar index on a column.
.. deprecated:: 0.25.0
Use :meth:`create_index` with a BTree, Bitmap, or LabelList config instead.
Example: ``table.create_index("column", config=BTree())``
"""
if index_type == "scalar":
warnings.warn(
'index_type="scalar" is deprecated; use "BTREE" instead',
DeprecationWarning,
stacklevel=2,
)
index_type = "BTREE"
if index_type == "BTREE":
config = BTree()
elif index_type == "BITMAP":
config = Bitmap()
elif index_type == "LABEL_LIST":
config = LabelList()
else:
raise ValueError(f"Unknown index type {index_type}")
return LOOP.run(
self._table.create_index(
column,
replace=replace,
exist_ok=exist_ok,
config=config,
wait_timeout=wait_timeout,
name=name,
)
)
@deprecation.deprecated(
deprecated_in="0.25.0",
current_version=__version__,
details="Use create_index() with config=FTS() instead.",
)
def create_fts_index(
self,
field_names: Union[str, List[str]],
*,
ordering_field_names: Optional[Union[str, List[str]]] = None,
replace: bool = False,
writer_heap_size: Optional[int] = 1024 * 1024 * 1024,
use_tantivy: bool = False,
tokenizer_name: Optional[str] = None,
with_position: bool = False,
# tokenizer configs:
base_tokenizer: BaseTokenizerType = "simple",
language: str = "English",
max_token_length: Optional[int] = 40,
lower_case: bool = True,
stem: Optional[bool] = None,
remove_stop_words: Optional[bool] = None,
custom_stop_words: Optional[List[str]] = None,
ascii_folding: bool = True,
ngram_min_length: int = 3,
ngram_max_length: int = 3,
prefix_only: bool = False,
block_size: int = 128,
document_granularity: DocumentGranularity = DocumentGranularity.ROW,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
):
"""Create a full-text search index on a column.
.. deprecated:: 0.25.0
Use :meth:`create_index` with an FTS config instead.
Example: ``table.create_index("text_column", config=FTS())``
"""
self._ensure_no_legacy_fts_index()
if use_tantivy:
raise ValueError(
"Tantivy-based FTS has been removed. "
"Remove use_tantivy and recreate the index with native FTS."
)
if ordering_field_names is not None:
raise ValueError(
"ordering_field_names was only supported by the removed "
"Tantivy-based FTS implementation."
)
if writer_heap_size != 1024 * 1024 * 1024:
raise ValueError(
"writer_heap_size was only supported by the removed "
"Tantivy-based FTS implementation."
)
if not isinstance(field_names, str):
raise ValueError(
"Native FTS indexes can only be created on a single field "
"at a time. To search over multiple text fields, create a "
"separate FTS index for each field."
)
if tokenizer_name is None:
tokenizer_configs = {
"base_tokenizer": base_tokenizer,
"language": language,
"with_position": with_position,
"max_token_length": max_token_length,
"lower_case": lower_case,
"stem": stem,
"remove_stop_words": remove_stop_words,
"custom_stop_words": custom_stop_words,
"ascii_folding": ascii_folding,
"ngram_min_length": ngram_min_length,
"ngram_max_length": ngram_max_length,
"prefix_only": prefix_only,
}
else:
tokenizer_configs = self.infer_tokenizer_configs(tokenizer_name)
tokenizer_configs["custom_stop_words"] = custom_stop_words
config = FTS(
block_size=block_size,
document_granularity=document_granularity,
**tokenizer_configs,
)
try:
LOOP.run(
self._table.create_index(
field_names,
replace=replace,
exist_ok=exist_ok,
config=config,
name=name,
wait_timeout=wait_timeout,
)
)
except (ValueError, RuntimeError) as e:
_maybe_add_fts_error_note(
e,
base_tokenizer=config.base_tokenizer,
language=config.language,
)
raise e
def add(
self,
data: DATA,
mode: AddMode = "append",
on_bad_vectors: OnBadVectorsType = "error",
fill_value: float = 0.0,
progress: Optional[Union[bool, Callable, Any]] = None,
write_parallelism: Optional[int] = None,
allow_external_blob_outside_bases: bool = False,
retries: Optional[int] = None,
) -> AddResult:
"""Add data to the table.
If vector columns are missing and the table
has embedding functions, then the vector columns
are automatically computed and added.
Parameters
----------
data: list-of-dict, pd.DataFrame
The data to insert into the table.
mode: str
The mode to use when writing the data. Valid values are
"append" and "overwrite". Overwrite adopts explicitly declared blob
layouts, while inferred bytes retain the existing blob v2 fields.
on_bad_vectors: str, default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
One of "error", "drop", "fill", "null".
fill_value: float, default 0.
The value to use when filling vectors. Only used if on_bad_vectors="fill".
progress: bool, callable, or tqdm-like, optional
A callback or tqdm-compatible progress bar. See
:meth:`Table.add` for details.
write_parallelism: int, optional
Number of partitions to write in parallel. Higher values increase
throughput but also peak memory use, since each partition buffers
data in flight. Defaults to an estimate based on the data size,
capped at the number of CPU cores. Lower this if bulk ingestion is
using too much memory.
allow_external_blob_outside_bases: bool, default False
Allow blob URIs outside registered bases. See :meth:`Table.add`.
LanceDB Embedded only.
retries: int, optional
Override the remote retry budget for this call (0 to 255). Zero
disables retries, including multipart parts. Ambiguous commits are
never replayed. Ignored by LanceDB Embedded.
Returns
-------
int
The number of vectors in the table.
"""
progress, owns = _normalize_progress(progress)
try:
return LOOP.run(
self._table.add(
data,
mode=mode,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
progress=progress,
write_parallelism=write_parallelism,
allow_external_blob_outside_bases=allow_external_blob_outside_bases,
retries=retries,
)
)
finally:
if owns:
progress.close()
def merge(
self,
other_table: Union[LanceTable, DATA],
left_on: str,
right_on: Optional[str] = None,
schema: Optional[Union[pa.Schema, LanceModel]] = None,
):
"""Merge another table into this table.
Performs a left join, where the dataset is the left side and other_table
is the right side. Rows existing in the dataset but not on the left will
be filled with null values, unless Lance doesn't support null values for
some types, in which case an error will be raised. The only overlapping
column allowed is the join column. If other overlapping columns exist,
an error will be raised.
Parameters
----------
other_table: LanceTable or Reader-like
The data to be merged. Acceptable types are:
- Pandas DataFrame, Pyarrow Table, Dataset, Scanner,
Iterator[RecordBatch], or RecordBatchReader
- LanceTable
left_on: str
The name of the column in the dataset to join on.
right_on: str or None
The name of the column in other_table to join on. If None, defaults to
left_on.
schema: pa.Schema or LanceModel, optional
The schema of the other_table.
If not provided, the schema is inferred from the data.
Examples
--------
>>> import lancedb
>>> import pyarrow as pa
>>> df = pa.table({'x': [1, 2, 3], 'y': ['a', 'b', 'c']})
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("dataset", df)
>>> table.to_pandas()
x y
0 1 a
1 2 b
2 3 c
>>> new_df = pa.table({'x': [1, 2, 3], 'z': ['d', 'e', 'f']})
>>> table.merge(new_df, 'x')
>>> table.to_pandas()
x y z
0 1 a d
1 2 b e
2 3 c f
"""
self._ensure_mutable()
if isinstance(other_table, LanceTable):
other_table = other_table.to_lance()
else:
other_table = _sanitize_data(
other_table,
schema,
)
self.to_lance().merge(
other_table, left_on=left_on, right_on=right_on, schema=schema
)
self.checkout_latest()
@cached_property
def embedding_functions(self) -> Dict[str, EmbeddingFunctionConfig]:
"""
Get the embedding functions for the table
Returns
-------
funcs: Dict[str, EmbeddingFunctionConfig]
A mapping of the vector column to the embedding function
or empty dict if not configured.
"""
return EmbeddingFunctionRegistry.get_instance().parse_functions(
self.schema.metadata
)
@overload
def search( # type: ignore
self,
query: Optional[Union[VEC, str, "PIL.Image.Image", Tuple]] = None,
vector_column_name: Optional[str] = None,
query_type: Literal["vector"] = "vector",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceVectorQueryBuilder: ...
@overload
def search(
self,
query: Optional[Union[VEC, str, "PIL.Image.Image", Tuple]] = None,
vector_column_name: Optional[str] = None,
query_type: Literal["fts"] = "fts",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceFtsQueryBuilder: ...
@overload
def search(
self,
query: Optional[
Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery]
] = None,
vector_column_name: Optional[str] = None,
query_type: Literal["hybrid"] = "hybrid",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceHybridQueryBuilder: ...
@overload
def search(
self,
query: None = None,
vector_column_name: Optional[str] = None,
query_type: QueryType = "auto",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceEmptyQueryBuilder: ...
def search(
self,
query: Optional[
Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery]
] = None,
vector_column_name: Optional[str] = None,
query_type: QueryType = "auto",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
fast_search: Optional[bool] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> LanceQueryBuilder:
"""Create a search query to find the nearest neighbors
of the given query vector. We currently support [vector search](https://lancedb.com/docs/search/vector-search/)
and [full-text search](https://lancedb.com/docs/search/full-text-search/).
Examples
--------
>>> import lancedb
>>> db = lancedb.connect("./.lancedb")
>>> data = [
... {"original_width": 100, "caption": "bar", "vector": [0.1, 2.3, 4.5]},
... {"original_width": 2000, "caption": "foo", "vector": [0.5, 3.4, 1.3]},
... {"original_width": 3000, "caption": "test", "vector": [0.3, 6.2, 2.6]}
... ]
>>> table = db.create_table("my_table", data)
>>> query = [0.4, 1.4, 2.4]
>>> (table.search(query)
... .where("original_width > 1000", prefilter=True)
... .select(["caption", "original_width", "vector"])
... .limit(2)
... .to_pandas())
caption original_width vector _distance
0 foo 2000 [0.5, 3.4, 1.3] 5.220000
1 test 3000 [0.3, 6.2, 2.6] 23.089996
Parameters
----------
query: list/np.ndarray/str/PIL.Image.Image, default None
The targeted vector to search for.
- *default None*.
Acceptable types are: list, np.ndarray, PIL.Image.Image
- If None then the
select/[where][lancedb.query.LanceQueryBuilder.where]/limit clauses
are applied to filter the table
vector_column_name: str, optional
The name of the vector column to search.
The vector column needs to be a pyarrow fixed size list type
*default "vector"*
- If not specified then the vector column is inferred from
the table schema
- If the table has multiple vector columns then the *vector_column_name*
needs to be specified. Otherwise, an error is raised.
query_type: str, default "auto"
"vector", "fts", or "auto"
If "auto" then the query type is inferred from the query;
If `query` is a list/np.ndarray then the query type is "vector";
If `query` is a PIL.Image.Image then either do vector search
or raise an error if no corresponding embedding function is found.
If the `query` is a string, then the query type is "vector" if the
table has embedding functions, else the query type is "fts"
fts_columns: str or list of str, default None
The column(s) to search in for full-text search.
If None then the search is performed on all indexed columns.
For now, only one column can be searched at a time.
operator: "AND" or "OR", optional
Combine terms in a string full-text query; the default is OR.
Use ``query_type="fts"`` when the table also has embeddings.
Returns
-------
LanceQueryBuilder
A query builder object representing the query.
Once executed, the query returns selected columns, the vector,
and also the "_distance" column which is the distance between the query
vector and the returned vector.
"""
if isinstance(query, FullTextQuery):
query_type = "fts"
if (
vector_column_name is None
and query_type != "fts"
and not (query is None and query_type != "hybrid")
):
schema = self.schema
else:
schema = None
vector_column_name = infer_vector_column_name(
schema=schema,
query_type=query_type,
query=query,
vector_column_name=vector_column_name,
)
return LanceQueryBuilder.create(
self,
query,
query_type,
vector_column_name=vector_column_name,
ordering_field_name=ordering_field_name,
fts_columns=fts_columns or [],
fast_search=fast_search,
operator=operator,
)
@classmethod
def create(
cls,
db: LanceDBConnection,
name: str,
data: Optional[DATA] = None,
schema: Optional[pa.Schema] = None,
mode: CreateMode = "create",
exist_ok: bool = False,
on_bad_vectors: OnBadVectorsType = "error",
fill_value: float = 0.0,
embedding_functions: Optional[List[EmbeddingFunctionConfig]] = None,
*,
namespace_path: Optional[List[str]] = None,
storage_options: Optional[Dict[str, str | bool]] = None,
data_storage_version: Optional[str] = None,
enable_v2_manifest_paths: Optional[bool] = None,
location: Optional[str] = None,
namespace_client: Optional[Any] = None,
pushdown_operations: Optional[set] = None,
route_pushdown_to_rust: bool = False,
initial_bases: Optional[List[Any]] = None,
allow_external_blob_outside_bases: bool = False,
):
"""
Create a new table.
Examples
--------
>>> import lancedb
>>> data = [
... {"x": 1, "vector": [1.0, 2]},
... {"x": 2, "vector": [3.0, 4]},
... {"x": 3, "vector": [5.0, 6]}
... ]
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
Parameters
----------
db: LanceDB
The LanceDB instance to create the table in.
name: str
The name of the table to create.
data: list-of-dict, dict, pd.DataFrame, default None
The data to insert into the table.
At least one of `data` or `schema` must be provided.
schema: pa.Schema or LanceModel, optional
The schema of the table. If not provided,
the schema is inferred from the data.
At least one of `data` or `schema` must be provided.
mode: str, default "create"
The mode to use when writing the data. Valid values are
"create", "overwrite", and "append".
exist_ok: bool, default False
If the table already exists then raise an error if False,
otherwise just open the table, it will not add the provided
data but will validate against any schema that's specified.
on_bad_vectors: str, default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
One of "error", "drop", "fill", "null".
fill_value: float, default 0.
The value to use when filling vectors. Only used if on_bad_vectors="fill".
embedding_functions: list of EmbeddingFunctionModel, default None
The embedding functions to use when creating the table.
data_storage_version: optional, str, default "stable"
Deprecated. Set `storage_options` when connecting to the database and set
`new_table_data_storage_version` in the options.
enable_v2_manifest_paths: optional, bool, default False
Deprecated. Set `storage_options` when connecting to the database and set
`new_table_enable_v2_manifest_paths` in the options.
initial_bases: list, optional
Storage bases to register in the table's manifest in the same
commit that creates it. Each entry can be a
[BasePath][lancedb.schema.BasePath], a dict with `path`, `name`,
`is_dataset_root`, and `id` keys, or a `lance.DatasetBasePath`.
Blob URIs under a registered base are stored relative to it. When
supplying IDs for multiple bases, use distinct explicit non-zero
IDs; [BasePath][lancedb.schema.BasePath] describes the pinned-Lance
limitation.
allow_external_blob_outside_bases: bool, default False
Store blob URIs that do not fall under a registered base. The row
keeps a reference, so the objects have to stay readable. The
opt-in applies only to the initial data; a later `add()` must
enable it again.
"""
if namespace_path is None:
namespace_path = []
self = cls.__new__(cls)
self._conn = db
self._namespace_path = namespace_path
self._index_cache_size = None
self._location = location
self._managed_versioning = None
self._namespace_client = namespace_client
self._pushdown_operations = pushdown_operations or set()
self._route_pushdown_to_rust = route_pushdown_to_rust
if data_storage_version is not None:
warnings.warn(
"setting data_storage_version directly on create_table is deprecated. "
"Use database_options instead.",
DeprecationWarning,
stacklevel=2,
)
if storage_options is None:
storage_options = {}
storage_options["new_table_data_storage_version"] = data_storage_version
if enable_v2_manifest_paths is not None:
warnings.warn(
"setting enable_v2_manifest_paths directly on create_table is "
"deprecated. Use database_options instead.",
DeprecationWarning,
stacklevel=2,
)
if storage_options is None:
storage_options = {}
storage_options["new_table_enable_v2_manifest_paths"] = str(
enable_v2_manifest_paths
).lower()
self._storage_options = storage_options
self._table = LOOP.run(
self._conn._conn.create_table(
name,
data,
schema=schema,
mode=mode,
exist_ok=exist_ok,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
embedding_functions=embedding_functions,
namespace_path=namespace_path,
storage_options=storage_options,
location=location,
namespace_client=namespace_client,
initial_bases=initial_bases,
allow_external_blob_outside_bases=allow_external_blob_outside_bases,
)
)
self._branch = self._table.current_branch()
self._checkout_version = None
return self
def delete(self, where: Union[str, Expr]) -> DeleteResult:
predicate = where._inner if isinstance(where, Expr) else where
return LOOP.run(self._table.delete(predicate))
def update(
self,
where: Optional[Union[str, Expr]] = None,
values: Optional[dict] = None,
*,
values_sql: Optional[Dict[str, str]] = None,
) -> UpdateResult:
"""
This can be used to update zero to all rows depending on how many
rows match the where clause.
Embeddings are not computed by this method. Updating an embedding source
column requires providing all associated vector columns in the same call;
otherwise a ValueError is raised. Use `merge_insert` to recompute embeddings
automatically.
Parameters
----------
where: str or [Expr][lancedb.expr.Expr], optional
The filter condition. Can be a SQL string or a type-safe
[Expr][lancedb.expr.Expr] built with [col][lancedb.expr.col] and
[lit][lancedb.expr.lit]. The filter must not be empty, or it will
error.
values: dict, optional
The values to update. The keys are the column names and the values
are the values to set.
values_sql: dict, optional
The values to update, expressed as SQL expression strings. These can
reference existing columns. For example, {"x": "x + 1"} will increment
the x column by 1.
Returns
-------
UpdateResult
- rows_updated: The number of rows that were updated
- version: The new version number of the table after the update
Examples
--------
>>> import lancedb
>>> from lancedb.expr import col
>>> import pandas as pd
>>> data = pd.DataFrame({"x": [1, 2, 3], "vector": [[1.0, 2], [3, 4], [5, 6]]})
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
>>> table.update(where=col("x") == 2, values={"vector": [10.0, 10]})
UpdateResult(rows_updated=1, version=2)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 3 [5.0, 6.0]
2 2 [10.0, 10.0]
"""
return LOOP.run(self._table.update(values, where=where, updates_sql=values_sql))
def _execute_query(
self,
query: Query,
*,
batch_size: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> pa.RecordBatchReader:
# Branch queries run locally: the server-side query protocol can't
# carry a branch yet.
# TODO: push down server-side once it can (with remote table support).
if (
_should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
and self.current_branch() is None
and not _requires_local_namespace_execution(query)
):
from lancedb.namespace import _execute_server_side_query
table_id = self._namespace_path + [self.name]
return _execute_server_side_query(self._namespace_client, table_id, query)
async_iter = LOOP.run(
self._table._execute_query(query, batch_size=batch_size, timeout=timeout)
)
def iter_sync():
try:
while True:
yield LOOP.run(async_iter.__anext__())
except StopAsyncIteration:
return
return pa.RecordBatchReader.from_batches(async_iter.schema, iter_sync())
def _execute_pull_query(
self,
query: Query,
*,
batch_size: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> pa.RecordBatchReader:
"""Execute a plain local scan with one Rust fetch per batch.
The caller has already checked that this is an embedded, non-namespace
plain lazy scan. A query shape that does not map to ``AsyncQuery``
keeps the existing reader path instead of guessing at its semantics.
"""
async_query = self._table._sync_query_to_async(query)
async_query._blob_row_id_prepared = query._blob_row_id_prepared
if isinstance(async_query, AsyncQuery):
return async_query._execute_batches_sync(
max_batch_length=batch_size, timeout=timeout
)
return self._execute_query(query, batch_size=batch_size, timeout=timeout)
def _execute_arrow(
self,
query: Query,
*,
timeout: Optional[timedelta] = None,
) -> pa.Table:
# Keep the Python namespace pushdown path, including its branch rule.
if (
_should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
and self.current_branch() is None
and not _requires_local_namespace_execution(query)
):
return self._execute_query(query, timeout=timeout).read_all()
async_query = self._table._sync_query_to_async(query)
async_query._blob_row_id_prepared = query._blob_row_id_prepared
if (
self._table._inner._is_native()
and self._namespace_client is None
and not self._route_pushdown_to_rust
and isinstance(async_query, (AsyncQuery, AsyncVectorQuery, AsyncFTSQuery))
):
return async_query._execute_eager_sync(timeout=timeout)
return LOOP.run(async_query._execute_eager(timeout=timeout))
def _explain_plan(self, query: Query, verbose: Optional[bool] = False) -> str:
return LOOP.run(self._table._explain_plan(query, verbose))
def _analyze_plan(
self,
query: Query,
*,
distributed_metrics: AnalyzePlanDistributedMetrics = "aggregate",
) -> str:
return LOOP.run(
self._table._analyze_plan(query, distributed_metrics=distributed_metrics)
)
def _output_schema(self, query: Query) -> pa.Schema:
return LOOP.run(self._table._output_schema(query))
def _do_merge(
self,
merge: LanceMergeInsertBuilder,
new_data: DATA,
on_bad_vectors: OnBadVectorsType,
fill_value: float,
retries: Optional[int] = None,
) -> MergeResult:
return LOOP.run(
self._table._do_merge(merge, new_data, on_bad_vectors, fill_value, retries)
)
@property
def _inner(self) -> LanceDBTable:
return self._table._inner
@deprecation.deprecated(
deprecated_in="0.21.0",
current_version=__version__,
details="Use `Table.optimize` instead.",
)
def cleanup_old_versions(
self,
older_than: Optional[timedelta] = None,
*,
delete_unverified: bool = False,
) -> "CleanupStats":
"""
Clean up old versions of the table, freeing disk space.
Parameters
----------
older_than: timedelta, default None
The minimum age of the version to delete. If None, then this defaults
to two weeks.
delete_unverified: bool, default False
Because they may be part of an in-progress transaction, files newer
than 7 days old are not deleted by default. If you are sure that
there are no in-progress transactions, then you can set this to True
to delete all files older than `older_than`.
Returns
-------
CleanupStats
The stats of the cleanup operation, including how many bytes were
freed.
"""
self._ensure_mutable()
return self.to_lance().cleanup_old_versions(
older_than, delete_unverified=delete_unverified
)
@deprecation.deprecated(
deprecated_in="0.21.0",
current_version=__version__,
details="Use `Table.optimize` instead.",
)
def compact_files(self, *args, **kwargs) -> CompactionStats:
"""
Run the compaction process on the table.
This can be run after making several small appends to optimize the table
for faster reads.
Arguments are passed onto `lance.dataset.DatasetOptimizer.compact_files`.
(see Lance documentation for more details) For most cases, the default
should be fine.
"""
self._ensure_mutable()
stats = self.to_lance().optimize.compact_files(*args, **kwargs)
self.checkout_latest()
return stats
def compact_async(
self, *, compaction_options: Optional[CompactionOptions] = None
) -> Job[CompactionResult]:
return Job(
LOOP.run(self._table.compact_async(compaction_options=compaction_options))
)
def explain_cleanup(self, *, min_age_seconds: Optional[int] = None) -> CleanupPlan:
return LOOP.run(self._table.explain_cleanup(min_age_seconds=min_age_seconds))
def cleanup_async(
self, *, min_age_seconds: Optional[int] = None
) -> Job[CleanupResult]:
return Job(LOOP.run(self._table.cleanup_async(min_age_seconds=min_age_seconds)))
def optimize(
self,
*,
cleanup_older_than: Optional[timedelta] = None,
delete_unverified: bool = False,
retrain: bool = False,
compaction_options: Optional[CompactionOptions] = None,
):
"""
Optimize the on-disk data and indices for better performance.
Modeled after ``VACUUM`` in PostgreSQL.
Optimization covers three operations:
* Compaction: Merges small files into larger ones
* Prune: Removes old versions of the dataset
* Index: Optimizes the indices, adding new data to existing indices
Parameters
----------
cleanup_older_than: timedelta, optional default 7 days
All files belonging to versions older than this will be removed. Set
to 0 days to remove all versions except the latest. The latest version
is never removed.
.. warning::
Setting this to 0 deletes the data files of every older
version, so any other reader or writer still using an older
version of the table will fail. Only set it to 0 if no other
process is working on this dataset.
delete_unverified: bool, default False
Files leftover from a failed transaction may appear to be part of an
in-progress operation (e.g. appending new data) and these files will not
be deleted unless they are at least 7 days old. If delete_unverified is True
then these files will be deleted regardless of their age.
.. warning::
This should only be set to True if you can guarantee that no other
process is currently working on this dataset. Otherwise the dataset
could be put into a corrupted state.
retrain: bool, default False
This parameter is no longer used and is deprecated.
compaction_options: CompactionOptions, optional
Options that control file compaction. For large rows, derive a lower
``target_rows_per_fragment`` from the expected row size to bound later
passes. This does not cap the first pass over an existing oversized
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
Notes
-----
The frequency an application should call optimize is based on the frequency of
data modifications. If data is frequently added, deleted, or updated then
optimize should be run frequently. A good rule of thumb is to run optimize if
you have added or modified 100,000 or more records or run more than 20 data
modification operations.
"""
LOOP.run(
self._table._do_optimize(
_optimize_cleanup_since_ms(cleanup_older_than, retrain),
delete_unverified,
compaction_options,
)
)
def list_indices(self) -> Iterable[IndexConfig]:
"""
List all indices that have been created with Self::create_index
"""
return LOOP.run(self._table.list_indices())
def tokenize(
self,
query: str,
*,
column: Optional[str] = None,
index_name: Optional[str] = None,
) -> Iterable[FtsToken]:
"""
Tokenize a query using the tokenizer configured on an FTS index.
Specify exactly one of ``column`` or ``index_name``.
Model-backed tokenizers such as ``jieba/*`` and ``lindera/*`` are
rebuilt in the client process from index metadata. In LanceDB Enterprise,
this means the same tokenizer model files must also exist locally
(under ``LANCE_LANGUAGE_MODEL_HOME``) and must match the files the
server uses. A mismatch cannot be detected: the returned tokens may
differ from what the server's index produces. If the model files are
missing, an error naming the tokenizer is raised.
"""
return LOOP.run(
self._table.tokenize(query, column=column, index_name=index_name)
)
def index_stats(self, index_name: str) -> Optional[IndexStatistics]:
"""
Retrieve statistics about an index
Parameters
----------
index_name: str
The name of the index to retrieve statistics for
Returns
-------
IndexStatistics or None
The statistics about the index. Returns None if the index does not exist.
"""
return LOOP.run(self._table.index_stats(index_name))
def add_columns(
self,
transforms: Mapping[str, str | FunctionApplication]
| FunctionApplication
| pa.Field
| List[pa.Field]
| pa.Schema
| DATA
| pa.RecordBatch
| pa.RecordBatchReader
| pl.DataFrame
| None = None,
*,
computed: Dict[str, str] | None = None,
auto_refresh_enabled: bool | None = None,
auto_refresh_interval_seconds: int | None = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
total_gpu_count: Optional[int] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
) -> AddColumnsResult:
_resource_options(
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
return LOOP.run(
self._table.add_columns(
transforms,
computed=computed,
auto_refresh_enabled=auto_refresh_enabled,
auto_refresh_interval_seconds=auto_refresh_interval_seconds,
total_cpu_millis=total_cpu_millis,
total_memory_bytes=total_memory_bytes,
total_gpu_count=total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
)
def refresh_column(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> "RefreshColumnResult":
"""Fill a computed column's unfilled rows and recompute those whose
inputs changed. See
[`AsyncTable.refresh_column`][lancedb.AsyncTable.refresh_column]."""
return LOOP.run(
self._table.refresh_column(
column,
where=where,
batch_size=batch_size,
max_concurrency=max_concurrency,
publication_interval_rows=publication_interval_rows,
total_cpu_millis=total_cpu_millis,
total_memory_bytes=total_memory_bytes,
supervisor_resources=supervisor_resources,
planning_workers=planning_workers,
gpu_class=gpu_class,
total_gpu_count=total_gpu_count,
retry_policy=retry_policy,
error_policy=error_policy,
max_failed_fraction=max_failed_fraction,
max_failed_rows=max_failed_rows,
row_timeout_seconds=row_timeout_seconds,
init_timeout_seconds=init_timeout_seconds,
num_workers=num_workers,
max_job_attempts=max_job_attempts,
max_executor_processes_per_worker=max_executor_processes_per_worker,
retry_errors=retry_errors,
retry_job_id=retry_job_id,
retry_error_type=retry_error_type,
include_quarantined=include_quarantined,
function_version=function_version,
recompute=recompute,
gpu_count=gpu_count,
)
)
def refresh_column_async(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> Job[RefreshColumnJobResult]:
"""Fill a computed column's unfilled rows and recompute those whose
inputs changed, returning a handle to the refresh job. See
[`Table.refresh_column_async`][lancedb.table.Table.refresh_column_async].
"""
return Job(
LOOP.run(
self._table.refresh_column_async(
column,
where=where,
batch_size=batch_size,
max_concurrency=max_concurrency,
publication_interval_rows=publication_interval_rows,
total_cpu_millis=total_cpu_millis,
total_memory_bytes=total_memory_bytes,
supervisor_resources=supervisor_resources,
planning_workers=planning_workers,
gpu_class=gpu_class,
total_gpu_count=total_gpu_count,
retry_policy=retry_policy,
error_policy=error_policy,
max_failed_fraction=max_failed_fraction,
max_failed_rows=max_failed_rows,
row_timeout_seconds=row_timeout_seconds,
init_timeout_seconds=init_timeout_seconds,
num_workers=num_workers,
max_job_attempts=max_job_attempts,
max_executor_processes_per_worker=max_executor_processes_per_worker,
retry_errors=retry_errors,
retry_job_id=retry_job_id,
retry_error_type=retry_error_type,
include_quarantined=include_quarantined,
function_version=function_version,
recompute=recompute,
gpu_count=gpu_count,
)
)
)
def function_errors(
self,
job_id: Optional[str] = None,
column: Optional[str] = None,
limit: Optional[int] = None,
history: bool = False,
page_token: Optional[str] = None,
) -> "FunctionErrors":
"""The per-row errors Function refreshes recorded on this table. See
[`Table.function_errors`][lancedb.table.Table.function_errors]."""
return LOOP.run(
self._table.function_errors(
job_id=job_id,
column=column,
limit=limit,
history=history,
page_token=page_token,
)
).as_sync(self)
def quarantine_failed(
self,
column: str,
*,
error_type: Optional[str] = None,
job_id: Optional[str] = None,
after_attempts: Optional[int] = None,
quarantined: bool = True,
) -> int:
return LOOP.run(
self._table.quarantine_failed(
column,
error_type=error_type,
job_id=job_id,
after_attempts=after_attempts,
quarantined=quarantined,
)
)
def function_provenance(
self,
column: str,
*,
limit: Optional[int] = None,
version: Optional[int] = None,
) -> FunctionProvenance:
return LOOP.run(
self._table.function_provenance(column, limit=limit, version=version)
)
def column_statuses(
self, column: Optional[str] = None
) -> list[FunctionColumnStatus]:
return LOOP.run(self._table.column_statuses(column=column))
def column_status(self, column: str) -> FunctionColumnStatus:
return LOOP.run(self._table.column_status(column))
def wait_fresh(
self, column: str, timeout: Optional[timedelta] = None
) -> FunctionColumnStatus:
return LOOP.run(self._table.wait_fresh(column, timeout))
def alter_columns(
self, *alterations: Iterable[Dict[str, str]]
) -> AlterColumnsResult:
return LOOP.run(self._table.alter_columns(*alterations))
def update_field_metadata(
self, *updates: dict[str, Any]
) -> UpdateFieldMetadataResult:
return LOOP.run(self._table.update_field_metadata(*updates))
def drop_columns(self, columns: Iterable[str]) -> DropColumnsResult:
return LOOP.run(self._table.drop_columns(columns))
def set_unenforced_primary_key(self, columns: Union[str, Iterable[str]]) -> None:
"""Set the unenforced primary key. See
[`AsyncTable.set_unenforced_primary_key`][lancedb.AsyncTable.set_unenforced_primary_key]."""
return LOOP.run(self._table.set_unenforced_primary_key(columns))
def enable_rte(self, spec: "RTEWriteSpec") -> None:
"""Install an RTEWriteSpec. See
[`AsyncTable.enable_rte`][lancedb.AsyncTable.enable_rte]."""
return LOOP.run(self._table.enable_rte(spec))
def disable_rte(self) -> None:
"""Remove the RTEWriteSpec. See
[`AsyncTable.disable_rte`][lancedb.AsyncTable.disable_rte]."""
return LOOP.run(self._table.disable_rte())
def get_rte_spec(self) -> Optional["RTEWriteSpec"]:
"""Read the installed RTEWriteSpec, or ``None``. See
[`AsyncTable.get_rte_spec`][lancedb.AsyncTable.get_rte_spec]."""
return LOOP.run(self._table.get_rte_spec())
def get_table_overrides(self) -> dict[str, Any]:
"""Read Enterprise table overrides for this table.
Table overrides configure Enterprise-managed background behavior such as
automatic compaction, cleanup, and reindex execution. This method is
supported only on remote Enterprise tables.
"""
return LOOP.run(self._table.get_table_overrides())
def update_table_overrides(self, **overrides: Any) -> dict[str, Any]:
"""Merge Enterprise table overrides into the current table overrides.
Fetches the current overrides, recursively merges nested objects locally,
then sends the complete replacement using REST PUT. Other values replace
existing values; unrelated fields from the read snapshot are preserved.
This read-modify-write is not atomic: concurrent edits can be overwritten,
even to unrelated fields. Coordinate writers when concurrent edits must
be preserved. An empty update only reads and returns the current overrides.
Unlike this SDK method, CLI ``set`` and REST PUT replace the overrides
object without merging. Use ``reset_table_overrides()`` to clear all
overrides. Supported only on remote Enterprise tables.
Examples
--------
>>> table.update_table_overrides( # doctest: +SKIP
... job_types={
... "compaction": {"enabled": True},
... "cleanup": {"enabled": False},
... }
... )
"""
return LOOP.run(self._table.update_table_overrides(**overrides))
def reset_table_overrides(self) -> dict[str, Any]:
"""Clear Enterprise table overrides for this table."""
return LOOP.run(self._table.reset_table_overrides())
def checkpoint_rte(self) -> None:
"""Synchronous version of
[`AsyncTable.checkpoint_rte`][lancedb.AsyncTable.checkpoint_rte]."""
return LOOP.run(self._table.checkpoint_rte())
def flush_rte(self) -> None:
"""Synchronous version of
[`AsyncTable.flush_rte`][lancedb.AsyncTable.flush_rte]."""
return LOOP.run(self._table.flush_rte())
def compact_rte(self) -> None:
"""Synchronous version of
[`AsyncTable.compact_rte`][lancedb.AsyncTable.compact_rte]."""
return LOOP.run(self._table.compact_rte())
def get_rte_stats(self, *, include_sstable_rows: bool = False) -> Optional[dict]:
"""Synchronous version of
[`AsyncTable.get_rte_stats`][lancedb.AsyncTable.get_rte_stats]."""
return LOOP.run(
self._table.get_rte_stats(include_sstable_rows=include_sstable_rows)
)
def close_rte_writers(self) -> None:
"""Close cached RTE shard writers. See
[`AsyncTable.close_rte_writers`][lancedb.AsyncTable.close_rte_writers]."""
return LOOP.run(self._table.close_rte_writers())
def uses_v2_manifest_paths(self) -> bool:
"""
Check if the table is using the new v2 manifest paths.
Returns
-------
bool
True if the table is using the new v2 manifest paths, False otherwise.
"""
return LOOP.run(self._table.uses_v2_manifest_paths())
def migrate_v2_manifest_paths(self):
"""
Migrate the manifest paths to the new format.
This will update the manifest to use the new v2 format for paths.
This function is idempotent, and can be run multiple times without
changing the state of the object store.
!!! danger
This should not be run while other concurrent operations are happening.
And it should also run until completion before resuming other operations.
You can use
[LanceTable.uses_v2_manifest_paths][lancedb.table.LanceTable.uses_v2_manifest_paths]
to check if the table is already using the new path style.
"""
LOOP.run(self._table.migrate_manifest_paths_v2())
@deprecation.deprecated(
deprecated_in="0.33.1",
current_version=__version__,
details="Use update_field_metadata() instead.",
)
def replace_field_metadata(self, field_name: str, new_metadata: Dict[str, str]):
"""
Replace the metadata of a field in the schema
.. deprecated:: 0.33.1
Use :func:`update_field_metadata` instead.
Parameters
----------
field_name: str
The name of the field to replace the metadata for
new_metadata: dict
The new metadata to set
"""
LOOP.run(self._table.replace_field_metadata(field_name, new_metadata))
def _handle_bad_vectors(
reader: pa.RecordBatchReader,
on_bad_vectors: Literal["error", "drop", "fill", "null"] = "error",
fill_value: float = 0.0,
target_schema: Optional[pa.Schema] = None,
metadata: Optional[dict] = None,
) -> pa.RecordBatchReader:
vector_columns = _find_vector_columns(reader.schema, target_schema, metadata)
if not vector_columns:
return reader
output_schema = _vector_output_schema(reader.schema, vector_columns)
def gen():
for batch in reader:
pending_dims = []
for vector_column in vector_columns:
dim = vector_column["expected_dim"]
if target_schema is not None and dim is None:
dim = _infer_vector_dim(batch[vector_column["name"]])
pending_dims.append(vector_column)
batch = _handle_bad_vector_column(
batch,
vector_column_name=vector_column["name"],
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
expected_dim=dim,
expected_value_type=vector_column["expected_value_type"],
)
for vector_column in pending_dims:
if vector_column["expected_dim"] is None:
vector_column["expected_dim"] = _infer_vector_dim(
batch[vector_column["name"]]
)
if batch.schema.equals(output_schema, check_metadata=True):
yield batch
continue
cast_batches = (
pa.Table.from_batches([batch]).cast(output_schema).to_batches()
)
if cast_batches:
yield pa.RecordBatch.from_arrays(
cast_batches[0].columns,
schema=output_schema,
)
return pa.RecordBatchReader.from_batches(output_schema, gen())
def _find_vector_columns(
reader_schema: pa.Schema,
target_schema: Optional[pa.Schema],
metadata: Optional[dict],
) -> List[dict]:
if target_schema is None:
vector_columns = []
for field in reader_schema:
named_vector_col = (
_is_list_like(field.type)
and pa.types.is_floating(field.type.value_type)
and field.name == VECTOR_COLUMN_NAME
)
likely_vector_col = (
pa.types.is_fixed_size_list(field.type)
and pa.types.is_floating(field.type.value_type)
and (field.type.list_size >= 10)
)
if named_vector_col or likely_vector_col:
vector_columns.append(
{
"name": field.name,
"expected_dim": None,
"expected_value_type": None,
}
)
return vector_columns
reader_column_names = set(reader_schema.names)
active_metadata = _merge_metadata(target_schema.metadata, metadata)
embedding_function_columns = set(
EmbeddingFunctionRegistry.get_instance().parse_functions(active_metadata).keys()
)
vector_columns = []
for field in target_schema:
if field.name not in reader_column_names:
continue
if not _is_list_like(field.type) or not pa.types.is_floating(
field.type.value_type
):
continue
reader_field = reader_schema.field(field.name)
named_vector_col = (
field.name in embedding_function_columns
or field.name == VECTOR_COLUMN_NAME
or (field.name == "embedding" and pa.types.is_fixed_size_list(field.type))
)
typed_fixed_vector_col = (
pa.types.is_fixed_size_list(reader_field.type)
and pa.types.is_floating(reader_field.type.value_type)
and reader_field.type.list_size >= 10
)
if named_vector_col or typed_fixed_vector_col:
vector_columns.append(
{
"name": field.name,
"expected_dim": (
field.type.list_size
if pa.types.is_fixed_size_list(field.type)
else None
),
"expected_value_type": field.type.value_type,
}
)
return vector_columns
def _vector_output_schema(
reader_schema: pa.Schema,
vector_columns: List[dict],
) -> pa.Schema:
columns_by_name = {column["name"]: column for column in vector_columns}
fields = []
for field in reader_schema:
column = columns_by_name.get(field.name)
if column is None:
output_type = field.type
else:
output_type = _vector_output_type(field, column)
fields.append(pa.field(field.name, output_type, field.nullable, field.metadata))
return pa.schema(fields, metadata=reader_schema.metadata)
def _vector_output_type(field: pa.Field, vector_column: dict) -> pa.DataType:
if not _is_list_like(field.type):
return field.type
if vector_column["expected_value_type"] is not None and (
pa.types.is_null(field.type.value_type)
or pa.types.is_integer(field.type.value_type)
or pa.types.is_unsigned_integer(field.type.value_type)
):
return pa.list_(vector_column["expected_value_type"])
if (
vector_column["expected_dim"] is not None
and pa.types.is_fixed_size_list(field.type)
and field.type.list_size != vector_column["expected_dim"]
):
return pa.list_(field.type.value_type)
return field.type
def _handle_bad_vector_column(
data: pa.RecordBatch,
vector_column_name: str,
on_bad_vectors: str = "error",
fill_value: float = 0.0,
expected_dim: Optional[int] = None,
expected_value_type: Optional[pa.DataType] = None,
) -> pa.RecordBatch:
"""
Ensure that the vector column exists and has type fixed_size_list(float)
Parameters
----------
data: pa.Table
The table to sanitize.
vector_column_name: str
The name of the vector column.
on_bad_vectors: str, default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
One of "error", "drop", "fill", "null".
fill_value: float, default 0.0
The value to use when filling vectors. Only used if on_bad_vectors="fill".
"""
position = data.column_names.index(vector_column_name)
vec_arr = data[vector_column_name]
if not _is_list_like(vec_arr.type):
return data
if (
expected_dim is not None
and pa.types.is_fixed_size_list(vec_arr.type)
and vec_arr.type.list_size != expected_dim
):
vec_arr = pa.array(vec_arr.to_pylist(), type=pa.list_(vec_arr.type.value_type))
data = data.set_column(position, vector_column_name, vec_arr)
if expected_value_type is not None and (
pa.types.is_null(vec_arr.type.value_type)
or pa.types.is_integer(vec_arr.type.value_type)
or pa.types.is_unsigned_integer(vec_arr.type.value_type)
):
vec_arr = pa.array(vec_arr.to_pylist(), type=pa.list_(expected_value_type))
data = data.set_column(position, vector_column_name, vec_arr)
if pa.types.is_floating(vec_arr.type.value_type):
has_nan = has_nan_values(vec_arr)
else:
has_nan = pa.array([False] * len(vec_arr))
if expected_dim is not None:
dim = expected_dim
elif pa.types.is_fixed_size_list(vec_arr.type):
dim = vec_arr.type.list_size
else:
dim = _infer_vector_dim(vec_arr)
if dim is None:
return data
is_null = pc.is_null(vec_arr)
# pc.list_value_length returns null for null list entries, so
# pc.not_equal(null, dim) also returns null. Use or_kleene so that
# True OR null = True (Kleene three-valued logic), ensuring null vectors
# are counted as wrong-dim.
has_wrong_dim = pc.or_kleene(
is_null,
pc.not_equal(pc.list_value_length(vec_arr), dim),
)
has_bad_vectors = pc.any(has_nan).as_py() or pc.any(has_wrong_dim).as_py()
if has_bad_vectors:
is_bad = pc.or_(has_nan, has_wrong_dim)
if on_bad_vectors == "error":
if pc.any(has_wrong_dim).as_py():
raise ValueError(
f"Vector column '{vector_column_name}' has variable length "
"vectors. Set on_bad_vectors='drop' to remove them, "
"set on_bad_vectors='fill' and fill_value=<value> to replace them, "
"or set on_bad_vectors='null' to replace them with null."
)
else:
raise ValueError(
f"Vector column '{vector_column_name}' has NaNs or null values. "
"Set on_bad_vectors='drop' to remove them, "
"set on_bad_vectors='fill' and fill_value=<value> to replace them, "
"or set on_bad_vectors='null' to replace them with null."
)
elif on_bad_vectors == "null":
vec_arr = pc.if_else(
is_bad,
pa.scalar(None),
vec_arr,
)
elif on_bad_vectors == "drop":
data = data.filter(pc.invert(is_bad))
vec_arr = data[vector_column_name]
elif on_bad_vectors == "fill":
if fill_value is None:
raise ValueError(
"`fill_value` must not be None if `on_bad_vectors` is 'fill'"
)
vec_arr = _fill_bad_vector_values(vec_arr, dim, fill_value)
else:
raise ValueError(f"Invalid value for on_bad_vectors: {on_bad_vectors}")
return data.set_column(position, vector_column_name, vec_arr)
def _fill_bad_vector_values(
arr: Union[pa.Array, pa.ChunkedArray],
dim: int,
fill_value: float,
) -> pa.Array:
if not isinstance(arr, pa.ChunkedArray):
arr = pa.chunked_array([arr])
arr = arr.combine_chunks()
# A fixed-size slice truncates long vectors and pads short vectors with nulls.
# Both padding and existing null elements need to be filled.
sliced = pc.list_slice(arr, 0, dim, return_fixed_size_list=True)
values = sliced.values
needs_fill = pc.is_null(values)
if pa.types.is_floating(values.type):
values_for_nan_check = (
values.cast(pa.float32()) if pa.types.is_float16(values.type) else values
)
needs_fill = pc.or_kleene(needs_fill, pc.is_nan(values_for_nan_check))
fill_scalar = pa.scalar(fill_value).cast(values.type)
filled_values = pc.if_else(needs_fill, fill_scalar, values)
filled = pa.FixedSizeListArray.from_arrays(filled_values, dim)
return filled.cast(arr.type)
def has_nan_values(arr: Union[pa.ListArray, pa.ChunkedArray]) -> pa.BooleanArray:
"""Find vectors with NaN or null components, including pandas-converted NaNs."""
if isinstance(arr, pa.ChunkedArray):
values = pa.chunked_array([chunk.flatten() for chunk in arr.chunks])
else:
values = arr.flatten()
if pa.types.is_float16(values.type):
# is_nan isn't yet implemented for f16, so we cast to f32
# https://github.com/apache/arrow/issues/45083
values_has_nan = pc.is_nan(values.cast(pa.float32()))
else:
values_has_nan = pc.is_nan(values)
# Pandas represents NaN components as Arrow nulls. is_nan returns null
# for these values, so count them as invalid before filtering parent indices.
values_has_nan = pc.fill_null(values_has_nan, True)
values_indices = pc.list_parent_indices(arr)
if arr.null_count:
# Match flatten(), which excludes child spans under null parent lists.
values_indices = pc.filter(
values_indices, pc.take(pc.is_valid(arr), values_indices)
)
has_nan_indices = pc.unique(pc.filter(values_indices, values_has_nan))
indices = pa.array(range(len(arr)), type=pa.uint32())
return pc.is_in(indices, has_nan_indices)
def _is_list_like(data_type: pa.DataType) -> bool:
return (
pa.types.is_list(data_type)
or pa.types.is_large_list(data_type)
or pa.types.is_fixed_size_list(data_type)
)
def _merge_metadata(*metadata_dicts: Optional[dict]) -> dict:
merged = {}
for metadata in metadata_dicts:
if metadata is None:
continue
for key, value in metadata.items():
if isinstance(key, str):
key = key.encode("utf-8")
if isinstance(value, str):
value = value.encode("utf-8")
merged[key] = value
return merged
def _name_suggests_vector_column(field_name: str) -> bool:
"""Check if a field name indicates a vector column."""
name_lower = field_name.lower()
return "vector" in name_lower or "embedding" in name_lower
def _infer_target_schema(
reader: pa.RecordBatchReader,
) -> Tuple[pa.Schema, pa.RecordBatchReader]:
schema = reader.schema
peeked = None
for i, field in enumerate(schema):
is_list_type = pa.types.is_list(field.type) or pa.types.is_large_list(
field.type
)
if _name_suggests_vector_column(field.name) and is_list_type:
if peeked is None:
peeked, reader = peek_reader(reader)
# Use the most common length of the list as the dimensions
dim = _modal_list_size(peeked.column(i))
if dim is None:
# Every list entry is null (e.g. embeddings not computed yet),
# so the dimension is unknowable. Leave the column as a
# variable-length list instead of guessing a fixed size.
continue
# Determine target type based on value type
if pa.types.is_floating(field.type.value_type):
target_type = pa.list_(pa.float32(), dim)
elif pa.types.is_integer(field.type.value_type):
values = peeked.column(i)
if isinstance(values, pa.ChunkedArray):
values = values.combine_chunks()
flattened = values.flatten()
valid_count = pc.count(flattened, mode="only_valid").as_py()
if valid_count == 0:
target_type = pa.list_(pa.uint8(), dim)
else:
min_max = pc.min_max(flattened)
min_value = min_max["min"].as_py()
max_value = min_max["max"].as_py()
if (min_value is not None and min_value < 0) or (
max_value is not None and max_value > 255
):
target_type = pa.list_(pa.float32(), dim)
else:
target_type = pa.list_(pa.uint8(), dim)
else:
continue # Skip non-numeric types
new_field = pa.field(
field.name, # preserve original field name
target_type,
nullable=field.nullable,
metadata=field.metadata,
)
schema = schema.set(i, new_field)
return schema, reader
def _modal_list_size(arr: Union[pa.ListArray, pa.ChunkedArray]) -> Optional[int]:
# Use the most common length of the list as the dimensions. pyarrow's
# `mode` skips nulls, so an all-null column yields an empty result; return
# None in that case so the caller can keep a variable-length list.
modes = pc.mode(pc.list_value_length(arr))
if len(modes) == 0:
return None
return modes[0].as_py()["mode"]
def _infer_vector_dim(arr: Union[pa.Array, pa.ChunkedArray]) -> Optional[int]:
if not _is_list_like(arr.type):
return None
lengths = pc.list_value_length(arr)
lengths = pc.filter(lengths, pc.greater(lengths, 0))
if len(lengths) == 0:
return None
return pc.mode(lengths)[0].as_py()["mode"]
def _validate_schema(schema: pa.Schema):
"""
Make sure the metadata is valid utf8
"""
if schema.metadata is not None:
_validate_metadata(schema.metadata)
def _validate_metadata(metadata: dict):
"""
Make sure the metadata values are valid utf8 (can be nested)
Raises ValueError if not valid utf8
"""
for k, v in metadata.items():
if isinstance(v, bytes):
try:
v.decode("utf8")
except UnicodeDecodeError:
raise ValueError(
f"Metadata key {k} is not valid utf8. "
"Consider base64 encode for generic binary metadata."
)
elif isinstance(v, dict):
_validate_metadata(v)
class AsyncTable:
"""
An AsyncTable is a collection of Records in a LanceDB Database.
An AsyncTable can be obtained from the
[AsyncConnection.create_table][lancedb.AsyncConnection.create_table] and
[AsyncConnection.open_table][lancedb.AsyncConnection.open_table] methods.
An AsyncTable object is expected to be long lived and reused for multiple
operations. AsyncTable objects will cache a certain amount of index data in memory.
This cache will be freed when the Table is garbage collected. To eagerly free the
cache you can call the [close][lancedb.AsyncTable.close] method. Once the
AsyncTable is closed, it cannot be used for any further operations.
An AsyncTable can also be used as a context manager, and will automatically close
when the context is exited. Closing a table is optional. If you do not close the
table, it will be closed when the AsyncTable object is garbage collected.
Examples
--------
Create using [AsyncConnection.create_table][lancedb.AsyncConnection.create_table]
(more examples in that method's documentation).
>>> import lancedb
>>> async def create_a_table():
... db = await lancedb.connect_async("./.lancedb")
... data = [{"vector": [1.1, 1.2], "b": 2}]
... table = await db.create_table("my_table", data=data)
... print(await table.query().limit(5).to_arrow())
>>> import asyncio
>>> asyncio.run(create_a_table())
pyarrow.Table
vector: fixed_size_list<item: float>[2]
child 0, item: float
b: int64
----
vector: [[[1.1,1.2]]]
b: [[2]]
Can append new data with [AsyncTable.add()][lancedb.table.AsyncTable.add].
>>> async def add_to_table():
... db = await lancedb.connect_async("./.lancedb")
... table = await db.open_table("my_table")
... await table.add([{"vector": [0.5, 1.3], "b": 4}])
>>> asyncio.run(add_to_table())
Can query the table with
[AsyncTable.vector_search][lancedb.table.AsyncTable.vector_search].
>>> async def search_table_for_vector():
... db = await lancedb.connect_async("./.lancedb")
... table = await db.open_table("my_table")
... results = (
... await table.vector_search([0.4, 0.4]).select(["b", "vector"]).to_pandas()
... )
... print(results)
>>> asyncio.run(search_table_for_vector())
b vector _distance
0 4 [0.5, 1.3] 0.82
1 2 [1.1, 1.2] 1.13
Search queries are much faster when an index is created. See
[AsyncTable.create_index][lancedb.table.AsyncTable.create_index].
"""
def __init__(
self,
table: LanceDBTable,
*,
namespace_path: Optional[List[str]] = None,
namespace_client: Optional[Any] = None,
pushdown_operations: Optional[set] = None,
route_pushdown_to_rust: bool = False,
):
"""Create a new AsyncTable object.
You should not create AsyncTable objects directly.
Use [AsyncConnection.create_table][lancedb.AsyncConnection.create_table] and
[AsyncConnection.open_table][lancedb.AsyncConnection.open_table] to obtain
Table objects."""
self._inner = table
self._namespace_path = namespace_path or []
self._namespace_client = namespace_client
self._pushdown_operations = pushdown_operations or set()
# See LanceTable.__init__: defer QueryTable pushdown to Rust (which emits
# the read-freshness header) for natively-built namespace clients.
self._route_pushdown_to_rust = route_pushdown_to_rust
def _set_namespace_context(
self,
*,
namespace_path: Optional[List[str]] = None,
namespace_client: Optional[Any] = None,
pushdown_operations: Optional[set] = None,
route_pushdown_to_rust: bool = False,
) -> "AsyncTable":
self._namespace_path = namespace_path or []
self._namespace_client = namespace_client
self._pushdown_operations = pushdown_operations or set()
self._route_pushdown_to_rust = route_pushdown_to_rust
return self
def __repr__(self):
return self._inner.__repr__()
def __enter__(self):
return self
def __exit__(self, *_):
self.close()
def is_open(self) -> bool:
"""Return True if the table is open."""
return self._inner.is_open()
def close(self):
"""Close the table and free any resources associated with it.
It is safe to call this method multiple times.
Any attempt to use the table after it has been closed will raise an error."""
return self._inner.close()
async def set_unenforced_primary_key(
self, columns: Union[str, Iterable[str]]
) -> None:
"""Set the unenforced primary key for this table to the given
ordered list of columns.
"Unenforced" means LanceDB does not check uniqueness on writes; the
columns are recorded in the schema as the primary key so that
features such as `merge_insert` can use them. Calling this again
replaces any previously-set primary key.
Parameters
----------
columns : str or Iterable[str]
Either a single column name (single-column key) or an ordered
iterable of column names (composite key). Each column dtype
must be one of: int32, int64, utf8, large_utf8, binary,
large_binary, fixed_size_binary.
"""
if isinstance(columns, str):
columns = [columns]
else:
columns = list(columns)
await self._inner.set_unenforced_primary_key(columns)
async def enable_rte(self, spec: "RTEWriteSpec") -> None:
"""Install an RTEWriteSpec on this table.
The spec selects Lance's MemWAL LSM-style write path for future
`merge_insert` calls. ``RTEWriteSpec`` chooses one of three sharding
strategies:
- ``RTEWriteSpec.bucket(column, num_buckets)`` — hash-bucket writes by
the single-column unenforced primary key.
- ``RTEWriteSpec.identity(column)`` — shard by the raw value of a
scalar column.
- ``RTEWriteSpec.unsharded()`` — route every write to a single shard.
All variants require the table to have an unenforced primary key set
via [`set_unenforced_primary_key`]; bucket sharding additionally
requires it to be the single column being bucketed.
By default the RTE maintains every index on the table, re-read as the
table changes, so an index created later is maintained too; one of a
kind the RTE cannot mirror is skipped. Name the set with
``with_maintained_indexes`` to pin it instead: each name must already
exist and be maintainable (a still-building index is rejected, not
omitted); ``[]`` maintains none.
Parameters
----------
spec : RTEWriteSpec
The sharding spec to install.
Examples
--------
>>> from lancedb import RTEWriteSpec
>>> # table.set_unenforced_primary_key("id")
>>> # table.enable_rte(RTEWriteSpec.bucket("id", 16))
"""
await self._inner.enable_rte(spec)
async def disable_rte(self) -> None:
"""Remove the RTEWriteSpec from this table.
Reverts to the standard `merge_insert` write path. Errors if no spec
is currently set, or if the RTE still holds rows that have not been
merged into the base table, since removing it would drop them.
"""
await self._inner.disable_rte()
async def get_rte_spec(self) -> Optional["RTEWriteSpec"]:
"""Read the RTEWriteSpec currently installed on this table.
Returns ``None`` when the RTE write path is not enabled at all — no
spec has been set, or one was removed with `disable_rte`.
That is a different answer from a spec whose ``maintained_indexes``
is ``None``, which is an installed spec selecting indexes
automatically.
The spec read back is the one that was installed, selection
included: ``None`` maintains every supported index the table has now
or gains later, ``[]`` maintains none, and a non-empty list maintains
exactly those. All three round-trip.
"""
return await self._inner.get_rte_spec()
async def get_table_overrides(self) -> dict[str, Any]:
"""Read Enterprise table overrides for this table.
Table overrides configure Enterprise-managed background behavior such as
automatic compaction, cleanup, and reindex execution. This method is
supported only on remote Enterprise tables.
"""
return json.loads(await self._inner.get_table_overrides())
async def update_table_overrides(self, **overrides: Any) -> dict[str, Any]:
"""Merge Enterprise table overrides into the current table overrides.
Fetches the current overrides, recursively merges nested objects locally,
then sends the complete replacement using REST PUT. Other values replace
existing values; unrelated fields from the read snapshot are preserved.
This read-modify-write is not atomic: concurrent edits can be overwritten,
even to unrelated fields. Coordinate writers when concurrent edits must
be preserved. An empty update only reads and returns the current overrides.
Unlike this SDK method, CLI ``set`` and REST PUT replace the overrides
object without merging. Use ``reset_table_overrides()`` to clear all
overrides. Supported only on remote Enterprise tables.
Examples
--------
>>> await table.update_table_overrides( # doctest: +SKIP
... job_types={
... "compaction": {"enabled": True},
... "cleanup": {"enabled": False},
... }
... )
"""
encoded = json.dumps(overrides)
return json.loads(await self._inner.update_table_overrides(encoded))
async def reset_table_overrides(self) -> dict[str, Any]:
"""Clear Enterprise table overrides for this table."""
return json.loads(await self._inner.reset_table_overrides())
# WAL-PK-FUSION: delete both hooks.
def _hybrid_pk_fusion_learned(self) -> bool:
"""See [`Table._hybrid_pk_fusion_learned`][lancedb.table.Table]."""
return self._inner.hybrid_pk_fusion_learned()
def _note_hybrid_pk_fusion(self) -> None:
"""See [`Table._note_hybrid_pk_fusion`][lancedb.table.Table]."""
self._inner.note_hybrid_pk_fusion()
async def checkpoint_rte(self) -> None:
"""Converge this table's RTE write path into its base table.
One flush, freezing every memtable into an SSTable, then compaction triggers
until every SSTable that existed at that moment has reached base.
The loop runs client-side, reading progress from ``get_rte_stats``.
Best-effort: SSTables created *while* it runs are deliberately not
waited on, which is what lets it terminate on a table taking writes.
Idempotent and safe on a cadence.
There is no deadline, and the caller owns that. It returns when the
target SSTables are gone, raises on a terminal server fault, and
otherwise waits however long the server takes. A slow table and a
stuck one are the same picture from the client: the compactor pool is
shared across every table on the node, so a checkpoint queued behind
unrelated work looks exactly like one that is merging. Wrap this in
``asyncio.wait_for`` for a wall-clock bound; abandoning it partway
costs nothing.
"""
await self._inner.checkpoint_rte()
async def flush_rte(self) -> None:
"""Freeze every table shard's active memtable into an SSTable.
Does not touch the base table — compacting SSTables into base is
`compact_rte`. On a node that has not claimed this table, this claims
it and replays its WAL log first.
"""
await self._inner.flush_rte()
async def compact_rte(self) -> None:
"""Trigger a background SSTable compaction pass per table shard.
Returns once the passes are dispatched, not once they finish: watch
``get_rte_stats`` for progress, or use ``checkpoint_rte`` to loop
until the current SSTables have reached base.
"""
await self._inner.compact_rte()
async def get_rte_stats(
self, *, include_sstable_rows: bool = False
) -> Optional[dict]:
"""Read live per-table-shard RTE state.
Answers "how far behind is my fresh tier", "which table shard is hot", and
"why is my fresh-tier vector search brute-force". Mutates no table
state, though on a node that has not claimed this table it claims it,
exactly as a read would.
Returns ``None`` only when the RTE write path is not enabled.
Parameters
----------
include_sstable_rows
Report a row count per SSTable. Off by default: each count
opens an uncached Lance dataset, and ``checkpoint_rte`` polls this
needing only generation numbers.
"""
return await self._inner.get_rte_stats(include_sstable_rows)
async def close_rte_writers(self) -> None:
"""Drain and close any cached RTE shard writers for this table.
When an RTE write spec is installed, `merge_insert` opens RTE shard
writers and caches them for reuse across calls. This closes them,
flushing pending data; writers reopen lazily on the next
`merge_insert`. It is a no-op when no writers are cached.
"""
await self._inner.close_rte_writers()
@property
def name(self) -> str:
"""The name of the table."""
return self._inner.name()
async def schema(self) -> pa.Schema:
"""The [Arrow Schema](https://arrow.apache.org/docs/python/api/datatypes.html#)
of this Table. Blob v2 fields retain their Arrow extension type.
"""
return _restore_blob_extensions(await self._inner.schema())
def _schema_sync(self) -> pa.Schema:
return _restore_blob_extensions(self._inner.schema_sync())
async def embedding_functions(self) -> Dict[str, EmbeddingFunctionConfig]:
"""
Get the embedding functions for the table
Returns
-------
funcs: Dict[str, EmbeddingFunctionConfig]
A mapping of the vector column to the embedding function
or empty dict if not configured.
"""
schema = await self.schema()
return EmbeddingFunctionRegistry.get_instance().parse_functions(schema.metadata)
async def count_rows(self, filter: Optional[str] = None) -> int:
"""
Count the number of rows in the table.
Parameters
----------
filter: str, optional
A SQL where clause to filter the rows to count.
"""
return await self._inner.count_rows(filter)
async def head(self, n=5) -> pa.Table:
"""
Return the first `n` rows of the table.
Parameters
----------
n: int, default 5
The number of rows to return.
"""
return await self.query().limit(n).to_arrow()
def query(self) -> AsyncQuery:
"""
Returns an [AsyncQuery][lancedb.query.AsyncQuery] that can be used
to search the table.
Use methods on the returned query to control query behavior. The query
can be executed with methods like [to_arrow][lancedb.query.AsyncQuery.to_arrow],
[to_pandas][lancedb.query.AsyncQuery.to_pandas] and more.
"""
return AsyncQuery(self._inner.query(), self)
def __datafusion_table_provider__(self, session: object) -> object:
return self._inner.__datafusion_table_provider__(session)
async def to_lance(self, **kwargs) -> lance.LanceDataset:
"""Return the Lance dataset backing a LanceDB Embedded table.
LanceDB Enterprise tables cannot be opened as Lance datasets by the
client. Use
[to_arrow][lancedb.table.AsyncTable.to_arrow] or
[query][lancedb.table.AsyncTable.query] to read them through the server.
Parameters
----------
**kwargs
Forwarded to `lance.dataset`.
Returns
-------
lance.LanceDataset
The Lance dataset at this table handle's version and branch.
Examples
--------
>>> async def get_lance_dataset(table):
... return await table.to_lance()
"""
if not self._inner._is_native():
raise NotImplementedError(
"to_lance() is not supported in LanceDB Enterprise; "
"query the server instead"
)
try:
import lance
except ImportError:
raise ImportError(
"The lance library is required to use this function. "
"Please install with `pip install pylance`."
)
# lance.dataset() can't open a branch directly, so open the base table
# and check out the branch ref (a None branch resolves to main).
branch = self.current_branch()
table_version = await self.version()
version = None if branch is not None else table_version
ds = lance.dataset(
await self.uri(),
version=version,
storage_options=await self.latest_storage_options(),
**kwargs,
)
if branch is not None:
ds = ds.checkout_version((branch, table_version))
return ds
async def to_pandas(self, blob_mode: BlobMode = "lazy", **kwargs) -> "pd.DataFrame":
"""Return the table as a pandas DataFrame.
Parameters
----------
blob_mode: str, default "lazy"
Controls how Lance blob columns are returned. LanceDB Enterprise supports
"descriptions"; "bytes" and "lazy" are not yet supported.
**kwargs
Forwarded to PyArrow / Lance pandas conversion.
Returns
-------
pd.DataFrame
"""
validate_blob_mode(blob_mode)
schema = await self.schema()
if blob_mode == "descriptions" or not schema_has_blob_field(schema):
arrow_tbl = await self.to_arrow()
if blob_mode == "descriptions":
arrow_tbl = strip_auto_row_ids(
arrow_tbl, row_addressable_blob_v2_paths(schema)
)
return arrow_tbl.to_pandas(**kwargs)
if not self._inner._is_native():
return await self.query().to_pandas(blob_mode=blob_mode, **kwargs)
if blob_mode == "lazy" and get_uri_scheme(await self.uri()) == "memory":
return (await self.to_arrow()).to_pandas(**kwargs)
if blob_mode == "bytes" and schema_has_blob_field(schema):
return await self.query().to_pandas(blob_mode=blob_mode, **kwargs)
return (await self.to_lance()).to_pandas(blob_mode=blob_mode, **kwargs)
async def to_arrow(self, *, blob_mode: BlobMode = "lazy") -> pa.Table:
"""Return the table as a pyarrow Table.
Parameters
----------
blob_mode: str, default "lazy"
``"lazy"`` returns blob v2 descriptors that carry the row id
``fetch_blobs`` reads. ``"descriptions"`` returns them without it.
``"bytes"`` returns payload bytes for supported row-addressable blob
columns and raises for blobs inside lists.
Returns
-------
pa.Table
"""
validate_blob_mode(blob_mode)
schema = await self.schema()
fetches_bytes = blob_mode == "bytes" and schema_has_blob_field(schema)
if (
not fetches_bytes
and _should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
):
tbl = (await self._execute_query(Query())).read_all()
if blob_mode == "descriptions":
tbl = strip_auto_row_ids(tbl, row_addressable_blob_v2_paths(schema))
return tbl
# The query builder refuses bytes under pushdown before running the query.
return await self.query().to_arrow(blob_mode=blob_mode)
async def create_index(
self,
column: str,
*,
replace: Optional[bool] = None,
config: Optional[
Union[
IvfFlat,
IvfPq,
IvfRq,
HnswPq,
HnswSq,
HnswFlat,
BTree,
Bitmap,
LabelList,
Fm,
NGram,
ZoneMap,
RTree,
ScalarPlugin,
FTS,
]
] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
):
"""Create an index to speed up queries
Indices can be created on vector columns or scalar columns.
Indices on vector columns will speed up vector searches.
Indices on scalar columns will speed up filtering (in both
vector and non-vector searches)
Parameters
----------
column: str
The column to index.
replace: bool, default True
Whether to replace the existing index
If this is false, and another index already exists on the same columns
and the same name, then an error will be returned. This is true even if
that index is out of date.
The default is True
config: default None
For advanced configuration you can specify the type of index you would
like to create. You can also specify index-specific parameters when
creating an index object.
wait_timeout: timedelta, optional
LanceDB Enterprise only: how long to wait for the index build to finish.
If not set, the call returns once the build is scheduled.
name: str, optional
The name of the index. If not provided, a default name will be generated.
exist_ok: bool, default False
Preserve an existing index with the requested or derived name. This
takes precedence over replace and keeps its definition and build.
train: bool, default True
Whether to train the index with existing data. Vector indices always train
with existing data.
Notes
-----
On LanceDB Embedded tables the index is built before this method
returns. On LanceDB Enterprise tables it returns once the build is
scheduled. Until the build finishes, a vector index may be missing
from [list_indices][lancedb.table.AsyncTable.list_indices], a scalar or
FTS index may be listed with 0 indexed rows, and queries won't use the
index. To block until the index is ready, pass ``wait_timeout``, call
[wait_for_index][lancedb.table.AsyncTable.wait_for_index], or use
[create_index_async][lancedb.table.AsyncTable.create_index_async] and
wait on its job when the server returned a job ID.
``wait_timeout`` waits for the submitted build when the server returns
a job ID. On older servers that don't return one, it falls back to
polling the index by name. ``wait_for_index`` always waits by name.
When replacing an existing index, those name-based waits may return
before the new build finishes because the old index can still match.
``create_index_async`` can track a specific build only when the server
returned a job ID; otherwise it returns an already-complete job and
callers must use ``wait_for_index`` for that build.
"""
if config is not None:
if not isinstance(
config,
(
IvfFlat,
IvfSq,
IvfPq,
IvfRq,
HnswPq,
HnswSq,
HnswFlat,
BTree,
Bitmap,
LabelList,
Fm,
NGram,
ZoneMap,
RTree,
ScalarPlugin,
FTS,
),
):
raise TypeError(
"config must be an instance of IvfSq, IvfPq, IvfRq, HnswPq, HnswSq,"
" BTree, Bitmap, LabelList, Fm, NGram, ZoneMap, RTree,"
" ScalarPlugin, or FTS, but got " + str(type(config))
)
try:
await self._inner.create_index(
column,
index=config,
replace=replace,
exist_ok=exist_ok,
wait_timeout=wait_timeout,
name=name,
train=train,
)
except (ValueError, RuntimeError) as e:
if isinstance(config, FTS):
_maybe_add_fts_error_note(
e,
base_tokenizer=config.base_tokenizer,
language=config.language,
)
raise e
async def create_index_async(
self,
column: str,
*,
replace: Optional[bool] = None,
config: Optional[
Union[
IvfFlat,
IvfPq,
IvfRq,
HnswPq,
HnswSq,
HnswFlat,
BTree,
Bitmap,
LabelList,
Fm,
NGram,
ZoneMap,
RTree,
ScalarPlugin,
FTS,
]
] = None,
wait_timeout: Optional[timedelta] = None,
name: Optional[str] = None,
exist_ok: bool = False,
train: bool = True,
) -> AsyncJob:
"""Create an index, returning a handle to the indexing job.
Takes the same arguments as :meth:`create_index`. The job may already
be complete when returned; callers must not assume the index exists
until :meth:`AsyncJob.wait` resolves.
In LanceDB Enterprise, the returned job tracks this submission only if
the server returns a job ID. If it does not, the job is already
complete and does not wait for the background build; use
``wait_for_index`` and account for the replacement caveat described in
[create_index][lancedb.table.AsyncTable.create_index].
"""
job = await self._inner.create_index_async(
column,
index=config,
replace=replace,
exist_ok=exist_ok,
wait_timeout=wait_timeout,
name=name,
train=train,
)
return AsyncJob(job)
async def drop_index(self, name: str) -> None:
"""
Drop an index from the table.
Parameters
----------
name: str
The name of the index to drop.
Notes
-----
This does not delete the index from disk, it just removes it from the table.
To delete the index, run [optimize][lancedb.table.AsyncTable.optimize]
after dropping the index.
Use [list_indices][lancedb.table.AsyncTable.list_indices] to find the names
of the indices.
"""
await self._inner.drop_index(name)
async def prewarm_index(self, name: str) -> None:
"""
Prewarm an index in the table.
This is a hint to the database that the index will be accessed in the
future and should be loaded into memory if possible. This can reduce
cold-start latency for subsequent queries.
This call initiates prewarming and returns once the request is accepted.
It is idempotent and safe to call from multiple clients concurrently.
It is generally wasteful to call this if the index does not fit into the
available cache. Not all index types support prewarming; unsupported
indices will silently ignore the request.
Parameters
----------
name: str
The name of the index to prewarm
"""
await self._inner.prewarm_index(name)
async def prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> None:
"""
Prewarm data for the table.
This is a hint to the database that the given columns will be accessed
in the future and the database should prefetch the data if possible.
Currently only supported in LanceDB Enterprise.
This call initiates prewarming and returns once the request is accepted.
Uses SQL PREWARM and requires Flight SQL connectivity. To track the
durable warming job, use ``prewarm_data_async()``.
This operation has a large upfront cost but can speed up future queries
that need to fetch the given columns. Large columns such as embeddings
or binary data may not be practical to prewarm. This feature is intended
for workloads that issue many queries against the same columns.
Parameters
----------
columns: list of str, optional
The columns to prewarm. If None, all columns are prewarmed.
filter: str, optional
A single SQL predicate for conservative metadata pruning.
parameters: sequence, mapping, or one-row Arrow batch/table, optional
Scalar values for ``$1``/``$name`` placeholders in ``filter``.
Values are sent separately from SQL; requires server support.
concurrency: int, optional
Positive per-executor warming concurrency; omitted uses the server default.
Examples
--------
>>> await table.prewarm_data( # doctest: +SKIP
... ["payload"], filter="quality > 0.7", concurrency=4)
"""
await self.prewarm_data_async(
columns, filter=filter, concurrency=concurrency, parameters=parameters
)
async def explain_prewarm_data(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> PrewarmDataPlan:
"""Preview prewarm without submitting a warming job.
See [Table.explain_prewarm_data][lancedb.table.Table.explain_prewarm_data]
for options, permissions, snapshot semantics, and limitations.
"""
return PrewarmDataPlan.from_json(
await self._inner.explain_prewarm_data(
columns,
filter=filter,
concurrency=concurrency,
parameters=to_parameter_batch(parameters),
)
)
async def prewarm_data_async(
self,
columns: Optional[List[str]] = None,
*,
filter: Optional[str] = None,
concurrency: Optional[int] = None,
parameters: Optional[QueryParameters] = None,
) -> AsyncJob[PrewarmDataResult]:
"""Submit data prewarm and return a durable handle after acceptance.
See [Table.prewarm_data_async][lancedb.table.Table.prewarm_data_async]
for options, permissions, and compatibility. Awaiting this method does
not wait for warming; use ``await job.wait()`` for completion.
Examples
--------
>>> job = await table.prewarm_data_async( # doctest: +SKIP
... ["video"], filter="quality > 0.7", concurrency=4)
>>> await job.wait() # doctest: +SKIP
"""
return _typed_job(
await self._inner.prewarm_data_async(
columns,
filter=filter,
concurrency=concurrency,
parameters=to_parameter_batch(parameters),
),
PrewarmDataResult.from_json,
)
async def wait_for_index(
self, index_names: Iterable[str], timeout: timedelta = timedelta(seconds=300)
) -> None:
"""
Wait for indexing to complete for the given index names.
This will poll the table until all the indices are fully indexed,
or raise a timeout exception if the timeout is reached.
Parameters
----------
index_names: str
The name of the indices to poll
timeout: timedelta
Timeout to wait for asynchronous indexing. The default is 5 minutes.
"""
await self._inner.wait_for_index(index_names, timeout)
async def stats(self) -> TableStatistics:
"""
Retrieve table and fragment statistics.
"""
return await self._inner.stats()
async def uri(self) -> str:
"""
Get the table URI (storage location).
In LanceDB Enterprise, this fetches the location from the server via describe.
In LanceDB Embedded, this returns the dataset URI.
Returns
-------
str
The full storage location of the table (e.g., S3/GCS path).
"""
return await self._inner.uri()
async def initial_storage_options(self) -> Optional[Dict[str, str]]:
"""Get the initial storage options that were passed in when opening this table.
For dynamically refreshed options (e.g., credential vending), use
:meth:`latest_storage_options`.
Warning: This is an internal API and the return value is subject to change.
Returns
-------
Optional[Dict[str, str]]
The storage options, or None if no storage options were configured.
"""
return await self._inner.initial_storage_options()
async def latest_storage_options(self) -> Optional[Dict[str, str]]:
"""Get the latest storage options, refreshing from provider if configured.
This method is useful for credential vending scenarios where storage options
may be refreshed dynamically. If no dynamic provider is configured, this
returns the initial static options.
Warning: This is an internal API and the return value is subject to change.
Returns
-------
Optional[Dict[str, str]]
The storage options, or None if no storage options were configured.
"""
return await self._inner.latest_storage_options()
async def add(
self,
data: DATA,
*,
mode: Optional[Literal["append", "overwrite"]] = "append",
on_bad_vectors: Optional[OnBadVectorsType] = None,
fill_value: Optional[float] = None,
progress: Optional[Union[bool, Callable, Any]] = None,
write_parallelism: Optional[int] = None,
allow_external_blob_outside_bases: bool = False,
retries: Optional[int] = None,
) -> AddResult:
"""Add more data to the [AsyncTable][lancedb.table.AsyncTable].
Plain binary values are cast into blob columns using the table's schema.
To construct Arrow record batches explicitly, use
[blob_array][lancedb.schema.blob_array].
Parameters
----------
data: DATA
The data to insert into the table. Acceptable types are:
- list-of-dict
- pandas.DataFrame
- pyarrow.Table or pyarrow.RecordBatch
mode: str
The mode to use when writing the data. Valid values are
"append" and "overwrite". Overwrite adopts explicitly declared blob
layouts, while inferred bytes retain the existing blob v2 fields.
on_bad_vectors: str, default "error"
How to handle vectors with incorrect sizes, NaNs, or null elements.
One of "error", "drop", "fill", "null".
fill_value: float, default 0.
The value to use when filling vectors. Only used if on_bad_vectors="fill".
progress: callable or tqdm-like, optional
A callback or tqdm-compatible progress bar. See
:meth:`Table.add` for details.
write_parallelism: int, optional
Number of partitions to write in parallel. Higher values increase
throughput but also peak memory use, since each partition buffers
data in flight. Defaults to an estimate based on the data size,
capped at the number of CPU cores. Lower this if bulk ingestion is
using too much memory.
allow_external_blob_outside_bases: bool, default False
Allow blob URIs outside registered bases. See :meth:`Table.add`.
LanceDB Embedded only.
retries: int, optional
Override the remote retry budget for this call (0 to 255). Zero
disables retries, including multipart parts. Ambiguous commits are
never replayed. Ignored by LanceDB Embedded.
"""
schema = await self.schema()
data = _serialize_json_values(data, schema)
blob_table = _blob_input_to_arrow(data, schema)
if blob_table is not None:
data = blob_table
if on_bad_vectors is None:
on_bad_vectors = "error"
if fill_value is None:
fill_value = 0.0
# _sanitize_data is an old code path, but we will use it until the
# new code path is ready.
if mode == "overwrite":
# For overwrite, apply the same preprocessing as create_table
# so vector columns are inferred as FixedSizeList.
data, _ = await asyncio.to_thread(
sanitize_create_table,
data,
None,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
)
if blob_v2_column_paths(schema):
# Plain bytes cannot be inferred as blob v2. Keep matching blob
# fields while allowing other columns to use the inferred schema.
target_schema = _preserve_blob_v2_fields(data.schema, schema)
data = _cast_to_target_schema(data, target_schema)
elif on_bad_vectors != "error" or (
schema.metadata is not None and b"embedding_functions" in schema.metadata
):
data = await asyncio.to_thread(
_sanitize_data,
data,
schema,
metadata=schema.metadata,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
allow_subschema=True,
)
_register_optional_converters()
# Converting an iterator peeks at its first item. A synchronous query in
# that iterator schedules work on LOOP, so peeking on LOOP's own thread
# would deadlock waiting for itself.
data = await asyncio.to_thread(to_scannable, data)
progress, owns = _normalize_progress(progress)
try:
return await self._inner.add(
data,
mode or "append",
progress=progress,
write_parallelism=write_parallelism,
allow_external_blob_outside_bases=allow_external_blob_outside_bases,
retries=retries,
)
except RuntimeError as e:
if "Cast error" in str(e):
raise ValueError(e)
elif "Vector column contains NaN" in str(e):
raise ValueError(e)
else:
raise
finally:
if owns:
progress.close()
def merge_insert(self, on: Union[str, Iterable[str]]) -> LanceMergeInsertBuilder:
"""
Returns a [`LanceMergeInsertBuilder`][lancedb.merge.LanceMergeInsertBuilder]
that can be used to create a "merge insert" operation
This operation can add rows, update rows, and remove rows all in a single
transaction. It is a very generic tool that can be used to create
behaviors like "insert if not exists", "update or insert (i.e. upsert)",
or even replace a portion of existing data with new data (e.g. replace
all data where month="january")
The merge insert operation works by combining new data from a
**source table** with existing data in a **target table** by using a
join. There are three categories of records.
"Matched" records are records that exist in both the source table and
the target table. "Not matched" records exist only in the source table
(e.g. these are new data) "Not matched by source" records exist only
in the target table (this is old data)
The builder returned by this method can be used to customize what
should happen for each category of data.
Please note that the data may appear to be reordered as part of this
operation. This is because updated rows will be deleted from the
dataset and then reinserted at the end with the new values.
Parameters
----------
on: Union[str, Iterable[str]]
A column (or columns) to join on. This is how records from the
source table and target table are matched. Typically this is some
kind of key or id column. Passing several columns matches on the
composite key: a source row updates a target row only when it
agrees on every one of them.
Examples
--------
>>> import lancedb
>>> data = pa.table({"a": [2, 1, 3], "b": ["a", "b", "c"]})
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> new_data = pa.table({"a": [2, 3, 4], "b": ["x", "y", "z"]})
>>> # Perform a "upsert" operation
>>> res = table.merge_insert("a") \\
... .when_matched_update_all() \\
... .when_not_matched_insert_all() \\
... .execute(new_data)
>>> res
MergeResult(version=2, num_updated_rows=2, num_inserted_rows=1, num_deleted_rows=0, num_attempts=1, num_rows=3)
>>> # The order of new rows is non-deterministic since we use
>>> # a hash-join as part of this operation and so we sort here
>>> table.to_arrow().sort_by("a").to_pandas()
a b
0 1 b
1 2 x
2 3 y
3 4 z
""" # noqa: E501
on = [on] if isinstance(on, str) else list(iter(on))
return LanceMergeInsertBuilder(self, on)
@overload
async def search(
self,
query: None = None,
vector_column_name: Optional[str] = None,
query_type: QueryType = "auto",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> AsyncQuery: ...
@overload
async def search(
self,
query: str,
vector_column_name: Optional[str] = None,
query_type: Literal["auto"] = ...,
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> Union[AsyncHybridQuery, AsyncFTSQuery, AsyncVectorQuery]: ...
@overload
async def search(
self,
query: str,
vector_column_name: Optional[str] = None,
query_type: Literal["hybrid"] = ...,
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> AsyncHybridQuery: ...
@overload
async def search(
self,
query: Union[VEC, "PIL.Image.Image", Tuple],
vector_column_name: Optional[str] = None,
query_type: Literal["auto"] = ...,
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> AsyncVectorQuery: ...
@overload
async def search(
self,
query: str,
vector_column_name: Optional[str] = None,
query_type: Literal["fts"] = ...,
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> AsyncFTSQuery: ...
@overload
async def search(
self,
query: Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery],
vector_column_name: Optional[str] = None,
query_type: Literal["vector"] = ...,
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> AsyncVectorQuery: ...
async def search(
self,
query: Optional[
Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery]
] = None,
vector_column_name: Optional[str] = None,
query_type: QueryType = "auto",
ordering_field_name: Optional[str] = None,
fts_columns: Optional[Union[str, List[str]]] = None,
*,
operator: Optional[Union[FullTextOperator, Literal["AND", "OR"]]] = None,
) -> Union[AsyncQuery, AsyncHybridQuery, AsyncFTSQuery, AsyncVectorQuery]:
"""Create a search query to find the nearest neighbors
of the given query vector. We currently support [vector search](https://lancedb.com/docs/search/vector-search/)
and [full-text search](https://lancedb.com/docs/search/full-text-search/).
All query options are defined in [AsyncQuery][lancedb.query.AsyncQuery].
Parameters
----------
query: list/np.ndarray/str/PIL.Image.Image, default None
The targeted vector to search for.
- *default None*.
Acceptable types are: list, np.ndarray, PIL.Image.Image
- If None then a plain [AsyncQuery][lancedb.query.AsyncQuery] is
returned, equivalent to calling [query][lancedb.table.AsyncTable.query].
The select/where/limit clauses are applied to filter the table.
vector_column_name: str, optional
The name of the vector column to search.
The vector column needs to be a pyarrow fixed size list type
- If not specified then the vector column is inferred from
the table schema
- If the table has multiple vector columns then the *vector_column_name*
needs to be specified. Otherwise, an error is raised.
query_type: str
*default "auto"*.
Acceptable types are: "vector", "fts", "hybrid", or "auto"
- If "auto" then the query type is inferred from the query;
- If `query` is a list/np.ndarray then the query type is
"vector";
- If `query` is a PIL.Image.Image then either do vector search,
or raise an error if no corresponding embedding function is found.
- If `query` is a string, then the query type is "vector" if the
table has embedding functions else the query type is "fts"
operator: "AND" or "OR", optional
Combine terms in a string full-text query; the default is OR.
Use ``query_type="fts"`` when the table also has embeddings.
Returns
-------
AsyncQuery, AsyncHybridQuery, AsyncFTSQuery, or AsyncVectorQuery
A query builder object representing the query.
"""
if operator is not None:
operator = FullTextOperator(operator)
if not isinstance(query, str) or query_type not in ("auto", "fts"):
raise ValueError("operator requires a string full-text query")
if query is None:
return self.query()
def is_embedding(query):
return isinstance(query, (list, np.ndarray, pa.Array, pa.ChunkedArray))
async def get_embedding_func(
vector_column_name: Optional[str],
query_type: QueryType,
query: Optional[Union[VEC, str, "PIL.Image.Image", Tuple, FullTextQuery]],
) -> Tuple[str, EmbeddingFunctionConfig]:
if isinstance(query, FullTextQuery):
query_type = "fts"
schema = await self.schema()
vector_column_name = infer_vector_column_name(
schema=schema,
query_type=query_type,
query=query,
vector_column_name=vector_column_name,
)
funcs = EmbeddingFunctionRegistry.get_instance().parse_functions(
schema.metadata
)
func = funcs.get(vector_column_name)
if func is None:
error = ValueError(
f"Column '{vector_column_name}' has no registered "
"embedding function."
)
if len(funcs) > 0:
add_note(
error,
"Embedding functions are registered for columns: "
f"{list(funcs.keys())}",
)
else:
add_note(
error, "No embedding functions are registered for any columns."
)
raise error
return vector_column_name, func
async def make_embedding(embedding, query):
if embedding is not None:
loop = asyncio.get_running_loop()
# This function is likely to block, since it either calls an expensive
# function or makes an HTTP request to an embeddings REST API. Run it
# on a dedicated executor so it can't starve the default executor that
# other blocking I/O shares. See
# https://github.com/lancedb/lancedb/issues/3310.
return (
await loop.run_in_executor(
embedding_executor(),
embedding.function.compute_query_embeddings_with_retry,
query,
)
)[0]
else:
return None
if query_type == "auto":
# Infer the query type.
if is_embedding(query):
vector_query = query
query_type = "vector"
elif isinstance(query, FullTextQuery):
query_type = "fts"
elif isinstance(query, str):
try:
(
indices,
(vector_column_name, embedding_conf),
) = await asyncio.gather(
self.list_indices(),
get_embedding_func(vector_column_name, "auto", query),
)
except ValueError as e:
if "Column" in str(
e
) and "has no registered embedding function" in str(e):
# If the column has no registered embedding function,
# then it's an FTS query.
query_type = "fts"
else:
raise e
else:
if embedding_conf is not None:
vector_query = await make_embedding(embedding_conf, query)
if any(
i.columns[0] == embedding_conf.source_column
and i.index_type == "FTS"
for i in indices
):
query_type = "hybrid"
else:
query_type = "vector"
else:
query_type = "fts"
else:
# it's an image or something else embeddable.
query_type = "vector"
elif query_type == "vector":
if is_embedding(query):
vector_query = query
else:
vector_column_name, embedding_conf = await get_embedding_func(
vector_column_name, query_type, query
)
vector_query = await make_embedding(embedding_conf, query)
elif query_type == "hybrid":
if is_embedding(query):
raise ValueError("Hybrid search requires a text query")
else:
vector_column_name, embedding_conf = await get_embedding_func(
vector_column_name, query_type, query
)
vector_query = await make_embedding(embedding_conf, query)
if operator is not None and query_type != "fts":
raise ValueError("operator requires a string full-text query")
if query_type == "vector":
builder = self.query().nearest_to(vector_query)
if vector_column_name:
builder = builder.column(vector_column_name)
return builder
elif query_type == "fts":
return self.query().nearest_to_text(
query, columns=fts_columns, operator=operator
)
elif query_type == "hybrid":
builder = self.query().nearest_to(vector_query)
if vector_column_name:
builder = builder.column(vector_column_name)
return builder.nearest_to_text(query, columns=fts_columns)
else:
raise ValueError(f"Unknown query type: '{query_type}'")
def vector_search(
self,
query_vector: Union[VEC, Tuple],
) -> AsyncVectorQuery:
"""
Search the table with a given query vector.
This is a convenience method for preparing a vector query and
is the same thing as calling `nearestTo` on the builder returned
by `query`. Seer [nearest_to][lancedb.query.AsyncQuery.nearest_to] for more
details.
"""
return self.query().nearest_to(query_vector)
def _sync_query_to_async(
self, query: Query
) -> (
AsyncHybridQuery
| AsyncFTSQuery
| AsyncVectorQuery
| AsyncQuery
| AsyncTakeQuery
):
if query.take_offsets is not None or query.take_row_ids is not None:
if query.take_offsets is not None and query.take_row_ids is not None:
raise ValueError("Only one of take_offsets and take_row_ids may be set")
if query.preserve_order is True and query.take_offsets is None:
raise ValueError("preserve_order only supports take_offsets")
if query.preserve_order is True and query.order_by:
raise ValueError(
"preserve_order cannot be combined with an explicit order_by"
)
take_query = (
self.take_row_ids(query.take_row_ids)
if query.take_row_ids is not None
else self.take_offsets(
query.take_offsets, preserve_order=query.preserve_order is True
)
)
if query.columns is not None:
columns = query.columns
# Native requests serialize dynamic projections as (name, SQL) pairs.
if (
isinstance(columns, list)
and columns
and isinstance(columns[0], tuple)
):
columns = dict(columns)
take_query = take_query.select(columns)
if query.use_rte is not None:
take_query = take_query.use_rte(query.use_rte)
if query.with_row_id:
take_query = take_query.with_row_id()
return take_query
async_query = self.query()
if query.limit is not None:
async_query = async_query.limit(query.limit)
if query.offset is not None:
async_query = async_query.offset(query.offset)
if query.columns:
columns = query.columns
if isinstance(columns, list) and columns and isinstance(columns[0], tuple):
columns = dict(columns)
async_query = async_query.select(columns)
if query.filter is not None:
async_query = async_query.where(query.filter)
if query.fast_search:
async_query = async_query.fast_search()
if query.use_rte is not None:
async_query = async_query.use_rte(query.use_rte)
if query.with_row_id:
async_query = async_query.with_row_id()
if query.order_by:
async_query = async_query.order_by(query.order_by)
if query.scan_in_order is not None:
async_query = async_query.scan_in_order(query.scan_in_order)
if query.vector:
async_query = async_query.nearest_to(query.vector).distance_range(
query.lower_bound, query.upper_bound
)
if query.distance_type is not None:
async_query = async_query.distance_type(query.distance_type)
if query.nprobes is not None:
async_query = async_query.nprobes(query.nprobes)
if query.search_effort is not None:
async_query = async_query.search_effort(query.search_effort)
if query.minimum_nprobes is not None and query.maximum_nprobes is not None:
async_query = async_query.minimum_nprobes(
query.minimum_nprobes
).maximum_nprobes(query.maximum_nprobes)
elif query.minimum_nprobes is not None:
async_query = async_query.minimum_nprobes(query.minimum_nprobes)
elif query.maximum_nprobes is not None:
async_query = async_query.maximum_nprobes(query.maximum_nprobes)
if query.refine_factor is not None:
async_query = async_query.refine_factor(query.refine_factor)
if query.vector_column:
async_query = async_query.column(query.vector_column)
if query.ef:
async_query = async_query.ef(query.ef)
if query.approx_mode:
async_query = async_query.approx_mode(query.approx_mode)
if query.bypass_vector_index:
async_query = async_query.bypass_vector_index()
if query.postfilter:
async_query = async_query.postfilter()
if query.full_text_query:
async_query = async_query.nearest_to_text(
query.full_text_query.query,
query.full_text_query.columns,
operator=query.full_text_query.operator,
)
return async_query
async def _execute_query(
self,
query: Query,
*,
batch_size: Optional[int] = None,
timeout: Optional[timedelta] = None,
) -> pa.RecordBatchReader:
if (
_should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
and not _requires_local_namespace_execution(query)
):
from lancedb.namespace import _execute_server_side_query
table_id = self._namespace_path + [self.name]
return _execute_server_side_query(self._namespace_client, table_id, query)
# The sync table calls into this method, so we need to map the
# query to the async version of the query and run that here. This is only
# used for that code path right now.
async_query = self._sync_query_to_async(query)
async_query._blob_row_id_prepared = query._blob_row_id_prepared
return await async_query.to_batches(
max_batch_length=batch_size, timeout=timeout
)
async def _execute_arrow(
self,
query: Query,
*,
timeout: Optional[timedelta] = None,
) -> pa.Table:
if (
_should_push_down_query_table(
self._namespace_client, self._pushdown_operations
)
and not self._route_pushdown_to_rust
and not _requires_local_namespace_execution(query)
):
return (await self._execute_query(query, timeout=timeout)).read_all()
async_query = self._sync_query_to_async(query)
async_query._blob_row_id_prepared = query._blob_row_id_prepared
return await async_query._execute_eager(timeout=timeout)
async def _explain_plan(self, query: Query, verbose: Optional[bool]) -> str:
# This method is used by the sync table
async_query = self._sync_query_to_async(query)
return await async_query.explain_plan(verbose)
async def _analyze_plan(
self,
query: Query,
*,
distributed_metrics: AnalyzePlanDistributedMetrics = "aggregate",
) -> str:
# This method is used by the sync table
async_query = self._sync_query_to_async(query)
return await async_query.analyze_plan(distributed_metrics)
async def _output_schema(self, query: Query) -> pa.Schema:
async_query = self._sync_query_to_async(query)
return await async_query.output_schema()
async def _do_merge(
self,
merge: LanceMergeInsertBuilder,
new_data: DATA,
on_bad_vectors: OnBadVectorsType,
fill_value: float,
retries: Optional[int] = None,
) -> MergeResult:
schema = await self.schema()
if on_bad_vectors is None:
on_bad_vectors = "error"
if fill_value is None:
fill_value = 0.0
data = _sanitize_data(
new_data,
schema,
metadata=schema.metadata,
on_bad_vectors=on_bad_vectors,
fill_value=fill_value,
allow_subschema=True,
)
if isinstance(data, pa.Table):
data = pa.RecordBatchReader.from_batches(data.schema, data.to_batches())
data = _drop_nullable_primary_key(data)
return await self._inner.execute_merge_insert(
data,
dict(
on=merge._on,
when_matched_update_all=merge._when_matched_update_all,
when_matched_update_all_condition=merge._when_matched_update_all_condition,
when_not_matched_insert_all=merge._when_not_matched_insert_all,
when_not_matched_by_source_delete=merge._when_not_matched_by_source_delete,
when_not_matched_by_source_condition=merge._when_not_matched_by_source_condition,
when_not_matched_by_source_condition_expr=merge._when_not_matched_by_source_condition_expr,
timeout=merge._timeout,
use_index=merge._use_index,
use_rte=merge._use_rte,
validate_single_shard=merge._validate_single_shard,
),
retries=retries,
)
async def delete(self, where: Union[str, Expr]) -> DeleteResult:
"""Delete rows from the table.
This can be used to delete a single row, many rows, all rows, or
sometimes no rows (if your predicate matches nothing).
Parameters
----------
where: str or :class:`~lancedb.expr.Expr`
The filter condition. Can be a SQL string or a type-safe
:class:`~lancedb.expr.Expr` built with :func:`~lancedb.expr.col`
and :func:`~lancedb.expr.lit`.
The filter must not be empty, or it will error.
Examples
--------
>>> import lancedb
>>> data = [
... {"x": 1, "vector": [1.0, 2]},
... {"x": 2, "vector": [3.0, 4]},
... {"x": 3, "vector": [5.0, 6]}
... ]
>>> db = lancedb.connect("./.lancedb")
>>> table = db.create_table("my_table", data)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 2 [3.0, 4.0]
2 3 [5.0, 6.0]
>>> table.delete("x = 2")
DeleteResult(num_deleted_rows=1, version=2)
>>> table.to_pandas()
x vector
0 1 [1.0, 2.0]
1 3 [5.0, 6.0]
If you have a list of values to delete, you can combine them into a
stringified list and use the `IN` operator:
>>> to_remove = [1, 5]
>>> to_remove = ", ".join([str(v) for v in to_remove])
>>> to_remove
'1, 5'
>>> table.delete(f"x IN ({to_remove})")
DeleteResult(num_deleted_rows=1, version=3)
>>> table.to_pandas()
x vector
0 3 [5.0, 6.0]
"""
predicate = where._inner if isinstance(where, Expr) else where
return await self._inner.delete(predicate)
async def update(
self,
updates: Optional[Dict[str, Any]] = None,
*,
where: Optional[Union[str, Expr]] = None,
updates_sql: Optional[Dict[str, str]] = None,
) -> UpdateResult:
"""
This can be used to update zero to all rows in the table.
If a filter is provided with `where` then only rows matching the
filter will be updated. Otherwise all rows will be updated.
Embeddings are not computed by this method. Updating an embedding source
column requires providing all associated vector columns in the same call;
otherwise a ValueError is raised. Use `merge_insert` to recompute embeddings
automatically.
Parameters
----------
updates: dict, optional
The updates to apply. The keys should be the name of the column to
update. The values should be the new values to assign. This is
required unless updates_sql is supplied.
where: str or [Expr][lancedb.expr.Expr], optional
The filter condition. Can be a SQL string or a type-safe
[Expr][lancedb.expr.Expr] built with [col][lancedb.expr.col] and
[lit][lancedb.expr.lit]. Only rows that satisfy this filter will
be updated.
updates_sql: dict, optional
The updates to apply, expressed as SQL expression strings. The keys should
be column names. The values should be SQL expressions. These can be SQL
literals (e.g. "7" or "'foo'") or they can be expressions based on the
previous value of the row (e.g. "x + 1" to increment the x column by 1)
Returns
-------
UpdateResult
An object containing:
- rows_updated: The number of rows that were updated
- version: The new version number of the table after the update
Examples
--------
>>> import asyncio
>>> import lancedb
>>> from lancedb.expr import col
>>> import pandas as pd
>>> async def demo_update():
... data = pd.DataFrame({"x": [1, 2], "vector": [[1, 2], [3, 4]]})
... db = await lancedb.connect_async("./.lancedb")
... table = await db.create_table("my_table", data)
... # x is [1, 2], vector is [[1, 2], [3, 4]]
... await table.update({"vector": [10, 10]}, where=col("x") == 2)
... # x is [1, 2], vector is [[1, 2], [10, 10]]
... await table.update(updates_sql={"x": "x + 1"})
... # x is [2, 3], vector is [[1, 2], [10, 10]]
>>> asyncio.run(demo_update())
"""
if updates is not None and updates_sql is not None:
raise ValueError("Only one of updates or updates_sql can be provided")
if updates is None and updates_sql is None:
raise ValueError("Either updates or updates_sql must be provided")
if updates is not None:
updates_sql = {k: value_to_sql(v) for k, v in updates.items()}
# Only the column mappings are needed here. Restoring embedding functions
# could load models even for updates that provide their own vectors.
schema = await self.schema()
column_pairs = _embedding_column_pairs(schema.metadata)
if column_pairs:
# Metadata names are literal top-level fields; targets may be quoted.
updated_columns = {tuple(_parse_field_path(name)) for name in updates_sql}
for source_column, vector_column in column_pairs:
source_updated = (source_column,) in updated_columns
vector_updated = (vector_column,) in updated_columns
if source_updated and not vector_updated:
raise ValueError(
f"Updating embedding source column {source_column!r} requires "
f"updating vector column {vector_column!r} in the same call. "
"Provide both columns or use merge_insert "
"to recompute embeddings."
)
predicate = where.to_sql() if isinstance(where, Expr) else where
return await self._inner.update(updates_sql, predicate)
async def add_columns(
self,
transforms: Mapping[str, str | FunctionApplication]
| FunctionApplication
| pa.Field
| List[pa.Field]
| pa.Schema
| DATA
| pa.RecordBatch
| pa.RecordBatchReader
| pl.DataFrame
| None = None,
*,
computed: dict[str, str] | None = None,
auto_refresh_enabled: bool | None = None,
auto_refresh_interval_seconds: int | None = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
total_gpu_count: Optional[int] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
) -> AddColumnsResult:
"""
Add new columns with defined values.
Parameters
----------
transforms: Mapping[str, str | FunctionApplication], FunctionApplication,
pa.Field, List[pa.Field], pa.Schema, pandas.DataFrame,
polars.DataFrame, pa.Table, pa.RecordBatch, pa.RecordBatchReader
A map of column name to a SQL expression to use to calculate the
value of the new column. These expressions will be evaluated for
each row in the table, and can reference existing columns.
Alternatively, you can pass a pyarrow field or schema to add
new columns with NULLs.
A mapping of column names to ``FunctionApplication`` values declares
all bindings in one schema commit. Each column holds its scalar
or entire named-struct result. A bare named-struct application expands
its ordered result fields as one atomic binding. Set field aliases
with ``rename(columns=...)``.
Function columns are supported only in LanceDB
Enterprise.
On local tables, a pandas or Polars DataFrame, PyArrow Table,
RecordBatch, or RecordBatchReader supplies precomputed new-column
values. Supply exactly one row per current live table row, in storage
scan order; pandas indexes are ignored. The input contains only new
columns. Readers are consumed once, and their batch boundaries need
not match table fragments. Keep the table unchanged between computing
these values and calling add_columns. Data inputs cannot be combined
with computed columns. Remote tables do not support data inputs.
computed: Dict[str, str], optional
LanceDB Embedded declaration and refresh require a source build with the
``experimental-functions`` Cargo feature, which published packages
do not enable. LanceDB Enterprise operations remain available.
A mapping from output column names to SQL expressions derives each
output field from its expression. A direct projection of a Blob v2
field inherits Blob v2 semantics; other expressions derive their
ordinary Arrow type. Mapping order is declaration and dependency
order.
Unlike ``transforms``, the expression is stored rather than
evaluated now: the column is committed with no values, and rows get
them from
[`refresh_column`][lancedb.table.AsyncTable.refresh_column].
A refresh also recomputes the rows whose inputs changed since they
were computed, so a mutated input is reflected by the next refresh.
While a declaration reads a column, that column cannot be renamed,
retyped or dropped.
In LanceDB Enterprise the expression is planned by
the server. Cannot be combined with ``transforms``.
auto_refresh_enabled: bool, optional
Enable automatic refresh for each computed output declared by this
call. Defaults to True. LanceDB Embedded stores the setting; automatic
scheduling requires LanceDB Enterprise.
auto_refresh_interval_seconds: int, optional
Positive minimum seconds after a successful refresh before another
automatic refresh is eligible. When omitted, the server determines when
to refresh. Unchanged inputs do
not force recomputation. Applies to every declared computed output.
These options cannot be used with one-time transforms or ordinary
all-null columns.
total_cpu_millis: int, optional
Default total CPU budget for manual and automatic Function refreshes.
total_memory_bytes: int, optional
Default total memory budget for these refreshes, in bytes.
total_gpu_count: int, optional
Default total GPU budget across the Function workers.
Examples
--------
>>> await table.add_columns( # doctest: +SKIP
... computed={"total": "price * quantity"},
... auto_refresh_enabled=True,
... auto_refresh_interval_seconds=300,
... )
Returns
-------
AddColumnsResult
version: the new version number of the table after adding columns.
"""
_resource_options(
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
if auto_refresh_enabled is not None and type(auto_refresh_enabled) is not bool:
raise ValueError("auto_refresh_enabled must be a boolean")
if auto_refresh_interval_seconds is not None and (
type(auto_refresh_interval_seconds) is not int
or not 0 < auto_refresh_interval_seconds <= (2**64 - 1) // 1000
):
raise ValueError(
"auto_refresh_interval_seconds must be a positive integer "
"no greater than u64::MAX / 1000"
)
options_supplied = (
auto_refresh_enabled is not None
or auto_refresh_interval_seconds is not None
or total_cpu_millis is not None
or total_memory_bytes is not None
or total_gpu_count is not None
or gpu_class is not None
or gpu_count is not None
)
bindings: list[tuple[FunctionApplication, str | None]] = []
if isinstance(transforms, FunctionApplication):
bindings.append((transforms, None))
elif isinstance(transforms, Mapping) and any(
isinstance(value, FunctionApplication) for value in transforms.values()
):
for name, application in transforms.items():
if not isinstance(application, FunctionApplication):
raise ValueError(
"add_columns cannot mix Function applications and SQL "
"expressions"
)
bindings.append((application, name))
if bindings:
if computed:
raise ValueError(
"add_columns cannot mix a Function application with SQL "
"computed columns"
)
for application, _ in bindings:
application._ensure_declarable()
return await self._inner.add_function_columns(
[(app.to_canonical_json(), name) for app, name in bindings],
auto_refresh_enabled,
auto_refresh_interval_seconds,
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class,
gpu_count,
)
if isinstance(transforms, pa.Field):
transforms = [transforms]
if isinstance(transforms, list) and all(
{isinstance(f, pa.Field) for f in transforms}
):
transforms = pa.schema(transforms)
if computed:
if transforms is not None and (
not isinstance(transforms, (Mapping, list, pa.Schema))
or bool(transforms)
):
raise ValueError(
"add_columns cannot take both transforms and computed columns"
)
return await self._inner.add_computed_columns(
list(computed.items()),
auto_refresh_enabled,
auto_refresh_interval_seconds,
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class,
gpu_count,
)
if options_supplied:
raise ValueError(
"automatic refresh options require computed or Function columns"
)
if transforms is None:
raise ValueError("add_columns requires transforms or computed columns")
if isinstance(transforms, pa.Schema):
return await self._inner.add_columns_with_schema(transforms)
if isinstance(transforms, Mapping):
return await self._inner.add_columns(list(transforms.items()))
return await self._inner.add_columns_from_reader(
_into_pyarrow_reader(transforms)
)
async def refresh_column(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> RefreshColumnResult:
"""
Fill the rows of a computed column that hold no value yet.
Local refresh requires the ``experimental-functions`` Cargo feature
in a source build. Published packages leave it disabled.
Declared with ``add_columns(computed=...)``, a column starts empty and
gets its values here. Rows appended since the last refresh are filled
by the next one, and rows whose inputs changed since they were computed
are recomputed; everything else is left as it is.
Supported for local tables only. For remote tables, a refresh runs as
a server job, through
[`refresh_column_async`][lancedb.table.Table.refresh_column_async].
Parameters
----------
column: str
The name of the computed column to fill.
where: str, optional
SQL filter selecting the rows to refresh, as in ``REFRESH COLUMN
... WHERE``. Unfilled rows outside it stay unfilled. LanceDB
Enterprise only; local refreshes reject this option.
batch_size: int, optional
Maximum rows per Function call on the remote server. Must be
greater than zero. None uses the server default. Has no effect on
local expression-backed columns.
max_concurrency: int, optional
Override the registered Function's maximum concurrent user-code
calls for this logical execution. Must be between 1 and
4294967295. None uses the registration default. This is not an
account-wide quota. LanceDB Enterprise only; local refreshes
reject this option.
publication_interval_rows: int, optional
Opt-in publication threshold for completed Function outputs. Must
be greater than zero. None keeps whole-fragment publication. The
actual publication boundary can be larger than this threshold
because it also depends on completed work and Function batching.
LanceDB Enterprise only; local refreshes reject this option.
total_cpu_millis: int, optional
Override the total CPU budget in millicores for this refresh.
total_memory_bytes: int, optional
Override the total memory budget in bytes for this refresh.
supervisor_resources: dict[str, int], optional
CPU, memory, and ephemeral-storage limits for the Function refresh
supervisor pod. Keys are ``cpu_millis``, ``memory_bytes`` and
``ephemeral_storage_bytes``; all are required and positive.
LanceDB Enterprise only; local refreshes reject it.
planning_workers: dict[str, object], optional
Resources and sizing for the Function refresh planning fleet.
``resources`` has the same keys as ``supervisor_resources``; the
other keys are ``max_workers``, ``rows_per_worker`` and
``fragments_per_worker``. LanceDB Enterprise only; local refreshes
reject it.
gpu_class: str, optional
GPU class for Function workers. Must be a lowercase name of 1-63
letters, digits, or hyphens, without a leading or trailing hyphen.
None keeps the Function's registered class or the deployment
default. LanceDB Enterprise only.
total_gpu_count: int, optional
Nonnegative total GPU budget for Function workers without changing
the other resource limits. Unspecified resources inherit column defaults,
then Function defaults, then deployment defaults. LanceDB Enterprise only.
retry_policy: FunctionRetryPolicy, optional
Override retries of failed user-code calls. Only use when repeating
partial work is safe. Set max_attempts=1 to disable retries.
Enterprise Functions only; local refreshes reject this option.
error_policy: {"fail", "skip"}, optional
Action after failure or retry exhaustion. Skip records the error
and attempt count, subject to the server's failure limits.
Workflow version 1 servers default to skip with a 5% failure ceiling
and publish successful output before reporting a threshold failure.
Older deployments retain their existing defaults.
max_failed_fraction: float, optional
Under a skip policy, fail the job once more than this share of
rows (0 to 1) has failed. LanceDB Enterprise only.
max_failed_rows: int, optional
Under a skip policy, fail the job once more than this many rows
have failed. LanceDB Enterprise only.
row_timeout_seconds: float, optional
Deadline for one Function call. LanceDB Enterprise only.
init_timeout_seconds: float, optional
Deadline for Function initialization. LanceDB Enterprise only.
num_workers: int, optional
Worker count for an expression column; registered Function
columns reject it and set the corresponding ``total_*`` options. LanceDB
Enterprise only.
max_job_attempts: int, optional
Total automatic Function refresh attempts, including the initial job.
Set to 1 to disable automatic job recovery.
max_executor_processes_per_worker: int, optional
Cap on executor processes per Function worker. LanceDB
Enterprise only.
retry_errors: bool, default False
Select current failed rows on the server. Cannot be combined with
``where``. Requires Enterprise workflow version 1 or later.
retry_job_id: str, optional
Restrict a failed-row retry to errors recorded by this job.
retry_error_type: str, optional
Restrict a failed-row retry to this exception class.
include_quarantined: bool, default False
Explicitly release and include quarantined rows in a failed-row retry.
function_version: str, optional
Atomically rebind this column's Function to the specified immutable
version. Requires Enterprise workflow version 1 or later.
recompute: {"failed", "all", "none"}, optional
Which rows to recompute during an upgrade. Requires
``function_version``; the default is ``all``. ``failed`` selects
current failures, and ``none`` preserves existing values. Quarantined
rows stay excluded unless explicitly included in a failed-row retry.
Returns
-------
RefreshColumnResult
The number of rows filled and the new version of the table.
"""
_resource_options(
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
return await self._inner.refresh_column(
column,
where=where,
batch_size=batch_size,
max_concurrency=max_concurrency,
publication_interval_rows=publication_interval_rows,
total_cpu_millis=total_cpu_millis,
total_memory_bytes=total_memory_bytes,
supervisor_resources=supervisor_resources,
planning_workers=planning_workers,
gpu_class=gpu_class,
total_gpu_count=total_gpu_count,
retry_policy=None
if retry_policy is None
else FunctionRetryPolicy.model_validate(retry_policy).model_dump_json(),
error_policy=error_policy,
max_failed_fraction=max_failed_fraction,
max_failed_rows=max_failed_rows,
row_timeout_seconds=row_timeout_seconds,
init_timeout_seconds=init_timeout_seconds,
num_workers=num_workers,
max_job_attempts=max_job_attempts,
max_executor_processes_per_worker=max_executor_processes_per_worker,
retry_errors=retry_errors,
retry_job_id=retry_job_id,
retry_error_type=retry_error_type,
include_quarantined=include_quarantined,
function_version=function_version,
recompute=recompute,
gpu_count=gpu_count,
)
async def resume_refresh(self, job_id: str) -> AsyncJob[RefreshColumnJobResult]:
"""Resume a failed Enterprise Function refresh on this table and branch."""
return _typed_job(
await self._inner.resume_refresh(job_id),
RefreshColumnJobResult.from_json,
)
async def retry_failed(
self,
column: str,
*,
error_type: Optional[str] = None,
job_id: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
**options: Any,
) -> AsyncJob[RefreshColumnJobResult]:
"""Start an exact failed-row retry; see ``Table.retry_failed``."""
return await self.refresh_column_async(
column,
retry_errors=True,
retry_error_type=error_type,
retry_job_id=job_id,
include_quarantined=include_quarantined,
function_version=function_version,
recompute="failed" if function_version is not None else None,
**options,
)
async def upgrade_column(
self,
column: str,
*,
version: str,
recompute: Literal["failed", "all", "none"] = "all",
**options: Any,
) -> AsyncJob[RefreshColumnJobResult]:
"""Upgrade the Function and choose which rows to recompute."""
return await self.refresh_column_async(
column, function_version=version, recompute=recompute, **options
)
async def quarantine_failed(
self,
column: str,
*,
error_type: Optional[str] = None,
job_id: Optional[str] = None,
after_attempts: Optional[int] = None,
quarantined: bool = True,
) -> int:
"""Quarantine current failures or release them for later refreshes."""
return await self._inner.quarantine_function_errors(
column,
quarantined=quarantined,
error_type=error_type,
job_id=job_id,
after_attempts=after_attempts,
)
async def function_provenance(
self,
column: str,
*,
limit: Optional[int] = None,
version: Optional[int] = None,
) -> FunctionProvenance:
"""Read per-row producing versions at a table snapshot."""
return FunctionProvenance.from_json(
await self._inner.function_provenance(column, limit=limit, version=version)
)
async def function_errors(
self,
job_id: Optional[str] = None,
column: Optional[str] = None,
limit: Optional[int] = None,
history: bool = False,
page_token: Optional[str] = None,
) -> "AsyncFunctionErrors":
"""
The per-row errors Function refreshes recorded on this table.
A refresh running under a skip policy records each row it skipped.
On servers advertising workflow version 1 or later, the default lists
current unresolved failures, deduplicated by row and Function column,
with attempt counts and first/last failure times. ``history=True``
returns retained observations, including repaired failures. Older servers
keep their legacy listing behavior and reject explicit history requests.
LanceDB Enterprise only; reading errors needs table read access because
messages can include the input that failed.
Parameters
----------
job_id: str, optional
Only errors recorded by this job.
column: str, optional
Only errors on this column.
limit: int, optional
At most this many records (server default 10000, cap 100000).
history: bool, default False
Include retained historical errors instead of current failures.
Requires Enterprise workflow version 1 or later. Historical records
cannot be used with the current-input ``rows()`` helper.
page_token: str, optional
Empty starts retained history, including repaired rows, for an exact
job_id. Continue with the response's
page_token until it is None, keeping all filters unchanged.
Returns
-------
AsyncFunctionErrors
``records``, ``fragments`` and ``truncated``, the last saying
whether the listing stopped at its limit.
Each record identifies its row with ``fragment_id``, ``row_offset``,
the physical ``row_address`` (``(fragment_id << 32) |
row_offset``), and, when the table uses stable row IDs, the stable
``row_id`` read from the refresh's pinned table version. Prefer
``row_id`` for retries: compaction and rewrite can change the
physical fields, while a stable row ID survives them.
To retry the recorded rows with stable row IDs:
.. code-block:: python
errors = await table.function_errors(column="embedding")
row_ids = sorted(
record.row_id
for record in errors.records
if record.row_id is not None
)
if row_ids:
row_id_list = ", ".join(str(row_id) for row_id in row_ids)
await database.execute_query(
f"REFRESH COLUMN embedding ON {table.name} "
f"WHERE _rowid IN ({row_id_list})"
)
For a table without stable row IDs, collect the non-null physical
addresses before the table is compacted or rewritten and run the
SQL refresh:
.. code-block:: python
errors = await table.function_errors(column="embedding")
addresses = sorted(
record.row_address
for record in errors.records
if record.row_address is not None
)
if addresses:
address_list = ", ".join(str(address) for address in addresses)
await database.execute_query(
f"REFRESH COLUMN embedding ON {table.name} "
f"WHERE _rowaddr IN ({address_list})"
)
Records whose per-row detail was capped have no address and appear
only in ``fragments``; they cannot be selected individually here.
Examples
--------
>>> errors = await table.function_errors(column="embedding") # doctest: +SKIP
>>> for record in errors.records: # doctest: +SKIP
... print(record.job_id, record.row_offset, record.error_message)
>>> if errors.truncated: # doctest: +SKIP
... print("listing stopped at the limit")
"""
raw = await self._inner.function_errors(
job_id=job_id,
column=column,
limit=limit,
history=history,
page_token=page_token,
)
return AsyncFunctionErrors(raw, self, history=history or page_token is not None)
async def column_statuses(
self, column: Optional[str] = None
) -> list[FunctionColumnStatus]:
"""Return exact status for Function-backed output columns.
All results describe one server table version. ``column=None`` returns
every Function output. A handle pinned to an exact version requests
that version.
"""
return await self._inner.column_statuses(column=column)
async def column_status(self, column: str) -> FunctionColumnStatus:
"""Return exact status for one Function-backed output column."""
return await self._inner.column_status(column)
async def wait_fresh(
self, column: str, timeout: Optional[timedelta] = None
) -> FunctionColumnStatus:
"""Poll until no stale or never-computed rows remain.
Failed rows do not block completion. Server and transport errors are
not retried. Only the typed finalization-pending response is retried
with bounded backoff; arbitrary 500 responses surface immediately.
"""
return await self._inner.wait_column_fresh(column, timeout)
async def refresh_column_async(
self,
column: str,
*,
where: Optional[str] = None,
batch_size: Optional[int] = None,
max_concurrency: Optional[int] = None,
publication_interval_rows: Optional[int] = None,
total_cpu_millis: Optional[int] = None,
total_memory_bytes: Optional[int] = None,
supervisor_resources: Optional[Dict[str, int]] = None,
planning_workers: Optional[Dict[str, Any]] = None,
gpu_class: Optional[str] = None,
gpu_count: Optional[int] = None,
total_gpu_count: Optional[int] = None,
retry_policy: Optional[FunctionRetryPolicy] = None,
error_policy: Optional[Literal["fail", "skip"]] = None,
max_failed_fraction: Optional[float] = None,
max_failed_rows: Optional[int] = None,
row_timeout_seconds: Optional[float] = None,
init_timeout_seconds: Optional[float] = None,
num_workers: Optional[int] = None,
max_job_attempts: Optional[int] = None,
max_executor_processes_per_worker: Optional[int] = None,
retry_errors: bool = False,
retry_job_id: Optional[str] = None,
retry_error_type: Optional[str] = None,
include_quarantined: bool = False,
function_version: Optional[str] = None,
recompute: Optional[Literal["failed", "all", "none"]] = None,
) -> AsyncJob[RefreshColumnJobResult]:
"""
Like :meth:`refresh_column`, but returns a handle to the refresh job
instead of blocking until it completes.
Local refresh requires a source build with ``experimental-functions``.
Remote jobs do not require this feature.
The job may already be complete when returned; callers must not assume
the column is filled until :meth:`AsyncJob.wait` resolves. Invalid
input -- an unknown column, or one that is not computed -- raises here
rather than failing the job. For local tables, the job runs in-process;
for remote tables, it is the server's backfill job.
Parameters
----------
column: str
The name of the computed column to fill.
where: str, optional
SQL filter selecting the rows to refresh, as in ``REFRESH COLUMN
... WHERE``. Unfilled rows outside it stay unfilled. LanceDB
Enterprise only; local refreshes reject this option.
batch_size: int, optional
Maximum rows per Function call on the remote server. Must be
greater than zero. None uses the server default. Lower this for
slow Functions to stay within the executor's per-call deadline.
Has no effect on local expression-backed columns.
max_concurrency: int, optional
Override the registered Function's maximum concurrent user-code
calls for this logical execution. Must be between 1 and
4294967295. None uses the registration default. This is not an
account-wide quota. LanceDB Enterprise only; local refreshes
reject this option.
publication_interval_rows: int, optional
Opt-in publication threshold for completed Function outputs. Must
be greater than zero. None keeps whole-fragment publication. The
actual publication boundary can be larger than this threshold
because it also depends on completed work and Function batching.
LanceDB Enterprise only; local refreshes reject this option.
total_cpu_millis: int, optional
Override the total CPU budget in millicores for this refresh.
total_memory_bytes: int, optional
Override the total memory budget in bytes for this refresh.
supervisor_resources: dict[str, int], optional
CPU, memory, and ephemeral-storage limits for the Function refresh
supervisor pod. Keys are ``cpu_millis``, ``memory_bytes`` and
``ephemeral_storage_bytes``; all are required and positive.
LanceDB Enterprise only; local refreshes reject it.
planning_workers: dict[str, object], optional
Resources and sizing for the Function refresh planning fleet.
``resources`` has the same keys as ``supervisor_resources``; the
other keys are ``max_workers``, ``rows_per_worker`` and
``fragments_per_worker``. LanceDB Enterprise only; local refreshes
reject it.
gpu_class: str, optional
GPU class for Function workers. Must be a lowercase name of 1-63
letters, digits, or hyphens, without a leading or trailing hyphen.
None keeps the Function's registered class or the deployment
default. LanceDB Enterprise only.
total_gpu_count: int, optional
Nonnegative total GPU budget for Function workers without changing
the other resource limits. Unspecified resources inherit column defaults,
then Function defaults, then deployment defaults. LanceDB Enterprise only.
retry_policy: FunctionRetryPolicy, optional
Override retries of failed user-code calls. Only use when repeating
partial work is safe. Set max_attempts=1 to disable retries.
Enterprise Functions only; local refreshes reject this option.
error_policy: {"fail", "skip"}, optional
Action after failure or retry exhaustion. Skip records the error
and attempt count, subject to the server's failure limits.
Workflow version 1 servers default to skip with a 5% failure ceiling
and publish successful output before reporting a threshold failure.
Older deployments retain their existing defaults.
max_failed_fraction: float, optional
Under a skip policy, fail the job once more than this share of
rows (0 to 1) has failed. LanceDB Enterprise only.
max_failed_rows: int, optional
Under a skip policy, fail the job once more than this many rows
have failed. LanceDB Enterprise only.
row_timeout_seconds: float, optional
Deadline for one Function call. LanceDB Enterprise only.
init_timeout_seconds: float, optional
Deadline for Function initialization. LanceDB Enterprise only.
num_workers: int, optional
Worker count for an expression column; registered Function
columns reject it and set the corresponding ``total_*`` options. LanceDB
Enterprise only.
max_job_attempts: int, optional
Total automatic Function refresh attempts, including the initial job.
Set to 1 to disable automatic job recovery.
max_executor_processes_per_worker: int, optional
Cap on executor processes per Function worker. LanceDB
Enterprise only.
retry_errors: bool, default False
Select current failed rows on the server. Cannot be combined with
``where``. Requires Enterprise workflow version 1 or later.
retry_job_id: str, optional
Restrict a failed-row retry to errors recorded by this job.
retry_error_type: str, optional
Restrict a failed-row retry to this exception class.
include_quarantined: bool, default False
Explicitly release and include quarantined rows in a failed-row retry.
function_version: str, optional
Atomically rebind this column's Function to the specified immutable
version. Requires Enterprise workflow version 1 or later.
recompute: {"failed", "all", "none"}, optional
Which rows to recompute during an upgrade. Requires
``function_version``; the default is ``all``. ``failed`` selects
current failures, and ``none`` preserves existing values. Quarantined
rows stay excluded unless explicitly included in a failed-row retry.
Returns
-------
AsyncJob[RefreshColumnResult]
A job whose successful ``wait`` returns row counts plus the source
and published table versions.
Examples
--------
>>> import asyncio
>>> import lancedb
>>> async def refresh_in_background():
... db = await lancedb.connect_async("./.lancedb")
... table = await db.create_table("computed_job_async_demo", [{"x": 1}])
... await table.add_columns(computed={"doubled": "x * 2"})
... job = await table.refresh_column_async("doubled")
... result = await job.wait()
... assert result.rows_assigned == 1
... return await job.status()
>>> asyncio.run(refresh_in_background()) # doctest: +SKIP
'finished'
"""
_resource_options(
total_cpu_millis,
total_memory_bytes,
total_gpu_count,
gpu_class=gpu_class,
gpu_count=gpu_count,
)
return _typed_job(
await self._inner.refresh_column_async(
column,
where=where,
batch_size=batch_size,
max_concurrency=max_concurrency,
publication_interval_rows=publication_interval_rows,
total_cpu_millis=total_cpu_millis,
total_memory_bytes=total_memory_bytes,
supervisor_resources=supervisor_resources,
planning_workers=planning_workers,
gpu_class=gpu_class,
total_gpu_count=total_gpu_count,
retry_policy=None
if retry_policy is None
else FunctionRetryPolicy.model_validate(retry_policy).model_dump_json(),
error_policy=error_policy,
max_failed_fraction=max_failed_fraction,
max_failed_rows=max_failed_rows,
row_timeout_seconds=row_timeout_seconds,
init_timeout_seconds=init_timeout_seconds,
num_workers=num_workers,
max_job_attempts=max_job_attempts,
max_executor_processes_per_worker=max_executor_processes_per_worker,
retry_errors=retry_errors,
retry_job_id=retry_job_id,
retry_error_type=retry_error_type,
include_quarantined=include_quarantined,
function_version=function_version,
recompute=recompute,
gpu_count=gpu_count,
),
RefreshColumnJobResult.from_json,
)
async def alter_columns(
self, *alterations: Iterable[dict[str, Any]]
) -> AlterColumnsResult:
"""
Alter column names and nullability.
alterations : Iterable[Dict[str, Any]]
A sequence of dictionaries, each with the following keys:
- "path": str
The column path to alter. For a top-level column, this is the name.
For a nested column, this is the dot-separated path, e.g. "a.b.c".
- "rename": str, optional
The new name of the column. If not specified, the column name is
not changed.
- "data_type": pyarrow.DataType, optional
The new data type of the column. Existing values will be casted
to this type. If not specified, the column data type is not changed.
- "nullable": bool, optional
Whether the column should be nullable. If not specified, the column
nullability is not changed. Only non-nullable columns can be changed
to nullable. Currently, you cannot change a nullable column to
non-nullable.
Each alteration must specify at least one of "rename", "data_type",
or "nullable". The legacy key "name" is also accepted for renaming.
Unknown keys raise ValueError before any alterations are applied.
Local tables reject renaming source or vector columns used by an
embedding function with ValueError, preserving the embedding binding.
Returns
-------
AlterColumnsResult
version: the new version number of the table after the alteration.
"""
return await self._inner.alter_columns(alterations)
async def update_field_metadata(
self, *updates: dict[str, Any]
) -> UpdateFieldMetadataResult:
"""Update per-field metadata. See
[`Table.update_field_metadata`][lancedb.table.Table.update_field_metadata]."""
return await self._inner.update_field_metadata(updates)
async def drop_columns(self, columns: Iterable[str]):
"""
Drop columns from the table.
Local tables reject dropping source or vector columns used by an
embedding function with ValueError, preserving the embedding binding.
Parameters
----------
columns : Iterable[str]
The names of the columns to drop.
"""
return await self._inner.drop_columns(columns)
async def version(self) -> int:
"""
Retrieve the version of the table
LanceDb supports versioning. Every operation that modifies the table increases
version. As long as a version hasn't been deleted you can `[Self::checkout]`
that version to view the data at that point. In addition, you can
`[Self::restore]` the version to replace the current table with a previous
version.
"""
return await self._inner.version()
async def _checkout_current(self) -> "AsyncTable":
"""Return an independent handle pinned to the currently selected snapshot."""
return AsyncTable(await self._inner._checkout_current())
async def _checkout_snapshot(self, branch: Optional[str], version: int) -> None:
"""Reopen a snapshot while retaining the handle's original branch."""
await self._inner._checkout_snapshot(branch, version)
async def _tracking_branch_identifier(self) -> Optional[str]:
"""Serialize the original branch identity for reopening."""
return await self._inner._tracking_branch_identifier()
async def _set_tracking_branch_identifier(self, identifier: str) -> None:
"""Restore branch identity after reopening a serialized snapshot."""
await self._inner._set_tracking_branch_identifier(identifier)
async def list_versions(self):
"""
List all versions of the table
"""
versions = await self._inner.list_versions()
for v in versions:
# Use integer math: float division on ~1e18 nanosecond
# values loses sub-millisecond precision.
seconds, nanos = divmod(v["timestamp"], 1_000_000_000)
v["timestamp"] = datetime.fromtimestamp(seconds) + timedelta(
microseconds=nanos // 1000
)
return versions
async def delta(
self,
from_version: int | str,
to_version: Optional[int | str] = None,
) -> "AsyncTableDelta":
"""Compare two versions or tags.
See [Table.delta][lancedb.table.Table.delta] for endpoint, stable-row-id,
and branch-tag semantics. The returned
[AsyncTableDelta][lancedb.table.AsyncTableDelta] streams
``AsyncRecordBatchReader`` values.
"""
return AsyncTableDelta(await self._inner.delta(from_version, to_version))
async def checkout(self, version: int | str):
"""
Checks out a specific version of the Table
Any read operation on the table will now access the data at the checked out
version. As a consequence, calling this method will disable any read consistency
interval that was previously set.
In LanceDB Embedded, a tag can select a read-only snapshot on another
branch. `checkout_latest()` returns to this handle's original branch.
Restoring a snapshot from another branch raises `ValueError` before writing.
Use [branches.checkout()][lancedb.table.AsyncBranches.checkout] to open a
handle on that branch before restoring its tag.
This is a read-only operation that turns the table into a sort of "view"
or "detached head". Other table instances will not be affected. To make the
change permanent you can use the `[Self::restore]` method.
Any operation that modifies the table will fail while the table is in a checked
out state. To return the table to a normal state use
`[Self::checkout_latest]`.
For Enterprise tables, a tag must belong to this handle's current branch.
To read a tag on another branch, first obtain that branch's handle with
`await table.branches.checkout("branch_name")`, then call
`await branch_table.checkout(tag)` on that handle. A tag on another
branch raises an error without changing the current selection.
Parameters
----------
version: int | str,
The version to check out. A version number (`int`) or a tag
(`str`) can be provided.
"""
try:
await self._inner.checkout(version)
except RuntimeError as e:
if "not found" in str(e):
raise ValueError(
f"Version {version} no longer exists. Was it cleaned up?"
)
else:
raise
async def checkout_latest(self):
"""
Ensures the table is pointing at the latest version
This can be used to manually update a table when the read_consistency_interval
is None
It can also be used to undo a `[Self::checkout]` operation
"""
await self._inner.checkout_latest()
async def restore(self, version: Optional[int | str] = None):
"""
Restore the table to the currently checked out version
In LanceDB Embedded, a snapshot on another branch cannot be restored
through this handle. Open that branch with `branches.checkout()` first;
an ambiguous cross-branch restore raises `ValueError` before writing.
This operation will fail if checkout has not been called previously
This operation will overwrite the latest version of the table with a
previous version. Any changes made since the checked out version will
no longer be visible.
Once the operation concludes the table will no longer be in a checked
out state and the read_consistency_interval, if any, will apply.
"""
await self._inner.restore(version)
def take_offsets(
self,
offsets: list[int],
*,
with_row_id: bool = False,
preserve_order: bool = False,
) -> AsyncTakeQuery:
"""
Take a list of offsets from the table.
Offsets are 0-indexed and relative to the current version of the table. Offsets
are not stable. A row with an offset of N may have a different offset in a
different version of the table (e.g. if an earlier row is deleted).
Offsets are mostly useful for sampling as the set of all valid offsets is easily
known in advance to be [0, len(table)).
By default, no guarantees are made regarding the order in which results
are returned. Repeated offsets produce repeated rows. Set
``preserve_order=True`` to return found rows in input order, including
repeated offsets. Missing non-negative offsets are omitted. Negative
offsets are rejected and are not Python-style relative indices.
``preserve_order`` cannot be combined with an explicit ``order_by``.
Parameters
----------
offsets: list[int]
The offsets to take.
with_row_id: bool, default False
If True, include the "_rowid" column in the results.
preserve_order: bool, default False
If True, return found rows in input order. Rust take queries apply
limit/offset after restoration; Python take builders and Query
conversion do not expose pagination.
Returns
-------
AsyncTakeQuery
A query object that can be executed to get the rows at the given
offsets.
"""
query = AsyncTakeQuery(self._inner.take_offsets(offsets), self)
if preserve_order:
query._inner.preserve_order()
if with_row_id:
query.with_row_id()
return query
def take_row_ids(
self, row_ids: list[int], *, with_row_id: bool = False
) -> AsyncTakeQuery:
"""
Take a list of row ids from the table.
Results follow the requested row-id order. Repeated IDs produce repeated
rows; missing or deleted IDs are omitted. This also applies to remote tables.
Row ids are not stable and are relative to the current version of the table.
They can change due to compaction and updates.
Unlike offsets, row ids are not 0-indexed and no assumptions should be made
about the possible range of row ids. In order to use this method you must
first obtain the row ids by scanning or searching the table.
Even so, row ids are more stable than offsets and can be useful in some
situations.
There is an ongoing effort to make row ids stable which is tracked at
https://github.com/lancedb/lancedb/issues/1120
Parameters
----------
row_ids: list[int]
The row ids to take.
with_row_id: bool, default False
If True, include the "_rowid" column in the results.
Returns
-------
AsyncTakeQuery
A query object that can be executed to get the rows.
"""
query = AsyncTakeQuery(self._inner.take_row_ids(row_ids), self)
if with_row_id:
query.with_row_id()
return query
async def blob_columns(self) -> list[str]:
return await self._inner.blob_columns()
async def fetch_blobs(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
) -> pa.LargeBinaryArray:
selection = _normalize_blob_row_ids(row_ids, column)
return await self._inner.fetch_blobs(
column,
selection.row_ids,
version=selection.version,
snapshot_identity=selection.snapshot_identity,
)
async def fetch_blob_ranges(
self,
column: str,
requests: Sequence[Sequence[int]],
) -> pa.LargeBinaryArray:
"""Materialize row-specific byte ranges.
See [Table.fetch_blob_ranges][lancedb.table.Table.fetch_blob_ranges].
"""
return await self._inner.fetch_blob_ranges(column, list(requests))
async def fetch_blob_files(
self,
column: str,
row_ids: Union[list[int], pa.Array, pa.ChunkedArray, pa.Table],
*,
buffer_size: Optional[int] = None,
) -> "list[Optional[BlobFile]]":
"""Open handles with configurable read-ahead.
See [Table.fetch_blob_files][lancedb.table.Table.fetch_blob_files].
"""
buffer_size = _validate_blob_buffer_size(buffer_size)
selection = _normalize_blob_row_ids(row_ids, column)
handles = await self._inner.fetch_blob_files(
column,
selection.row_ids,
buffer_size=buffer_size,
version=selection.version,
snapshot_identity=selection.snapshot_identity,
)
return _wrap_blob_files(handles)
async def open_blob_file(
self,
descriptor: Union[dict, pa.StructScalar, None],
*,
buffer_size: Optional[int] = None,
) -> Optional[BlobFile]:
"""Open a snapshot-bound descriptor as a seekable file.
See [Table.open_blob_file][lancedb.table.Table.open_blob_file].
"""
buffer_size = _validate_blob_buffer_size(buffer_size)
descriptor = _normalize_blob_descriptor(descriptor)
if descriptor is None:
return None
return BlobFile(
await self._inner.open_blob_file(json.dumps(descriptor), buffer_size)
)
async def open_blob_files(
self, descriptors: Iterable, *, buffer_size: Optional[int] = None
) -> AsyncIterator[Optional[BlobFile]]:
"""Asynchronously iterate over files in descriptor order, preserving nulls.
Use ``async for file in table.open_blob_files(descriptors)``. Each
non-null file must be closed after use.
```python
async for video in videos.open_blob_files(rows["video"]):
if video is not None:
with video:
payload = await video.aread()
```
"""
for descriptor in descriptors:
yield await self.open_blob_file(descriptor, buffer_size=buffer_size)
@property
def tags(self) -> AsyncTags:
"""Tag management for the dataset.
Similar to Git, tags are a way to add metadata to a specific version of the
dataset.
.. warning::
Tagged versions are exempted from the
:py:meth:`optimize(cleanup_older_than)` process.
To remove a version that has been tagged, you must first
:py:meth:`~Tags.delete` the associated tag.
"""
return AsyncTags(self._inner)
@property
def branches(self) -> AsyncBranches:
"""Branch management for the table.
Branches are isolated, writable lines of history forked from another
branch (or version). Writes on a branch do not affect ``main``.
"""
return AsyncBranches(self._inner)
def current_branch(self) -> Optional[str]:
"""The branch of the visible snapshot, or ``None`` for ``main``.
During Embedded tag time travel, this can differ from the original
branch that ``checkout_latest()`` returns to.
"""
return self._inner.current_branch()
async def explain_cleanup(
self, *, min_age_seconds: Optional[int] = None
) -> CleanupPlan:
"""Read-only preview; see Table.explain_cleanup."""
_validate_min_age_seconds(min_age_seconds)
return CleanupPlan.from_json(await self._inner.explain_cleanup(min_age_seconds))
async def cleanup_async(
self, *, min_age_seconds: Optional[int] = None
) -> AsyncJob[CleanupResult]:
"""Submit cleanup. See
[Table.cleanup_async][lancedb.table.Table.cleanup_async].
"""
_validate_min_age_seconds(min_age_seconds)
return _typed_job(
await self._inner.cleanup_async(min_age_seconds), CleanupResult.from_json
)
async def compact_async(
self, *, compaction_options: Optional[CompactionOptions] = None
) -> AsyncJob[CompactionResult]:
"""Submit remote compaction; await the returned job for actual results.
See [Table.compact_async][lancedb.table.Table.compact_async] for options,
permissions, completion semantics and compatibility requirements.
"""
return _typed_job(
await self._inner.compact_async(compaction_options),
CompactionResult.from_json,
)
async def optimize(
self,
*,
cleanup_older_than: Optional[timedelta] = None,
delete_unverified: bool = False,
retrain=False,
compaction_options: Optional[CompactionOptions] = None,
) -> OptimizeStats:
"""
Optimize the on-disk data and indices for better performance.
.. note::
On a LanceDB Enterprise (remote) table, only compaction is
currently supported. Pruning old versions and index optimization
are silently skipped there instead of being attempted, so this
call returns successfully, with zeroed pruning statistics, once
the compaction job has been submitted; it does not wait for that
job to finish. Remote compaction supports per-job
``target_rows_per_fragment``, ``max_source_rows``, ``io_buffer_size``,
and ``batch_size``. Omitted fields inherit table tuning, then
deployment/built-in defaults. Other non-default compaction settings raise
``NotImplementedError`` before submission. The server must support
these SQL options; older-server errors are propagated without
dropping the requested settings.
Modeled after ``VACUUM`` in PostgreSQL.
Optimization covers three operations:
* Compaction: Merges small files into larger ones
* Prune: Removes old versions of the dataset
* Index: Optimizes the indices, adding new data to existing indices
Parameters
----------
cleanup_older_than: timedelta, optional default 7 days
All files belonging to versions older than this will be removed. Set
to 0 days to remove all versions except the latest. The latest version
is never removed.
.. warning::
Setting this to 0 deletes the data files of every older
version, so any other reader or writer still using an older
version of the table will fail. Only set it to 0 if no other
process is working on this dataset.
delete_unverified: bool, default False
Files leftover from a failed transaction may appear to be part of an
in-progress operation (e.g. appending new data) and these files will not
be deleted unless they are at least 7 days old. If delete_unverified is True
then these files will be deleted regardless of their age.
.. warning::
This should only be set to True if you can guarantee that no other
process is currently working on this dataset. Otherwise the dataset
could be put into a corrupted state.
retrain: bool, default False
This parameter is no longer used and is deprecated.
compaction_options: CompactionOptions, optional
Options that control file compaction. For large rows, derive a lower
``target_rows_per_fragment`` from the expected row size to bound later
passes. This does not cap the first pass over an existing oversized
fragment; see [CompactionOptions][lancedb.table.CompactionOptions].
Notes
-----
The frequency an application should call optimize is based on the frequency of
data modifications. If data is frequently added, deleted, or updated then
optimize should be run frequently. A good rule of thumb is to run optimize if
you have added or modified 100,000 or more records or run more than 20 data
modification operations.
"""
return await self._do_optimize(
_optimize_cleanup_since_ms(cleanup_older_than, retrain),
delete_unverified,
compaction_options,
)
async def _do_optimize(
self,
cleanup_since_ms: Optional[int],
delete_unverified: bool,
compaction_options: Optional[CompactionOptions] = None,
) -> OptimizeStats:
return await self._inner.optimize(
cleanup_since_ms=cleanup_since_ms,
delete_unverified=delete_unverified,
compaction_options=compaction_options,
)
async def list_indices(self) -> Iterable[IndexConfig]:
"""
List all indices that have been created with Self::create_index
On LanceDB Enterprise tables, an index whose build hasn't finished may
be missing (vector indices) or listed with 0 indexed rows (scalar and
FTS indices), and ``num_unindexed_rows`` may be ``None``. Use
[wait_for_index][lancedb.table.AsyncTable.wait_for_index] to wait until
an index is ready.
"""
return await self._inner.list_indices()
async def tokenize(
self,
query: str,
*,
column: Optional[str] = None,
index_name: Optional[str] = None,
) -> Iterable[FtsToken]:
"""
Tokenize a query using the tokenizer configured on an FTS index.
Specify exactly one of ``column`` or ``index_name``.
Model-backed tokenizers such as ``jieba/*`` and ``lindera/*`` are
rebuilt in the client process from index metadata. In LanceDB Enterprise,
this means the same tokenizer model files must also exist locally
(under ``LANCE_LANGUAGE_MODEL_HOME``) and must match the files the
server uses. A mismatch cannot be detected: the returned tokens may
differ from what the server's index produces. If the model files are
missing, an error naming the tokenizer is raised.
"""
return await self._inner.tokenize(query, column=column, index_name=index_name)
async def index_stats(self, index_name: str) -> Optional[IndexStatistics]:
"""
Retrieve statistics about an index
Parameters
----------
index_name: str
The name of the index to retrieve statistics for
Returns
-------
IndexStatistics or None
The statistics about the index. Returns None if the index does not exist.
"""
stats = await self._inner.index_stats(index_name)
if stats is None:
return None
else:
return IndexStatistics(**stats)
async def uses_v2_manifest_paths(self) -> bool:
"""
Check if the table is using the new v2 manifest paths.
Returns
-------
bool
True if the table is using the new v2 manifest paths, False otherwise.
"""
return await self._inner.uses_v2_manifest_paths()
async def migrate_manifest_paths_v2(self):
"""
Migrate the manifest paths to the new format.
This will update the manifest to use the new v2 format for paths.
This function is idempotent, and can be run multiple times without
changing the state of the object store.
!!! danger
This should not be run while other concurrent operations are happening.
And it should also run until completion before resuming other operations.
You can use
[AsyncTable.uses_v2_manifest_paths][lancedb.table.AsyncTable.uses_v2_manifest_paths]
to check if the table is already using the new path style.
"""
await self._inner.migrate_manifest_paths_v2()
@deprecation.deprecated(
deprecated_in="0.33.1",
current_version=__version__,
details="Use update_field_metadata() instead.",
)
async def replace_field_metadata(
self, field_name: str, new_metadata: dict[str, str]
):
"""
Replace the metadata of a field in the schema
.. deprecated:: 0.33.1
Use :func:`update_field_metadata` instead.
Parameters
----------
field_name: str
The name of the field to replace the metadata for
new_metadata: dict
The new metadata to set
"""
await self._inner.replace_field_metadata(field_name, new_metadata)
@dataclass
class IndexStatistics:
"""
Statistics about an index.
Attributes
----------
num_indexed_rows: int
The number of rows that are covered by this index.
num_unindexed_rows: int
The number of rows that are not covered by this index.
index_type: str
The type of index that was created.
distance_type: Optional[str]
The distance type used by the index.
num_indices: Optional[int]
The number of parts the index is split into.
"""
num_indexed_rows: int
num_unindexed_rows: int
index_type: Literal[
"IVF_FLAT",
"IVF_SQ",
"IVF_PQ",
"IVF_RQ",
"IVF_HNSW_SQ",
"IVF_HNSW_PQ",
"IVF_HNSW_FLAT",
"FTS",
"BTREE",
"BITMAP",
"LABEL_LIST",
"NGRAM",
"ZONEMAP",
"RTREE",
]
distance_type: Optional[Literal["l2", "cosine", "dot"]] = None
num_indices: Optional[int] = None
# This exists for backwards compatibility with an older API, which returned
# a dictionary instead of a class.
def __getitem__(self, key):
return getattr(self, key)
@dataclass
class TableStatistics:
"""
Statistics about a table and fragments.
Attributes
----------
total_bytes: int
The total size, in bytes, of the table's data files, index files, and
overlay files. Read from the manifest, so this excludes deletion files
and manifests.
num_rows: int
The total number of rows in the table.
num_deleted_rows: Optional[int]
The total number of rows marked as deleted across all fragments of the
table. These rows are not counted in ``num_rows``, but still occupy space
on disk until the table is compacted, so a large value here indicates
that the table should be optimized. Fragments in which every row was
deleted are dropped outright, so their rows are not counted here.
``None`` when the backend does not report deletion counts.
num_indices: int
The total number of indices in the table.
fragment_stats: FragmentStatistics
Statistics about fragments in the table.
"""
total_bytes: int
num_rows: int
num_deleted_rows: Optional[int]
num_indices: int
fragment_stats: FragmentStatistics
@dataclass
class FragmentStatistics:
"""
Statistics about fragments.
Attributes
----------
num_fragments: int
The total number of fragments in the table.
num_small_fragments: int
The total number of small fragments in the table.
Small fragments have low row counts and may need to be compacted.
lengths: FragmentSummaryStats
Statistics about the number of rows in the table fragments.
"""
num_fragments: int
num_small_fragments: int
lengths: FragmentSummaryStats
@dataclass
class FragmentSummaryStats:
"""
Statistics about fragments sizes
Attributes
----------
min: int
The number of rows in the fragment with the fewest rows.
max: int
The number of rows in the fragment with the most rows.
mean: int
The mean number of rows in the fragments.
p25: int
The 25th percentile of number of rows in the fragments.
p50: int
The 50th percentile of number of rows in the fragments.
p75: int
The 75th percentile of number of rows in the fragments.
p99: int
The 99th percentile of number of rows in the fragments.
"""
min: int
max: int
mean: int
p25: int
p50: int
p75: int
p99: int
def _validate_branch_source(
from_ref: Optional[str],
from_version: Optional[int],
from_tag: Optional[str],
) -> None:
if from_tag is None:
return
conflicts = [
name
for name, value in (("from_ref", from_ref), ("from_version", from_version))
if value is not None
]
if conflicts:
raise ValueError(
"from_tag cannot be combined with "
+ " or ".join(conflicts)
+ "; pass only one source"
)
class Tags:
"""
Table tag manager.
"""
def __init__(self, table):
self._table = table
def list(self) -> Dict[str, Tag]:
"""
List all table tags.
Returns
-------
dict[str, Tag]
A dictionary mapping tag names to version numbers.
"""
return LOOP.run(self._table.tags.list())
def get_version(self, tag: str) -> int:
"""
Get the version of a tag.
Parameters
----------
tag: str,
The name of the tag to get the version for.
"""
return LOOP.run(self._table.tags.get_version(tag))
def create(self, tag: str, version: Optional[int] = None) -> None:
"""
Create a tag for a given table version.
Parameters
----------
tag: str,
The name of the tag to create. This name must be unique among all tag
names for the table.
version: int, optional
The table version to tag. Defaults to the version visible to this
handle, including a checked-out version.
"""
LOOP.run(self._table.tags.create(tag, version))
def delete(self, tag: str) -> None:
"""
Delete tag from the table.
Parameters
----------
tag: str,
The name of the tag to delete.
"""
LOOP.run(self._table.tags.delete(tag))
def update(self, tag: str, version: int) -> None:
"""
Update tag to a new version.
Parameters
----------
tag: str,
The name of the tag to update.
version: int,
The new table version to tag.
"""
LOOP.run(self._table.tags.update(tag, version))
class Branches:
"""
Table branch manager.
"""
def __init__(self, parent: "LanceTable"):
self._parent = parent
self._table = parent._table
def list(self) -> Dict[str, Any]:
"""List all branches, mapping name to branch metadata."""
return LOOP.run(self._table.branches.list())
def create(
self,
name: str,
from_ref: Optional[str] = None,
from_version: Optional[int] = None,
*,
from_tag: Optional[str] = None,
) -> "Table":
"""Create a branch and return a handle scoped to it.
Parameters
----------
name: str
Name of the new branch.
from_ref: str, optional
Source branch to fork from. Defaults to ``main``.
from_version: int, optional
A specific version on ``from_ref`` to fork from. Defaults to latest.
from_tag: str, optional
Source tag to fork from. Cannot be combined with ``from_ref`` or
``from_version``.
"""
_validate_branch_source(from_ref, from_version, from_tag)
async_table = LOOP.run(
self._table.branches.create(name, from_ref, from_version, from_tag=from_tag)
)
return self._wrap(async_table)
def checkout(self, name: str, version: Optional[int] = None) -> "Table":
"""Check out an existing branch and return a handle scoped to it.
Parameters
----------
name: str
Name of the branch to check out.
version: int, optional
A specific version on the branch to pin. When set, the returned
handle is a read-only view of that version; when omitted it tracks
the branch's latest and stays writable.
"""
async_table = LOOP.run(self._table.branches.checkout(name, version))
return self._wrap(async_table, version)
def delete(self, name: str) -> None:
"""Delete a branch."""
LOOP.run(self._table.branches.delete(name))
def diff(self, from_branch: str) -> Dict[str, Any]:
"""Diff a branch against main."""
return LOOP.run(self._table.branches.diff(from_branch))
def cherry_pick(self, from_branch: str, dry_run: bool = False) -> Dict[str, Any]:
"""Cherry-pick a branch onto main, or dry-run.
Parameters
----------
from_branch: str
Branch to cherry-pick from.
dry_run: bool, default False
When True, only preview. When False, attempt the cherry-pick.
Notes
-----
A failed cherry-pick returns ``status="failed"`` instead of raising.
"""
return LOOP.run(self._table.branches.cherry_pick(from_branch, dry_run))
def _wrap(
self, async_table: "AsyncTable", version: Optional[int] = None
) -> "Table":
# Delegate to the parent so the branch handle keeps its concrete type
# (LanceTable / RemoteTable) and connection context; `version` is the
# explicit pin so a remote handle can restore branch+version on reopen.
return self._parent._wrap_branch_handle(async_table, version)
class TableDelta:
"""Streaming row changes between two table versions.
Created by [Table.delta][lancedb.table.Table.delta]. The endpoints are
fixed when the delta is created.
Row-level readers require stable row ids at both endpoints. Create the
table with ``storage_options={"new_table_enable_stable_row_ids": "true"}``
to enable them. [list_transactions][lancedb.table.TableDelta.list_transactions]
works without stable row ids.
A nonempty range that contains a ``Restore``, or whose transaction history
is incomplete, rejects the row-level readers. Recompute the checkpoint with
a full rescan and reinitialize it at the end version; transaction history
remains available.
"""
def __init__(self, inner: Any):
self._inner = inner
self._async = AsyncTableDelta(inner)
@property
def begin_version(self) -> int:
"""The version this delta starts from (exclusive)."""
return self._inner.begin_version()
@property
def end_version(self) -> int:
"""The version this delta ends at (inclusive)."""
return self._inner.end_version()
def inserted_rows(self) -> pa.RecordBatchReader:
"""Stream rows inserted in ``(begin_version, end_version]``.
Each batch carries the table columns plus ``_rowid``,
``_row_created_at_version``, and ``_row_last_updated_at_version``.
"""
return _record_batch_reader(LOOP.run(self._async.inserted_rows()))
def updated_rows(self) -> pa.RecordBatchReader:
"""Stream rows updated in ``(begin_version, end_version]``.
Newly inserted rows are excluded; use
[upserted_rows][lancedb.table.TableDelta.upserted_rows] for both.
"""
return _record_batch_reader(LOOP.run(self._async.updated_rows()))
def upserted_rows(self) -> pa.RecordBatchReader:
"""Stream inserted and updated rows in ``(begin_version, end_version]``."""
return _record_batch_reader(LOOP.run(self._async.upserted_rows()))
def deleted_row_ids(self) -> pa.RecordBatchReader:
"""Stream the ``_rowid`` values of rows deleted in the range.
Each batch carries one ``_rowid`` column of type ``uint64``. Deleted
rows are not reconstructed from earlier versions.
"""
return _record_batch_reader(LOOP.run(self._async.deleted_row_ids()))
def list_transactions(self) -> List[Dict[str, Any]]:
"""List transactions committed in ``(begin_version, end_version]``.
Each item has ``read_version``, ``uuid``, ``operation``, ``tag``, and
``transaction_properties``. Unlike row readers, this works without
stable row ids.
"""
return LOOP.run(self._async.list_transactions())
class AsyncTableDelta:
"""Async streaming row changes between two table versions.
Created by [AsyncTable.delta][lancedb.table.AsyncTable.delta]. See
[TableDelta][lancedb.table.TableDelta] for the endpoint and stable-row-id
contract.
"""
def __init__(self, inner: Any):
self._inner = inner
@property
def begin_version(self) -> int:
"""The version this delta starts from (exclusive)."""
return self._inner.begin_version()
@property
def end_version(self) -> int:
"""The version this delta ends at (inclusive)."""
return self._inner.end_version()
async def inserted_rows(self) -> AsyncRecordBatchReader:
"""Stream rows inserted in ``(begin_version, end_version]``."""
return AsyncRecordBatchReader(await self._inner.inserted_rows())
async def updated_rows(self) -> AsyncRecordBatchReader:
"""Stream rows updated in ``(begin_version, end_version]``."""
return AsyncRecordBatchReader(await self._inner.updated_rows())
async def upserted_rows(self) -> AsyncRecordBatchReader:
"""Stream inserted and updated rows in ``(begin_version, end_version]``."""
return AsyncRecordBatchReader(await self._inner.upserted_rows())
async def deleted_row_ids(self) -> AsyncRecordBatchReader:
"""Stream the ``_rowid`` values of rows deleted in the range."""
return AsyncRecordBatchReader(await self._inner.deleted_row_ids())
async def list_transactions(self) -> List[Dict[str, Any]]:
"""List transactions committed in ``(begin_version, end_version]``."""
return await self._inner.list_transactions()
def _record_batch_reader(stream) -> pa.RecordBatchReader:
def iter_sync():
try:
while True:
yield LOOP.run(stream.__anext__())
except StopAsyncIteration:
return
return pa.RecordBatchReader.from_batches(stream.schema, iter_sync())
class AsyncTags:
"""
Async table tag manager.
"""
def __init__(self, table):
self._table = table
async def list(self) -> Dict[str, Tag]:
"""
List all table tags.
Returns
-------
dict[str, Tag]
A dictionary mapping tag names to version numbers.
"""
return await self._table.tags.list()
async def get_version(self, tag: str) -> int:
"""
Get the version of a tag.
Parameters
----------
tag: str,
The name of the tag to get the version for.
"""
return await self._table.tags.get_version(tag)
async def create(self, tag: str, version: Optional[int] = None) -> None:
"""
Create a tag for a given table version.
Parameters
----------
tag: str,
The name of the tag to create. This name must be unique among all tag
names for the table.
version: int, optional
The table version to tag. Defaults to the version visible to this
handle, including a checked-out version.
"""
await self._table.tags.create(tag, version)
async def delete(self, tag: str) -> None:
"""
Delete tag from the table.
Parameters
----------
tag: str,
The name of the tag to delete.
"""
await self._table.tags.delete(tag)
async def update(self, tag: str, version: int) -> None:
"""
Update tag to a new version.
Parameters
----------
tag: str,
The name of the tag to update.
version: int,
The new table version to tag.
"""
await self._table.tags.update(tag, version)
class AsyncBranches:
"""Async table branch manager."""
def __init__(self, table):
self._table = table
async def list(self) -> Dict[str, Any]:
"""List all branches, mapping name to branch metadata."""
return await self._table.branches.list()
async def create(
self,
name: str,
from_ref: Optional[str] = None,
from_version: Optional[int] = None,
*,
from_tag: Optional[str] = None,
) -> "AsyncTable":
"""Create a branch and return a handle scoped to it.
Parameters
----------
name: str
Name of the new branch.
from_ref: str, optional
Source branch to fork from. Defaults to ``main``.
from_version: int, optional
A specific version on ``from_ref`` to fork from. Defaults to latest.
from_tag: str, optional
Source tag to fork from. Cannot be combined with ``from_ref`` or
``from_version``.
"""
_validate_branch_source(from_ref, from_version, from_tag)
# "main" and None are two spellings of the root branch in lance; normalize
# so from_ref="main" behaves identically to the default. This must happen
# after validating from_tag conflicts so an explicit from_ref is not lost.
if from_ref == "main":
from_ref = None
inner = await self._table.branches.create(
name, from_ref, from_version, from_tag=from_tag
)
return AsyncTable(inner)
async def checkout(self, name: str, version: Optional[int] = None) -> "AsyncTable":
"""Check out an existing branch and return a handle scoped to it.
Parameters
----------
name: str
Name of the branch to check out.
version: int, optional
A specific version on the branch to pin. When set, the returned
handle is a read-only view of that version; when omitted it tracks
the branch's latest and stays writable.
"""
return AsyncTable(await self._table.branches.checkout(name, version))
async def delete(self, name: str) -> None:
"""Delete a branch."""
await self._table.branches.delete(name)
async def diff(self, from_branch: str) -> Dict[str, Any]:
"""Diff a branch against main."""
return await self._table.branches.diff(from_branch)
async def cherry_pick(
self, from_branch: str, dry_run: bool = False
) -> Dict[str, Any]:
"""Cherry-pick a branch onto main, or dry-run.
A failed cherry-pick returns ``status="failed"`` instead of raising.
"""
return await self._table.branches.cherry_pick(from_branch, dry_run)