mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-13 15:22:30 +00:00
Move the public website pages into `docs/web` beside the SDK, with
shared navigation and executable example sources in `docs/web-tests`.
The combined website is assembled by
[lancedb/docs#355](https://github.com/lancedb/docs/pull/355), with six
Enterprise replacements from
[lancedb/sophon#7681](https://github.com/lancedb/sophon/pull/7681).
This includes Function guidance, runnable Quickstarts, regenerated
snippets, production contact/demo fixes, the legacy SQL route, and the
LeRobot redirect. LeRobot remains attributed to Geneva; all 34 Geneva
pages remain in the docs repository until Function officially launches.
Materialized-view guidance follows the full rebuild refresh introduced
by #4373: local refresh writes the complete result, has no `full`
argument, supports `source_version`, and no longer requires stable row
IDs. Deployment SQL refresh options and stable-ID setup are qualified by
version/configuration. Function input guidance distinguishes ordinary
stored columns from computed inputs supported by newer deployments.
**Validation:** independently accepted local checks include 95 Python
tests (real Enterprise excluded), eight TypeScript tests, Rust
Quickstart, snippet parity, typos, deterministic assembly, Mint
validation and links/anchors/redirects. The combined baseline retains
all 181 production routes and 17 legacy redirects. Examples ran locally
against source-built Python SDK 0.40.0b13; existing GitHub workflows do
not run `docs/web-tests`. New main at `069b2f7d` is 0.41.0b0 and adds
computed Function inputs; that SDK was not exercised by the accepted
tests. Latest prose corrections receive focused site validation.
**Merge/rollout:** merge this and #7681 before #355's combined CI, which
reads producer `main`. This is migration infrastructure: section
overlays, source-owned skills and automatic versioned paired releases
remain follow-ups. The current manual publisher is transitional. Hosted
validation, a supported SDK/Enterprise release pair and a real
Enterprise walkthrough remain pending.
Production Mintlify watches `lancedb/docs:deploy-freeze`, directory
`docs`; preserve that source through review. The separate SDK GitHub
Pages reference workflow rebuilds on main merges.
**Updated-head CI (9a50f8db):**
[Typos](https://github.com/lancedb/lancedb/actions/runs/37654185443/job/112904990558),
[PR
title](https://github.com/lancedb/lancedb/actions/runs/37654179507/job/112904974595)
and
[Aikido](https://app.aikido.dev/featurebranch/scan/215386145?groupId=74229)
pass. Critical findings in the example lockfiles were patched: all Sharp
copies use 0.35.5, protobufjs 7.6.6, tar 7.5.22, quinn-proto 0.11.19,
zerovec 0.11.8 and zerovec-derive 0.11.6. The SDK's product dependencies
are unchanged. The revised lockfiles pass eight TypeScript examples,
HuggingFace decode/resize/crop/rescale/normalize plus AVIF roundtrip,
and Rust Quickstart. Aikido passing does not mean every advisory is
cleared; npm still reports 29 high and five moderate advisories in the
example dependency graph. No scanner bypass or suppression was added.
The Function prose correction passes combined/standalone Mint,
deterministic assembly, routes and assets. The revised 325-file site
checksum is
`175bec06864abb400dd292e667ba93448c0479b8359fd7bf6146afabf9040647`. This
PR remains open for independent review; further merges are on hold.
---------
Co-authored-by: Brendan Clement <brendan.clement@lancedb.com>
243 lines
7.9 KiB
Python
243 lines
7.9 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
"""Examples for docs/web/management/function.mdx.
|
|
|
|
Registering a Function and filling its column need a LanceDB Enterprise
|
|
deployment, so `test_function_enterprise` runs only when the connection
|
|
variables the Enterprise examples already use are set: LANCEDB_URI,
|
|
LANCEDB_API_KEY and LANCEDB_HOST_OVERRIDE, plus LANCEDB_REGION if the
|
|
deployment needs one. The walkthrough calls its table `function_demo` and its
|
|
Function `double`; the test creates both under names unique to its run, never
|
|
replaces an existing object, and drops only what it created.
|
|
|
|
The other tests run locally and check the client side of the same code: what
|
|
`@udf` sends for registration, how a registered version binds a column, and
|
|
where a local connection stops. They do not run a Function remotely.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
import lancedb
|
|
import pyarrow as pa
|
|
import pytest
|
|
from lancedb import col
|
|
from lancedb.functions import FunctionApplication, FunctionVersion
|
|
|
|
# --8<-- [start:function_define]
|
|
from lancedb import udf
|
|
|
|
|
|
@udf
|
|
def double(value: float) -> float:
|
|
return value * 2
|
|
|
|
|
|
# --8<-- [end:function_define]
|
|
|
|
# The definition the walkthrough registers. The Enterprise test registers a copy
|
|
# of it under a name that only its run uses.
|
|
WALKTHROUGH_DOUBLE = double
|
|
|
|
|
|
def describe(value: float):
|
|
return {"half": value / 2, "note": None}
|
|
|
|
|
|
# A Function version as the service returns it after registration: the SDK's
|
|
# shared golden reply, with the name and signature of the definition above.
|
|
SERVICE_REPLY = (
|
|
Path(__file__).parents[3]
|
|
/ "rust"
|
|
/ "lancedb"
|
|
/ "tests"
|
|
/ "fixtures"
|
|
/ "first_class_functions"
|
|
/ "v1"
|
|
/ "remote_function_job.json"
|
|
)
|
|
|
|
|
|
def registered_version(definition) -> FunctionVersion:
|
|
request = definition.registration_request
|
|
reply = json.loads(SERVICE_REPLY.read_text())["result"]
|
|
reply["name"] = request.name
|
|
reply["signature"] = json.loads(request.signature.to_canonical_json())
|
|
return FunctionVersion.from_json(json.dumps(reply))
|
|
|
|
|
|
def require_env(var_name: str) -> str:
|
|
"""Skip the test unless the required environment variable is provided."""
|
|
value = os.environ.get(var_name)
|
|
if not value:
|
|
pytest.skip(f"Set {var_name} to run this Enterprise example")
|
|
return value
|
|
|
|
|
|
def test_function_runs_locally_as_python():
|
|
# --8<-- [start:function_local_call]
|
|
print(double(1.5))
|
|
# 3.0
|
|
# --8<-- [end:function_local_call]
|
|
assert double(1.5) == 3.0
|
|
|
|
|
|
def test_function_registration_request():
|
|
request = double.registration_request
|
|
assert request.name == "double"
|
|
assert [
|
|
(parameter.name, parameter.arrow_type, parameter.nullable)
|
|
for parameter in request.signature.inputs
|
|
] == [("value", "float64", False)]
|
|
output = request.signature.output
|
|
assert (output.kind, output.arrow_type, output.nullable) == (
|
|
"scalar",
|
|
"float64",
|
|
False,
|
|
)
|
|
version = request.runtime.python_version
|
|
assert version == f"{sys.version_info.major}.{sys.version_info.minor}"
|
|
assert request.runtime.environment.packages == ()
|
|
|
|
|
|
def test_function_output_nullability():
|
|
inputs = pa.schema([pa.field("value", pa.float64(), nullable=False)])
|
|
|
|
# A named-struct output may declare nullable fields.
|
|
fields = [
|
|
pa.field("half", pa.float64(), nullable=False),
|
|
pa.field("note", pa.string(), nullable=True),
|
|
]
|
|
struct = udf(input_schema=inputs, output_schema=pa.schema(fields))(describe)
|
|
output = struct.registration_request.signature.output
|
|
assert output.kind == "named_struct"
|
|
assert [(field.name, field.nullable) for field in output.fields] == [
|
|
("half", False),
|
|
("note", True),
|
|
]
|
|
|
|
# A scalar output, and a struct declared as one nullable field, may not be.
|
|
for nullable in (
|
|
pa.field("result", pa.float64(), nullable=True),
|
|
pa.field("result", pa.struct(fields), nullable=True),
|
|
):
|
|
with pytest.raises(ValueError, match="non-nullable"):
|
|
udf(input_schema=inputs, output_schema=nullable)(describe)
|
|
|
|
|
|
def test_function_registration_is_remote(tmp_path):
|
|
db = lancedb.connect(tmp_path)
|
|
with pytest.raises(NotImplementedError):
|
|
db.create_function(double)
|
|
|
|
|
|
def test_function_version_binds_a_column(tmp_path):
|
|
version = registered_version(double)
|
|
|
|
application = version(value=col("value"))
|
|
|
|
assert isinstance(application, FunctionApplication)
|
|
assert application.function.name == "double"
|
|
assert application.function.version == version.version
|
|
assert [
|
|
(binding.parameter, binding.kind, binding.value["path"])
|
|
for binding in application.inputs
|
|
] == [("value", "column", "value")]
|
|
with pytest.raises(TypeError, match="direct col"):
|
|
version(value=1.5)
|
|
with pytest.raises(TypeError, match="missing"):
|
|
version(x=col("value"))
|
|
|
|
# The binding is accepted by add_columns, which a local table refuses:
|
|
# a Function column needs LanceDB Cloud or Enterprise.
|
|
table = lancedb.connect(tmp_path).create_table("t", [{"value": 1.0}])
|
|
with pytest.raises(NotImplementedError, match="Cloud and Enterprise"):
|
|
table.add_columns({"doubled": application})
|
|
|
|
|
|
def test_function_enterprise(monkeypatch):
|
|
require_env("LANCEDB_URI")
|
|
require_env("LANCEDB_API_KEY")
|
|
require_env("LANCEDB_HOST_OVERRIDE")
|
|
|
|
# --8<-- [start:function_connect]
|
|
import os
|
|
|
|
import lancedb
|
|
|
|
db = lancedb.connect(
|
|
uri=os.environ["LANCEDB_URI"], # db://your-database
|
|
api_key=os.environ["LANCEDB_API_KEY"],
|
|
region=os.environ.get("LANCEDB_REGION", "us-east-1"),
|
|
host_override=os.environ["LANCEDB_HOST_OVERRIDE"],
|
|
)
|
|
# --8<-- [end:function_connect]
|
|
|
|
# In a shared database `function_demo` or `double` may already exist, so
|
|
# this run creates both under names only it uses, and drops only those.
|
|
run = uuid.uuid4().hex[:12]
|
|
table_name = f"function_demo_{run}"
|
|
double = udf(name=f"double_{run}")(WALKTHROUGH_DOUBLE.__wrapped__)
|
|
create_table = db.create_table
|
|
|
|
def create_run_table(name, *args, **kwargs):
|
|
assert name == "function_demo"
|
|
return create_table(table_name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(db, "create_table", create_run_table)
|
|
|
|
table = None
|
|
version = None
|
|
try:
|
|
# --8<-- [start:function_create_table]
|
|
import pyarrow as pa
|
|
|
|
schema = pa.schema([pa.field("value", pa.float64(), nullable=False)])
|
|
table = db.create_table(
|
|
"function_demo",
|
|
[{"value": 1.0}, {"value": 2.0}, {"value": 3.0}],
|
|
schema=schema,
|
|
)
|
|
# --8<-- [end:function_create_table]
|
|
|
|
# --8<-- [start:function_register]
|
|
job = db.create_function_async(double)
|
|
version = job.wait()
|
|
# --8<-- [end:function_register]
|
|
assert version.name == f"double_{run}"
|
|
|
|
# --8<-- [start:function_apply]
|
|
from lancedb import col
|
|
|
|
table.add_columns({"doubled": version(value=col("value"))})
|
|
# --8<-- [end:function_apply]
|
|
|
|
# --8<-- [start:function_refresh]
|
|
refresh = table.refresh_column_async("doubled")
|
|
result = refresh.wait()
|
|
print(result.rows_assigned)
|
|
# 3
|
|
# --8<-- [end:function_refresh]
|
|
assert result.rows_assigned == 3
|
|
assert result.rows_failed == 0
|
|
|
|
# --8<-- [start:function_inspect]
|
|
rows = table.search().select(["value", "doubled"]).to_list()
|
|
print(sorted((row["value"], row["doubled"]) for row in rows))
|
|
# [(1.0, 2.0), (2.0, 4.0), (3.0, 6.0)]
|
|
# --8<-- [end:function_inspect]
|
|
assert sorted((row["value"], row["doubled"]) for row in rows) == [
|
|
(1.0, 2.0),
|
|
(2.0, 4.0),
|
|
(3.0, 6.0),
|
|
]
|
|
finally:
|
|
if table is not None:
|
|
db.drop_table(table_name)
|
|
if version is not None:
|
|
db.drop_function(version.name, version=version.version)
|