fix: explain Flight SQL endpoint errors during function registration (#5342)

## Summary

Function registration uses Flight SQL. If the SDK has no usable SQL URI
or cannot establish a connection, it points to the cluster’s published
Flight SQL URI and the Public SQL or PrivateLink SQL settings. The
diagnostic retains the original exception type, cause text, and request
ID when present.

## Validation

- Built main (`0abc7d48`) and the SDK change (`4bfdbb217`) in the same
isolated Linux VM with the locked Python environment. The final commit
updates only the test assertion for platform-dependent connection
errors.
- Compared 22 calls against a healthy HTTPS REST fixture and Flight
endpoints. The expected five registration failures gained the hint.
After accounting for request IDs, all original errors and successful
results matched.
- New regression tests on main had two expected failures and three
passing controls. The SDK change passed 86 tests with 16 skipped across
registration, Function catalog, SQL, and view creation. Rust formatting
and Ruff passed.
- All 50 GitHub checks passed on `7c2dad8bc`, including the [Python
workflow](https://github.com/lancedb/lancedb/actions/runs/46796061600)
and the [Rust macOS 26
rerun](https://github.com/lancedb/lancedb/actions/runs/46796061943/job/143185732469).
This commit is contained in:
Yang Cen
2026-12-02 12:15:59 +08:00
committed by GitHub
parent 3afd0ced9c
commit 5373da5760
6 changed files with 175 additions and 36 deletions
@@ -0,0 +1,94 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
import socket
import pytest
import lancedb
from lancedb.functions import function
from lancedb.remote.errors import SqlError
@function
def identity(value: int) -> int:
return value
@pytest.mark.parametrize("sql_host_override", [None, "grpc://127.0.0.1/bad-path"])
def test_function_registration_sql_address(sql_host_override):
db = lancedb.connect(
"db://catalog",
api_key="test-key",
host_override="https://127.0.0.1",
sql_host_override=sql_host_override,
)
with pytest.raises(ValueError) as query_error:
db.execute_query("SELECT 1")
with pytest.raises(ValueError) as registration_error:
db.create_function(identity)
assert type(registration_error.value) is type(query_error.value)
assert registration_error.value.__cause__ is query_error.value.__cause__
assert "Public SQL" not in str(query_error.value)
if sql_host_override is None:
assert "sql_host_override is required" in str(query_error.value)
assert str(registration_error.value).startswith(str(query_error.value))
assert "Public SQL or PrivateLink SQL" in str(registration_error.value)
else:
assert str(registration_error.value) == str(query_error.value)
@pytest.mark.asyncio
async def test_function_registration_unreachable_sql():
# Hold the port open without listening so another process cannot claim it.
# Depending on the OS, connecting to this port is refused or times out.
connect_error = (
r"(?i)request_id=[0-9a-f-]{36}\): "
r"(?:transport error: [^\n]*(?:refused|deadline has elapsed)"
r"|SQL connection timed out)"
)
with socket.socket() as sql_socket:
sql_socket.bind(("127.0.0.1", 0))
db = await lancedb.connect_async(
"db://catalog",
api_key="test-key",
host_override="https://127.0.0.1",
sql_host_override=f"grpc://127.0.0.1:{sql_socket.getsockname()[1]}",
client_config={"timeout_config": {"connect_timeout": 5}},
)
with pytest.raises(RuntimeError, match=connect_error) as exc:
await db.create_function_async(identity)
assert type(exc.value) is RuntimeError
assert exc.value.__cause__ is None
assert "Public SQL or PrivateLink SQL" in str(exc.value)
with pytest.raises(RuntimeError, match=connect_error) as exc:
await db.execute_query("SELECT 1")
assert "Public SQL" not in str(exc.value)
@pytest.mark.parametrize(
("exception_type", "grpc_code"),
[("FlightUnauthenticatedError", 16), ("FlightUnavailableError", 14)],
)
def test_function_registration_preserves_flight_status(exception_type, grpc_code):
flight = pytest.importorskip("pyarrow.flight")
error = getattr(flight, exception_type)
class RejectRegistration(flight.FlightServerBase):
def do_get(self, context, ticket):
assert b'CREATE FUNCTION "identity" FROM' in ticket.ticket
raise error("registration rejected by Flight server")
with RejectRegistration("grpc://127.0.0.1:0") as server:
db = lancedb.connect(
"db://catalog",
api_key="test-key",
host_override="https://127.0.0.1",
sql_host_override=f"grpc://127.0.0.1:{server.port}",
)
with pytest.raises(SqlError) as exc:
db.create_function(identity)
assert exc.value.grpc_code == grpc_code
assert "registration rejected by Flight server" in str(exc.value)
assert "Public SQL" not in str(exc.value)