Skip to content

Commit 26380ee

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/permission-relative-paths
# Conflicts: # packages/opencode/test/tool/bash.test.ts
2 parents 6c17622 + e4e10c0 commit 26380ee

19 files changed

Lines changed: 1095 additions & 132 deletions

File tree

‎packages/drivers/src/duckdb.ts‎

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { assertStoreExists, requireStorePath } from "./file-store"
6-
import type { ConnectionConfig, Connector, ConnectorResult, ExecuteOptions, SchemaColumn } from "./types"
6+
import type { ConnectionConfig, Connector, ConnectorResult, ExecuteOptions, SchemaColumn, SchemaMetadata } from "./types"
77
import { loadOptionalDriver } from "./resolve"
88

99
// altimate_change start — configurable, generous open budget
@@ -101,15 +101,34 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
101101
}
102102
// altimate_change end
103103

104-
function query(sql: string): Promise<any[]> {
104+
function query(sql: string, signal?: AbortSignal): Promise<any[]> {
105105
return new Promise((resolve, reject) => {
106-
connection.all(sql, (err: Error | null, rows: any[]) => {
107-
if (err) reject(wrapDuckDBError(err))
108-
else resolve(rows ?? [])
109-
})
106+
signal?.throwIfAborted()
107+
// The legacy DuckDB SDK's interrupt() is a no-op. Stop waiting and avoid
108+
// subsequent work on abort; the native query still finishes in the SDK.
109+
const onAbort = () => reject(signal!.reason)
110+
signal?.addEventListener("abort", onAbort, { once: true })
111+
try {
112+
connection.all(sql, (err: Error | null, rows: any[]) => {
113+
signal?.removeEventListener("abort", onAbort)
114+
if (err) reject(wrapDuckDBError(err))
115+
else resolve(rows ?? [])
116+
})
117+
} catch (err) {
118+
signal?.removeEventListener("abort", onAbort)
119+
reject(err)
120+
}
110121
})
111122
}
112123

124+
async function schemaNames(signal?: AbortSignal): Promise<string[]> {
125+
const rows = await query(
126+
"SELECT DISTINCT schema_name FROM information_schema.schemata WHERE catalog_name = current_database() ORDER BY schema_name",
127+
signal,
128+
)
129+
return rows.map((r) => r.schema_name as string)
130+
}
131+
113132
function queryWithParams(sql: string, params: any[]): Promise<any[]> {
114133
return new Promise((resolve, reject) => {
115134
connection.all(sql, ...params, (err: Error | null, rows: any[]) => {
@@ -282,17 +301,14 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
282301
},
283302

284303
async listSchemas(): Promise<string[]> {
285-
const rows = await query(
286-
"SELECT schema_name FROM information_schema.schemata ORDER BY schema_name",
287-
)
288-
return rows.map((r) => r.schema_name as string)
304+
return schemaNames()
289305
},
290306

291307
async listTables(
292308
schema: string,
293309
): Promise<Array<{ name: string; type: string }>> {
294310
const rows = await queryWithParams(
295-
`SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = $1 ORDER BY table_name`,
311+
`SELECT table_name, table_type FROM information_schema.tables WHERE table_catalog = current_database() AND table_schema = $1 ORDER BY table_name`,
296312
[schema],
297313
)
298314
return rows.map((r) => ({
@@ -306,7 +322,7 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
306322
table: string,
307323
): Promise<SchemaColumn[]> {
308324
const rows = await queryWithParams(
309-
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 ORDER BY ordinal_position`,
325+
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_catalog = current_database() AND table_schema = $1 AND table_name = $2 ORDER BY ordinal_position`,
310326
[schema, table],
311327
)
312328
return rows.map((r) => ({
@@ -316,6 +332,43 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
316332
}))
317333
},
318334

335+
async introspectSchema(signal?: AbortSignal): Promise<SchemaMetadata> {
336+
const schemas = await schemaNames(signal)
337+
const rows = await query(
338+
`SELECT t.table_schema, t.table_name, t.table_type,
339+
c.column_name, c.data_type, c.is_nullable
340+
FROM information_schema.tables t
341+
LEFT JOIN information_schema.columns c
342+
ON c.table_catalog = t.table_catalog
343+
AND c.table_schema = t.table_schema
344+
AND c.table_name = t.table_name
345+
WHERE t.table_catalog = current_database()
346+
ORDER BY t.table_schema, t.table_name, c.ordinal_position`,
347+
signal,
348+
)
349+
const tables: SchemaMetadata["tables"] = []
350+
let table: SchemaMetadata["tables"][number] | undefined
351+
for (const row of rows) {
352+
if (!table || table.schema !== row.table_schema || table.name !== row.table_name) {
353+
table = {
354+
schema: row.table_schema,
355+
name: row.table_name,
356+
type: row.table_type === "VIEW" ? "view" : "table",
357+
columns: [],
358+
}
359+
tables.push(table)
360+
}
361+
if (row.column_name !== null) {
362+
table.columns.push({
363+
name: row.column_name,
364+
data_type: row.data_type,
365+
nullable: row.is_nullable === "YES",
366+
})
367+
}
368+
}
369+
return { schemas, tables }
370+
},
371+
319372
async close() {
320373
if (db) {
321374
await new Promise<void>((resolve) => {

‎packages/drivers/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Re-export types
2-
export type { Connector, ConnectorResult, SchemaColumn, ConnectionConfig } from "./types"
2+
export type { Connector, ConnectorResult, SchemaColumn, SchemaMetadata, ConnectionConfig } from "./types"
33

44
// Re-export config normalization
55
export { normalizeConfig, sanitizeConnectionString } from "./normalize"

‎packages/drivers/src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export interface SchemaColumn {
2020
nullable: boolean
2121
}
2222

23+
export interface SchemaMetadata {
24+
schemas: string[]
25+
tables: Array<{ schema: string; name: string; type: string; columns: SchemaColumn[] }>
26+
}
27+
2328
export interface ExecuteOptions {
2429
/** Skip the default LIMIT injection and post-truncation. Use when the caller
2530
* needs the complete, untruncated result set (e.g. data-diff pipelines). */
@@ -32,5 +37,6 @@ export interface Connector {
3237
listSchemas(): Promise<string[]>
3338
listTables(schema: string): Promise<Array<{ name: string; type: string }>>
3439
describeTable(schema: string, table: string): Promise<SchemaColumn[]>
40+
introspectSchema?(signal?: AbortSignal): Promise<SchemaMetadata>
3541
close(): Promise<void>
3642
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { afterEach, describe, expect, spyOn, test } from "bun:test"
2+
import { connect } from "../src/duckdb"
3+
import type { Connector } from "../src/types"
4+
5+
// Run in a separate process because other suites mock the DuckDB module.
6+
const duckdbTest = process.env.ALTIMATE_DUCKDB_E2E === "1" ? test : test.skip
7+
8+
let connector: Connector | undefined
9+
let allSpy: ReturnType<typeof spyOn> | undefined
10+
let duckdb: typeof import("duckdb")
11+
12+
afterEach(async () => {
13+
allSpy?.mockRestore()
14+
allSpy = undefined
15+
await connector?.close()
16+
connector = undefined
17+
})
18+
19+
async function open() {
20+
duckdb = await import("duckdb").then((module) => module.default)
21+
connector = await connect({ type: "duckdb", path: ":memory:" })
22+
await connector.connect()
23+
return connector
24+
}
25+
26+
describe("DuckDB schema introspection", () => {
27+
duckdbTest("lists each schema once and scopes tables and columns to the current catalog", async () => {
28+
const driver = await open()
29+
await driver.execute("CREATE TABLE shared (local_id INTEGER NOT NULL)")
30+
await driver.execute("ATTACH ':memory:' AS other")
31+
await driver.execute("CREATE TABLE other.main.shared (foreign_id VARCHAR)")
32+
await driver.execute("CREATE TABLE other.main.foreign_only (id INTEGER)")
33+
34+
expect(await driver.listSchemas()).toEqual(["main"])
35+
expect(await driver.listTables("main")).toEqual([{ name: "shared", type: "table" }])
36+
expect(await driver.describeTable("main", "shared")).toEqual([
37+
{ name: "local_id", data_type: "INTEGER", nullable: false },
38+
])
39+
})
40+
41+
duckdbTest("reads complete table and view metadata with a constant number of queries", async () => {
42+
const driver = await open()
43+
await driver.execute("CREATE SCHEMA empty_schema")
44+
await driver.execute("CREATE TABLE events (id INTEGER NOT NULL, label VARCHAR)")
45+
await driver.execute("CREATE VIEW event_ids AS SELECT id FROM events")
46+
await driver.execute("ATTACH ':memory:' AS other")
47+
await driver.execute("CREATE TABLE other.main.events (foreign_id BIGINT)")
48+
for (let i = 0; i < 24; i++) {
49+
await driver.execute(`CREATE TABLE item_${i} (id INTEGER)`)
50+
}
51+
allSpy = spyOn(duckdb.Connection.prototype, "all")
52+
53+
const metadata = await driver.introspectSchema!()
54+
55+
expect(allSpy).toHaveBeenCalledTimes(2)
56+
expect(metadata.schemas).toEqual(["empty_schema", "main"])
57+
expect(metadata.tables).toHaveLength(26)
58+
expect(metadata.tables.find((table) => table.name === "events")).toEqual({
59+
schema: "main",
60+
name: "events",
61+
type: "table",
62+
columns: [
63+
{ name: "id", data_type: "INTEGER", nullable: false },
64+
{ name: "label", data_type: "VARCHAR", nullable: true },
65+
],
66+
})
67+
expect(metadata.tables.find((table) => table.name === "event_ids")).toMatchObject({
68+
schema: "main",
69+
type: "view",
70+
columns: [{ name: "id", data_type: "INTEGER", nullable: true }],
71+
})
72+
})
73+
74+
duckdbTest("does not truncate a large metadata inventory", async () => {
75+
const driver = await open()
76+
await driver.execute(
77+
`CREATE TABLE wide_table (${Array.from({ length: 1100 }, (_, i) => `field_${i} INTEGER`).join(", ")})`,
78+
)
79+
80+
const metadata = await driver.introspectSchema!()
81+
82+
expect(metadata.tables).toHaveLength(1)
83+
expect(metadata.tables[0].columns).toHaveLength(1100)
84+
expect(metadata.tables[0].columns.at(-1)?.name).toBe("field_1099")
85+
})
86+
87+
duckdbTest("rejects an already aborted introspection without querying", async () => {
88+
const driver = await open()
89+
allSpy = spyOn(duckdb.Connection.prototype, "all")
90+
const controller = new AbortController()
91+
controller.abort(new Error("Index cancelled"))
92+
93+
await expect(driver.introspectSchema!(controller.signal)).rejects.toThrow("Index cancelled")
94+
expect(allSpy).not.toHaveBeenCalled()
95+
})
96+
97+
duckdbTest("stops waiting on abort and starts no further metadata queries", async () => {
98+
const driver = await open()
99+
let complete: ((error: Error | null, rows: unknown[]) => void) | undefined
100+
allSpy = spyOn(duckdb.Connection.prototype, "all").mockImplementation(function (_sql, ...args) {
101+
complete = args.at(-1) as typeof complete
102+
return this
103+
})
104+
const controller = new AbortController()
105+
const pending = driver.introspectSchema!(controller.signal)
106+
controller.abort(new Error("Index cancelled"))
107+
108+
await expect(pending).rejects.toThrow("Index cancelled")
109+
complete!(null, [{ schema_name: "main" }])
110+
await Promise.resolve()
111+
expect(allSpy).toHaveBeenCalledTimes(1)
112+
})
113+
})

0 commit comments

Comments
 (0)