mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 20:44:00 +00:00
feat(nodejs): add Enterprise table override APIs (#5077)
Add TypeScript `getTableOverrides`, `updateTableOverrides`, and `resetTableOverrides` for LanceDB Enterprise tables, delegating to the shared Rust implementation used by Python. Document that updates perform GET → recursive local merge → full PUT. Unrelated fields are preserved from the read snapshot, but concurrent edits can be overwritten. CLI `set` and REST PUT retain replacement semantics. Fix an empty SDK update to return the current snapshot without a PUT. Live testing found that the previous implementation submitted an empty replacement and failed when no overrides were configured. The fix applies to Python, Rust, and TypeScript. Add mock HTTP/local coverage and an opt-in Enterprise integration suite for namespaces, branch isolation, REST replacement, validation failures, historical handles, and empty updates.
This commit is contained in:
@@ -2929,3 +2929,216 @@ describe("job wait deadlines", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("table overrides", () => {
|
||||
it.each([[], ["analytics", "features"]])(
|
||||
"reads, recursively merges, and resets in namespace %j",
|
||||
async (...namespacePath: string[]) => {
|
||||
const requests: { method?: string; url: string; body?: unknown }[] = [];
|
||||
const current = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: {
|
||||
compaction: {
|
||||
enabled: true,
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
target_rows_per_fragment: 1000,
|
||||
},
|
||||
cleanup: { enabled: false },
|
||||
},
|
||||
custom: { keep: true, replace: [1, 2], clear: "value" },
|
||||
};
|
||||
const patch = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: { compaction: { enabled: false } },
|
||||
custom: { replace: [3], clear: null, label: "測試" },
|
||||
};
|
||||
const merged = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: {
|
||||
compaction: {
|
||||
enabled: false,
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
target_rows_per_fragment: 1000,
|
||||
},
|
||||
cleanup: { enabled: false },
|
||||
},
|
||||
custom: { keep: true, replace: [3], clear: null, label: "測試" },
|
||||
};
|
||||
await withMockDatabase(
|
||||
(req, res) => {
|
||||
const url = req.url ?? "";
|
||||
if (url.includes("/describe/")) {
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(JSON.stringify({ version: 1, schema: { fields: [] } }));
|
||||
return;
|
||||
}
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (chunk) => chunks.push(chunk));
|
||||
req.on("end", () => {
|
||||
const body = chunks.length
|
||||
? JSON.parse(Buffer.concat(chunks).toString())
|
||||
: undefined;
|
||||
requests.push({ method: req.method, url, body });
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(
|
||||
JSON.stringify(
|
||||
req.method === "GET"
|
||||
? current
|
||||
: req.method === "PUT"
|
||||
? body.overrides
|
||||
: {},
|
||||
),
|
||||
);
|
||||
});
|
||||
},
|
||||
async (db) => {
|
||||
const table = await db.openTable("test", namespacePath);
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(current);
|
||||
await expect(table.updateTableOverrides(patch)).resolves.toEqual(
|
||||
merged,
|
||||
);
|
||||
// The native conversion and merge must not mutate the caller's object.
|
||||
expect(patch.job_types.compaction).toEqual({ enabled: false });
|
||||
expect(patch.custom.replace).toEqual([3]);
|
||||
await expect(table.resetTableOverrides()).resolves.toEqual({});
|
||||
},
|
||||
);
|
||||
expect(requests.map((request) => request.method)).toEqual([
|
||||
"GET",
|
||||
"GET",
|
||||
"PUT",
|
||||
"POST",
|
||||
]);
|
||||
for (const request of requests) {
|
||||
const url = new URL(request.url, "http://localhost");
|
||||
expect(url.pathname).toBe(
|
||||
request.method === "POST"
|
||||
? "/admin/table/overrides/reset"
|
||||
: "/admin/table/overrides",
|
||||
);
|
||||
if (request.method === "PUT") {
|
||||
expect(request.body).toEqual({
|
||||
db: "dev",
|
||||
namespace: namespacePath,
|
||||
table: "test",
|
||||
overrides: merged,
|
||||
});
|
||||
} else {
|
||||
expect(url.searchParams.get("db")).toBe("dev");
|
||||
expect(url.searchParams.get("table")).toBe("test");
|
||||
expect(url.searchParams.getAll("namespace")).toEqual(namespacePath);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.each([{}, { custom: { keep: true } }])(
|
||||
"only reads the snapshot %j for an empty update",
|
||||
async (current) => {
|
||||
const methods: string[] = [];
|
||||
await withMockDatabase(
|
||||
(req, res) => {
|
||||
if (req.url?.includes("/describe/")) {
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(JSON.stringify({ version: 1, schema: { fields: [] } }));
|
||||
return;
|
||||
}
|
||||
methods.push(req.method ?? "");
|
||||
res
|
||||
.writeHead(req.method === "GET" ? 200 : 400)
|
||||
.end(JSON.stringify(current));
|
||||
},
|
||||
async (db) => {
|
||||
const table = await db.openTable("test");
|
||||
await expect(table.updateTableOverrides({})).resolves.toEqual(
|
||||
current,
|
||||
);
|
||||
},
|
||||
);
|
||||
expect(methods).toEqual(["GET"]);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([null, [], true, 1, "bad"])(
|
||||
"rejects a non-object update %j before reading overrides",
|
||||
async (value) => {
|
||||
const requests: string[] = [];
|
||||
await withMockDatabase(
|
||||
(req, res) => {
|
||||
requests.push(req.url ?? "");
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(JSON.stringify({ version: 1, schema: { fields: [] } }));
|
||||
},
|
||||
async (db) => {
|
||||
const table = await db.openTable("test");
|
||||
await expect(
|
||||
table.updateTableOverrides(
|
||||
value as unknown as Record<string, unknown>,
|
||||
),
|
||||
).rejects.toThrow(/must be a JSON object/);
|
||||
},
|
||||
);
|
||||
expect(requests).toEqual(["/v1/table/test/describe/"]);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([403, 404])(
|
||||
"propagates GET failure %i without a PUT",
|
||||
async (status) => {
|
||||
const methods: string[] = [];
|
||||
await withMockDatabase(
|
||||
(req, res) => {
|
||||
if (req.url?.includes("/describe/")) {
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(JSON.stringify({ version: 1, schema: { fields: [] } }));
|
||||
} else {
|
||||
methods.push(req.method ?? "");
|
||||
res.writeHead(status).end("overrides unavailable");
|
||||
}
|
||||
},
|
||||
async (db) => {
|
||||
const table = await db.openTable("test");
|
||||
await expect(
|
||||
table.updateTableOverrides({ custom: true }),
|
||||
).rejects.toThrow(/overrides unavailable/);
|
||||
},
|
||||
);
|
||||
expect(methods).toEqual(["GET"]);
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects writes on historical and closed handles before HTTP", async () => {
|
||||
const requests: string[] = [];
|
||||
await withMockDatabase(
|
||||
(req, res) => {
|
||||
requests.push(req.url ?? "");
|
||||
res
|
||||
.writeHead(200)
|
||||
.end(JSON.stringify({ version: 1, schema: { fields: [] } }));
|
||||
},
|
||||
async (db) => {
|
||||
const table = await db.openTable("test");
|
||||
await table.checkout(1);
|
||||
requests.length = 0;
|
||||
await expect(
|
||||
table.updateTableOverrides({ custom: true }),
|
||||
).rejects.toThrow(/fixed at version 1/);
|
||||
await expect(table.resetTableOverrides()).rejects.toThrow(
|
||||
/fixed at version 1/,
|
||||
);
|
||||
table.close();
|
||||
await expect(table.getTableOverrides()).rejects.toThrow(/closed/);
|
||||
await expect(
|
||||
table.updateTableOverrides({ custom: true }),
|
||||
).rejects.toThrow(/closed/);
|
||||
await expect(table.resetTableOverrides()).rejects.toThrow(/closed/);
|
||||
},
|
||||
);
|
||||
expect(requests).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5886,3 +5886,49 @@ describe("durable compaction validation", () => {
|
||||
).rejects.toThrow("safe integer");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Enterprise table overrides", () => {
|
||||
it("keeps override methods optional for existing custom Table subclasses", async () => {
|
||||
// Declare the old Table surface; only the new methods must be inherited.
|
||||
// Keeping these override methods abstract must fail type checking here.
|
||||
// biome-ignore lint/correctness/noUnusedVariables: declaration merging supplies the legacy members
|
||||
interface LegacyTable
|
||||
extends Omit<
|
||||
Table,
|
||||
"getTableOverrides" | "updateTableOverrides" | "resetTableOverrides"
|
||||
> {}
|
||||
class LegacyTable extends Table {}
|
||||
|
||||
const table = new LegacyTable();
|
||||
await expect(table.getTableOverrides()).rejects.toThrow(
|
||||
"getTableOverrides is not supported on this table type",
|
||||
);
|
||||
await expect(table.updateTableOverrides({ custom: true })).rejects.toThrow(
|
||||
"updateTableOverrides is not supported on this table type",
|
||||
);
|
||||
await expect(table.resetTableOverrides()).rejects.toThrow(
|
||||
"resetTableOverrides is not supported on this table type",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects get, update, and reset on local tables", async () => {
|
||||
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
|
||||
const db = await connect(tmpDir.name);
|
||||
const table = await db.createTable("items", [{ id: 1 }]);
|
||||
try {
|
||||
await expect(table.getTableOverrides()).rejects.toThrow(
|
||||
/only on remote Enterprise tables/,
|
||||
);
|
||||
await expect(
|
||||
table.updateTableOverrides({ custom: true }),
|
||||
).rejects.toThrow(/only on remote Enterprise tables/);
|
||||
await expect(table.resetTableOverrides()).rejects.toThrow(
|
||||
/only on remote Enterprise tables/,
|
||||
);
|
||||
} finally {
|
||||
table.close();
|
||||
db.close();
|
||||
tmpDir.removeCallback();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { Connection, Table, connect } from "../lancedb";
|
||||
|
||||
// Run against an isolated Enterprise deployment:
|
||||
// LANCEDB_TABLE_OVERRIDES_E2E=1 LANCEDB_URI=db://test \
|
||||
// LANCEDB_HOST_OVERRIDE=http://localhost:10024 LANCEDB_API_KEY=... \
|
||||
// pnpm test __test__/table_overrides_integration.test.ts --runInBand
|
||||
const maybeDescribe = process.env.LANCEDB_TABLE_OVERRIDES_E2E
|
||||
? describe
|
||||
: describe.skip;
|
||||
|
||||
maybeDescribe("table overrides live integration", () => {
|
||||
let db: Connection;
|
||||
const namespace = [`overrides_${randomUUID().replaceAll("-", "_")}`];
|
||||
const nested = [...namespace, "nested"];
|
||||
const uri = process.env.LANCEDB_URI ?? "db://test";
|
||||
const host = process.env.LANCEDB_HOST_OVERRIDE;
|
||||
const apiKey = process.env.LANCEDB_API_KEY;
|
||||
|
||||
beforeAll(async () => {
|
||||
if (!host || !apiKey) {
|
||||
throw new Error(
|
||||
"Set LANCEDB_HOST_OVERRIDE and LANCEDB_API_KEY for live tests",
|
||||
);
|
||||
}
|
||||
db = await connect(uri, { apiKey, hostOverride: host });
|
||||
await db.createNamespace(namespace);
|
||||
await db.createNamespace(nested);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (db) {
|
||||
try {
|
||||
await db.dropNamespace(nested);
|
||||
await db.dropNamespace(namespace);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async function withTable(
|
||||
namespacePath: string[],
|
||||
run: (table: Table, name: string) => Promise<void>,
|
||||
) {
|
||||
const name = `items_${randomUUID().replaceAll("-", "_")}`;
|
||||
const table = await db.createTable(name, [{ id: 1 }], namespacePath);
|
||||
try {
|
||||
await run(table, name);
|
||||
} finally {
|
||||
table.close();
|
||||
await db.dropTable(name, namespacePath);
|
||||
}
|
||||
}
|
||||
|
||||
it.each([
|
||||
{ label: "root", path: [] },
|
||||
{ label: "nested", path: nested },
|
||||
])(
|
||||
"merges, replaces via REST, and resets in $label namespace",
|
||||
async ({ path }) => {
|
||||
await withTable(path, async (table, name) => {
|
||||
await expect(table.getTableOverrides()).resolves.toEqual({});
|
||||
await expect(table.updateTableOverrides({})).resolves.toEqual({});
|
||||
const initial = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: {
|
||||
compaction: { enabled: false },
|
||||
cleanup: { enabled: false },
|
||||
},
|
||||
custom: { keep: "測試", values: [1, 2], nullable: "old" },
|
||||
};
|
||||
await expect(table.updateTableOverrides(initial)).resolves.toEqual(
|
||||
initial,
|
||||
);
|
||||
const patch = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: { compaction: { enabled: true } },
|
||||
custom: { values: [3], nullable: null },
|
||||
};
|
||||
const expected = {
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
job_types: {
|
||||
compaction: { enabled: true },
|
||||
cleanup: { enabled: false },
|
||||
},
|
||||
custom: { keep: "測試", values: [3], nullable: null },
|
||||
};
|
||||
await expect(table.updateTableOverrides(patch)).resolves.toEqual(
|
||||
expected,
|
||||
);
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(expected);
|
||||
await expect(table.updateTableOverrides({})).resolves.toEqual(expected);
|
||||
|
||||
const replacement = { custom: { replacement: true } };
|
||||
const response = await fetch(`${host}/admin/table/overrides`, {
|
||||
method: "PUT",
|
||||
headers: { "x-api-key": apiKey!, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
db: new URL(uri).hostname,
|
||||
namespace: path,
|
||||
table: name,
|
||||
overrides: replacement,
|
||||
}),
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
expect(await response.json()).toEqual(replacement);
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(replacement);
|
||||
await expect(table.resetTableOverrides()).resolves.toEqual({});
|
||||
await expect(table.getTableOverrides()).resolves.toEqual({});
|
||||
await expect(table.countRows()).resolves.toBe(1);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("isolates branch updates and resets from main", async () => {
|
||||
await withTable(nested, async (table) => {
|
||||
const initial = { custom: { main: true } };
|
||||
await table.updateTableOverrides(initial);
|
||||
const branch = await (await table.branches()).create("experiment");
|
||||
try {
|
||||
const expected = { custom: { main: true, branch: true } };
|
||||
await expect(
|
||||
branch.updateTableOverrides({ custom: { branch: true } }),
|
||||
).resolves.toEqual(expected);
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(initial);
|
||||
await expect(branch.resetTableOverrides()).resolves.toEqual({});
|
||||
await expect(branch.getTableOverrides()).resolves.toEqual({});
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(initial);
|
||||
} finally {
|
||||
branch.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("propagates server validation errors without changing stored overrides", async () => {
|
||||
await withTable(nested, async (table) => {
|
||||
const initial = { custom: { keep: true } };
|
||||
await table.updateTableOverrides(initial);
|
||||
await expect(
|
||||
table.updateTableOverrides({
|
||||
// biome-ignore lint/style/useNamingConvention: server wire format
|
||||
auto_refresh_interval_seconds: -1,
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
await expect(table.getTableOverrides()).resolves.toEqual(initial);
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects writes at a fixed version", async () => {
|
||||
await withTable(nested, async (table) => {
|
||||
await table.checkout(1);
|
||||
await expect(
|
||||
table.updateTableOverrides({ custom: true }),
|
||||
).rejects.toThrow(/fixed at version/);
|
||||
await expect(table.resetTableOverrides()).rejects.toThrow(
|
||||
/fixed at version/,
|
||||
);
|
||||
await table.checkoutLatest();
|
||||
await expect(table.getTableOverrides()).resolves.toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1106,6 +1106,56 @@ export abstract class Table {
|
||||
* @returns {Promise<RTEWriteSpec | undefined>}
|
||||
*/
|
||||
abstract getRTESpec(): Promise<RTEWriteSpec | undefined>;
|
||||
/**
|
||||
* Read the stored overrides for a remote Enterprise table.
|
||||
*
|
||||
* Overrides configure background behavior such as automatic compaction,
|
||||
* cleanup, and reindex execution. Keys use the server's snake_case names.
|
||||
* Returns an empty object when no overrides are set. Local tables are unsupported.
|
||||
* Custom table implementations inherit a default rejection unless they override
|
||||
* this method.
|
||||
*/
|
||||
async getTableOverrides(): Promise<Record<string, unknown>> {
|
||||
throw new Error("getTableOverrides is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively merge overrides into a remote Enterprise table's stored overrides.
|
||||
*
|
||||
* Fetches the current overrides, 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 {@link Table.resetTableOverrides} to clear all overrides.
|
||||
* Local tables and custom table implementations without override support reject
|
||||
* this operation.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* await table.updateTableOverrides({
|
||||
* job_types: { compaction: { enabled: false } },
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
async updateTableOverrides(
|
||||
// biome-ignore lint/correctness/noUnusedVariables: retain the public parameter name in generated docs
|
||||
overrides: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> {
|
||||
throw new Error("updateTableOverrides is not supported on this table type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear a remote Enterprise table's overrides and return an empty object.
|
||||
* Local tables and custom table implementations without override support reject
|
||||
* this operation.
|
||||
*/
|
||||
async resetTableOverrides(): Promise<Record<string, unknown>> {
|
||||
throw new Error("resetTableOverrides is not supported on this table type");
|
||||
}
|
||||
/**
|
||||
* Drain and close any cached RTE shard writers held for this table.
|
||||
*
|
||||
@@ -1916,6 +1966,20 @@ export class LocalTable extends Table {
|
||||
| undefined;
|
||||
}
|
||||
|
||||
async getTableOverrides(): Promise<Record<string, unknown>> {
|
||||
return await this.inner.getTableOverrides();
|
||||
}
|
||||
|
||||
async updateTableOverrides(
|
||||
overrides: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> {
|
||||
return await this.inner.updateTableOverrides(overrides);
|
||||
}
|
||||
|
||||
async resetTableOverrides(): Promise<Record<string, unknown>> {
|
||||
return await this.inner.resetTableOverrides();
|
||||
}
|
||||
|
||||
async closeRTEWriters(): Promise<void> {
|
||||
return await this.inner.closeRTEWriters();
|
||||
}
|
||||
|
||||
@@ -821,6 +821,37 @@ impl Table {
|
||||
Ok(spec.map(RTEWriteSpec::from))
|
||||
}
|
||||
|
||||
#[napi(catch_unwind, ts_return_type = "Promise<Record<string, unknown>>")]
|
||||
pub async fn get_table_overrides(&self) -> napi::Result<serde_json::Value> {
|
||||
self.inner_ref()?
|
||||
.get_table_overrides()
|
||||
.await
|
||||
.default_error()
|
||||
}
|
||||
|
||||
#[napi(
|
||||
catch_unwind,
|
||||
ts_args_type = "overrides: Record<string, unknown>",
|
||||
ts_return_type = "Promise<Record<string, unknown>>"
|
||||
)]
|
||||
pub async fn update_table_overrides(
|
||||
&self,
|
||||
overrides: serde_json::Value,
|
||||
) -> napi::Result<serde_json::Value> {
|
||||
self.inner_ref()?
|
||||
.update_table_overrides(overrides)
|
||||
.await
|
||||
.default_error()
|
||||
}
|
||||
|
||||
#[napi(catch_unwind, ts_return_type = "Promise<Record<string, unknown>>")]
|
||||
pub async fn reset_table_overrides(&self) -> napi::Result<serde_json::Value> {
|
||||
self.inner_ref()?
|
||||
.reset_table_overrides()
|
||||
.await
|
||||
.default_error()
|
||||
}
|
||||
|
||||
#[napi(catch_unwind, js_name = "closeRTEWriters")]
|
||||
pub async fn close_rte_writers(&self) -> napi::Result<()> {
|
||||
self.inner_ref()?.close_rte_writers().await.default_error()
|
||||
|
||||
Reference in New Issue
Block a user