fix(grid): show column comments for grouped query results
This commit is contained in:
1 parent
4c260b1a2f
commit
8271ba2a71
8 files changed
+1220
-8
No files matched your search
@@ -0,0 +1,156 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { analyzeEditableQueryEditability, analyzeSelectStructureForDisplay, resolveSourceColumnsByOrdinal, type EditableQueryInfo, type EditableQuerySource } from "@/lib/sql/sqlAnalysis";
|
||||
|
||||
/**
|
||||
* Mirrors queryStore.editableQuerySources: single-source analyses store the
|
||||
* source on the analysis root, so tests normalize it exactly like the store
|
||||
* before resolving result columns by ordinal.
|
||||
*/
|
||||
function displayTableSources(analysis: EditableQueryInfo, columns: readonly string[]): Array<{ source: EditableQuerySource; columns: { name: string }[] }> {
|
||||
const source: EditableQuerySource = analysis.sources?.length
|
||||
? analysis.sources[0]!
|
||||
: {
|
||||
key: `${analysis.tableAlias ?? analysis.tableName}:0`,
|
||||
catalog: analysis.catalog,
|
||||
catalogQuoted: analysis.catalogQuoted,
|
||||
schema: analysis.schema,
|
||||
schemaQuoted: analysis.schemaQuoted,
|
||||
tableName: analysis.tableName,
|
||||
tableNameQuoted: analysis.tableNameQuoted,
|
||||
alias: analysis.tableAlias,
|
||||
};
|
||||
return [{ source, columns: columns.map((name) => ({ name })) }];
|
||||
}
|
||||
|
||||
/**
|
||||
* GROUP BY / HAVING queries stay read-only (`aggregation`) but still expose a
|
||||
* display-only structural analysis so the result grid can resolve comments for
|
||||
* result columns that map directly back to a base-table column. Aggregate,
|
||||
* computed and ambiguous projections must stay unresolved (no guessed comment).
|
||||
*/
|
||||
describe("grouped query display column mapping", () => {
|
||||
describe("display structure analysis", () => {
|
||||
it("parses projections and sources for a GROUP BY query without making it editable", () => {
|
||||
const editability = analyzeEditableQueryEditability("SELECT department, COUNT(*) AS total FROM users GROUP BY department");
|
||||
expect(editability).toEqual({ editable: false, reason: "aggregation" });
|
||||
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT department, COUNT(*) AS total FROM users GROUP BY department");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
expect(analysis.tableName).toBe("users");
|
||||
expect(analysis.columns.map((column) => column.resultName)).toEqual(["department", "total"]);
|
||||
expect(analysis.columns[0]).toMatchObject({ sourceName: "department" });
|
||||
// Aggregate expression has no direct source column.
|
||||
expect(analysis.columns[1]?.sourceName).toBeUndefined();
|
||||
});
|
||||
|
||||
it("parses HAVING queries without an explicit GROUP BY", () => {
|
||||
const editability = analyzeEditableQueryEditability("SELECT department, COUNT(*) AS total FROM users GROUP BY department HAVING COUNT(*) > 1");
|
||||
expect(editability).toEqual({ editable: false, reason: "aggregation" });
|
||||
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT department, COUNT(*) AS total FROM users GROUP BY department HAVING COUNT(*) > 1");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
expect(analysis.columns.map((column) => column.resultName)).toEqual(["department", "total"]);
|
||||
});
|
||||
|
||||
it("parses multi-source grouped queries with per-source qualifiers", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT u.department, COUNT(o.id) AS order_count FROM users u JOIN orders o ON o.user_id = u.id GROUP BY u.department");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
expect(analysis.sources!.map((source) => source.tableName)).toEqual(["users", "orders"]);
|
||||
expect(analysis.columns.map((column) => column.resultName)).toEqual(["department", "order_count"]);
|
||||
expect(analysis.columns[0]?.sourceKey).toBe("u:0");
|
||||
});
|
||||
|
||||
it("returns null for unparseable / dangerous display shapes", () => {
|
||||
expect(analyzeSelectStructureForDisplay("WITH x AS (SELECT 1) SELECT * FROM x")).toBeNull();
|
||||
expect(analyzeSelectStructureForDisplay("SELECT a FROM (SELECT 1 AS a) t")).toBeNull();
|
||||
expect(analyzeSelectStructureForDisplay("INSERT INTO t VALUES (1)")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ordinal resolution", () => {
|
||||
it("maps direct physical columns and leaves aggregate expressions unresolved (T1)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT department, COUNT(*) AS total FROM users GROUP BY department");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal("mysql", analysis, displayTableSources(analysis, ["department", "id"]), 2);
|
||||
expect(resolved).toEqual([{ sourceKey: "users:0", sourceColumn: "department" }, undefined]);
|
||||
});
|
||||
|
||||
it("resolves aliased physical columns back to the base column (T2)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT department AS dept, COUNT(*) AS total FROM users GROUP BY department");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal("mysql", analysis, displayTableSources(analysis, ["department", "id"]), 2);
|
||||
expect(resolved).toEqual([{ sourceKey: "users:0", sourceColumn: "department" }, undefined]);
|
||||
});
|
||||
|
||||
it("maps multiple grouped physical columns (T5)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT department, status, COUNT(*) FROM users GROUP BY department, status");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal("mysql", analysis, displayTableSources(analysis, ["department", "status", "id"]), 3);
|
||||
expect(resolved).toEqual([{ sourceKey: "users:0", sourceColumn: "department" }, { sourceKey: "users:0", sourceColumn: "status" }, undefined]);
|
||||
});
|
||||
|
||||
it("never lets an aggregate expression inherit its operand's comment (T6/T7)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT DATE(created_at) AS day, SUM(amount) AS total_amount FROM orders GROUP BY DATE(created_at)");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal("mysql", analysis, displayTableSources(analysis, ["created_at", "amount", "id"]), 2);
|
||||
expect(resolved).toEqual([undefined, undefined]);
|
||||
});
|
||||
|
||||
it("resolves qualified grouped columns across joined sources (T4) and keeps aggregates unresolved", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT u.department, COUNT(o.id) AS order_count FROM users u JOIN orders o ON o.user_id = u.id GROUP BY u.department");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const usersSource = analysis.sources![0]!;
|
||||
const ordersSource = analysis.sources![1]!;
|
||||
const resolved = resolveSourceColumnsByOrdinal(
|
||||
"mysql",
|
||||
analysis,
|
||||
[
|
||||
{ source: usersSource, columns: [{ name: "id" }, { name: "department" }] },
|
||||
{ source: ordersSource, columns: [{ name: "id" }, { name: "user_id" }, { name: "amount" }] },
|
||||
],
|
||||
2,
|
||||
);
|
||||
expect(resolved).toEqual([{ sourceKey: "u:0", sourceColumn: "department" }, undefined]);
|
||||
});
|
||||
|
||||
it("keeps quoted identifier semantics for grouped columns (T8)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay('SELECT "ID", COUNT(*) AS total FROM users GROUP BY "ID"');
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal("postgres", analysis, displayTableSources(analysis, ["id", "ID"]), 2);
|
||||
// Quoted "ID" matches the physical quoted column exactly; unquoted id stays distinct.
|
||||
expect(resolved).toEqual([{ sourceKey: "users:0", sourceColumn: "ID" }, undefined]);
|
||||
});
|
||||
|
||||
it("leaves ambiguous unqualified grouped columns unresolved (T9)", () => {
|
||||
const analysis = analyzeSelectStructureForDisplay("SELECT id, COUNT(*) AS total FROM users u JOIN orders o ON o.user_id = u.id GROUP BY id");
|
||||
expect(analysis).not.toBeNull();
|
||||
if (!analysis) return;
|
||||
|
||||
const resolved = resolveSourceColumnsByOrdinal(
|
||||
"mysql",
|
||||
analysis,
|
||||
[
|
||||
{ source: analysis.sources![0]!, columns: [{ name: "id" }, { name: "department" }] },
|
||||
{ source: analysis.sources![1]!, columns: [{ name: "id" }, { name: "user_id" }] },
|
||||
],
|
||||
2,
|
||||
);
|
||||
expect(resolved).toEqual([undefined, undefined]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,186 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ColumnInfo } from "@/types/database";
|
||||
|
||||
/**
|
||||
* Trigger-count regression suite for the columns facet of table metadata.
|
||||
*
|
||||
* These assert the public contract the display-only (grouped-query comment)
|
||||
* loader depends on, so a later refactor of the cache internals keeps the
|
||||
* behaviour stable:
|
||||
*
|
||||
* - columns-only loads issue `getColumns` but never `listIndexes`;
|
||||
* - cold vs warm request counts;
|
||||
* - in-flight deduplication for concurrent same-table loads;
|
||||
* - full<->columns interoperability (no duplicate `getColumns`);
|
||||
* - invalidation and per-table isolation.
|
||||
*/
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getColumns: vi.fn(),
|
||||
listIndexes: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
getColumns: mocks.getColumns,
|
||||
listIndexes: mocks.listIndexes,
|
||||
}));
|
||||
|
||||
import { clearTableMetadataCache, getCachedTableMetadata, invalidateTableMetadataCache, loadTableColumns, loadTableMetadata, TABLE_METADATA_CACHE_TTL_MS } from "@/lib/metadata/tableMetadataCache";
|
||||
|
||||
// The coordinator starts its loader on a microtask (Promise.resolve().then), so
|
||||
// flush before asserting remote call counts.
|
||||
async function flush() {
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
}
|
||||
|
||||
function column(name: string): ColumnInfo {
|
||||
return { name, data_type: "integer", is_nullable: false, column_default: null, is_primary_key: true, extra: null };
|
||||
}
|
||||
|
||||
const usersRequest = { connectionId: "c1", database: "db", schema: "public", tableName: "users", databaseType: "postgres" } as const;
|
||||
const ordersRequest = { connectionId: "c1", database: "db", schema: "public", tableName: "orders", databaseType: "postgres" } as const;
|
||||
const paymentsRequest = { connectionId: "c1", database: "db", schema: "public", tableName: "payments", databaseType: "postgres" } as const;
|
||||
|
||||
function callCount(mock: ReturnType<typeof vi.fn>): number {
|
||||
return mock.mock.calls.length;
|
||||
}
|
||||
|
||||
describe("tableMetadataCache columns facet request counts", () => {
|
||||
beforeEach(() => {
|
||||
clearTableMetadataCache();
|
||||
vi.clearAllMocks();
|
||||
mocks.getColumns.mockImplementation(async (_c: string, _d: string, _s: string, table: string) => [column(table)]);
|
||||
mocks.listIndexes.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
it("R1 — cold single-source columns load: 1 getColumns, 0 listIndexes", async () => {
|
||||
const result = await loadTableColumns({ ...usersRequest });
|
||||
expect(result.columns[0]?.name).toBe("users");
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
});
|
||||
|
||||
it("R2 — warm repeat of the same columns load: 0 additional remote calls", async () => {
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
});
|
||||
|
||||
it("R3 — cold multi-source columns loads: N getColumns, 0 listIndexes", async () => {
|
||||
await Promise.all([loadTableColumns({ ...usersRequest }), loadTableColumns({ ...ordersRequest }), loadTableColumns({ ...paymentsRequest })]);
|
||||
expect(callCount(mocks.getColumns)).toBe(3);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
});
|
||||
|
||||
it("R5 — concurrent same-source columns loads dedupe to one getColumns", async () => {
|
||||
let releaseFirst: (value: ColumnInfo[]) => void = () => {};
|
||||
mocks.getColumns.mockReturnValueOnce(
|
||||
new Promise<ColumnInfo[]>((resolve) => {
|
||||
releaseFirst = resolve;
|
||||
}),
|
||||
);
|
||||
const first = loadTableColumns({ ...usersRequest });
|
||||
const second = loadTableColumns({ ...usersRequest });
|
||||
await flush();
|
||||
// The second call must join the first's in-flight load, not start another.
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
releaseFirst([column("id")]);
|
||||
const [a, b] = await Promise.all([first, second]);
|
||||
expect(a.columns[0]?.name).toBe("id");
|
||||
expect(b.columns[0]?.name).toBe("id");
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
});
|
||||
|
||||
it("R5b — concurrent full metadata and columns-only loads share the columns call", async () => {
|
||||
// loadTableMetadata starts its columns facet synchronously, so a concurrent
|
||||
// display-only columns load dedupes against it: exactly one getColumns.
|
||||
let releaseColumns: (value: ColumnInfo[]) => void = () => {};
|
||||
mocks.getColumns.mockReturnValueOnce(
|
||||
new Promise<ColumnInfo[]>((resolve) => {
|
||||
releaseColumns = resolve;
|
||||
}),
|
||||
);
|
||||
const full = loadTableMetadata({ ...usersRequest });
|
||||
const colsOnly = loadTableColumns({ ...usersRequest });
|
||||
await flush();
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
releaseColumns([column("id")]);
|
||||
const [fullResult, colsResult] = await Promise.all([full, colsOnly]);
|
||||
expect(fullResult.metadata.columns[0]?.name).toBe("id");
|
||||
expect(colsResult.columns[0]?.name).toBe("id");
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
// listIndexes is only needed by the full load, and only once.
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
});
|
||||
|
||||
it("R6 — full metadata first, then columns-only: 0 additional getColumns / 0 listIndexes", async () => {
|
||||
await loadTableMetadata({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
});
|
||||
|
||||
it("R7 — columns-only first, then full metadata: 0 additional getColumns, 1 listIndexes", async () => {
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
await loadTableMetadata({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
});
|
||||
|
||||
it("R7b — promoting near-expiry columns does not reset the full metadata TTL", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
vi.setSystemTime(new Date("2026-08-18T00:00:00Z"));
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
vi.advanceTimersByTime(TABLE_METADATA_CACHE_TTL_MS - 100);
|
||||
|
||||
await loadTableMetadata({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
|
||||
vi.advanceTimersByTime(200);
|
||||
expect(getCachedTableMetadata({ ...usersRequest })).toBeUndefined();
|
||||
|
||||
await loadTableMetadata({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(2);
|
||||
expect(callCount(mocks.listIndexes)).toBe(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("R8 — invalidating a table forces the next columns load to re-query", async () => {
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
invalidateTableMetadataCache({ connectionId: "c1", database: "db", tableName: "users" });
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(2);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
});
|
||||
|
||||
it("R9 — caching one table does not satisfy a different table", async () => {
|
||||
await loadTableColumns({ ...usersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(1);
|
||||
await loadTableColumns({ ...ordersRequest });
|
||||
expect(callCount(mocks.getColumns)).toBe(2);
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
});
|
||||
|
||||
it("R10 — a failing columns-only load rejects and never triggers listIndexes", async () => {
|
||||
mocks.getColumns.mockRejectedValue(new Error("metadata unavailable"));
|
||||
await expect(loadTableColumns({ ...usersRequest })).rejects.toThrow("metadata unavailable");
|
||||
expect(callCount(mocks.listIndexes)).toBe(0);
|
||||
// A subsequent load is not poisoned by the failed attempt.
|
||||
mocks.getColumns.mockResolvedValueOnce([column("id")]);
|
||||
const retry = await loadTableColumns({ ...usersRequest });
|
||||
expect(retry.columns[0]?.name).toBe("id");
|
||||
});
|
||||
});
|
||||
@@ -51,7 +51,7 @@ export class MetadataResultCache<T> {
|
||||
private readonly now: () => number;
|
||||
|
||||
constructor(private readonly options: MetadataResultCacheOptions) {
|
||||
this.now = options.now ?? Date.now;
|
||||
this.now = options.now ?? (() => Date.now());
|
||||
}
|
||||
|
||||
get(scope: MetadataScopeInput, options?: { allowStale?: boolean }): MetadataCacheHit<T> | undefined {
|
||||
|
||||
@@ -39,6 +39,31 @@ export interface TableMetadataLoadResult {
|
||||
ageMs: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Columns-only table metadata. Used by read-only/display consumers (e.g. query
|
||||
* result column comments for grouped results) that must not pay for index
|
||||
* discovery. Shares the same scope/TTL/invalidation contract as the full table
|
||||
* metadata cache, and the full loader reuses this column facet so both paths
|
||||
* never issue duplicate `getColumns` calls.
|
||||
*/
|
||||
export interface TableColumnsMetadata {
|
||||
schema?: string;
|
||||
tableName: string;
|
||||
tableType?: string;
|
||||
catalog?: string;
|
||||
database?: string;
|
||||
columns: ColumnInfo[];
|
||||
cachedAt: number;
|
||||
}
|
||||
|
||||
export interface TableColumnsLoadResult {
|
||||
columns: ColumnInfo[];
|
||||
tableType?: string;
|
||||
cacheStatus: MetadataLoadCacheStatus;
|
||||
ageMs: number;
|
||||
cachedAt: number;
|
||||
}
|
||||
|
||||
const tableMetadataCache = new MetadataResultCache<TableMetadata>({
|
||||
ttlMs: TABLE_METADATA_CACHE_TTL_MS,
|
||||
maxEntries: TABLE_METADATA_CACHE_MAX_ENTRIES,
|
||||
@@ -49,6 +74,50 @@ const tableMetadataCoordinator = new MetadataLoadCoordinator((event) => {
|
||||
});
|
||||
const tableIndexesLoads = new Map<string, { parts: ReturnType<typeof metadataScopeParts>; promise: Promise<IndexInfo[]>; expiresAt: number }>();
|
||||
|
||||
// Columns facet: shared by the full table-metadata loader and the display-only
|
||||
// loader, so a grouped-query comment enrichment and an editable query never
|
||||
// issue duplicate `getColumns` calls for the same table (and the display path
|
||||
// never triggers `listIndexes`). Uses the same TTL, scope keys, in-flight
|
||||
// coordinator and invalidation stamps as the full table metadata cache.
|
||||
const TABLE_COLUMNS_CACHE_MAX_ENTRIES = 240;
|
||||
const tableColumnsCache = new MetadataResultCache<TableColumnsMetadata>({
|
||||
ttlMs: TABLE_METADATA_CACHE_TTL_MS,
|
||||
maxEntries: TABLE_COLUMNS_CACHE_MAX_ENTRIES,
|
||||
});
|
||||
|
||||
const tableColumnsCoordinator = new MetadataLoadCoordinator((event) => {
|
||||
console.debug("[DBX][metadata-load:columns-coordinator]", event);
|
||||
});
|
||||
|
||||
interface InFlightTableColumnsScope {
|
||||
parts: ReturnType<typeof metadataScopeParts>;
|
||||
count: number;
|
||||
}
|
||||
const inFlightTableColumnsScopes = new Map<string, InFlightTableColumnsScope>();
|
||||
const tableColumnsInvalidationStamps = new Map<string, number>();
|
||||
|
||||
function registerInFlightTableColumnsScope(scopeKey: string, scope: MetadataScopeInput): void {
|
||||
const entry = inFlightTableColumnsScopes.get(scopeKey);
|
||||
if (entry) {
|
||||
entry.count++;
|
||||
} else {
|
||||
inFlightTableColumnsScopes.set(scopeKey, { parts: metadataScopeParts(scope), count: 1 });
|
||||
}
|
||||
}
|
||||
|
||||
function unregisterInFlightTableColumnsScope(scopeKey: string): void {
|
||||
const entry = inFlightTableColumnsScopes.get(scopeKey);
|
||||
if (!entry) return;
|
||||
entry.count--;
|
||||
if (entry.count > 0) return;
|
||||
inFlightTableColumnsScopes.delete(scopeKey);
|
||||
tableColumnsInvalidationStamps.delete(scopeKey);
|
||||
}
|
||||
|
||||
function bumpTableColumnsInvalidationStamp(scopeKey: string): void {
|
||||
tableColumnsInvalidationStamps.set(scopeKey, (tableColumnsInvalidationStamps.get(scopeKey) ?? 0) + 1);
|
||||
}
|
||||
|
||||
// 失效代数(按 scope key 隔离):跨越失效边界的旧加载完成后不得写缓存——
|
||||
// 结构变更后 force 拉到的新值可能被保存前启动、最后返回的在途加载回填覆盖。
|
||||
// 只登记在途 scope 并只对匹配失效条件的 key 递增代数,避免失效表 A 时
|
||||
@@ -103,6 +172,91 @@ export function getCachedTableMetadata(request: Pick<TableMetadataRequest, "conn
|
||||
return { metadata: hit.value, cacheStatus: hit.stale ? "stale" : "hit", ageMs: hit.ageMs };
|
||||
}
|
||||
|
||||
export function getCachedTableColumns(request: Pick<TableMetadataRequest, "connectionId" | "database" | "schema" | "tableName" | "tableType" | "driverProfile" | "databaseType" | "catalog">): TableColumnsLoadResult | undefined {
|
||||
const hit = tableColumnsCache.get(tableMetadataScope(request));
|
||||
if (!hit) return undefined;
|
||||
return { columns: hit.value.columns, tableType: hit.value.tableType, cacheStatus: hit.stale ? "stale" : "hit", ageMs: hit.ageMs, cachedAt: hit.cachedAt };
|
||||
}
|
||||
|
||||
/**
|
||||
* Load only the column metadata for a table, with the same TTL cache, in-flight
|
||||
* deduplication and invalidation contract as full table metadata.
|
||||
*
|
||||
* Reuse rules (guaranteed by this implementation and asserted in the request
|
||||
* counts regression tests):
|
||||
*
|
||||
* - Case A: if a fresh full table-metadata entry already exists for the table,
|
||||
* its columns are returned directly — a display enrichment never re-fetches
|
||||
* columns (or indexes).
|
||||
* - If a fresh columns-only entry exists, it is returned with no remote call.
|
||||
* - Absent a cache hit, a single remote `api.getColumns` is issued (no
|
||||
* `listIndexes`), deduplicated for concurrent callers via the shared columns
|
||||
* coordinator.
|
||||
*/
|
||||
export async function loadTableColumns(request: TableMetadataRequest): Promise<TableColumnsLoadResult> {
|
||||
const scope = tableMetadataScope(request);
|
||||
const trace = createMetadataLoadTrace(scope);
|
||||
if (!request.force) {
|
||||
// Case A: reuse a fresh full table-metadata cache entry so a display
|
||||
// enrichment that follows a full metadata load issues zero remote calls.
|
||||
const full = tableMetadataCache.get(scope);
|
||||
if (full) {
|
||||
logMetadataLoadTrace(request.traceLogger, trace, "cache-hit", {
|
||||
cacheStatus: full.stale ? "stale" : "hit",
|
||||
resultCount: full.value.columns.length,
|
||||
stale: full.stale,
|
||||
});
|
||||
return { columns: full.value.columns, tableType: full.value.tableType, cacheStatus: full.stale ? "stale" : "hit", ageMs: full.ageMs, cachedAt: full.cachedAt };
|
||||
}
|
||||
const cached = tableColumnsCache.get(scope);
|
||||
if (cached) {
|
||||
logMetadataLoadTrace(request.traceLogger, trace, "cache-hit", {
|
||||
cacheStatus: cached.stale ? "stale" : "hit",
|
||||
resultCount: cached.value.columns.length,
|
||||
stale: cached.stale,
|
||||
});
|
||||
return { columns: cached.value.columns, tableType: cached.value.tableType, cacheStatus: cached.stale ? "stale" : "hit", ageMs: cached.ageMs, cachedAt: cached.cachedAt };
|
||||
}
|
||||
}
|
||||
|
||||
logMetadataLoadTrace(request.traceLogger, trace, "cache-miss", { cacheStatus: request.force ? "refresh" : "miss", force: request.force === true });
|
||||
const scopeKey = metadataScopeKey(scope);
|
||||
const invalidationStampAtStart = tableColumnsInvalidationStamps.get(scopeKey) ?? 0;
|
||||
registerInFlightTableColumnsScope(scopeKey, scope);
|
||||
let metadata: TableColumnsMetadata;
|
||||
try {
|
||||
metadata = await tableColumnsCoordinator.run(
|
||||
scope,
|
||||
async () => {
|
||||
// Display-only loader: columns only, never index discovery.
|
||||
const columns = await api.getColumns(request.connectionId, request.database, request.schema ?? "", request.tableName, request.catalog);
|
||||
return {
|
||||
schema: request.schema || undefined,
|
||||
tableName: request.tableName,
|
||||
tableType: request.tableType,
|
||||
catalog: request.catalog,
|
||||
database: request.database,
|
||||
columns,
|
||||
cachedAt: Date.now(),
|
||||
};
|
||||
},
|
||||
{ force: request.force, kind: scope.kind },
|
||||
);
|
||||
|
||||
if (invalidationStampAtStart === (tableColumnsInvalidationStamps.get(scopeKey) ?? 0)) {
|
||||
tableColumnsCache.set(scope, metadata);
|
||||
}
|
||||
} finally {
|
||||
unregisterInFlightTableColumnsScope(scopeKey);
|
||||
}
|
||||
logMetadataLoadTrace(request.traceLogger, trace, "done", {
|
||||
cacheStatus: request.force ? "refresh" : "miss",
|
||||
resultCount: metadata.columns.length,
|
||||
force: request.force === true,
|
||||
});
|
||||
return { columns: metadata.columns, tableType: metadata.tableType, cacheStatus: request.force ? "refresh" : "miss", ageMs: 0, cachedAt: metadata.cachedAt };
|
||||
}
|
||||
|
||||
export function tableMetadataToDataTabMeta(metadata: TableMetadata, overrides?: { schema?: string }): NonNullable<QueryTab["tableMeta"]> {
|
||||
return {
|
||||
schema: overrides ? overrides.schema : metadata.schema,
|
||||
@@ -156,6 +310,12 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
||||
registerInFlightTableMetadataScope(scopeKey, scope);
|
||||
let metadata: TableMetadata;
|
||||
try {
|
||||
// Start the columns facet synchronously (before the first await) so its
|
||||
// in-flight registration is visible to any synchronous invalidation that
|
||||
// follows — otherwise a follower could dedupe against a stale column load
|
||||
// that invalidation was too early to clear. Concurrent display-only column
|
||||
// requests dedupe against this same coordinator entry.
|
||||
const columnsPromise = loadTableColumns(request);
|
||||
metadata = await tableMetadataCoordinator.run(
|
||||
scope,
|
||||
async () => {
|
||||
@@ -163,9 +323,9 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
||||
// discovery independently unless an unqualified Vastbase relation must
|
||||
// first report its visible schema for the index lookup.
|
||||
const resolveVastbaseSchema = request.databaseType === "vastbase" && !request.schema;
|
||||
const columnsPromise = api.getColumns(request.connectionId, request.database, request.schema ?? "", request.tableName, request.catalog);
|
||||
const indexesPromise = resolveVastbaseSchema ? undefined : loadTableIndexes(request).catch((): IndexInfo[] => []);
|
||||
const columns = await columnsPromise;
|
||||
const columnsResult = await columnsPromise;
|
||||
const columns = columnsResult.columns;
|
||||
const resolvedSchema = resolveVastbaseSchema ? columns.find((column) => column.resolved_schema)?.resolved_schema : request.schema;
|
||||
const indexes = columns.length > 0 ? await (indexesPromise ?? loadTableIndexes({ ...request, schema: resolvedSchema }).catch((): IndexInfo[] => [])) : [];
|
||||
const primaryKeys = editableRowIdentifierColumns(request.databaseType as DatabaseType, columns, indexes, request.tableType);
|
||||
@@ -178,7 +338,7 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
||||
columns,
|
||||
indexes,
|
||||
primaryKeys,
|
||||
cachedAt: Date.now(),
|
||||
cachedAt: columnsResult.cachedAt,
|
||||
};
|
||||
},
|
||||
{ force: request.force, kind: scope.kind },
|
||||
@@ -186,7 +346,7 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
||||
|
||||
// 必须在 unregister 前比较:最后一个在途加载注销时会顺带清掉代数记录
|
||||
if (invalidationStampAtStart === (tableMetadataInvalidationStamps.get(scopeKey) ?? 0)) {
|
||||
tableMetadataCache.set(scope, metadata);
|
||||
tableMetadataCache.set(scope, metadata, { cachedAt: metadata.cachedAt });
|
||||
}
|
||||
} finally {
|
||||
unregisterInFlightTableMetadataScope(scopeKey);
|
||||
@@ -210,17 +370,34 @@ export function invalidateTableMetadataCache(match: MetadataCacheInvalidation):
|
||||
bumpTableMetadataInvalidationStamp(scopeKey);
|
||||
tableMetadataCoordinator.clear(scopeKey);
|
||||
}
|
||||
// Same invalidation contract for the columns facet: stale in-flight column
|
||||
// results must not write back across the invalidation boundary either.
|
||||
for (const [scopeKey, entry] of inFlightTableColumnsScopes) {
|
||||
if (!matches(entry.parts)) continue;
|
||||
bumpTableColumnsInvalidationStamp(scopeKey);
|
||||
tableColumnsCoordinator.clear(scopeKey);
|
||||
}
|
||||
for (const [scopeKey, entry] of tableIndexesLoads) {
|
||||
if (matches(entry.parts)) tableIndexesLoads.delete(scopeKey);
|
||||
}
|
||||
return tableMetadataCache.invalidate(match);
|
||||
const removedFull = tableMetadataCache.invalidate(match);
|
||||
// Columns facet is invalidated as a side effect but, to preserve the existing
|
||||
// public return contract (count of table-metadata entries invalidated), it is
|
||||
// not added to the returned total.
|
||||
tableColumnsCache.invalidate(match);
|
||||
return removedFull;
|
||||
}
|
||||
|
||||
export function clearTableMetadataCache(): void {
|
||||
for (const scopeKey of inFlightTableMetadataScopes.keys()) {
|
||||
bumpTableMetadataInvalidationStamp(scopeKey);
|
||||
}
|
||||
for (const scopeKey of inFlightTableColumnsScopes.keys()) {
|
||||
bumpTableColumnsInvalidationStamp(scopeKey);
|
||||
}
|
||||
tableMetadataCoordinator.clear();
|
||||
tableColumnsCoordinator.clear();
|
||||
tableIndexesLoads.clear();
|
||||
tableMetadataCache.clear();
|
||||
tableColumnsCache.clear();
|
||||
}
|
||||
@@ -281,6 +281,73 @@ export function analyzeEditableQueryEditability(sql: string): QueryEditability {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a SELECT statement's projection columns and source tables far enough to
|
||||
* resolve display-only result metadata (result-column → source-column mapping
|
||||
* by projection ordinal) WITHOUT deciding editability.
|
||||
*
|
||||
* GROUP BY / HAVING queries are classified `aggregation` (read-only) by
|
||||
* `analyzeEditableQueryEditability`, so that function bails out before parsing
|
||||
* projections or sources. Their directly projected base-table columns are still
|
||||
* resolvable for column comments, so the display layer uses this function to
|
||||
* recover that structure. It is consumed ONLY by display metadata enrichment
|
||||
* and must never feed row mutation logic — editability is decided exclusively
|
||||
* by `analyzeEditableQueryEditability`.
|
||||
*
|
||||
* Returns `null` when the statement cannot be structurally parsed for display:
|
||||
* CTEs, set operations, subquery/external sources, or non-SELECT statements.
|
||||
*/
|
||||
export function analyzeSelectStructureForDisplay(sql: string): EditableQueryInfo | null {
|
||||
const normalized = stripSqlComments(sql)
|
||||
.replace(/;+\s*$/, "")
|
||||
.trim();
|
||||
if (!normalized) return null;
|
||||
if (/^\s*WITH\b/i.test(normalized)) return null;
|
||||
if (!/^SELECT\b/i.test(normalized)) return null;
|
||||
if (hasTopLevelKeyword(normalized, ["UNION", "INTERSECT", "EXCEPT", "MINUS"])) return null;
|
||||
if (normalized.includes(";")) return null;
|
||||
|
||||
const fromIndex = findTopLevelKeyword(normalized, "FROM", 0);
|
||||
if (fromIndex < 0) return null;
|
||||
|
||||
const rawSelectBody = normalized.slice("SELECT".length, fromIndex).trim();
|
||||
const distinct = /^DISTINCT\b/i.test(rawSelectBody);
|
||||
const selectBodyWithoutDistinct = distinct ? rawSelectBody.replace(/^DISTINCT\b/i, "").trimStart() : rawSelectBody;
|
||||
const selectBody = stripSqlServerTopClause(selectBodyWithoutDistinct);
|
||||
|
||||
// Locate the FROM body end including GROUP/HAVING so a grouped query still
|
||||
// extracts a clean source list for metadata loading.
|
||||
const fromEnd = firstTopLevelKeywordIndex(normalized, ["WHERE", "GROUP", "HAVING", "ORDER", "LIMIT", "OFFSET", "FETCH", "FOR"], fromIndex + "FROM".length);
|
||||
const fromBody = normalized.slice(fromIndex + "FROM".length, fromEnd < 0 ? normalized.length : fromEnd).trim();
|
||||
if (isExternalFromSource(fromBody)) return null;
|
||||
const sources = parseFromSources(fromBody);
|
||||
if (!sources.length) return null;
|
||||
const source = sources[0]!;
|
||||
|
||||
const selectStar = sources.length === 1 && isSelectStar(selectBody, source.alias);
|
||||
const columns = selectStar ? [] : parseSelectColumns(selectBody, sources);
|
||||
if (!selectStar && columns.length === 0) return null;
|
||||
if (sources.length > 1 && columns.some((column) => column.star && !column.sourceKey)) return null;
|
||||
|
||||
const analysis: EditableQueryInfo = {
|
||||
catalog: source.catalog,
|
||||
catalogQuoted: source.catalogQuoted,
|
||||
schema: source.schema,
|
||||
schemaQuoted: source.schemaQuoted,
|
||||
tableName: source.tableName,
|
||||
tableNameQuoted: source.tableNameQuoted,
|
||||
tableAlias: source.alias,
|
||||
selectStar,
|
||||
columns,
|
||||
...(distinct ? { distinct: true } : {}),
|
||||
};
|
||||
if (sources.length > 1) {
|
||||
analysis.sources = sources;
|
||||
analysis.multiSource = true;
|
||||
}
|
||||
return analysis;
|
||||
}
|
||||
|
||||
export function queryEditabilityMessageKey(reason: QueryEditabilityReason): string {
|
||||
return `grid.queryEditUnsupported.${reason}`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user