1 | |
2 |
|
3 | export type DynSelect_warehouse_id = string |
4 |
|
5 | |
6 | export async function warehouse_id(auth: RT.Databricks) { |
7 | const base = auth.workspace_url.replace(/\/$/, "") |
8 | const response = await fetch(`${base}/api/2.0/sql/warehouses`, { |
9 | headers: { |
10 | Authorization: `Bearer ${auth.token}`, |
11 | Accept: "application/json", |
12 | }, |
13 | }) |
14 | if (!response.ok) { |
15 | throw new Error(`${response.status} ${await response.text()}`) |
16 | } |
17 | const { warehouses } = (await response.json()) as { |
18 | warehouses?: { id: string; name?: string; state?: string }[] |
19 | } |
20 | return (warehouses ?? []).map((w) => ({ |
21 | value: w.id, |
22 | label: `${w.name ?? w.id} (${w.state ?? "?"})`, |
23 | })) |
24 | } |
25 |
|
26 | |
27 | * Execute SQL Statement |
28 | * Run a SQL statement on a SQL warehouse. If it finishes within wait_timeout (5s-50s, e.g. "30s") results are returned inline under result.data_array; otherwise the response carries a statement_id with status.state PENDING/RUNNING — poll Get SQL Statement until SUCCEEDED. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Databricks, |
32 | warehouse_id: DynSelect_warehouse_id, |
33 | statement: string, |
34 | catalog: string | undefined, |
35 | schema: string | undefined, |
36 | wait_timeout: string | undefined |
37 | ) { |
38 | const base = auth.workspace_url.replace(/\/$/, "") |
39 | const url = new URL(`${base}/api/2.0/sql/statements/`) |
40 |
|
41 | const body: { [key: string]: any } = { warehouse_id, statement } |
42 | if (catalog !== undefined && catalog !== "") body.catalog = catalog |
43 | if (schema !== undefined && schema !== "") body.schema = schema |
44 | if (wait_timeout !== undefined && wait_timeout !== "") |
45 | body.wait_timeout = wait_timeout |
46 |
|
47 | const response = await fetch(url, { |
48 | method: "POST", |
49 | headers: { |
50 | Authorization: `Bearer ${auth.token}`, |
51 | "Content-Type": "application/json", |
52 | Accept: "application/json", |
53 | }, |
54 | body: JSON.stringify(body), |
55 | }) |
56 |
|
57 | if (!response.ok) { |
58 | throw new Error(`${response.status} ${await response.text()}`) |
59 | } |
60 |
|
61 | return await response.json() |
62 | } |
63 |
|