mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-29 14:56:51 +00:00
Remote materialized-view refreshes drained the SQL result and then fabricated `mode=rebuild`, `rows_written=<total view rows>`, and `source_version=0`. This made incremental refreshes report incorrect statistics. Make these three result fields optional across Rust, Python, and TypeScript, and leave them unset for remote SQL refreshes. Remove the post-refresh row count request. Local refreshes continue to populate all metrics, and `version` remains the view version observed after completion. Remote per-operation statistics remain unavailable until the SQL job adapter can retrieve them. Expose the forced-rebuild option through the Rust builder, synchronous/asynchronous Python refresh and job methods, and TypeScript refresh. Remote `full=true` sends `WITH (full = true)`; the default lets the server choose its refresh mode. Local source-version pinning and remote job cancellation are preserved. Local refresh regression tests respect the `experimental-functions` build gate, while remote refresh coverage also runs in default builds. Validation: - Rust: 20 materialized-view tests and 21 remote SQL tests passed with `remote,experimental-functions`; `cargo fmt --all` passed. - Python: rebuilt the extension in the uv-managed environment. The default build passed 23 tests with 27 feature-dependent skips. The opt-in build passed 61 tests with 3 expected default-build-only skips, covering the materialized-view suite and 14 async query-serialization cases from current main. Python Ruff lint and formatting checks passed. - TypeScript: default and opt-in native builds and TypeScript compilation passed. The default build passed 5 tests with 10 expected feature-dependent skips; the opt-in build passed all 14 materialized-view tests with the default-build-only test skipped. Lint/format checks and documentation generation passed. - Repository-wide `ruff check .` reports 48 pre-existing violations in unchanged `docs/web-tests` files; `ruff check python` passes. Fixes #4670 <!-- lance-gatekeeper-fix:v1 agent=ece43ec4b88a48d1789f60005fa3ecfc generation=1 --> Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com> Co-authored-by: Xuanwo <github@xuanwo.io>
208 lines
6.7 KiB
TypeScript
208 lines
6.7 KiB
TypeScript
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
import { RefreshMaterializedViewResult } from "./native";
|
|
import { Table } from "./table";
|
|
|
|
/** Schema metadata key holding a materialized view's definition. */
|
|
export const DEFINITION_META_KEY = "mv.definition";
|
|
|
|
/**
|
|
* The newest stored layout this version reads: `{"format": N, "query": "<SQL>"}`,
|
|
* format 2 being a query with `GROUP BY`.
|
|
*/
|
|
export const DEFINITION_FORMAT = 2;
|
|
|
|
/**
|
|
* The query that defines a materialized view, as stored:
|
|
* `SELECT columns FROM [ns.]table [, function(args) AS alias | , UNNEST(column) AS alias]
|
|
* [WHERE predicate] [GROUP BY expr, ...] [LIMIT n]`. A Function in `FROM`
|
|
* position yields one row per element it returns.
|
|
*/
|
|
export interface MaterializedViewDefinition {
|
|
/** The defining query, in the canonical spelling the server stores. */
|
|
query: string;
|
|
}
|
|
|
|
/**
|
|
* The view's columns: column names, `[alias, SQL expression]` pairs, or a
|
|
* record of the same. A bare name projects itself.
|
|
*/
|
|
export type MaterializedViewSelect =
|
|
| (string | [string, string])[]
|
|
| Record<string, string>;
|
|
|
|
/**
|
|
* @internal Reject a numeric option N-API would otherwise silently coerce:
|
|
* `Infinity` reaches Rust as 0, `1.5` as 1.
|
|
*/
|
|
export function validateNonNegativeInteger(
|
|
value: number | undefined,
|
|
name: string,
|
|
): void {
|
|
if (value !== undefined && !(Number.isSafeInteger(value) && value >= 0)) {
|
|
throw new Error(`${name} must be a non-negative integer`);
|
|
}
|
|
}
|
|
|
|
/** @internal Quote a column name as a Lance SQL identifier (backticks). */
|
|
function quoteIdentifier(name: string): string {
|
|
return "`" + name.replace(/`/g, "``") + "`";
|
|
}
|
|
|
|
/**
|
|
* @internal Normalize a select argument into `[alias, expression]` pairs.
|
|
* A bare name projects itself and is quoted, so any valid column name works;
|
|
* pair and record entries are kept verbatim because their right side is an
|
|
* expression.
|
|
*/
|
|
export function normalizeSelect(
|
|
select?: MaterializedViewSelect,
|
|
): [string, string][] | undefined {
|
|
if (select === undefined) {
|
|
return undefined;
|
|
}
|
|
if (Array.isArray(select)) {
|
|
return select.map((item) =>
|
|
typeof item === "string" ? [item, quoteIdentifier(item)] : item,
|
|
);
|
|
}
|
|
return Object.entries(select);
|
|
}
|
|
|
|
/** @internal Parse a definition off a table's stored schema metadata. */
|
|
export function definitionFromMetadata(
|
|
metadata: Map<string, string>,
|
|
name: string,
|
|
): MaterializedViewDefinition {
|
|
const raw = metadata.get(DEFINITION_META_KEY);
|
|
if (raw === undefined) {
|
|
throw new Error(`Table '${name}' is not a materialized view`);
|
|
}
|
|
return definitionFromJson(raw, name);
|
|
}
|
|
|
|
/** @internal Parse the backend-independent definition returned by native code. */
|
|
export function definitionFromJson(
|
|
raw: string,
|
|
name: string,
|
|
): MaterializedViewDefinition {
|
|
// biome-ignore lint/suspicious/noExplicitAny: raw JSON
|
|
const value: any = JSON.parse(raw);
|
|
if (value.format !== undefined) {
|
|
// A newer writer's layout is reported, never guessed at.
|
|
if (!Number.isInteger(value.format) || value.format > DEFINITION_FORMAT) {
|
|
throw new Error(
|
|
`materialized view '${name}' is stored in format ${value.format}, ` +
|
|
"which this version of lancedb cannot refresh",
|
|
);
|
|
}
|
|
return { query: value.query };
|
|
}
|
|
// The structured layout written before the format number.
|
|
if (value.kind !== "select" && value.kind !== "namespaced_select") {
|
|
throw new Error(
|
|
`materialized view '${name}' is stored in format kind '${value.kind}', ` +
|
|
"which this version of lancedb cannot refresh",
|
|
);
|
|
}
|
|
const limit = value.limit ?? undefined;
|
|
// JSON.parse rounds integers past 2^53; every exact u64 parses to a safe
|
|
// integer and every rounded one does not, so this rejects precisely the
|
|
// values a number cannot carry.
|
|
if (limit !== undefined && !Number.isSafeInteger(limit)) {
|
|
throw new Error(
|
|
`materialized view '${name}' has a stored limit too large to represent exactly`,
|
|
);
|
|
}
|
|
return { query: legacyQuery(value, limit) };
|
|
}
|
|
|
|
function legacyIdent(name: string): string {
|
|
return /^[a-z_][a-z0-9_]*$/.test(name)
|
|
? name
|
|
: `\`${name.replace(/`/g, "``")}\``;
|
|
}
|
|
|
|
/** Render the pre-format structured layout as the query it described. */
|
|
// biome-ignore lint/suspicious/noExplicitAny: raw JSON
|
|
function legacyQuery(value: any, limit: number | undefined): string {
|
|
// biome-ignore lint/suspicious/noExplicitAny: raw JSON
|
|
const projections: any[] = value.projections ?? [];
|
|
const columns =
|
|
projections.length === 0
|
|
? "*"
|
|
: projections
|
|
.map((p) =>
|
|
p.expression === p.output || p.expression === `\`${p.output}\``
|
|
? p.expression
|
|
: `${p.expression} AS ${legacyIdent(p.output)}`,
|
|
)
|
|
.join(", ");
|
|
const table = [...(value.source_namespace ?? []), value.source_table]
|
|
.map(legacyIdent)
|
|
.join(".");
|
|
let query = `SELECT ${columns} FROM ${table}`;
|
|
if (value.filter !== undefined && value.filter !== null) {
|
|
query += ` WHERE ${value.filter}`;
|
|
}
|
|
if (limit !== undefined) {
|
|
query += ` LIMIT ${limit}`;
|
|
}
|
|
return query;
|
|
}
|
|
|
|
/**
|
|
* A handle on a materialized view: its table plus its definition.
|
|
*
|
|
* Obtained from {@link Connection#createMaterializedView} or
|
|
* {@link Connection#openMaterializedView}. The view is a normal table --
|
|
* queries, indexes and search all apply through {@link MaterializedView#table}
|
|
* -- whose contents are maintained by {@link MaterializedView#refresh}.
|
|
*/
|
|
export class MaterializedView {
|
|
private readonly inner: Table;
|
|
|
|
constructor(table: Table) {
|
|
this.inner = table;
|
|
}
|
|
|
|
get name(): string {
|
|
return this.inner.name;
|
|
}
|
|
|
|
/** The view, as the table it is. */
|
|
table(): Table {
|
|
return this.inner;
|
|
}
|
|
|
|
/** The query that defines the view. */
|
|
async definition(): Promise<MaterializedViewDefinition> {
|
|
return definitionFromJson(
|
|
await this.inner.materializedViewDefinition(),
|
|
this.name,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Recompute the view from its source.
|
|
* Local refresh requires a source build with `experimental-functions`.
|
|
*
|
|
* `full: true` forces a remote rebuild; otherwise the server chooses the
|
|
* refresh mode. Local views always rebuild, and `sourceVersion` selects a
|
|
* source version instead of the latest. Remote SQL refreshes do not support
|
|
* source-version pinning. Their results leave `mode`, `rowsWritten`, and
|
|
* `sourceVersion` absent because the SQL job adapter does not expose these metrics.
|
|
*/
|
|
async refresh(options?: {
|
|
full?: boolean;
|
|
sourceVersion?: number;
|
|
}): Promise<RefreshMaterializedViewResult> {
|
|
validateNonNegativeInteger(options?.sourceVersion, "sourceVersion");
|
|
return await this.inner.refreshMaterializedView(
|
|
options?.sourceVersion,
|
|
options?.full,
|
|
);
|
|
}
|
|
}
|