-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathexecute_sql.ts
More file actions
178 lines (169 loc) · 5.34 KB
/
Copy pathexecute_sql.ts
File metadata and controls
178 lines (169 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type {
DatabricksExecuteSqlParams,
DatabricksExecuteSqlResponse,
} from '@/tools/databricks/types'
import type { ToolConfig } from '@/tools/types'
export const executeSqlTool: ToolConfig<DatabricksExecuteSqlParams, DatabricksExecuteSqlResponse> =
{
id: 'databricks_execute_sql',
name: 'Databricks Execute SQL',
description:
'Execute a SQL statement against a Databricks SQL warehouse and return results inline. Supports parameterized queries and Unity Catalog.',
version: '1.0.0',
params: {
host: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Databricks workspace host (e.g., dbc-abc123.cloud.databricks.com)',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Databricks Personal Access Token',
},
warehouseId: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The ID of the SQL warehouse to execute against',
},
statement: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The SQL statement to execute (max 16 MiB)',
},
catalog: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Unity Catalog name (equivalent to USE CATALOG)',
},
schema: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Schema name (equivalent to USE SCHEMA)',
},
rowLimit: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Maximum number of rows to return',
},
waitTimeout: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description:
'How long to wait for results (e.g., "50s"). Range: "0s" or "5s" to "50s". Default: "50s"',
},
},
request: {
url: (params) => {
const host = params.host
.trim()
.replace(/^https?:\/\//, '')
.replace(/\/$/, '')
return `https://${host}/api/2.0/sql/statements/`
},
method: 'POST',
headers: (params) => ({
'Content-Type': 'application/json',
Authorization: `Bearer ${params.apiKey}`,
}),
body: (params) => {
const body: Record<string, unknown> = {
warehouse_id: params.warehouseId,
statement: params.statement,
format: 'JSON_ARRAY',
disposition: 'INLINE',
wait_timeout: params.waitTimeout || '50s',
}
if (params.catalog) body.catalog = params.catalog
if (params.schema) body.schema = params.schema
if (params.rowLimit) body.row_limit = params.rowLimit
return body
},
},
transformResponse: async (response: Response) => {
const data = await response.json()
if (!response.ok) {
throw new Error(data.message || data.error?.message || 'Failed to execute SQL statement')
}
const status = data.status?.state ?? 'UNKNOWN'
if (status === 'FAILED') {
throw new Error(
data.status?.error?.message ||
`SQL statement execution failed: ${data.status?.error?.error_code ?? 'UNKNOWN'}`
)
}
const columns =
data.manifest?.schema?.columns?.map(
(col: { name: string; position: number; type_name: string }) => ({
name: col.name ?? '',
position: col.position ?? 0,
typeName: col.type_name ?? '',
})
) ?? null
return {
success: true,
output: {
statementId: data.statement_id ?? '',
status,
columns,
data: data.result?.data_array ?? null,
totalRows: data.manifest?.total_row_count ?? null,
truncated: data.manifest?.truncated ?? false,
},
}
},
outputs: {
statementId: {
type: 'string',
description: 'Unique identifier for the executed statement',
},
status: {
type: 'string',
description: 'Execution status (SUCCEEDED, PENDING, RUNNING, FAILED, CANCELED, CLOSED)',
},
columns: {
type: 'array',
description: 'Column schema of the result set',
optional: true,
items: {
type: 'object',
properties: {
name: { type: 'string', description: 'Column name' },
position: { type: 'number', description: 'Column position (0-based)' },
typeName: {
type: 'string',
description:
'Column type (STRING, INT, LONG, DOUBLE, BOOLEAN, TIMESTAMP, DATE, DECIMAL, etc.)',
},
},
},
},
data: {
type: 'array',
description:
'Result rows as a 2D array of strings where each inner array is a row of column values',
optional: true,
items: {
type: 'array',
description: 'A single row of column values as strings',
},
},
totalRows: {
type: 'number',
description: 'Total number of rows in the result',
optional: true,
},
truncated: {
type: 'boolean',
description: 'Whether the result set was truncated due to row_limit or byte_limit',
},
},
}