-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStatementExecutionClient.cs
More file actions
416 lines (350 loc) · 18.2 KB
/
StatementExecutionClient.cs
File metadata and controls
416 lines (350 loc) · 18.2 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (c) 2025 ADBC Drivers Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace AdbcDrivers.Databricks.StatementExecution
{
/// <summary>
/// Interface for Statement Execution API operations.
/// </summary>
internal interface IStatementExecutionClient
{
/// <summary>
/// Creates a new SQL session.
/// </summary>
/// <param name="request">The session creation request.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The session creation response.</returns>
Task<CreateSessionResponse> CreateSessionAsync(CreateSessionRequest request, CancellationToken cancellationToken);
/// <summary>
/// Deletes an existing SQL session.
/// </summary>
/// <param name="sessionId">The session ID to delete.</param>
/// <param name="warehouseId">The warehouse ID (required by Databricks API).</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task DeleteSessionAsync(string sessionId, string warehouseId, CancellationToken cancellationToken);
/// <summary>
/// Executes a SQL statement.
/// </summary>
/// <param name="request">The statement execution request.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The statement execution response.</returns>
Task<ExecuteStatementResponse> ExecuteStatementAsync(ExecuteStatementRequest request, CancellationToken cancellationToken);
/// <summary>
/// Gets the status and results of a SQL statement.
/// </summary>
/// <param name="statementId">The statement ID.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The statement status and results.</returns>
Task<GetStatementResponse> GetStatementAsync(string statementId, CancellationToken cancellationToken);
/// <summary>
/// Gets a specific result chunk by index (for incremental fetching).
/// </summary>
/// <param name="statementId">The statement ID.</param>
/// <param name="chunkIndex">The zero-based chunk index.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The result chunk data.</returns>
Task<ResultData> GetResultChunkAsync(string statementId, long chunkIndex, CancellationToken cancellationToken);
/// <summary>
/// Cancels a running SQL statement.
/// </summary>
/// <param name="statementId">The statement ID to cancel.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task CancelStatementAsync(string statementId, CancellationToken cancellationToken);
/// <summary>
/// Closes a SQL statement and releases its resources.
/// </summary>
/// <param name="statementId">The statement ID to close.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task CloseStatementAsync(string statementId, CancellationToken cancellationToken);
}
/// <summary>
/// Client for communicating with the Databricks Statement Execution API.
/// </summary>
internal class StatementExecutionClient : IStatementExecutionClient
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
private const string SessionsEndpoint = "/api/2.0/sql/sessions";
private const string StatementsEndpoint = "/api/2.0/sql/statements";
private const string PreviewSessionsEndpoint = "/2.0/preview/sql/sessions";
private const string PreviewStatementsEndpoint = "/2.0/preview/sql/statements";
private readonly string _sessionsEndpoint;
private readonly string _statementsEndpoint;
// JSON serialization options - ignore null values when writing
private static readonly JsonSerializerOptions s_jsonOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
/// <summary>
/// Initializes a new instance of the <see cref="StatementExecutionClient"/> class.
/// </summary>
/// <param name="httpClient">The HTTP client to use for requests.</param>
/// <param name="host">The Databricks workspace host.</param>
/// <param name="usePreviewEndpoint">When true, uses /2.0/sql/... paths instead of /api/2.0/sql/... paths.</param>
public StatementExecutionClient(HttpClient httpClient, string host, bool usePreviewEndpoint = false)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentException("Host cannot be null or whitespace.", nameof(host));
}
// Ensure the host doesn't have a trailing slash and has https://
host = host.TrimEnd('/');
if (!host.StartsWith("http://") && !host.StartsWith("https://"))
{
host = "https://" + host;
}
_baseUrl = host;
_sessionsEndpoint = usePreviewEndpoint ? PreviewSessionsEndpoint : SessionsEndpoint;
_statementsEndpoint = usePreviewEndpoint ? PreviewStatementsEndpoint : StatementsEndpoint;
}
/// <summary>
/// Creates a new SQL session.
/// </summary>
/// <param name="request">The session creation request.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The session creation response.</returns>
public async Task<CreateSessionResponse> CreateSessionAsync(
CreateSessionRequest request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var url = $"{_baseUrl}{_sessionsEndpoint}";
var jsonContent = JsonSerializer.Serialize(request, s_jsonOptions);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = content
};
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var sessionResponse = JsonSerializer.Deserialize<CreateSessionResponse>(responseContent, s_jsonOptions);
if (sessionResponse == null)
{
throw new DatabricksException("Failed to deserialize CreateSessionResponse");
}
return sessionResponse;
}
/// <summary>
/// Deletes an existing SQL session.
/// </summary>
/// <param name="sessionId">The session ID to delete.</param>
/// <param name="warehouseId">The warehouse ID (required by Databricks API).</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task DeleteSessionAsync(string sessionId, string warehouseId, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(sessionId))
{
throw new ArgumentException("Session ID cannot be null or whitespace.", nameof(sessionId));
}
if (string.IsNullOrWhiteSpace(warehouseId))
{
throw new ArgumentException("Warehouse ID cannot be null or whitespace.", nameof(warehouseId));
}
// Databricks requires warehouse_id as query parameter even for DELETE
var url = $"{_baseUrl}{_sessionsEndpoint}/{sessionId}?warehouse_id={Uri.EscapeDataString(warehouseId)}";
var httpRequest = new HttpRequestMessage(HttpMethod.Delete, url);
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
}
/// <summary>
/// Executes a SQL statement.
/// </summary>
/// <param name="request">The statement execution request.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The statement execution response.</returns>
public async Task<ExecuteStatementResponse> ExecuteStatementAsync(
ExecuteStatementRequest request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var url = $"{_baseUrl}{_statementsEndpoint}";
var jsonContent = JsonSerializer.Serialize(request, s_jsonOptions);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = content
};
if (request.IsMetadata)
{
httpRequest.Headers.TryAddWithoutValidation("x-databricks-sea-can-run-fully-sync", "true");
}
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var executeResponse = JsonSerializer.Deserialize<ExecuteStatementResponse>(responseContent, s_jsonOptions);
if (executeResponse == null)
{
throw new DatabricksException("Failed to deserialize ExecuteStatementResponse");
}
// Check for FAILED state and throw exception (like JDBC driver does)
if (executeResponse.Status?.State == "FAILED")
{
var errorMessage = $"Statement execution failed. State: {executeResponse.Status.State}";
if (executeResponse.Status.Error != null)
{
errorMessage += $". Error Code: {executeResponse.Status.Error.ErrorCode}, Message: {executeResponse.Status.Error.Message}";
}
throw new DatabricksException(errorMessage);
}
return executeResponse;
}
/// <summary>
/// Gets the status and results of a SQL statement.
/// </summary>
/// <param name="statementId">The statement ID.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The statement status and results.</returns>
public async Task<GetStatementResponse> GetStatementAsync(
string statementId,
CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(statementId))
{
throw new ArgumentException("Statement ID cannot be null or whitespace.", nameof(statementId));
}
var url = $"{_baseUrl}{_statementsEndpoint}/{statementId}";
var httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var getResponse = JsonSerializer.Deserialize<GetStatementResponse>(responseContent, s_jsonOptions);
if (getResponse == null)
{
throw new DatabricksException("Failed to deserialize GetStatementResponse");
}
return getResponse;
}
/// <summary>
/// Gets a specific result chunk by index (for incremental fetching).
/// </summary>
/// <param name="statementId">The statement ID.</param>
/// <param name="chunkIndex">The zero-based chunk index.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The result chunk data.</returns>
public async Task<ResultData> GetResultChunkAsync(
string statementId,
long chunkIndex,
CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(statementId))
{
throw new ArgumentException("Statement ID cannot be null or whitespace.", nameof(statementId));
}
if (chunkIndex < 0)
{
throw new ArgumentException("Chunk index must be non-negative.", nameof(chunkIndex));
}
var url = $"{_baseUrl}{_statementsEndpoint}/{statementId}/result/chunks/{chunkIndex}";
var httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var resultData = JsonSerializer.Deserialize<ResultData>(responseContent, s_jsonOptions);
if (resultData == null)
{
throw new DatabricksException("Failed to deserialize ResultData");
}
return resultData;
}
/// <summary>
/// Cancels a running SQL statement.
/// </summary>
/// <param name="statementId">The statement ID to cancel.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task CancelStatementAsync(string statementId, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(statementId))
{
throw new ArgumentException("Statement ID cannot be null or whitespace.", nameof(statementId));
}
var url = $"{_baseUrl}{_statementsEndpoint}/{statementId}/cancel";
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url);
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
}
/// <summary>
/// Closes a SQL statement and releases its resources.
/// Note: Uses DELETE method on /statements/{statement_id}, not POST to /close endpoint.
/// </summary>
/// <param name="statementId">The statement ID to close.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task CloseStatementAsync(string statementId, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(statementId))
{
throw new ArgumentException("Statement ID cannot be null or whitespace.", nameof(statementId));
}
// Databricks uses DELETE on /statements/{statement_id}, not POST to /close
var url = $"{_baseUrl}{_statementsEndpoint}/{statementId}";
var httpRequest = new HttpRequestMessage(HttpMethod.Delete, url);
var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
await EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
}
/// <summary>
/// Ensures that the HTTP response indicates success, otherwise throws an exception.
/// </summary>
/// <param name="response">The HTTP response message.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task EnsureSuccessStatusCodeAsync(HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return;
}
var errorContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var errorMessage = $"Statement Execution API request failed with status code {(int)response.StatusCode} ({response.StatusCode})";
// Try to parse error details from JSON response
try
{
var errorResponse = JsonSerializer.Deserialize<ServiceError>(errorContent, s_jsonOptions);
if (errorResponse?.ErrorCode != null || errorResponse?.Message != null)
{
errorMessage = $"{errorMessage}. Error Code: {errorResponse.ErrorCode ?? "Unknown"}, Message: {errorResponse.Message ?? "Unknown"}";
}
else
{
errorMessage = $"{errorMessage}. Response: {errorContent}";
}
}
catch
{
// If JSON parsing fails, include raw error content
errorMessage = $"{errorMessage}. Response: {errorContent}";
}
throw new DatabricksException(errorMessage);
}
}
}